I specified a set of vertices in the points array to draw a triangle which is supposedly starting from the top middle of the window - (+0.0f, +1.0f, +0.0f) - going through the bottom left corner - (-1.0f, -1.0f, +0.0f) - and finally ending in the bottom right corner of the window- +1.0f, -1.0f, +0.0f. The triangle, however, isn't rendered as expected above, since it looks like it starts from the top middle of the window, then goes on to the top left corner, and finally ends up right in the middle of the window. I included two screenshots to aid my problem description below.
Expected triangle hinted in red
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>
#include <GLFW\glfw3.h>
int main() {
// GLFW
if (!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
return EXIT_FAILURE;
}
// Create window
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Test", NULL, NULL);
if (!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
// GLEW
glewExperimental = GL_TRUE;
glewInit();
// Get version info
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
printf("Renderer: %s\n", renderer);
printf("Supported OpenGL version: %s\n", version);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
/* Put code here */
// Vertices to be rendered
float points[] = {
+0.0f, +1.0f, +0.0f
-1.0f, -1.0f, +0.0f
+1.0f, -1.0f, +0.0f
};
// Vertex buffer object
GLuint bufferId = 0;
glGenBuffers(1, &bufferId);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
// Vertex attribute object
GLuint attributeId = 0;
glGenVertexArrays(1, &attributeId);
glBindVertexArray(attributeId);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Shaders
const char* vertexShaderSource =
"#version 400\n"
"in vec3 position;"
"void main () {"
" gl_Position = vec4(position.x, position.y, position.z, 1.0);"
"}";
const char* fragmentShaderSource =
"#version 400\n"
"out vec4 outColor;"
"void main () {"
" outColor = vec4 (0.0, 0.0, 1.0, 1.0);"
"}";
// Compile shaders
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// Link shaders
GLuint shaderPorgram = glCreateProgram();
glAttachShader(shaderPorgram, fragmentShader);
glAttachShader(shaderPorgram, vertexShader);
glLinkProgram(shaderPorgram);
// Render loop
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderPorgram);
glBindVertexArray(attributeId);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return EXIT_SUCCESS;
}