Tutorial: OpenGL 3.1 The First Triangle (C++/Win)

From OpenGL Wiki
Revision as of 14:16, 13 July 2009 by Aks (talk | contribs) (New page: == Overview == It is hard to write a tutorial about new and difficult stuff, but yet simple enough for newcomers. In order to keep this tutorial as short as possible, I will assume that yo...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Overview

It is hard to write a tutorial about new and difficult stuff, but yet simple enough for newcomers. In order to keep this tutorial as short as possible, I will assume that you have already programmed in OpenGL and I will emphasize only differences between OpenGL 2.1 and OpenGL 3.0/3.1.

This tutorial is based on Rosario Leonardi’s [1]on the OpenGL.org forum. The problem with Leonardi's code is that it cannot be executed "out of the box", and crushes when DrawArrays() function is called, because VBOs are not initialized. Although the code is clear and logical, drivers I had to deal with simply didn't share that viewpoint. So, the code is little bit changed and organized into appropriate classes.

Adding GLEW Support

Dealing with OpenGL 3.1 is hard enough, so I'll skip gymnastics with OpenGL extension and use OpenGL Extension Wrangler Library (GLEW). GLEW is a cross-platform open-source C/C++ extension loading library, and can be freely downloaded from the following site: [2]. The following snippet of code includes support for GLEW, and should be placed somewhere in your code. If you are building a Visual Studio MFC application, which I recommend, the best place for that is somewhere at the end of stdafx.h file.

   //--- OpenGL ---
   #include "glew.h"
   #include "wglew.h"
   #pragma comment(lib, "glew32.lib")
   //--------------

GLRenderer Class

We will start with creation of class CGLRenderer. This class should gather together all OpenGL related code. My students will recognize the functions I insisted on during the lectures. The header file is the same as in good old OpenGL 2.1, but the implementation will be severely changed.

class CGLRenderer
{
public:
        CGLRenderer(void);
        virtual ~CGLRenderer(void);
        CreateGLContext(CDC* pDC);              // Creates OpenGL Rendering Context
        void PrepareScene(CDC* pDC);            // Scene preparation stuff
        void Reshape(CDC* pDC, int w, int h);   // Changing viewport
        void DestroyScene(CDC* pDC);            // Cleanup

protected:
        void SetData();                         // Creates VAO and VBOs and fill them with data

protected:
        HGLRC    m_hrc;                        // OpenGL Rendering Context 
        CGLProgram* m_pProgram;                // Program
        CGLShader* m_pVertSh;                  // Vertex shader
        CGLShader* m_pFragSh;                  // Fragment shader
        
        unsigned int m_vaoID;                  // vertex array object
        unsigned int m_vboID[2];               // two VBOs
};

Rendering Context Creation

First we have to create an OpenGL Rendering Context. This is the task for CreateGLContext() function.

bool CGLRenderer::CreateGLContext(CDC* pDC)
{
        PIXELFORMATDESCRIPTOR pfd;
        memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
        pfd.nSize  = sizeof(PIXELFORMATDESCRIPTOR);
        pfd.nVersion   = 1;
        pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 32;
        pfd.cDepthBits = 32;
        pfd.iLayerType = PFD_MAIN_PLANE;
        
        int nPixelFormat = ChoosePixelFormat(pDC->m_hDC, &pfd);
        
        if (nPixelFormat == 0) return false;
        
        BOOL bResult = SetPixelFormat (pDC->m_hDC, nPixelFormat, &pfd);
        
        if (!bResult) return false; 
        
        HGLRC tempContext = wglCreateContext(pDC->m_hDC);
        wglMakeCurrent(pDC->m_hDC,tempContext);
        
        GLenum err = glewInit();
        if (GLEW_OK != err)
        {
                AfxMessageBox(_T("GLEW is not initialized!"));
        }
        
        int attribs[] =
        {
                WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
                WGL_CONTEXT_MINOR_VERSION_ARB, 1,
                WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
                0
        };
        
        m_hrc = wglCreateContextAttribsARB(pDC->m_hDC,0, attribs);
        wglMakeCurrent(NULL,NULL);
        wglDeleteContext(tempContext);
        
        if (!m_hrc) return false;
        
        return true;
}

Choosing and setting pixel format are the same as in previous version of OpenGL. The new tricks that should be done are:

  • Create standard OpenGL (2.1) rendering context which will be used only temporarily (tempContext), and make it current
HGLRC tempContext = wglCreateContext(pDC->m_hDC);
wglMakeCurrent(pDC->m_hDC,tempContext);
  • Initialize GLEW
GLenum err = glewInit();
  • Setup attributes for a brand new OpenGL 3.1 rendering context
int attribs[] =
{
        WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
        WGL_CONTEXT_MINOR_VERSION_ARB, 1,
        WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
        0
};
  • Create new rendering context
m_hrc = wglCreateContextAttribsARB(pDC->m_hDC,0, attribs);
  • Delete tempContext
wglMakeCurrent(NULL,NULL);
wglDeleteContext(tempContext);

Have you noticed something odd in this initialization? In order to create new OpenGL rendering context you have to call function wglCreateContextAttribsARB(), which is an OpenGL function and requires OpenGL to be active when it is called. How can we fulfill this when we are at the beginning of OpenGL rendering context creation? The only way is to create an old context, activate it, and while it is active create a new one. Very inconsistent, but we have to live with it!

In this example, we’ve created OpenGL 3.1 rendering context (major version is set to 3, and minor to 1). It requires NVidia’s ForceWare 182.52 drivers or newer. If you don’t have new drivers, or for any other reason creation fails, try to change minor version to 0. OpenGL 3.0 rendering context can be created with NVidia’s ForceWare 181.00 drivers or newer, or ATI Catalyst 9.1 drivers or newer.

Scene Preparation

After we have created rendering context, the next step is to prepare scene. In the function PrepareScene() we will do whatever we have to do just once, before the scene is drawn for the first time.

void CGLRenderer::PrepareScene(CDC *pDC)
{
        wglMakeCurrent(pDC->m_hDC, m_hrc);
        glClearColor (1.0, 1.0, 1.0, 0.0);
        
        m_pProgram = new CGLProgram();
        m_pVertSh = new CGLShader(GL_VERTEX_SHADER);
        m_pFragSh = new CGLShader(GL_FRAGMENT_SHADER);
        
        m_pVertSh->Load(_T("minimal.vert"));
        m_pFragSh->Load(_T("minimal.frag"));
        
        m_pVertSh->Compile();
        m_pFragSh->Compile();
        
        m_pProgram->AttachShader(m_pVertSh);
        m_pProgram->AttachShader(m_pFragSh);
        
        m_pProgram->BindAttribLocation(0, "in_Position");
        m_pProgram->BindAttribLocation(1, "in_Color");
        
        m_pProgram->Link();
        m_pProgram->Use();
        
        Setdata();
        
        wglMakeCurrent(NULL, NULL);
}

Shaders

Vertex shader is very simple. It just sends input values to the output, and converts vec3 to vec4. Constructors are the same as in previous versions of GLSL. The main difference, in regard to GLSL 1.2, is that there is no more attribute and varying qualifiers for variables inside shaders. Attribute variables are now in(put) and varying variables are out(put) for the vertex shaders. Uniforms stay the same.

// Vertex Shader – file "minimal.vert"

#version 140

in  vec3 in_Position;
in  vec3 in_Color;
out vec3 ex_Color;

void main(void)
{
        gl_Position = vec4(in_Position, 1.0);
        ex_Color = in_Color;
}

Fragment shader is even simpler. Varying variables in fragment shaders are now declared as in variables. Take care that the name of in(put) variable in fragment shader must be the same as out(put) variable in vertex shader.

// Fragment Shader – file "minimal.frag"

#version 140

in  vec3 ex_Color;
out vec4 out_Color;

void main(void)
{
        out_Color = vec4(ex_Color,1.0);
}

If you have problem with compiling shader’s code (for the reason OpenGL 3.1 is not supported), just change the version number. Instead of 140, put 130. These shaders are so simple that the code is the same in GLSL version 1.3 and version 1.4.

Setting Data

Function SetData() creates VAO and VBOs and fill them with data.

void CGLRenderer::SetData()
{
        float* vert = new float[9];     // vertex array
        float* col  = new float[9];     // color array
        GLint ret;
        // Spatial Coordinates
        vert[0] = 0.0;  vert[1] = 0.5;  vert[2] =-1.0;
        vert[3] =-0.5;  vert[4] =-0.5;  vert[5] =-1.0;
        vert[6] = 0.5;  vert[7] =-0.5;  vert[8] =-1.0;
        
        // Color
        col[0] = 1.0;  col[1] = 0.0;  col[2] = 0.0;
        col[3] = 0.0;  col[4] = 1.0;  col[5] = 0.0;
        col[6] = 0.0;  col[7] = 0.0;  col[8] = 1.0;
        
        glGenVertexArrays(1, &m_vaoID);
        glBindVertexArray(m_vaoID);
        
        glGenBuffers(2, m_vboId);
        
        glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
        glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vert, GL_STATIC_DRAW);
        
        glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
        glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), col, GL_STATIC_DRAW);
        
        delete [] vert;
        delete [] col;
}

