Opengl不会显示任何内容
Opengl will not display anything
我正在尝试显示一个小三角形。
这是我的初始化代码:
void PlayerInit(Player P1)
{
glewExperimental = GL_TRUE;
glewInit();
//initialize buffers needed to draw the player
GLuint vao;
GLuint buffer;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &buffer);
//bind the buffer and vertex objects
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
//Set up a buffer to hold 6 floats for position, and 9 floats for color
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*18, NULL, GL_STATIC_DRAW);
//push the vertices of the player into the buffer
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*9, CalcPlayerPoints(P1.GetPosition()));
//push the color of each player vertex into the buffer
glBufferSubData(GL_ARRAY_BUFFER, sizeof(float)*9, sizeof(float)*9, CalcPlayerColor(1));
//create and compile the vertex/fragment shader objects
GLuint vs = create_shader("vshader.glsl" ,GL_VERTEX_SHADER);
GLuint fs = create_shader("fshader.glsl" ,GL_FRAGMENT_SHADER);
//create a program object and link the shaders
GLuint program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
//error checking for linking
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if(!linked)
{
std::cerr<< "Shader program failed to link" <<std::endl;
GLint logSize;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog(program, logSize, NULL, logMsg);
std::cerr<< logMsg << std::endl;
delete [] logMsg;
exit(EXIT_FAILURE);
}
glUseProgram(program);
//create attributes for color and position to pass to shaders
//enable each attribute
GLuint Pos = glGetAttribLocation( program, "vPosition");
glEnableVertexAttribArray(Pos);
GLuint Col = glGetAttribLocation( program, "vColor");
glEnableVertexAttribArray(Col);
//set a pointer at the proper offset into the buffer for each attribute
glVertexAttribPointer(Pos, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (sizeof(float)*0));
glVertexAttribPointer(Col, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (sizeof(float)*9));
}
我没有太多编写着色器链接器的经验,所以我认为这可能是问题所在。我在着色器加载程序中进行了一些错误检查,但没有任何结果。所以我认为这很好。
接下来我有我的显示器和主要功能:
//display function for the game
void GameDisplay( void )
{
//set the background color
glClearColor(1.0, 0.0, 1.0, 1.0);
//clear the screen
glClear(GL_COLOR_BUFFER_BIT);
//Draw the Player
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
//main function
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutCreateWindow("Asteroids");
Player P1 = Player(0.0, 0.0);
PlayerInit(P1);
glutDisplayFunc(GameDisplay);
glutMainLoop();
return 0;
}
顶点着色器:
attribute vec3 vPosition;
attribute vec3 vColor;
varying vec4 color;
void main()
{
color = vec4(vColor, 1.0);
gl_Position = vec4(vPosition, 1.0);
}
片段着色器
varying vec4 color;
void main()
{
gl_FragColor = color;
}
这就是所有相关的代码。CalcPlayerPoints只返回一个大小为9的浮点数组来保存三角形坐标。CalcPlayerColor也有类似的功能。
最后一个可能有助于诊断问题的问题是,每当我试图通过关闭应用程序的窗口来退出程序时,我都会在glutmain循环中得到一个断点,但如果我关闭控制台窗口,它会很好地退出。
编辑:我添加了着色器以供参考。编辑:我使用的是opengl 3.1版
如果没有着色器,我们无法判断错误代码是否不是GLSL(错误的顶点转换等)您是否尝试过检查glGetError
,看看问题是否不是来自初始化代码?
也许可以尝试将片段着色器的输出设置为vec4(1.0, 0.0, 0.0, 1.0)
,以检查其正常输出是否格式错误。
您的最后一个问题似乎揭示了一个未定义的行为,如错误的内存分配/释放,这可能发生在Player
类中(顺便考虑一下,在初始化代码中将对象作为引用传递,因为它可能会触发一个浅拷贝,然后触发一个双自由指针)。
原来我从用于计算顶点的函数返回顶点数组的方式有问题。修复后,其余代码运行良好。
相关文章:
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- QChart在使用QDateTimeAxis时不显示任何系列数据
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 为什么下面的Hello World程序在PowerShell上没有显示任何输出?同一程序在CMD上显示正确的输出
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- C++ 中是否有任何内置阶乘函数?
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 在不到一分钟的情况下,在一秒钟内显示计时器
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数
- 如何将文件的路径(或任何文本)显示/附加到编辑框中?(C++ Win32 API)
- 是否有任何内置的CUDA函数允许CUDA内核向主机代码报告错误?
- 我如何在常数时间(O(1))内获得字母表(1-26)中字符的数值/位置,而不使用任何内置方法或函数
- C++中的任何内置哈希方法
- c++库中的任何内置夹子方法