[英]Can't print OpenGL buffer to console in a GLEW application
我正在使用這里找到的教程。 我正在使用GLFW。 我的窗口加載正常,但是在調用時
GLuint vertexBuffer;
glGenBuffers( 1, &vertexBuffer );
printf( "%u\n", vertexBuffer );
它沒有寫到控制台,如果我關閉openGL窗口(如果不關閉控制台,則不會中斷)。 我猜指針有問題嗎? 但這對我來說似乎是正確的,而這正是他在本教程中所獲得的。
這是我的整個很小的.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 );
}
它無法將其寫入控制台,因為從未到達相關代碼。
只要打開窗口,代碼中就會運行幾乎無限的while循環。
while(glfwGetWindowParam(GLFW_OPENED))
{
glfwSwapBuffers();
}
您應該將所有初始化代碼放在此循環之前。
glewExperimental = GL_TRUE;
glewInit();
並在循環之前或循環中創建緩沖區對象。 實際上,當您要將新內容加載到現有場景時,可以在循環內部創建緩沖區對象。
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
printf("%u\n", vertexBuffer);
您的最終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.