1. 程式人生 > >【OpenGL】詳解第一個OpenGL程式

【OpenGL】詳解第一個OpenGL程式

///////////////////////////////////////////////////////////////////////
//
// triangles.cpp
//
///////////////////////////////////////////////////////////////////////


//--------------------------------------------------------------------
//
// 在程式一開頭,我們包含了所需的標頭檔案,
// 聲明瞭一些全域性變數(但通常是不用全域性變數在做的,這裡只是為了說明一些基本問題)
// 以及其他一些有用的程式結構
//

#include <iostream>
using namespace std;

#include "vgl.h"
#include "LoadShaders.h"

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;

//---------------------------------------------------------------------
//
// init
//
// init()函式用於設定我們後面會用到的一些資料.例如頂點資訊,紋理等
//

void init(void) {
    glGenVertexArrays(NumVAOs, VAOs);
    glBindVertexArray(VAOs[Triangles]);

    // 我們首先指定了要渲染的兩個三角形的位置資訊.
    GLfloat  vertices[NumVertices][2] = {
        { -0.90, -0.90 },  // Triangle 1
        {  0.85, -0.90 },
        { -0.90,  0.85 },
        {  0.90, -0.85 },  // Triangle 2
        {  0.90,  0.90 },
        { -0.85,  0.90 }
    };

    glGenBuffers(NumBuffers, Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
                     vertices, GL_STATIC_DRAW);

    // 然後使用了必需的vertex和fragment shaders
    ShaderInfo  shaders[] = {
            { GL_VERTEX_SHADER, "triangles.vert" },
            { GL_FRAGMENT_SHADER, "triangles.frag" },
            { GL_NONE, NULL }
    };

    // LoadShaders()是我們自定義(這裡沒有給出)的一個函式,
    // 用於簡化為GPU準備shaders的過程,後面會詳細講述
    GLuint program = LoadShaders(shaders);
    glUseProgram(program);
    // 最後這部分我們成為shader plumbing,
    // 我們把需要的資料和shader程式中的變數關聯在一起,後面會詳細講述
    glVertexAttribPointer(vPosition, 2, GL_FLOAT,
                          GL_FALSE, 0, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(vPosition);
}

//---------------------------------------------------------------------
//
// display
//
// 這個函式是真正進行渲染的地方.它呼叫OpenGL的函式來請求資料進行渲染.
// 幾乎所有的display函式都會進行下面的三個步驟.
//

void display(void) {
    // 1. 呼叫glClear()清空視窗
    glClear(GL_COLOR_BUFFER_BIT);

    // 2. 發起OpenGL呼叫來請求渲染你的物件
    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    // 3. 請求將影象繪製到視窗
    glFlush();
}

//---------------------------------------------------------------------
//
// main
//
// main()函式用於建立視窗,呼叫init()函式,最後進入到事件迴圈(event loop).
// 這裡仍會看到一些以gl開頭的函式,但和上面的有所不同.
// 這些函式來自第三方庫,以便我們可以在不同的系統中更方便地使用OpenGL.
// 這裡我們使用的是GLUT和GLEW.
//

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512, 512);
    glutInitContextVersion(4, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow(argv[0]);

    if (glewInit()) {
        cerr << "Unable to initialize GLEW ... exiting" << endl; exit(EXIT_FAILURE);
    }
    init();

    glutDisplayFunc(display);

    glutMainLoop();
}