Vertex buffer objects (VBO) are familiar item since OpenGL version 1.5, but the vertex array objects are the brad new feature of OpenGL 3.0. Vertex array objects (VAO) encapsulate vertex array state on the client side. These objects allow applications to rapidly switch between large sets of array state. In addition, layered libraries can return to the default array state by simply creating and binding a new vertex array object. More about VAO can be read in the [3].

Setting Viewport

Reshape() function just sets a viewport.

void CGLRenderer::Reshape(CDC *pDC, int w, int h)
{
        wglMakeCurrent(pDC->m_hDC, m_hrc);
        glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
        wglMakeCurrent(NULL, NULL);
}

Drawing

DrawScene(), as its name implies, draws the scene. We have all data already loaded into VBOs, and now we have to tell OpenGL which buffers should be activated and which one should be used for vertex spatial coordinates, and which one for color. To do this, we have to Bind appropriate buffer, enable vertex attribute array bound to appropriate input (i.e. attribute) variable in vertex shader code, and specify data. By specifying data we actually say that the current VBO is a repository for vertex attribute data, and declares the organization of the repository (how many components per vertex, which type, and so on).

void CGLRenderer::DrawScene(CDC *pDC)
{
        wglMakeCurrent(pDC->m_hDC, m_hrc);
                                                          
        glClear(GL_COLOR_BUFFER_BIT);
        
        glBindVertexArray(m_vaoID);     
        
        glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0] );
        glEnableVertexAttribArray(0);
        glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); 
        
        glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1] );
        glEnableVertexAttribArray(1);
        glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
        
        glDrawArrays(GL_TRIANGLES, 0, 3);
        
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        
        glFlush ();
        SwapBuffers(pDC->m_hDC);
        wglMakeCurrent(NULL, NULL);
}

Cleaning up

And, at the end we have to clean up the whole mass...

void CGLRenderer::DestroyScene(CDC *pDC)
{
        wglMakeCurrent(pDC->m_hDC, m_hrc);
        
        m_pProgram->DetachShader(m_pVertSh);
        m_pProgram->DetachShader(m_pFragSh);
        
        delete m_pProgram;
        m_pProgram = NULL;
        
        delete m_pVertSh;
        m_pVertSh = NULL;
        delete m_pFragSh;
        m_pFragSh = NULL;
        
        wglMakeCurrent(NULL,NULL);
        if(m_hrc)
        {
                wglDeleteContext(m_hrc);
                m_hrc = NULL;
        }
}