openGL 纹理和颜色

发布于 2024-11-16 14:27:57 字数 4230 浏览 2 评论 0原文

我正在学习如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拧巴小姐 2024-11-23 14:27:57

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

//...

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:

// ...
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

//...
指尖上的星空 2024-11-23 14:27:57

尝试在绘制纹理面之前启用 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
...

Try enabling GL_TEXTURE2D right before your textured faces are drawn, and disable it immediately after.

...
//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
...
╰つ倒转 2024-11-23 14:27:57

你是绝对正确的,现在这是正确的代码:

    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();

you are absolutely right, here is the correct code now:

    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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文