【问题标题】:Problems with Othographic Projection | OpenGL正射投影的问题 | OpenGL
【发布时间】:2021-06-04 02:58:46
【问题描述】:

我正在尝试使用 glm::ortho 创建一个带有简单矩形的正交投影矩阵,但由于某种原因它没有按预期进行。我期待左下角的矩形,但这就是我得到的(我正在使用 Xcode):

代码如下:

#define Window_Width 1024.0f
#define Window_Height 700.0f

int main(int argc, const char * argv[]) {

GLFWwindow* window;

if (!glfwInit()) { return -1; }


glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

window = glfwCreateWindow(Window_Width, Window_Height, "Hello Window", NULL, NULL);
if (!window) { glfwTerminate(); return -1; }

glfwSetFramebufferSizeCallback(window, frameBuffer_resize_callback);

glfwMakeContextCurrent(window);

glfwSwapInterval(1);

glewExperimental = GL_TRUE;

if (glewInit() != GLEW_OK) { print("Error -- Glew Init"); }
    

float positions[] = { // Draw Rectangle positions
    -0.5f, -0.5f, //1// Top Left
     0.5f, -0.5f, //2// Top Right
     0.5f,  0.5f, //3// Bottom Right
    -0.5f,  0.5f  //4// Bottom Left
};

unsigned int indicies[] {
    0, 1, 2, // First Triangle
    0, 2, 3  // Second Triangle
};

// Vertex Array
unsigned int vao;
GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao));

// Vertex Buffer
unsigned int vbo;
GLCall(glGenBuffers(1, &vbo));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, vbo));
GLCall(glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW));


GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0));

// Index Buffer
unsigned int ibo;
GLCall(glGenBuffers(1, &ibo));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW));

//Shaders

Shader source = ParseShader("Resources/Shaders/Shaders.glsl");

unsigned int shader = CreateShader(source.VertexShader, source.FragmentShader);
GLCall(glUseProgram(shader));

glViewport( 0, 0, Window_Width, Window_Height );

glm::mat4 projection = glm::ortho(0.0f, Window_Width, 0.0f, Window_Height, -1.0f, 1.0f);    

int location = glGetUniformLocation(shader, "u_MVP");
if (location == -1) { print("Uniform location does not exists"); }

GLCall(glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(projection)));


while (!glfwWindowShouldClose(window)) {
    
    glClearColor(0.f, 0.f, 0.f, 1.f);
    
    GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
    
    
    //Swap front and back buffers
    glfwSwapBuffers(window);
    glFlush();
    
    // Poll for and process events
    glfwPollEvents();
}

顶点着色器:

#version 330 core

layout(location = 0) in vec4 position;

uniform mat4 u_MVP;

void main()
{
  gl_Position = position * u_MVP;
}

片段着色器:

#version 330 core

void main()
{
    color = vec4(1.0f);
}

【问题讨论】:

    标签: c++ xcode opengl glm-math orthographic


    【解决方案1】:

    片段着色器无法编译,因为您没有指定片段着色器的输出:

    #version 330 core
    
    out vec4 color;
    
    void main()
    {
        color = vec4(1.0f);
    }
    

    注意,矩形在窗口左下角只有 1 个像素。

    【讨论】:

    • 其实我忘记写"out vec4 color"了,对不起,我还是不明白为什么矩形在窗口的中心而且看起来像一条线,我是期望窗口左下角有一个小矩形。
    • @José 我无法重现该问题。
    • @José 问题中的代码不是您的原始代码,不是吗。
    • 除Shader类及其函数外,原代码在问题中。
    • 我想知道问题是否出在 IDE (Xcode) 中。您是否在另一个 IDE 中测试过代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多