OpenGL ES 加载tga格式图片
OpenGL ES 加载tga格式图片
开发工具:vs2013
相关书籍:OpenGL ES 3.0 编程指南 官网:http://www.opengles-book.com/
框架(&环境):OpenGL ES 3.0 编程指南源码
1.加载tga格式图片,贴代码
#include <stdio.h> #include "esUtil.h" //注意点:保证内存是连续的,不然读取错误 #pragma pack(push,x1) // Byte alignment (8-bit) #pragma pack(1) typedef struct { unsigned char IdSize, MapType, ImageType; unsigned short PaletteStart, PaletteSize; unsigned char PaletteEntryDepth; unsigned short X, Y, Width, Height; unsigned char ColorDepth, Descriptor; } TGA_HEADER; #pragma pack(pop,x1) typedef struct { // Handle to program object GLuint programObject; // Sample locations GLint baseMapLoc; // Texture handle GLuint baseMapTexId; }UserData; GLuint LoadTexture(void *ioContext, char *fileName) { int width, height; // 1 example TGA_HEADER Header; char* buffer = NULL; //load tga(加载tga图片) FILE *fp = fopen(fileName, "rb"); if (fp == NULL) { esLogMessage("esLoadTGA FAILED to load : { %s }\n", fileName); return 0; } int bytesRead = fread(&Header, sizeof(TGA_HEADER), 1, fp); width = Header.Width; height = Header.Height; if (Header.ColorDepth == 8 || Header.ColorDepth == 24 || Header.ColorDepth == 32) { int bytesToRead = sizeof (char)* (width) * (height) * Header.ColorDepth / 8; buffer = (char*)malloc(bytesToRead); if (buffer) { //注意点:bytesToRead bytesRead = fread(buffer, bytesToRead, 1, fp); fclose(fp); } } // 2 example //char *buffer = esLoadTGA(ioContext, fileName, &width, &height); if (buffer == NULL) { esLogMessage("Error loading (%s) image.\n", fileName); return 0; } GLuint texId; glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); free(buffer); return texId; } //初始化 int Init(ESContext* esContext) { UserData *userData = esContext->userData; char vShaderStr[] = "#version 300 es \n" "layout(location = 0) in vec4 a_position; \n" "layout(location = 1) in vec2 a_texCoord; \n" "out vec2 v_texCoord; \n" "void main() \n" "{ \n" " gl_Position = a_position; \n" " v_texCoord = a_texCoord; \n" "} \n"; char fShaderStr[] = "#version 300 es \n" "precision mediump float; \n" "in vec2 v_texCoord; \n" "layout(location = 0) out vec4 outColor; \n" "uniform sampler2D s_baseMap; \n" "uniform sampler2D s_lightMap; \n" "void main() \n" "{ \n" " vec4 baseColor; \n" " vec4 lightColor; \n" " \n" " baseColor = texture( s_baseMap, v_texCoord ); \n" " lightColor = texture( s_lightMap, v_texCoord ); \n" " outColor = baseColor * (lightColor + 0.25); \n" "} \n"; // Load the shaders and get a linked program object userData->programObject = esLoadProgram(vShaderStr, fShaderStr); // Get the sampler location userData->baseMapLoc = glGetUniformLocation(userData->programObject, "s_baseMap"); // Load the textures userData->baseMapTexId = LoadTexture(esContext->platformData, "basemap.tga"); if (userData->baseMapTexId == 0) { return FALSE; } glClearColor(1.0f, 1.0f, 1.0f, 0.0f); return TRUE; } //绘制 void Draw(ESContext* esContext) { UserData *userData = esContext->userData; GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0 0.0f, 0.0f, // TexCoord 0 -0.5f, -0.5f, 0.0f, // Position 1 0.0f, 1.0f, // TexCoord 1 0.5f, -0.5f, 0.0f, // Position 2 1.0f, 1.0f, // TexCoord 2 0.5f, 0.5f, 0.0f, // Position 3 1.0f, 0.0f // TexCoord 3 }; GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; // Set the viewport glViewport(0, 0, esContext->width, esContext->height); // Clear the color buffer glClear(GL_COLOR_BUFFER_BIT); // Use the program object glUseProgram(userData->programObject); // Load the vertex position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof (GLfloat), vVertices); // Load the texture coordinate glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof (GLfloat), &vVertices[3]); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); // Bind the base map glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, userData->baseMapTexId); // Set the base map sampler to texture unit to 0 glUniform1i(userData->baseMapLoc, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); } //退出处理 void ShutDown(ESContext* esContext) { UserData* userData = esContext->userData; // Delete texture object glDeleteTextures(1, &userData->baseMapTexId); // Delete program object glDeleteProgram(userData->programObject); } //main函数执行 int esMain( ESContext *esContext) { esContext->userData = malloc(sizeof (UserData)); esCreateWindow(esContext, "MultiTexture", 320, 240, ES_WINDOW_RGB); if (!Init(esContext)) { return GL_FALSE; } esRegisterDrawFunc(esContext, Draw); esRegisterShutdownFunc(esContext, ShutDown); system("pause"); return GL_TRUE; }
-- THE END --
浙公网安备 33010602011771号