【发布时间】:2018-06-01 17:07:26
【问题描述】:
我的代码没有将纹理渲染到它正在绘制的三角形上。当我在其他项目中使用它时,三角形以正确的方式绘制并且图像加载和纹理代码已经工作。
unsigned char* gen_tex(std::string filename, int *texWidth, int *texHeight, int *texChannels)
{
unsigned char* image = SOIL_load_image(("Data/"+filename).c_str(), &*texWidth, &*texHeight, &*texChannels, SOIL_LOAD_AUTO);
return image;
}
void clean_tex(unsigned char* image)
{
SOIL_free_image_data(image);
}
^图像加载和清理功能似乎没有问题。
这是与纹理相关的唯一代码区域,它每帧都运行。三角形本身正确显示在屏幕上,但只是使用设置的 glColor 绘制
int texWidth;
int texHeight;
int texChannels;
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLuint tex;
glGenTextures(1, &tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
unsigned char* image = gen_tex(std::string("white.png"),&texWidth,&texHeight,&texChannels);
if(texChannels == 4)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
}
clean_tex(image);
for(int i=0; i<tNum; i++)
{
glBegin(GL_TRIANGLES);
glTexCoord2f( 0, 0); glVertex3f(triangles[i].A.x, triangles[i].A.y, triangles[i].A.z);
glTexCoord2f(0.5,0.5); glVertex3f(triangles[i].B.x, triangles[i].B.y, triangles[i].B.z);
glTexCoord2f( 0, 1); glVertex3f(triangles[i].C.x, triangles[i].C.y, triangles[i].C.z);
glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
【问题讨论】:
-
glGetError()说什么?你能发布一个最小的完整代码吗? -
glGetError() 只是输出 0