我是C / C ++的学生和初学者,我尝试在opengl中制作动画,我的文字和动画可以正常工作,但是我想使用bmp这样的文件在其中放置背景图片,或者随你。因此,经过几次阅读后,我尝试了Quads技术,该技术只是显示一个Quad并将纹理绑定到它。
我使用STB_Image库,似乎我正确指向了我的文件(如果我在文件名上犯了错误,我的程序肯定会得到更快的响应)。然后我实施了打印,以查看它是否捕获了正确的文件并且可以打印!
我的代码看起来像这样,我的结果是在正确的坐标中出现一个白色正方形,但没有纹理出现,文件已正确加载(打印了正确的大小),它没有绑定或没有出现在正方形上...有任何想法吗 ?预先感谢。
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
int width, height, nrChannels;
unsigned char* data = stbi_load("test.jpg", &width, &height, &nrChannels, 0);
if (data == NULL) {
printf(“加载图像时出错\ n”);出口(1);
}
printf("Loaded image with a width of %dpx, a height of %dpx and %d channels\n", width, height, nrChannels);
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glVertex2f(2.0, 1.0);
glTexCoord2f(2.0, 1.0);
glVertex2f(8.0, 1.0);
glTexCoord2f(8.0, 1.0);
glVertex2f(8.0, 7.0);
glTexCoord2f(8.0, 7.0);
glVertex2f(2.0, 7.0);
glTexCoord2f(2.0, 7.0);
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glTexImage2D
指定纹理对象的二维纹理图像。您必须要做的是绘制四边形。此外,必须通过glTexImage2D
启用二维纹理化。纹理坐标必须在[0.0,1.0]范围内(请参见glEnable(GL_TEXTURE_2D)
)。
How do opengl texture coordinates work?
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
glBindTexture(GL_TEXTURE_2D, 0);