没有 glwf/glut 的 opengl 简单示例
opengl simple example without glwf/glut
我正在尝试编写一个基本的 OpenGL 循环(没有 GLWF 和 GLUT;只有 GLEW),它只清除 window。
然而,window一直是白色,window的颜色没有被清除为红色。
您能找到代码无法运行的错误吗?
关于我的代码的任何其他建议?什么可以做得更好?
/*Window is created using CreateWindowExA()*/
/* glewInit() etc */
RenderingContext = wglCreateContextAttribsARB(DeviceContext, 0, version_attribs);
if (!wglMakeCurrent(DeviceContext, RenderingContext))
{
OutputDebugStringA("Error\n");
exit(-1);
}
bool Running = true;
while (Running){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Simply clear the window with red
static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, red);
// here some other code
}
任何 gl-command 都将在 当前上下文 上执行。
您使用 wglMakeCurrent(NULL, NULL);
这意味着:"exclude all gl-commands for this thread"
改为使用wglMakeCurrent(this_window_device, the_context_I_want_to_use);
编辑:
此外,渲染是对 后帧缓冲区 完成的(我假设您使用的是双缓冲区像素格式)。要将其显示到 window(前帧缓冲区)中,您需要调用 wglSwapBuffers
.
编辑 2:
我建议您通读 wiki about creaating a context。
显示了 window 和上下文创建的示例。
我正在尝试编写一个基本的 OpenGL 循环(没有 GLWF 和 GLUT;只有 GLEW),它只清除 window。 然而,window一直是白色,window的颜色没有被清除为红色。
您能找到代码无法运行的错误吗? 关于我的代码的任何其他建议?什么可以做得更好?
/*Window is created using CreateWindowExA()*/
/* glewInit() etc */
RenderingContext = wglCreateContextAttribsARB(DeviceContext, 0, version_attribs);
if (!wglMakeCurrent(DeviceContext, RenderingContext))
{
OutputDebugStringA("Error\n");
exit(-1);
}
bool Running = true;
while (Running){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Simply clear the window with red
static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, red);
// here some other code
}
任何 gl-command 都将在 当前上下文 上执行。
您使用 wglMakeCurrent(NULL, NULL);
这意味着:"exclude all gl-commands for this thread"
改为使用wglMakeCurrent(this_window_device, the_context_I_want_to_use);
编辑:
此外,渲染是对 后帧缓冲区 完成的(我假设您使用的是双缓冲区像素格式)。要将其显示到 window(前帧缓冲区)中,您需要调用 wglSwapBuffers
.
编辑 2:
我建议您通读 wiki about creaating a context。
显示了 window 和上下文创建的示例。