PDA

View Full Version : Triangle example not working



jmricher49
05-28-2017, 07:05 AM
Dear all,
I am a newbie in OpenGL trying to draw some triangles but I have been fighting for 3 hours to get nothing displayed. I have installed the examples of the tutorial ogl-OpenGL-tutorial_0015_21 and it works fine. So I am wondering if it is my code or the libraries that I am using that cause problem. I am using sharders and the default SimpleVertexShader.vertexshader and SimpleFragmentShader.fragmentshader of the tutorial but not the same libraries, I am using the ones of Ubuntu and not the one compiled during the tutorial installation. Anyway I tried to replace the Ubuntu libs by the ones of the tutorial but it doesn't change anything.

My code is the following:


#include <cstdlib>
#include <iostream>
using namespace std;

#include <GL/glew.h>
#include <GLFW/glfw3.h> // gestion fenetre et clavier
#include <glm/glm.hpp> // opérations mathématiques
using namespace glm;

//#include "shader.hpp"
extern GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path);

int main(int argc, char *argv[]) {
// Initialise GLFW
if(!glfwInit()) {
cerr << "Failed to initialize GLFW\n";
return EXIT_FAILURE;
}

glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL

// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL ){
cerr << "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n";
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window); // Initialize GLEW

//glewExperimental=true; // Needed in core profile

if (glewInit() != GLEW_OK) {
cerr << "Failed to initialize GLEW\n";
return EXIT_FAILURE;
}

// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

// Dark blue background
glClearColor(0.3f, 0.6f, 0.4f, 0.0f);

// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );

// Get a handle for our buffers
GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");

static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, -10.0f,
1.0f, -1.0f, -10.0f,
0.0f, 1.0f, -10.0f,


0.0f, 0.0f, -1.0f,
1.0f, 0.0f, -1.0f,
0.0f, 2.0f, -1.0f,

0.0f, 2.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 2.0f, 0.0f

};

cerr << "size of data = " << sizeof(g_vertex_buffer_data) << endl;

GLuint vertex_buffer;
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glBindBuffer( GL_ARRAY_BUFFER, 0 );


do{
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );

// Use our shader
glUseProgram(programID);

glEnableVertexAttribArray(vertexPosition_modelspac eID);

glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glVertexAttribPointer(
vertexPosition_modelspaceID, // must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 9); // Starting from vertex 0; 6 vertices total -> 2 triangles

glDisableVertexAttribArray(vertexPosition_modelspa ceID);

// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();

} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );

// Cleanup VBO
glDeleteBuffers(1, &vertex_buffer);
glDeleteProgram(programID);

// Close OpenGL window and terminate GLFW
glfwTerminate();

return EXIT_SUCCESS;
}

I would appreciate some help because I am getting crazy trying to find out why I don't have anything.
Best regards,
Jean-Michel

Dark Photon
05-28-2017, 08:07 PM
If you're a newbie, it might be best to start with a working example program and gradually make changes to it, rather than start with a bunch of broken code and try to figure out what all the problems are.

As for your currently broken test program, let's see the source for your shaders and the source for the LoadShaders() function. With those, folks could actually compile and run your test program and potentially provide you more advice.