正射投影问题| OpenGL

问题描述

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

Image

代码如下:

#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,//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,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,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,Window_Width,Window_Height );

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

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

GLCall(gluniformMatrix4fv(location,glm::value_ptr(projection)));


while (!glfwWindowShouldClose(window)) {
    
    glClearColor(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);
}

解决方法

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

cURL error 28: Failed to connect to testapi.certccie.com port 443: Connection timed out (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

请注意,矩形仅位于窗口左下角的 1 个像素处。