簡體   English   中英

創建一個基本的OpenGl上下文

[英]Creating a basic OpenGl context

我正在閱讀OpenGl紅皮書,而我幾乎陷入了第一個教程的困境。 如果我使用freeglut和glew,一切都會正常,但是我想自己處理輸入。 因此,我放棄了freeglut,並開始編寫自己的代碼。 我看了其他一些教程並完成了代碼,但是什么也沒顯示。 似乎FreeGlut在后台執行了一些巫術魔術,但是我不知道自己缺少什么。 我已經試過了:

int attributeListInt[19];
int pixelFormat[1];
unsigned int formatCount;
int result;
PIXELFORMATDESCRIPTOR pixelFormatDescriptor;
int attributeList[5];

context = GetDC (hwnd);
if (!context)
    return -1;

attributeListInt[0] = WGL_SUPPORT_OPENGL_ARB;
attributeListInt[1] = TRUE;
attributeListInt[2] = WGL_DRAW_TO_WINDOW_ARB;
attributeListInt[3] = TRUE;
attributeListInt[4] = WGL_ACCELERATION_ARB;
attributeListInt[5] = WGL_FULL_ACCELERATION_ARB;
attributeListInt[6] = WGL_COLOR_BITS_ARB;
attributeListInt[7] = 24;
attributeListInt[8] = WGL_DEPTH_BITS_ARB;
attributeListInt[9] = 24;
attributeListInt[10] = WGL_DOUBLE_BUFFER_ARB;
attributeListInt[11] = TRUE;
attributeListInt[12] = WGL_SWAP_METHOD_ARB;
attributeListInt[13] = WGL_SWAP_EXCHANGE_ARB;
attributeListInt[14] = WGL_PIXEL_TYPE_ARB;
attributeListInt[15] = WGL_TYPE_RGBA_ARB;
attributeListInt[16] = WGL_STENCIL_BITS_ARB;
attributeListInt[17] = 8;
attributeListInt[18] = 0;

result = wglChoosePixelFormatARB (context, attributeListInt, NULL, 1, pixelFormat, &formatCount);
if (result != 1)
    return -1;

result = SetPixelFormat (context, pixelFormat[0], &pixelFormatDescriptor);
if (result != 1)
    return -1;
attributeList[0] = WGL_CONTEXT_MAJOR_VERSION_ARB;
attributeList[1] = 4;
attributeList[2] = WGL_CONTEXT_MINOR_VERSION_ARB;
attributeList[3] = 2;
attributeList[4] = 0;

rendercontext = wglCreateContextAttribsARB (context, 0, attributeList);
if (rendercontext == NULL)
    return -1;

result = wglMakeCurrent (context, rendercontext);
if (result != 1)
    return -1;

glClearDepth (1.0f);
glFrontFace (GL_CCW);
glEnable (GL_CULL_FACE);
glCullFace (GL_BACK);

return 0;

這樣可以設置圖形上下文,但是顯然不足以使所有功能正常運行。 本教程未包含有關視圖或投影矩陣的任何內容,因此我不確定是否應添加類似內容。 但是窗戶仍然是黑色的。

這是教程代碼,已調整為我的代碼:

#define BUFFER_OFFSET(offset) ((void *)(offset))

bool init ();
bool mainloop ();

enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;

int main (int argc, char** argv)
{
    Window w;

    w.init (&mainloop);
    if (!init ())
        return 0;

    w.run ();
    w.shutdown ();

    return 0;
}

bool init ()
{
    glGenVertexArrays (NumVAOs, VAOs);
    glBindVertexArray (VAOs[Triangles]);
    GLfloat vertices[NumVertices][2] = {
        {-0.90f, -0.90f}, // Triangle 1
        {0.85f, -0.90f},
        {-0.90f, 0.85f},
        {0.90f, -0.85f}, // Triangle 2
        {0.90f, 0.90f},
        {-0.85f, 0.90f}
    };

    glGenBuffers (NumBuffers, Buffers);
    glBindBuffer (GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    glBufferData (GL_ARRAY_BUFFER, sizeof(vertices),
        vertices, GL_STATIC_DRAW);
    ShaderInfo shaders[] = {
        {GL_VERTEX_SHADER, "triangles.vert"},
        {GL_FRAGMENT_SHADER, "triangles.frag"},
        {GL_NONE, NULL}
    };

    GLuint program = LoadShaders (shaders);
    glUseProgram (program);
    glVertexAttribPointer (vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET (0));
    glEnableVertexAttribArray (vPosition);

    return true;
}

bool mainloop ()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glBindVertexArray (VAOs[Triangles]);
    glDrawArrays (GL_TRIANGLES, 0, NumVertices);
    glFlush ();

    return true;
}

創建OpenGL上下文並非易事。 尤其是如果您要使用必須通過OpenGL擴展機制加載的wglChoosePixelFormatARB ……這首先需要一個運行正常的OpenGL上下文。 我認為您看到這是種雞與蛋的問題。 另外,用於創建OpenGL上下文的Window需要某些屬性才能可靠地工作。 對於一個WndClass,應該設置CS_OWNDC樣式,並且窗口樣式應包括WS_CLIPSIBLINGS | WS_CLIPCHILDREN WS_CLIPSIBLINGS | WS_CLIPCHILDREN

我最近用我的小型wglarb助手工具處理了上述的雞和蛋問題: https : //github.com/datenwolf/wglarb

它還帶有一個小型測試程序,該程序演示如何使用它。

我建議您使用該功能提供的功能。 我以某種方式編寫了這個小幫助程序庫,如果您的程序使用其他擴展加載機制,它不會受到負面影響。 但是,它還不是線程安全的。 我將不得不最終添加。 我花了一些時間使代碼線程安全。 現在,您可以使用公開的功能而不必擔心同步。 所有這些都以可靠的方式在內部完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM