Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Triangle example not working

  1. #1
    Junior Member Newbie
    Join Date
    May 2017
    Posts
    1

    Triangle example not working

    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:

    Code :
    #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_modelspaceID);
     
    		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_modelspaceID);
     
    		// 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

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,124
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •