首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有OpenGL 4的旋转矩阵

带有OpenGL 4的旋转矩阵
EN

Stack Overflow用户
提问于 2014-05-27 21:32:39
回答 1查看 1.6K关注 0票数 0

我试着用矩阵旋转一个OpenGL 4的三角形,但是这个三角形在一半的时候出现了相反的方向。这显然不是旋转的方向,所以我一定是找错了深度缓冲器。我尝试过解决这个问题,但是我还没有找到任何OpenGL 4旋转的例子,只有那些使用折旧函数的例子。这是我的代码:

代码语言:javascript
复制
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include "maths_funcs.h"
#include "maths_funcs.cpp"

const char* vertex_shader =
"#version 400\n"
"layout(location = 0) in vec3 vertex_position;"
"layout(location = 1) in vec3 vertex_colour;"
"uniform mat4 pmatrix;"
"uniform mat4 rmatrix;"
"uniform mat4 smatrix;"
"out vec3 colour;"
"void main () {"
"  colour = vertex_colour;"
"  gl_Position = smatrix * rmatrix * vec4 (vertex_position, 1.0);"
"}";

const char* fragment_shader =
"#version 400\n"
"in vec3 colour;"
"out vec4 frag_colour;"
"void main () {"
"  frag_colour = vec4 (colour, 1.0);"
"}";

float angle = 0.0f;

int main () {
    // start GL context and O/S window using the GLFW helper library
    if (!glfwInit ()) {
        fprintf (stderr, "ERROR: could not start GLFW3\n");
        return 1;
    } 

    GLFWwindow* window = glfwCreateWindow (640, 480, "Triangle Rotation test", NULL, NULL);
    if (!window) {
        fprintf (stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent (window);

    // start GLEW extension handler
    glewExperimental = GL_TRUE;
    glewInit ();

    // get version info
    const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString (GL_VERSION); // version as a string
    printf ("Renderer: %s\n", renderer);
    printf ("OpenGL version supported %s\n", version);

    // tell GL to only draw onto a pixel if the shape is closer to the viewer
    glEnable (GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

    float points[] = {
        0.0f,  0.5f,  0.0f,
        0.5f, -0.5f,  0.0f,
        -0.5f, -0.5f,  0.0f
    };

    float colours[] = {
        1.0f, 0.0f,  0.0f,
        0.0f, 1.0f,  0.0f,
        0.0f, 0.0f,  1.0f
    };

    float pmatrix[] = {
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f, 
        0.0f, 0.0f, 1.0f, 0.0f, 
        0.0f, 0.0f, 0.0f, 1.0f
    };

    float rmatrix[] = {
        cos(10), 0.0f, sin(10), 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f, 
        -sin(10), 0.0f, cos(10), 0.0f, 
        0.0f, 0.0f, 0.0f, 1.0f
    };

    float smatrix[] = {
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, cos(10), sin(10), 0.0f, 
        0.0f, -sin(10), cos(10), 0.0f, 
        0.0f, 0.0f, 0.0f, 1.0f
    };

    GLuint vbo = 0;
    glGenBuffers (1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);

    unsigned int colours_vbo = 0;
    glGenBuffers (1, &colours_vbo);
    glBindBuffer (GL_ARRAY_BUFFER, colours_vbo);
    glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), colours, GL_STATIC_DRAW);

    GLuint vao = 0;
    glGenVertexArrays (1, &vao);
    glBindVertexArray (vao);
    glEnableVertexAttribArray (0);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glBindBuffer (GL_ARRAY_BUFFER, colours_vbo);
    glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    GLuint vs = glCreateShader (GL_VERTEX_SHADER);
    glShaderSource (vs, 1, &vertex_shader, NULL);
    glCompileShader (vs);
    GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
    glShaderSource (fs, 1, &fragment_shader, NULL);
    glCompileShader (fs);

    GLuint shader_programme = glCreateProgram ();
    glAttachShader (shader_programme, fs);
    glAttachShader (shader_programme, vs);
    glLinkProgram (shader_programme);

    int pmatrix_location = glGetUniformLocation (shader_programme, "pmatrix");
    int rmatrix_location = glGetUniformLocation (shader_programme, "rmatrix");
    int smatrix_location = glGetUniformLocation (shader_programme, "smatrix");

    while (!glfwWindowShouldClose (window)) {
        // wipe the drawing surface clear
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram (shader_programme);
        rmatrix[0] = cos(angle);
        rmatrix[2] = sin(angle);
        rmatrix[8] = -sin(angle);
        rmatrix[10] = cos(angle);

        smatrix[5] = cos(angle);
        smatrix[6] = sin(angle);
        smatrix[9] = -sin(angle);
        smatrix[10] = cos(angle);
        glUniformMatrix4fv (pmatrix_location, 1, GL_FALSE, pmatrix);
        glUniformMatrix4fv (rmatrix_location, 1, GL_FALSE, rmatrix);
        glUniformMatrix4fv (smatrix_location, 1, GL_FALSE, smatrix);
        glBindVertexArray (vao);
        // draw points 0-3 from the currently bound VAO with current in-use shader
        glDrawArrays (GL_TRIANGLES, 0, 3);
        // update other events like input handling 
        glfwPollEvents ();
        // put the stuff we've been drawing onto the display
        glfwSwapBuffers (window);

        angle+=0.01f;
        if (angle >= 3.14f) {
            angle = -3.14f;
        }
    }

    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-28 04:35:15

因为你没有使用投影矩阵,你必须在你的顶点着色器中反演z坐标。更改这一行:

代码语言:javascript
复制
gl_Position = smatrix * rmatrix * vec4 (vertex_position, 1.0);

像这样的事情:

代码语言:javascript
复制
vec4 eyePos = smatrix * rmatrix * vec4 (vertex_position, 1.0);
gl_Position = vec4(eyePos.xy, -eyePos.z, eyePos.w);

gl_Position值在剪辑坐标中,经过w除法后得到NDC (归一化设备坐标)。世界坐标通常在右手坐标系中指定,z轴指向屏幕外,NDC是一个左手坐标系,z轴指向屏幕的。

典型的投影转换负责将z坐标从屏幕外翻转到屏幕上.但是,如果你没有使用投影变换,你必须翻转z坐标你自己。

另外,您可能需要再次检查是否确实有深度缓冲区。也许在glfwCreateWindow()之前添加这个

代码语言:javascript
复制
glfwWindowHint(GLFW_DEPTH_BITS, 24);

根据我在GLFW文档中可以找到的内容,这可能是默认的。但是,明确地指定它不会有什么害处。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23899335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档