【问题标题】:OpenGL not rendering with texturesOpenGL不使用纹理渲染
【发布时间】: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

标签: c++ opengl


【解决方案1】:

你需要按如下方式绑定纹理:

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

与您的问题没有直接关系:

  • 你不应该每帧都加载纹理,那是非常低效的。只需加载一次并在框架之间重复使用。
  • 不要使用即时模式 API。这是遗留问题,对您的健康有害。

【讨论】:

    【解决方案2】:

    好的,这是一个真正的面部手掌时刻。 我设法不小心删除了对 glColor() 的调用,因此它以那种色调绘制了整个东西。看到我是个傻瓜并使用纯色纹理,它以错误的颜色渲染,我一直认为什么都没有出现。 我忘记了我有时是多么愚蠢。无论如何谢谢,问题现在解决了。

    【讨论】:

      猜你喜欢
      • 2014-08-09
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多