我正在学习如何在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;
}
答案 0 :(得分:5)
OpenGL是一个状态机。这意味着最后一个纹理坐标集将应用于所有后续顶点。因此,即使您停止提供纹理坐标,它仍将在应用于整个面的最后一个设置纹理坐标处应用纹理的颜色。
要停止纹理,您必须使用glDisable(GL_TEXTURE_2D);
将代码放入代码中来禁用纹理:
// ...
App.SetActive();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D); // <- enable texturing for the one face
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
glEnd(); // <- first end the current primitive list
glDisable(GL_TEXTURE_2D); // <- disable texturing for the rest of the faces
glBegin(GL_QUADS);
//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
//...
答案 1 :(得分:2)
尝试在绘制纹理面之前启用GL_TEXTURE2D,然后立即禁用它。
...
//back
glEnable(GL_TEXTURE2D);
glBegin(GL_QUADS);
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
glEnd();
glDisable(GL_TEXTURE2D);
glBegin(GL_QUADS);
//bottom
...
答案 2 :(得分:0)
你是对的,现在这里是正确的代码:
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
//back
glColor3f(1.0f,1.0f,1.0f); //white
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
glEnd();
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
//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();