void initCircleTexture(char *fileName) { // load TGA file TGAFILE *tgaFile = (TGAFILE*)calloc(sizeof(TGAFILE), 1); LoadTGAFile(fileName, tgaFile); // set pointers and glstates glActiveTexture(GL_TEXTURE0); glGenTextures(1, &woodTex); glBindTexture(GL_TEXTURE_2D, woodTex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tgaFile->imageWidth, tgaFile->imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, tgaFile->imageData); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tgaFile->imageWidth, tgaFile->imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tgaFile->imageData); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // glBindTexture(GL_TEXTURE_2D, woodTex); std::cout<<woodTex<<"\n"; // free data free(tgaFile->imageData); free(tgaFile); }
Material::Material(ColorMaterial c) : texture(0) , sampler(0) { // Load Texture // Texture is Targa TGAFILE tgaColor; // Change Abs Path to WorkingDir Path std::string s(c.colorFileName); s = s.substr(s.find_last_of('/') + 1); s = "Textures/" + s; bool result = LoadTGAFile(&tgaColor, s.c_str()); assert(result == true); // Has to be RGB uncompressed assert(tgaColor.imageTypeCode == 2); // To the GL glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexStorage2D(GL_TEXTURE_2D, 4, (tgaColor.bitCount == 24) ? GL_RGB8 : GL_RGBA8, tgaColor.imageWidth, tgaColor.imageHeight); // Do the Actual Loading glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tgaColor.imageWidth, tgaColor.imageHeight, (tgaColor.bitCount == 24) ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, tgaColor.imageData); glGenerateMipmap(GL_TEXTURE_2D); // Tex Parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 8); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA); // TODO: too many samplers since all texture sampled as same // this can be reduced glGenSamplers(1, &sampler); glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8.0f); free(tgaColor.imageData); // Load Normal Map }
// 载入天空 void CSky::Load() { // 天空盒子 LoadTGAFile("Data\\Texture\\LightSky.tga", &tgaFileBox); glGenTextures(1, &textureBox); glBindTexture(GL_TEXTURE_2D, textureBox); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tgaFileBox.imageWidth, tgaFileBox.imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tgaFileBox.imageData); // 天顶 LoadTGAFile("Data\\Texture\\LightSkyTop.tga", &tgaFileTop); glGenTextures(1, &textureTop); glBindTexture(GL_TEXTURE_2D, textureTop); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tgaFileTop.imageWidth, tgaFileTop.imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tgaFileTop.imageData); }
void initCubeMap() { // set TGA data into OpenGL // free data // set pointers and glstates glEnable(GL_TEXTURE_CUBE_MAP_EXT); glActiveTexture(GL_TEXTURE0); std::vector<const GLchar*> faces; // load TGA files faces.push_back("textures/Back.tga"); faces.push_back("textures/Front.tga"); faces.push_back("textures/Down.tga"); faces.push_back("textures/Up.tga"); faces.push_back("textures/Left.tga"); faces.push_back("textures/Right.tga"); glGenTextures(1, &cubeTexId); TGAFILE *tgaFile = (TGAFILE*)calloc(sizeof(TGAFILE), 1); glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTexId); for(unsigned int i = 0; i < faces.size(); i++){ LoadTGAFile(faces[i], tgaFile); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, tgaFile->imageWidth, tgaFile->imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, tgaFile->imageData); free(tgaFile->imageData); } free(tgaFile); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTexId); }
void BuildFont(bool UseDDS) // Build Our Font Display List { /* if (LoadTGA(&textureAlphaFont[0], "Data/font.tga")) { // Typical Texture Generation Using Data From The TGA ( CHANGE )textureAlphaFontFont[1] glGenTextures(1, &textureAlphaFont[0].texID); // Create The Texture ( CHANGE ) glBindTexture(GL_TEXTURE_2D, textureAlphaFont[0].texID); glTexImage2D(GL_TEXTURE_2D, 0, textureAlphaFont[0].bpp / 8, textureAlphaFont[0].width, textureAlphaFont[0].height, 0, textureAlphaFont[0].type, GL_UNSIGNED_BYTE, textureAlphaFont[0].imageData); if(IsSupportFBO) { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); } if (textureAlphaFont[0].imageData) // If Texture Image Exists ( CHANGE ) { free(textureAlphaFont[0].imageData); // Free The Texture Image Memory ( CHANGE ) } } */ if(UseDDS) { CDDS loadDDS; AsciiFontTexId=loadDDS.loadCompressedTexture("Data/Font.dds"); } else { AsciiFontTexId=LoadTGAFile("Data/Font.tga"); } float cx; // Holds Our X Character Coord float cy; // Holds Our Y Character Coord base=glGenLists(256); // Creating 256 Display Lists glBindTexture(GL_TEXTURE_2D, AsciiFontTexId); // Select Our Font Texture for (int loop=0; loop<256; loop++) // Loop Through All 256 Lists { cx=float(loop%16)/16.0f; // X Position Of Current Character cy=float(loop/16)/16.0f; // Y Position Of Current Character glNewList(base+loop,GL_COMPILE); // Start Building A List glBegin(GL_QUADS); // Use A Quad For Each Character glTexCoord2f(cx+0.015625f,1.0f-cy-0.0625f); // Texture Coord (Bottom Left) glVertex2i(0,0); // Vertex Coord (Bottom Left) glTexCoord2f(cx+0.0625f+0.015625f,1.0f-cy-0.0625f); // Texture Coord (Bottom Right) glVertex2i(16,0); // Vertex Coord (Bottom Right) glTexCoord2f(cx+0.0625f+0.015625f,1.0f-cy); // Texture Coord (Top Right) glVertex2i(16,16); // Vertex Coord (Top Right) glTexCoord2f(cx+0.015625f,1.0f-cy); // Texture Coord (Top Left) glVertex2i(0,16); // Vertex Coord (Top Left) glEnd(); // Done Building Our Quad (Character) glTranslated(10.0f,0.0f,0.0f); // Move To The Right Of The Character glEndList(); // Done Building The Display List } // Loop Until All 256 Are Built }
// 同时载入模型和皮肤/纹理 int CMD2Model::Load(char* modelFile, char* skinFile) { // 文件指针 FILE* filePtr; // 模型文件的长度 int fileLen; // 文件缓存 char* buffer; // 模型头 modelHeader_t* modelHeader; // 纹理数据 stIndex_t* stPtr; // 帧数据 frame_t* frame; // 索引变量 vector_t* vertexListPtr; // 索引变量 mesh_t* bufIndexPtr; /* 打开模型文件 */ filePtr = fopen(modelFile, "rb"); if (NULL == filePtr) { return NULL; } /* 获取文件长度 */ fseek(filePtr, 0, SEEK_END); fileLen = ftell(filePtr); fseek(filePtr, 0, SEEK_SET); /* 将整个文件读入缓存 */ buffer = new char[fileLen + 1]; fread(buffer, sizeof(char), fileLen, filePtr); // 从缓存中提取模型头 modelHeader = (modelHeader_t*)buffer; // 为模型的所有顶点分配内存,包括动画 vertexList = new vector_t[modelHeader->numXYZ * modelHeader->numFrames]; /* 存储有效的模型数据 */ // 顶点的数量 numVertices = modelHeader->numXYZ; // 帧的数量 numFrames = modelHeader->numFrames; // 每一帧的大小 frameSize = modelHeader->frameSize; /* 循环遍历每一帧,将模型顶点载入 */ for (int j = 0; j < numFrames; j++) { // 当前帧中顶点数据的偏移量 frame = (frame_t*) &buffer[modelHeader->offsetFrames + frameSize * j]; // 基于帧的细节信息计算顶点的位置 vertexListPtr = (vector_t*)&vertexList[numVertices * j]; for (int i = 0; i < numVertices; i++) { vertexListPtr[i].point[0] = frame->scale[0] * frame->fp[i].v[0] + frame->translate[0]; vertexListPtr[i].point[1] = frame->scale[1] * frame->fp[i].v[1] + frame->translate[1]; vertexListPtr[i].point[2] = frame->scale[2] * frame->fp[i].v[2] + frame->translate[2]; } } /* 载入模型纹理 */ TGAFILE tgaFile; LoadTGAFile(skinFile, &tgaFile); modelTex = new texture_t; modelTex->data = tgaFile.imageData; modelTex->width = tgaFile.imageWidth; modelTex->height = tgaFile.imageHeight; modelTex->textureType = TGA; SetupSkin(modelTex); // 存储纹理坐标的数量 numST = modelHeader->numST; // 为模型的纹理坐标分配内存 st = new texCoord_t[numST]; // 设置纹理指针,指向纹理坐标的偏移量 stPtr = (stIndex_t*)&buffer[modelHeader->offsetST]; // 为模型计算和存储纹理坐标 for (int i = 0; i < numST; i++) { st[i].s = (float)stPtr[i].s / (float)modelTex->width; st[i].t = (float)stPtr[i].t / (float)modelTex->height; } // 设置总的三角形的数量 numTriangles = modelHeader->numTris; // 为三角形索引列表分配内存 triIndex = new mesh_t[numTriangles]; // 指向缓存中的三角形索引 bufIndexPtr = (mesh_t*)&buffer[modelHeader->offsetTris]; // 生成一个网格(三角形)列表 for (int j = 0; j < numFrames; j++) { // 每一帧的所有三角形 for (int i = 0; i < numTriangles; i++) { triIndex[i].meshIndex[0] = bufIndexPtr[i].meshIndex[0]; triIndex[i].meshIndex[1] = bufIndexPtr[i].meshIndex[1]; triIndex[i].meshIndex[2] = bufIndexPtr[i].meshIndex[2]; triIndex[i].stIndex[0] = bufIndexPtr[i].stIndex[0]; triIndex[i].stIndex[1] = bufIndexPtr[i].stIndex[1]; triIndex[i].stIndex[2] = bufIndexPtr[i].stIndex[2]; } } /* 关闭文件并释放内存 */ fclose(filePtr); delete[] buffer; currentFrame = 0; nextFrame = 1; interpol = 0.0f; return 1; } // End of Load()
bool CSkyBox::Init(void) { glGenQueries(1,&SunQuery); if(!IsSupportFBO) { SunTexID=LoadTGAFile("Data/sky/sun.tga"); SkyTexID[0]=LoadTGAFile("Data/sky/BK.tga"); SkyTexID[1]=LoadTGAFile("Data/sky/DN.tga"); SkyTexID[2]=LoadTGAFile("Data/sky/FR.tga"); SkyTexID[3]=LoadTGAFile("Data/sky/LF.tga"); SkyTexID[4]=LoadTGAFile("Data/sky/RT.tga"); SkyTexID[5]=LoadTGAFile("Data/sky/UP.tga"); return true; } CDDS loadDDS; SunTexID=loadDDS.loadCompressedTexture("Data/sky/sun.dds"); SkyTexID[0]=loadDDS.loadCompressedTexture("Data/sky/BK.dds"); SkyTexID[1]=loadDDS.loadCompressedTexture("Data/sky/DN.dds"); SkyTexID[2]=loadDDS.loadCompressedTexture("Data/sky/FR.dds"); SkyTexID[3]=loadDDS.loadCompressedTexture("Data/sky/LF.dds"); SkyTexID[4]=loadDDS.loadCompressedTexture("Data/sky/RT.dds"); SkyTexID[5]=loadDDS.loadCompressedTexture("Data/sky/UP.dds"); for(int i=0;i<6;i++) { glBindTexture(GL_TEXTURE_2D, SkyTexID[i]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } //if(SkyTexID[0]==0) glGenTextures(1, &SunCubeID); glBindTexture(GL_TEXTURE_CUBE_MAP_EXT, SunCubeID); //if(SkyTexID[4]==0) if(LoadTGA(&SkyTex[4],"Data/sky/LF.tga")) { //glGenTextures(1,&SkyTex[4].texID); //glBindTexture(GL_TEXTURE_2D, SkyTex[4].texID); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT, 0, SkyTex[4].bpp/8, SkyTex[4].width, SkyTex[4].height, 0, SkyTex[4].type, GL_UNSIGNED_BYTE, SkyTex[4].imageData); if(IsSupportFBO) { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); } if (SkyTex[4].imageData) // If Texture Image Exists ( CHANGE ) { free(SkyTex[4].imageData); // Free The Texture Image Memory ( CHANGE ) } //SkyTexID[4]=SkyTex[4].texID; } else return false; //if(SkyTexID[3]==0) if(LoadTGA(&SkyTex[3],"Data/sky/RT.tga")) { //glGenTextures(1,&SkyTex[3].texID); //glBindTexture(GL_TEXTURE_2D, SkyTex[3].texID); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT, 0, SkyTex[3].bpp/8, SkyTex[3].width, SkyTex[3].height, 0, SkyTex[3].type, GL_UNSIGNED_BYTE, SkyTex[3].imageData); if(IsSupportFBO) { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); } if (SkyTex[3].imageData) // If Texture Image Exists ( CHANGE ) { free(SkyTex[3].imageData); // Free The Texture Image Memory ( CHANGE ) } //SkyTexID[3]=SkyTex[3].texID; } else return false; //if(SkyTexID[5]==0) if(LoadTGA(&SkyTex[5],"Data/sky/UP.tga")) { //glGenTextures(1,&SkyTex[5].texID); //glBindTexture(GL_TEXTURE_2D, SkyTex[5].texID); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT, 0, SkyTex[5].bpp/8, SkyTex[5].width, SkyTex[5].height, 0, SkyTex[5].type, GL_UNSIGNED_BYTE, SkyTex[5].imageData); if(IsSupportFBO) { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); } if (SkyTex[5].imageData) // If Texture Image Exists ( CHANGE ) { free(SkyTex[5].imageData); // Free The Texture Image Memory ( CHANGE ) } //SkyTexID[5]=SkyTex[5].texID; } else return false; //if(SkyTexID[1]==0) if(LoadTGA(&SkyTex[1],"Data/sky/DN.tga")) { //glGenTextures(1,&SkyTex[1].texID); //glBindTexture(GL_TEXTURE_2D, SkyTex[1].texID); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT, 0, SkyTex[1].bpp/8, SkyTex[1].width, SkyTex[1].height, 0, SkyTex[1].type, GL_UNSIGNED_BYTE, SkyTex[1].imageData); if(IsSupportFBO) { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); } if (SkyTex[1].imageData) // If Texture Image Exists ( CHANGE ) { free(SkyTex[1].imageData); // Free The Texture Image Memory ( CHANGE ) } //SkyTexID[1]=SkyTex[1].texID; } else return false; if(LoadTGA(&SkyTex[0],"Data/sky/FR.tga")) { //glGenTextures(1,&SkyTex[0].texID); //glBindTexture(GL_TEXTURE_2D, SkyTex[0].texID); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT, 0, SkyTex[0].bpp/8, SkyTex[0].width, SkyTex[0].height, 0, SkyTex[0].type, GL_UNSIGNED_BYTE, SkyTex[0].imageData); if(IsSupportFBO) { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); } if (SkyTex[0].imageData) // If Texture Image Exists ( CHANGE ) { free(SkyTex[0].imageData); // Free The Texture Image Memory ( CHANGE ) } //SkyTexID[0]=SkyTex[0].texID; } else return false; //if(SkyTexID[2]==0) if(LoadTGA(&SkyTex[2],"Data/sky/BK.tga")) { //glGenTextures(1,&SkyTex[2].texID); //glBindTexture(GL_TEXTURE_2D, SkyTex[2].texID); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT, 0, SkyTex[2].bpp/8, SkyTex[2].width, SkyTex[2].height, 0, SkyTex[2].type, GL_UNSIGNED_BYTE, SkyTex[2].imageData); if(IsSupportFBO) { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } else { glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT,GL_TEXTURE_MAG_FILTER,GL_NEAREST); } if (SkyTex[2].imageData) // If Texture Image Exists ( CHANGE ) { free(SkyTex[2].imageData); // Free The Texture Image Memory ( CHANGE ) } //SkyTexID[2]=SkyTex[2].texID; } else return false; return true; }