有效的OpenGL上下文 [英] Valid OpenGL context

查看:83
本文介绍了有效的OpenGL上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中如何以及在什么阶段创建有效的OpenGL上下文?即使是简单的OpenGL代码,我也遇到错误.

How and at what stage is a valid OpenGL context created in my code? I'm getting errors on even simple OpenGL code.

推荐答案

comp.graphics.api.opengl上的帖子看来,大多数新手似乎都在第一个OpenGL程序上投入了大量精力.在大多数情况下,该错误是由于即使在创建有效的OpenGL上下文之前也调用了OpenGL函数引起的. OpenGL是一种状态机.只有在机器启动并嗡嗡作响的就绪状态下,机器才能投入工作.

From the posts on comp.graphics.api.opengl, it seems like most newbies burn their hands on their first OpenGL program. In most cases, the error is caused due to OpenGL functions being called even before a valid OpenGL context is created. OpenGL is a state machine. Only after the machine has been started and humming in the ready state, can it be put to work.

以下是创建有效OpenGL上下文的一些简单代码:

Here is some simple code to create a valid OpenGL context:

#include <stdlib.h>
#include <GL/glut.h>

// Window attributes
static const unsigned int WIN_POS_X = 30;
static const unsigned int WIN_POS_Y = WIN_POS_X;
static const unsigned int WIN_WIDTH = 512;
static const unsigned int WIN_HEIGHT = WIN_WIDTH;

void glInit(int, char **);

int main(int argc, char * argv[])
{
    // Initialize OpenGL
    glInit(argc, argv);

    // A valid OpenGL context has been created.
    // You can call OpenGL functions from here on.

    glutMainLoop();

    return 0;
}

void glInit(int argc, char ** argv)
{
    // Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowPosition(WIN_POS_X, WIN_POS_Y);
    glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
    glutCreateWindow("Hello OpenGL!");

    return;
}

注意:

  • 这里的关注电话是glutCreateWindow().它不仅创建了一个窗口,而且还创建了一个OpenGL上下文.
  • glutCreateWindow()创建的窗口直到调用glutMainLoop()时才可见.
  • The call of interest here is glutCreateWindow(). It not only creates a window, but also creates an OpenGL context.
  • The window created with glutCreateWindow() is not visible until glutMainLoop() is called.

这篇关于有效的OpenGL上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