1

OpenGL glm 计算似乎在我的程序中不起作用。即使我使用 glm translate 函数每帧用一个变量平移 z 轴,也没有任何移动。我错过了什么吗?

主文件

#define GLEW_STATIC
#define NO_SDL_GLEXT
#include "glew.h"
#include <sdl.h>
#undef main
#include "SDL_opengl.h"
#include "timer.h"
#include <time.h>
#include <shader.h>
using namespace std;
#include <glm/gtc/matrix_projection.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
unsigned int vaoID[1]; // Our Vertex Array Object
unsigned int vboID[1]; // Our Vertex Buffer Object
glm::mat4 projectionMatrix; // Store the projection matrix
glm::mat4 viewMatrix; // Store the view matrix
glm::mat4 modelMatrix; // Store the model matrix
Shader *shader; // Our GLSL shader
float ztransform(0);
bool exited(false);
SDL_Event event;
const int FRAMES_PER_SECOND = 60;
void createSquare(void) {
    float* vertices = new float[18];    // Vertices for our square
    vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
    vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
    vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner
    vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner
    vertices[12] = -0.5; vertices[13] = -0.5; vertices[14] = 0.0; // Bottom left corner
    vertices[15] = 0.5; vertices[16] = 0.5; vertices[17] = 0.0; // Top Right corner
    glGenVertexArrays(1, &vaoID[0]); // Create our Vertex Array Object
    glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object so we can use it
    glGenBuffers(1, vboID); // Generate our Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); // Bind our Vertex Buffer Object
    glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
    glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
    glEnableVertexAttribArray(0); // Disable our Vertex Array Object
    glBindVertexArray(0); // Disable our Vertex Buffer Object
    delete [] vertices; // Delete our vertices from memory
}
void startGL()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);
    glewInit();
    glClearColor(0.4f, 0.0f, 1.0f, 0.0f);
    projectionMatrix = glm::perspective(60.0f, (float)800 / (float)600, 0.1f, 100.f);  // Create our perspective projection matrix
    shader = new Shader("shader.vert", "shader.frag"); // Create our shader by loading our vertex and fragment shader
    createSquare();
}
void drawstuff()
{
    glViewport(0, 0, 800, 600); // Set the viewport size to fill the window
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clear required buffers
    viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, ztransform)); // Create our view matrix which will translate us back 5 units
    modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f));  // Create our model matrix which will halve the size of our model
    shader->bind(); // Bind our shader
    int projectionMatrixLocation = glGetUniformLocation(shader->id(), "projectionMatrix"); // Get the location of our projection matrix in the shader
    int viewMatrixLocation = glGetUniformLocation(shader->id(), "viewMatrix"); // Get the location of our view matrix in the shader
    int modelMatrixLocation = glGetUniformLocation(shader->id(), "modelMatrix"); // Get the location of our model matrix in the shader
    glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &projectionMatrix[0][0]); // Send our projection matrix to the shader
    glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]); // Send our view matrix to the shader
    glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &modelMatrix[0][0]); // Send our model matrix to the shader
    glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object
    glDrawArrays(GL_TRIANGLES, 0, 6); // Draw our square
    glBindVertexArray(0); // Unbind our Vertex Array Object
    shader->unbind(); // Unbind our shader
}
int main (int argc, char* args[])
{
    Timer fps;
    startGL();
    while(exited == false)
    {
        while( SDL_PollEvent(&event) )
        {
            if( event.type == SDL_QUIT )
                exited = true;
        }
        drawstuff();
        ztransform+=.1
        SDL_GL_SwapBuffers();
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
    }
    SDL_Quit();
    return 0;
}

着色器.frag

#version 150 core

in vec3 pass_Color;

out vec4 out_Color;

void main(void)
{
      out_Color = vec4(pass_Color, 1.0);
}

着色器.vert

#version 150 core

in vec3 in_Position;
in vec3 in_Color;

out vec3 pass_Color;

void main(void)
{
     gl_Position = vec4(in_Position, 1.0);
     pass_Color = in_Color;
}
4

1 回答 1

4

您必须在顶点着色器中应用您的转换。

你应该在你的顶点着色器中定义

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

然后将这些转换应用于您的输入位置(注意:我可能弄错了顺序)

gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_position, 1.0);

不过,通常情况下,您可以在 c++ 程序中将这 3 个矩阵相乘并传入一个modelViewProjection矩阵。

于 2013-01-24T04:05:06.820 回答