c++ - 模型、 View 和投影矩阵不适用于 opengl glsl

标签 c++ opengl-es glsl

我正在尝试使用 open gl 制作我的第一个立方体。不幸的是,我坚持轻松旋转。它应该是这样的:

img

相反,我越来越奇怪,甚至不知道如何调用它:https://youtu.be/0A5Hi-8bygE

顶点着色器

#version 330 core

layout (location = 0) in vec2 a_position;
layout (location = 1) in vec2 a_uv;
layout (location = 2) in vec4 a_color;

out vec2 v_fragmentPosition;
out vec2 v_fragmentUV;
out vec4 v_fragmentColor;

uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;

void main()
{
    gl_Position = u_projection * u_view * u_model * vec4(a_position, 0.0, 1.0);

    v_fragmentPosition = a_position;
    v_fragmentUV = vec2(a_uv.x, 1 - a_uv.y);
    v_fragmentColor = a_color;
}

片段

#version 330 core

out vec4 outColor;

in vec2 v_fragmentPosition;
in vec2 v_fragmentUV;
in vec4 v_fragmentColor;

void main()
{
    outColor = v_fragmentColor;
}

主要

#include <glad.h>

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

#include "Mesh.h"
#include "Shader.h"

int main(int argc,char*args[])
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    SDL_Window* window = SDL_CreateWindow("fdas", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(window);

    gladLoadGLLoader(SDL_GL_GetProcAddress);

    glEnable(GL_DEPTH_TEST);

    Shader shader;

    std::string vertex = shader.LoadFromFile("vertex.shader");
    std::string fragment = shader.LoadFromFile("fragment.shader");
    shader.CreateShaderProgram(vertex.c_str(), fragment.c_str());
    shader.useProgram();

    Mesh mesh;
    mesh.CreateVertexArray();

    bool quit = false;
    SDL_Event event;
    while (!quit)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
            exit(0);
                }
        }
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glm::mat4 model;
        model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));

        glm::mat4 view;
        view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));

        glm::mat4 projection;
        projection = glm::perspective(glm::radians(45.0f), 1280 / 720.0f, 0.1f, 100.0f);

        GLint u_model = shader.GetUniformLocation("u_model");
        GLint u_view = shader.GetUniformLocation("u_view");
        GLint u_projection = shader.GetUniformLocation("u_projection");

        glUniformMatrix4fv(u_model, 1, GL_FALSE, glm::value_ptr(model));
        glUniformMatrix4fv(u_view, 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(u_projection, 1, GL_FALSE, glm::value_ptr(projection));

        Rect destRect = { -0.5f, -0.5f, 1.0f, 1.0f };   
        Rect uvRect = { 0.0f, 0.0f, 1.0f, 1.0f };
        ColorRGBA color = { 255, 255, 128, 255 };

        mesh.CreateQuad(destRect, uvRect, color);
        mesh.Draw(0);

        SDL_GL_SwapWindow(window);
    }
    return 0;
}

网格

void Mesh::CreateVertexArray()
{
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
    glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));

    glBindVertexArray(0);
}

void Mesh::CreateQuad(Rect destRect, Rect uvRect, ColorRGBA color)
{
    vertex[0].color = color; // topleft
    vertex[0].setPosition(destRect.x, destRect.y + destRect.h);
    vertex[0].setUV(uvRect.x, uvRect.y + uvRect.h);

    vertex[1].color = color;
    vertex[1].setPosition(destRect.x, destRect.y); // bottom left
    vertex[1].setUV(uvRect.x, uvRect.y);

    vertex[2].color = color;
    vertex[2].setPosition(destRect.x + destRect.w, destRect.y); // bottom right
    vertex[2].setUV(uvRect.x + uvRect.w, uvRect.y);

    vertex[3].color = color;
    vertex[3].setPosition(destRect.x + destRect.w, destRect.y + destRect.h); // top right
    vertex[3].setUV(uvRect.x + uvRect.w, uvRect.y + uvRect.h);

    GLuint indices[6] = {
        0, 1, 2,
        2, 3, 0
    };

    glBindVertexArray(vao);

    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(vertex), NULL, GL_STATIC_DRAW); // orphan the buffer
    glBufferSubData(GL_ARRAY_BUFFER, 0, 6 * sizeof(vertex), vertex);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), NULL, GL_STATIC_DRAW); // orphan the buffer
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, 6 * sizeof(GLuint), indices);

    glBindVertexArray(0);

}

void Mesh::Draw(GLuint texture)
{
    glBindVertexArray(vao);

  //  glEnable(GL_CULL_FACE);
   // glCullFace(GL_FRONT);
    //glFrontFace(GL_CW);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);

    glBindVertexArray(0);
}

顶点

struct Rect {
    float x, y;
    float w, h;
};
struct Position {
    float x, y;
};

struct UV {
    float u, v;
};

struct ColorRGBA {
    GLubyte r, g, b, a;
};
class Vertex
{
public:
    Vertex();
    ~Vertex();

    void setPosition(float x, float y);
    void setcolor(ColorRGBA color);
    void setUV(float u, float v);

    UV uv;
    ColorRGBA color;
    Position position;
};

我不会展示着色器类,因为它有很多好处。

我知道,因为我做过实验。

我在名为 u_expColor 的片段类中创建了一个新的制服,并设置了 outColor = u_expColor 并且改变了四边形的颜色。

我还在顶点着色器中创建了统一的 vec4 u_expPos 并说 gl_Position = e_expPos 并且它创建了四边形没有问题。

只有当我想在顶点着色器中通过 mvp 创建/乘以位置时,才会出现类似 you tube 视频的问题。

如何解决?

最佳答案

谢谢 http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/#putting-it-all-together 我已经修好了.我看到他有

glm::mat4 model = glm::mat4(1.0f) 

这就是我所要做的。

关于c++ - 模型、 View 和投影矩阵不适用于 opengl glsl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48266344/

相关文章:

c++ - OpenGL 中的压缩纹理批处理

无法发现我的 GLSL/OpenGL 代码的问题

javascript - WebGL:没有 VBO 绑定(bind)到启用的顶点。顶点索引需要 "vertexAttribPointer"吗?

c++ - 每个文件的末尾是否总是有一个换行符 (\n)?

c++ - 为什么模板参数推导/替换在这里失败?

opengl-es - 在 webgl 中缩放不可变对象(immutable对象)

ios - 打开 GL ES 对象未正确呈现

c++ - 使用 ns2 以混杂模式监控数据包

c++ - Visual Studio 2012 C++ 编译程序未加载文件

glsl - 通过 GLSL/WebGL 旋转图像