【问题标题】:Why is my texture getting rotated by 90 degrees?为什么我的纹理会旋转 90 度?
【发布时间】:2021-12-10 13:50:00
【问题描述】:

我正在尝试绘制一个带有纹理的简单四边形。我正在使用 gluOrtho2d 来设置正交投影。我无法理解为什么四边形内部的纹理顺时针旋转了 90°。纹理是一个简单的 PNG 文件。

这是有问题的代码:-

#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <SFML/Graphics.hpp>
#include <iostream>


using namespace std;


const int WINDOW_WIDTH = 800, WINDOW_HEIGHT = 600;
sf::Texture texture;


void draw() {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    float x = 0;
    float y = 0;

    sf::Texture::bind(&texture);

    glBegin(GL_QUADS);
    glVertex2f(x, y);
    glTexCoord2f(0, 0);
    glVertex2f(x + 400, y);
    glTexCoord2f(1, 0);
    glVertex2f(x + 400, y + 400);
    glTexCoord2f(1, 1);
    glVertex2f(x, y + 400);
    glTexCoord2f(0, 1);
    glEnd();
}


int main() {
    sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "problem");
    window.setActive(true);


    texture.loadFromFile("texture.png");

    glewInit();
    glEnable(GL_TEXTURE_2D);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
    glMatrixMode(GL_MODELVIEW);

    bool isRunning = true;
    while(isRunning) {
        sf::Event event;
        while(window.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                isRunning = false;
            }
        }
        draw();
        window.display();
    }
}

这是输出。在这里可以看到纹理旋转了 90°

【问题讨论】:

    标签: c++ opengl sfml


    【解决方案1】:

    glVertex*() 调用“锁定”顶点的当前颜色/纹理坐标/位置,并将这些值传递给 GL 驱动程序:

    glVertex 命令在 glBegin/glEnd 对中用于指定点、线和多边形顶点。 调用glVertex时与顶点关联的当前颜色、法线、纹理坐标和雾坐标。

    你现在的样子:

    glVertex2f(x, y);
    glTexCoord2f(0, 0);
    

    ...第一个 glVertex() 调用正在重新使用最近设置的纹理坐标,来自前一帧的那些:glTexCoord2f(0, 1)

    所以你需要为一个顶点设置纹理坐标之前的位置:

    glTexCoord2f(0, 0);
    glVertex2f(x, y);
    glTexCoord2f(1, 0);
    glVertex2f(x + 400, y);
    glTexCoord2f(1, 1);
    glVertex2f(x + 400, y + 400);
    glTexCoord2f(0, 1);
    glVertex2f(x, y + 400);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      相关资源
      最近更新 更多