C++ otherographic投影的问题| OpenGL
我正在尝试使用glm::ortho创建一个带有简单矩形的正交投影矩阵,但由于某些原因,它并没有按预期进行。我期待着左下角的矩形,但这是我得到的(我使用的是Xcode): 代码如下:C++ otherographic投影的问题| OpenGL,c++,xcode,opengl,glm-math,orthographic,C++,Xcode,Opengl,Glm Math,Orthographic,我正在尝试使用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; } g
#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);
}
片段着色器未编译,因为您未指定片段着色器的输出:
#版本330核心
输出vec4颜色;
void main()
{
颜色=vec4(1.0f);
}
请注意,矩形在窗口左下角只有1个像素。事实上,我忘了写“out vec4 color”,很抱歉,但我仍然不明白为什么矩形在窗口的中心,看起来像一条线,我希望窗口左下角有一个小矩形。@José我无法重现该问题。@José问题中的代码不是您的原始代码,不是吗。除了着色器类及其函数外,原始代码也在问题中。我想知道问题是否出在IDE(Xcode)中。您是否在另一个IDE中测试了代码?