简体   繁体   中英

Can't print OpenGL buffer to console in a GLEW application

I'm using a tutorial found here . I'm using GLFW. My window is loading fine, but when calling

GLuint vertexBuffer;
glGenBuffers( 1, &vertexBuffer );
printf( "%u\n", vertexBuffer );

it is not writing to the console, and it breaks if I close the openGL window( ,not if I close the console). I guess it's something wrong with the pointer? But it seems right to me and it's exactly how he has it in his tutorial.

Here is my entire, very small .cpp (VS2012) :

#define GLEW_STATIC

#include <GL/glew.h>
#include <GL/glfw.h>
#include <stdio.h>
#include <stdlib.h>

#pragma comment( lib, "glfw.lib")
#pragma comment( lib, "opengl32.lib")
#pragma comment( lib, "glew32s.lib")

int main() {

    glfwInit();
    glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
    glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 2 );
    glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

    glfwOpenWindowHint( GLFW_WINDOW_NO_RESIZE, GL_TRUE );
    glfwOpenWindow( 800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW );
    glfwSetWindowTitle( "OpenGL" );

    printf("This works");
    while( glfwGetWindowParam( GLFW_OPENED ) ) {
        glfwSwapBuffers();
    }

    glewExperimental = GL_TRUE;
    glewInit();

    GLuint vertexBuffer;
    glGenBuffers( 1, &vertexBuffer );
    printf( "%u\n", vertexBuffer );

    glfwTerminate();

    exit( EXIT_SUCCESS );
}

It can't write that to the console because the related code is never reached.

There is a nearly infinite while loop in your code running as long as the window is opened.

while(glfwGetWindowParam(GLFW_OPENED))
{
    glfwSwapBuffers();
}

You should place all initialization code before this loop.

glewExperimental = GL_TRUE;
glewInit();

And create the buffer object before or in the loop. In practice you would create buffer objects inside the loop when you want to load new content to an existing scene.

GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
printf("%u\n", vertexBuffer);

Your final main function could look like the following.

int main()
{
    // GLFW initialization
    glfwInit();
    // ...
    glfwOpenWindow(800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
    glfwSetWindowTitle("My first OpenGL Application");

    // GLEW initialization
    glewExperimental = GL_TRUE;
    glewInit();

    // vertex buffer
    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    printf("%u\n", vertexBuffer);

    // main loop
    bool running = true;
    while(running) {
        // exit
        if (!glfwGetWindowParam(GLFW_OPENED))
            running = false;

        // display
        glfwSwapBuffers();
    }

    // clean up
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM