OpenGL 2D Examples - 102 Using the Basics

Discuss programming topics that involve the OpenGL API.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

OpenGL 2D Examples - 102 Using the Basics

Post by Baker »

This focuses on applying the 2D basics and assumes you understand:

1. how draw a line/point/polygon
2. anti-alias them if needed
3. Know the basic opengl functions

All this was covered in the last thread which ended with drawing a speedometer needle on a speedometer texture:

Image

This thread is going to start to get into applying these concepts to try things and then getting into alpha masking and alpha blending and more versatile use of textures.

Image

Drawing a Checkboard

Not much explanation is needed here, but the reason I am bothering to do this is because one common use of 2D graphics is tile oriented games.

Initialization:

Code: Select all

glClearColor (0, 0, 0, 0)
glMatrixMode (GL_PROJECTION) // Projection matrix
glOrtho(0, 512, 512, 0, 0, 1)  // Define 2D coordinates = 512 x 512
Rendering each frame:

Code: Select all

    glClear (GL_COLOR_BUFFER_BIT)  // Clear it
    
    For i = 0 To 7
        For j = 0 To 7
            If (i + j) And 1 Then  // Even=odd test
                glColor3f (1, 1, 1)    // White
            Else
                glColor3f (1, 0, 0)    // Red
            End If
            glBegin (GL_QUADS)
                x1 = i * 32 + 128  // 128 = left start, 32 = tile size
                y1 = j * 32 + 128 // 128 = top start, 32 = tile size
                x2 = x1 + 32
                y2 = y1 + 32
                
                glVertex2d (x1, y1)
                glVertex2d (x2, y1)
                glVertex2d (x2, y2)
                glVertex2d (x1, y2)
            glEnd ()
        Next
    Next

Post Reply