1

I have problem in seeing my triangle in a black window. The window though just closes as it opens, and does not allow me to see what's going on inside it. I have seen somewhere on the net that i has something to do with the minor-versioning, of which I have no clue how to check on my VGA card.

Here is my full code:

#define GLEW_STATIC
#include <stdio.h>
#include <GL\glew.h>
#include <GL\GLU.h>
#include <GL\glut.h>
#include <glm.hpp>
#include <GL\gl.h>
#include <GLFW\glfw3.h>

using namespace glm;
using namespace std;




int main()
{
    glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
    glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL

    GLFWwindow* window;

    window = glfwCreateWindow(1024, 768, "First Window in OpenGL", NULL, NULL);

    if (window == NULL)
    {
        fprintf(stderr, "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 -1;
    }

    glfwMakeContextCurrent(window);

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

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

    // identifying our vertex buffer
    GLuint 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);



    glewExperimental = true; // Needed in core profile 
    if (glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    do{
        // 1rst attribute buffer : vertices
        glEnableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

        glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
            );

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

        glDisableVertexAttribArray(0);

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

    return 0;
}

Can anyone please help me on keeping the window open and just close it with ESC key as it is expected from the code?

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105

1 Answers1

5

The reason the window closes immediately is because you have a segmentation fault. This is most likely due to failing to initialize things in proper order.

Initialize GLFW before running any glfw function calls, like so:

// Initialise GLFW
if( !glfwInit() ) {
    fprintf( stderr, "Failed to initialize GLFW\n" );
    return -1;
}

glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL

GLFWwindow* window;
window = gl

Then, move the initialization of glew to right after you create and set the GL context:

if (window == NULL) {
  fprintf(stderr, "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 -1;
}
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE; // Needed in core profile 
const GLenum err = glewInit(); 
if (err != GLEW_OK) {
  fprintf(stderr, "Failed to initialize GLEW\n");
  fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  return -1;
}

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

// ... rest of code follows

I have tested it, and your code should work fine now, and the window won't close until you hit ESC.

swalog
  • 4,403
  • 3
  • 32
  • 60
  • thanks, when I do that, I get a bunch of reports in my output for several files. Errors are like this: `'openGL.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.` – Mostafa Talebi Aug 26 '14 at 07:18
  • Seems unrelated to your original question, and most likely you should create a new question to address it. Have you checked this? http://stackoverflow.com/questions/15937707/error-message-cannot-find-or-open-the-pdb-file – swalog Aug 26 '14 at 07:25
  • No it is related. I have solved it using your solution. I did a mistake to implement your solution. Thanks a lot Swalog – Mostafa Talebi Aug 26 '14 at 07:28