openGL 纹理和颜色
我正在学习如何在 openGL 中应用纹理。 我将纹理应用到立方体的一个面上,并使用原始颜色为其他面着色。 在我应用纹理之前,颜色看起来很好,在应用它之后,它们变得更暗。 怎么了?
void LoadGLTextures() {
// Load Texture
Image *image1;
// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}
if (!ImageLoad("dd.bmp", image1)) {
exit(1);
}
// Create Texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
}
int main()
{
sf::Window App(sf::VideoMode(600, 600, 32), "SFML OpenGL", sf::Style::Close);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
LoadGLTextures();
glBindTexture(GL_TEXTURE_2D, texture[0]);
while (App.IsOpened())
{
App.SetActive();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//back
glColor3f(0.0f,0.0f,0.0f); //cyan
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.2f,0.2f,0.2f); //top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.2f,0.2f,0.2f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.2f,-0.2f,0.2f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.2f,-0.2f,0.2f); //bottom left
//bottom
glColor3f(0.0f,1.0f,0.0f); //green
glVertex3f(0.2f,-0.2f,0.2f); //back right
glVertex3f(-0.2f,-0.2f,0.2f); //back left
glVertex3f(-0.2f,-0.2f,-0.2f); //front left
glVertex3f(0.2f,-0.2f,-0.2f); //front right
//front
glColor3f(0.0f,0.0f,1.0f); //blue
glVertex3f(0.2f,-0.2f,-0.2f); //bottom right
glVertex3f(-0.2f,-0.2f,-0.2f); //bottom left
glVertex3f(-0.2f,0.2f,-0.2f); //top left
glVertex3f(0.2f,0.2f,-0.2f); //top right
//top
glColor3f(1.0f,1.0f,0.0f); //yellow
glVertex3f(0.2f,0.2f,-0.2f); //front right
glVertex3f(-0.2f,0.2f,-0.2f); //front left
glVertex3f(-0.2f,0.2f,0.2f); //back left
glVertex3f(0.2f,0.2f,0.2f); //back right
//left
glColor3f(0.0f,1.0f,1.0f); //pink
glVertex3f(-0.2f,-0.2f,-0.2f); //bottom front
glVertex3f(-0.2f,-0.2f,0.2f); //bottom back
glVertex3f(-0.2f,0.2f,0.2f); //top back
glVertex3f(-0.2f,0.2f,-0.2f); //top front
//right
glColor3f(1.0f,0.0f,0.0f); //red
glVertex3f(0.2f,-0.2f,-0.2f); //bottom front
glVertex3f(0.2f,-0.2f,0.2f); //bottom back
glVertex3f(0.2f,0.2f,0.2f); //top back
glVertex3f(0.2f,0.2f,-0.2f); //top front
glEnd();
glFlush();
App.Display();
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
glRotated(20.0d,0.0d,0.2d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
glRotated(-20.0d,0.0d,0.2d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
glRotated(20.0d,0.2d,0.0d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
glRotated(-20.0d,0.2d,0.0d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageUp))
glRotated(20.0d,0.0d,0.0d,0.2d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageDown))
glRotated(-20.0d,0.0d,0.0d,0.2d);
}
}
return EXIT_SUCCESS;
}
I'm learning how to apply texture in openGL.
I applied a texture to one face of a cube and colored the other faces using primitive colors.
Before i apply the texture the colors looked fine, and after i apply it they become darker.
what's wrong?
void LoadGLTextures() {
// Load Texture
Image *image1;
// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}
if (!ImageLoad("dd.bmp", image1)) {
exit(1);
}
// Create Texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
}
int main()
{
sf::Window App(sf::VideoMode(600, 600, 32), "SFML OpenGL", sf::Style::Close);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
LoadGLTextures();
glBindTexture(GL_TEXTURE_2D, texture[0]);
while (App.IsOpened())
{
App.SetActive();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//back
glColor3f(0.0f,0.0f,0.0f); //cyan
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.2f,0.2f,0.2f); //top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.2f,0.2f,0.2f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.2f,-0.2f,0.2f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.2f,-0.2f,0.2f); //bottom left
//bottom
glColor3f(0.0f,1.0f,0.0f); //green
glVertex3f(0.2f,-0.2f,0.2f); //back right
glVertex3f(-0.2f,-0.2f,0.2f); //back left
glVertex3f(-0.2f,-0.2f,-0.2f); //front left
glVertex3f(0.2f,-0.2f,-0.2f); //front right
//front
glColor3f(0.0f,0.0f,1.0f); //blue
glVertex3f(0.2f,-0.2f,-0.2f); //bottom right
glVertex3f(-0.2f,-0.2f,-0.2f); //bottom left
glVertex3f(-0.2f,0.2f,-0.2f); //top left
glVertex3f(0.2f,0.2f,-0.2f); //top right
//top
glColor3f(1.0f,1.0f,0.0f); //yellow
glVertex3f(0.2f,0.2f,-0.2f); //front right
glVertex3f(-0.2f,0.2f,-0.2f); //front left
glVertex3f(-0.2f,0.2f,0.2f); //back left
glVertex3f(0.2f,0.2f,0.2f); //back right
//left
glColor3f(0.0f,1.0f,1.0f); //pink
glVertex3f(-0.2f,-0.2f,-0.2f); //bottom front
glVertex3f(-0.2f,-0.2f,0.2f); //bottom back
glVertex3f(-0.2f,0.2f,0.2f); //top back
glVertex3f(-0.2f,0.2f,-0.2f); //top front
//right
glColor3f(1.0f,0.0f,0.0f); //red
glVertex3f(0.2f,-0.2f,-0.2f); //bottom front
glVertex3f(0.2f,-0.2f,0.2f); //bottom back
glVertex3f(0.2f,0.2f,0.2f); //top back
glVertex3f(0.2f,0.2f,-0.2f); //top front
glEnd();
glFlush();
App.Display();
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
glRotated(20.0d,0.0d,0.2d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
glRotated(-20.0d,0.0d,0.2d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
glRotated(20.0d,0.2d,0.0d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
glRotated(-20.0d,0.2d,0.0d,0.0d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageUp))
glRotated(20.0d,0.0d,0.0d,0.2d);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageDown))
glRotated(-20.0d,0.0d,0.0d,0.2d);
}
}
return EXIT_SUCCESS;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
OpenGL 是一个状态机。这意味着最后一个纹理坐标集将应用于所有后续顶点。因此,即使您停止提供纹理坐标,它仍然会在应用于整个面的最后设置的纹理坐标处应用纹理的颜色。
要停止纹理,您必须使用
glDisable(GL_TEXTURE_2D);
禁用纹理,将其放入代码中:OpenGL is a state machine. This means that the last texture coordinate set will be applied to all following vertices. So even if you stop supplying texture coordinates it will still apply the texture's colour at the last set texture coordinate applied to the whole faces.
To stop texturing you have to disable texturing with
glDisable(GL_TEXTURE_2D);
Putting it in your code:尝试在绘制纹理面之前启用 GL_TEXTURE2D,并在绘制之后立即禁用它。
Try enabling GL_TEXTURE2D right before your textured faces are drawn, and disable it immediately after.
你是绝对正确的,现在这是正确的代码:
you are absolutely right, here is the correct code now: