正投影设置

时间:2017-08-07 15:02:27

标签: c++ opengl orthographic

我安装了所需的所有元素(着色器,vao / vbo,绑定,绘图),但我的表单仍然没有出现。如果我省略了视图和投影矩阵,则会显示该表单,但如果我添加它们全部消失,我忘记包含在以下代码中:

编辑的代码:

main.cpp:

#include <GL/glew.h>
#include <SFML/Window.hpp>

#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <vector>

#include "ShaderProgram.h"
#include "VHandler.h"

using namespace std;
using namespace sf;

const int WIDTH = 800;
const int HEIGHT = 600;

int main()
{
    /*************************************/
    ContextSettings settings;
    settings.depthBits = 24;
    settings.stencilBits = 8;
    /*************************************/

    /*************************************/
    Window window(VideoMode(WIDTH,HEIGHT),
                  "GLWindow",
                  Style::Titlebar|Style::Close,
                  settings);    
    bool running = true;
    /*************************************/

    /*************************************/
    glewExperimental = GL_TRUE;
    glewInit();
    /*************************************/

    /*************************************/
    ShaderProgram shader;
    shader.loadShaders("shaders/vShader.vert","shaders/fShader.frag");
    /*************************************/

    /*************************************/
    /**VAOs & VBOs**/
    vector<float> vertices{{0.0f, 0.0f,0.0f,
                            0.0f, 1.0f,0.0f,
                            1.0f, 1.0f,0.0f,

                            0.0f, 0.0f,0.0f,
                            1.0f, 1.0f,0.0f,
                            1.0f, 0.0f,0.0f}};

    vector<float> colors{{1,0,0,0,1,0,0,0,1,1,1,1}};

    VHandler handler;
    handler.loadData(vertices,colors);
    /*************************************/


    /*************************************/
    GLuint uniProjection = glGetUniformLocation(shader.getProgram(),"projection");
    GLuint uniView = glGetUniformLocation(shader.getProgram(),"view");
    GLuint uniTransform = glGetUniformLocation(shader.getProgram(),"transform");

    glm::mat4 projection = glm::mat4(1);
    projection = glm::ortho(0.0f,static_cast<float>(WIDTH),static_cast<float>(HEIGHT),0.0f,1.0f,-1.0f);
    /*************************************/

    while(running)
    {
        Event windowEvt;
        while(window.pollEvent(windowEvt))
        {
            if(windowEvt.type == Event::Closed){running = false; break;}
        }

        /*************************************/
        glClearColor(0,0,0,1);
        glClear(GL_COLOR_BUFFER_BIT);
        /*************************************/

        /*************************************/
        shader.startProgram();
        handler.bindVao();
        /*************************************/

        /*************************************/     
        glUniformMatrix4fv(uniProjection,1,GL_FALSE,glm::value_ptr(projection));

        glm::mat4 view;
        glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));

        glm::mat4 transform = glm::mat4(1);
        transform = glm::translate(transform, glm::vec3(0.0f, 0.0f, 0.0f));
        transform = glm::scale(transform, glm::vec3(10.0f, 10.0f, 0.0f));
        glUniformMatrix4fv(uniTransform, 1, GL_FALSE, glm::value_ptr(transform));
        /*************************************/

        /*************************************/
        glDrawArrays(GL_TRIANGLES, 0, 6);
        handler.unbindVao();
        shader.endProgram();
        /*************************************/

        /*************************************/
        window.display();
        /*************************************/
    }

    /*************************************/
    handler.cleanMemory();
    shader.cleanShaders();
    window.close();
    /*************************************/

    return 0;
}

vertexShader:

#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
out vec3 Color;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 transform;

void main()
{
Color = color;
//gl_Position = projection * view * transform * vec4(position, 1.0); //Can't see anything
//gl_Position = projection * transform * vec4(position, 1.0); //Can't see anything
gl_Position = transform * vec4(position, 1.0); //Works correctly
}

如果我理解正交投影的操作,我的表格应该出现在左上角,宽度和高度为10px,但我不知道我错过了什么。

1 个答案:

答案 0 :(得分:0)

正如@Ripi评论的那样,这个问题的答案是:

glUniformMatrix4fv(uniProjection,1,GL_FALSE,glm::value_ptr(projection));

激活着色器shader.startProgram();后,在顶点着色器中考虑矩阵。