PDA

View Full Version : OpenGL doesn't draw anything



Venekt
12-31-2015, 02:12 AM
Hello,

I want to learn OpenGL but it doesn't work. The actually problem is that OpenGL opened a window but it doesn't draw a triangle like in the tutorial.
It appears only a dark blue background. I think I made a mistake... :(

Code:

main.cpp


#include <cstdlib>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "loadShaders.h"

// Window sizes
#define WIN_SIZE_X 1024
#define WIN_SIZE_Y 768

// The window
GLFWwindow *window;

// WinMain to avoid a console application
int main() {
// Init GLFW
if (!glfwInit()) {
exit(-1);
}

// Settings for GLFW
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

// Create the window
window = glfwCreateWindow(WIN_SIZE_X, WIN_SIZE_Y, "OpenGL", NULL, NULL);

// Center the window on the desktop
const GLFWvidmode *MODE = glfwGetVideoMode(glfwGetPrimaryMonitor());
const int WIDTH = MODE->width;
const int HEIGHT = MODE->height;
glfwSetWindowPos(window, (WIDTH - WIN_SIZE_X) / 2, (HEIGHT - WIN_SIZE_Y) / 2);

// If it fail then will terminate the program
if (window == NULL) {
glfwTerminate();
exit(-1);
}

// Make a context to OpenGL
glfwMakeContextCurrent(window);

// Activate GLEW
glewExperimental = GL_TRUE;
GLenum err = glewInit();

if (err != GLEW_OK) {
exit(-1);
}

// VAO
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

// 3 vertices to make a triangle
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};

// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

// Main loop with press the escape key to terminate the program
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

GLuint programID = loadShaders("C:/Users/AMD-Power/Documents/Visual Studio 2015/Projects/OpenGL/OpenGL2/simpleVertexShader.glsl", "C:/Users/AMD-Power/Documents/Visual Studio 2015/Projects/OpenGL/OpenGL2/simpleFragmentShader.glsl");
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

do {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glfwSwapBuffers(window);
glfwPollEvents();

glUseProgram(programID);

// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);

} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);

glfwDestroyWindow(window);
glfwTerminate();

return 0;
}

Venekt
12-31-2015, 08:21 AM
Problem is solved. :)