简体   繁体   English

无法在GLEW应用程序中打印OpenGL缓冲区以进行控制台

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

I'm using a tutorial found here . 我正在使用这里找到的教程。 I'm using GLFW. 我正在使用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). 它没有写到控制台,如果我关闭openGL窗口(如果不关闭控制台,则不会中断)。 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) : 这是我的整个很小的.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循环。

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. 您的最终main功能可能如下所示。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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