246SA石器时代论坛|石器服务端|石器技术论坛|单机石器服务端|linux石器时代服务端|石器时代|石器发布网  
  
查看: 10|回复: 0

【学习】OpenGL Texture Example

[复制链接]

535

主题

552

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
18867
发表于 2020-4-2 23:40:40 | 显示全部楼层 |阅读模式
[C++] syntaxhighlighter_viewsource syntaxhighlighter_copycode
void EnableTransparency()
{
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

GLuint LoadTexture(char *filename,int *textw,int *texth) {

        SDL_Surface *surface;
        GLuint textureid;
        int mode;

        surface = IMG_Load(filename);

        // Or if you don't use SDL_image you can use SDL_LoadBMP here instead:
        // surface = SDL_LoadBMP(filename);

        // could not load filename
        if (!surface) {

                return 0;

        }

        // work out what format to tell glTexImage2D to use...
        if (surface->format->BytesPerPixel == 3) { // RGB 24bit

                mode = GL_RGB;

        } else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit

                mode = GL_RGBA;

        } else {

                SDL_FreeSurface(surface);
                return 0;

        }

         *textw=surface->w;
         *texth=surface->h;
        // create one texture name
        glGenTextures(1, &textureid);

        // tell opengl to use the generated texture name
        glBindTexture(GL_TEXTURE_2D, textureid);

        // this reads from the sdl surface and puts it into an opengl texture
        glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);

        // these affect how this texture is drawn later on...
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

        // clean up
        SDL_FreeSurface(surface);

        return textureid;

}

void DrawTexture(int x, int y, GLuint textureid,int textw,int texth) {

        //int textw,texth;
        // tell opengl to use the generated texture name
        glBindTexture(GL_TEXTURE_2D, textureid);
        glEnable(GL_TEXTURE_2D);

        // make a rectangle
        glBegin(GL_QUADS);

        // top left
        glTexCoord2i(0, 0);
        glVertex3f(x, y, 0);

        // top right
        glTexCoord2i(1, 0);
        glVertex3f(x+textw, y, 0);

        // bottom right
        glTexCoord2i(1, 1);
        glVertex3f(x+textw, y+texth, 0);

        // bottom left
        glTexCoord2i(0, 1);
        glVertex3f(x, y+texth, 0);

        glEnd();
        
        glDisable(GL_TEXTURE_2D );
}

int main(int argc,char **argv)
{
  ....

  GLuint myglu;
  int textw,texth;

  myglu=LoadTexture("foo",&textw,&texth);
  DrawTexture(100,100,myglu,testw,texth);
  ....
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表