Triangle shows up but soon vanishes


Hello. I just was able to run for the first time this OpenGL tutorial: Actually, not this one, but code of the 2.1 version of it.

Anyway, the goal is to show a simple red triangle. And it does happen, for a brief fraction of a second. Then it vanishes and all I am left is with the background window. I don’t have an idea of what can be the cause, so I will put all the OGL code and hope anyone here can give me a hint of what can be the cause.

void OpenGL::init (Scene& scene /*,char * tex_mem*/) {
   cout << "OpenGL::init" << endl;
   bool running=false;
   /*if (!GLEW_VERSION_2_0) {
      cout << "glew not available. closing..." << endl;
      exit (-1);
   glfwInit ();
   glEnable (GL_DEPTH_TEST);
   if (!glfwOpenWindow (1024,768,8,8,8,0,8,0,GLFW_WINDOW)) {
      exit (1);
   glewInit ();
   glfwSetWindowTitle ("Test window:");
   glfwSetWindowSizeCallback (windowResize);
   glfwSetKeyCallback (handleKeypress);
   glClearColor (0.0f,0.66f,1.0f,1.0f);
   GLuint programID = loadShaders ("/media/34GB/demos/Ogl/SimpleVertexShader.vertexshader","/media/34GB/demos/Ogl/SimpleFragmentShader.fragmentshader");
   GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
   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,
   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);
   do {
      display (scene,programID,vertexPosition_modelspaceID,vertex_buffer);
      running = glfwGetWindowParam (GLFW_OPENED);
   } while (running);
   glDeleteBuffers(1, &vertex_buffer);

void OpenGL::display (Scene& scene, GLuint programID, }
   glUseProgram (programID);
   glEnableVertexAttribArray (vertexPosition_modelspaceID);
   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
   vertexPosition_modelspaceID, // The attribute we want to configure
      3,                  // size
      GL_FLOAT,           // type
      GL_FALSE,           // normalized?
      0,                  // stride
      (void*)0            // array buffer offset
	glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle

   /*Vec3f cam_ori =  scene.getCamera_ori ();

   vector <Mtl> material_container = scene.getMtlContainer();
   int n_triangles = scene.getNTriangles();
   vector <Triangle>& tri_array = scene.getTriangleContainer();*/

   glDeleteBuffers(1, &vertex_buffer);
#version 120

void main()

	// Output color = red 
	gl_FragColor = vec4(1,0,0,1);

#version 120
// Input vertex data, different for all executions of this shader.
attribute vec3 vertexPosition_modelspace;

void main(){

	gl_Position = vec4(vertexPosition_modelspace, 1.0);


I can put the code of the method loadShaders if necessary, I just did not because its the largest method, but can easily do so.

By the way, the code of the shaders is not being correctly formatted. What I am doing wrong? Thanks to all for the help.


The syntax of some of your program won’t compile, so it’s unclear exactly what got copy/pasted correctly and what didn’t. However…

Put a print at the top and bottom of your display() method, and I think it’ll be obvious. Notice that you are deleting resources at the bottom that you will need in the next call. Also, don’t forget to Check for OpenGL Errors.