Intro document

erectboboSoftware and s/w Development

Dec 14, 2013 (8 years and 1 month ago)

284 views

GET2

W1


Page
1

of
3

OpenGL Graphics

Game
players
often expect 2D and 3D graphics within games. OpenGL
is an Application
programming interface, (API
) that

allows both 2D and 3D objects to be drawn and transformed as
required.


Using OpenGL within a Windows environment

OpenG
L is a platform
-
independent API that provides hundreds of graphics functions to access a
computer’s graphics hardware.


Within this module we are going to use OpenGL within a Microsoft Windows environment. As
such, we need to include code that handles Win
dow functionality.


Rather than
pore through code required to use Windows, a framework has been written, by Phil
Carlisle, which handles the main displaying and management of Windows messages
.

See module
web site for a copy of the framework.


The princi
pal concept for Windows programming is that a
WinMain

function contains a
while
-
loop

that runs continuously. Occurrences such as maximi
s
ing and minimsing windows, moving a
mouse and pressing a key are all ‘
events
’ that are placed within a message queue, a
nd we can use
specific switch / case statements within a function
WndProc

to respond accordingly to each
message.


Game Class

To aid development, we often create a Game class, which initializes the game and contains
functions, which are called from within
the WinMain function, that initialise OpenGL statements,
update game object positions, draw / render the game objects and swap memory buffers, as
required. See framework

on module website
http://data.bolt
on.ac.uk/staff/sm8

with link
GET2
.


OpenGL states

OpenGL renders objects depending on its current state.

OpenGL functions start with
gl

and a
relevant state can be interrogated with
glGet

functions. So, for example, the current colo
u
r, which
once set wi
ll be used for all objects
,

can be set with the
glColor3f

function.


States are often set with the particular state capitalised. For example,
GL_COLOR_BUFFER_BIT


Drawing 2D objects

Objects can be drawn with object vertices represented in x, y and z dim
ensions.

The z direction
represents values into and out of the screen. A positive z value represents a value away from the
screen, whilst a
negative

z value represents a value
into

the screen.

For this week, only 2D objects
will be represented, so the z

dimension will not be used.


The glVertex2
f function can be used to specify vertex co
-
ordinates in
2

floating point dimensions.
For example, to

draw
a triangle
, the following code
could
be used:



glBegin(GL_TRIANGLES);



glVertex2f(
-
0.5f,
-
0.2f);



glVer
tex2f(
-
0.8f,
-
0.8f);



glVertex2f(
-
0.2f,
-
0.8f);


glEnd();


The keyword GL_
TRIANGLES

informs OpenGL that
3

co
-
ordinates are expected to be
encountered within the glBegin and glEnd statements.


GET2

W1


Page
2

of
3

Exercise 1

Change the code so that a triangle is drawn in the t
op right
-
hand quarter of the screen. Change the
triangle to display in red using the glColor3f function, which has 3 parameters with values from 0 to
1, representing Red, Green and Blue respectively.


Add code that draws a blue triangle in the bottom left
-
hand corner of the screen.


Additional primitive shapes

In addition to triangles, other primitive shapes that can be drawn are: points,
lines,
quadrilaterals
and polygons.


Exercise

2

Use GL_POINTS to draw 3 points as a triangle outline in the top left
-
ha
nd quarter.
Use
function
glPointSize to increase the size of a point
, from a default value of 1.0
.


Draw a rectangle in the bottom right
-
hand quarter using GL_QUADS.


For one of the triangles, draw a line from each vertex to the centre of the triangle, u
sing
GL_LINES.



Adding text

OpenGL does not render text as standard, but a simple approach is to use a pre
-
written Font class.


Copy your current project to a new folder. Download the BFont header / source files from the
module web site to the same fol
der as your project.


Within the Game header file, include the BFont header and add a private attribute pointer to a
BFont.


Within the Game source file, locate the
Initialise()
function and create a font object, with
hDC as the first parameter
, which is a

handle to a device context


essentially the screen
-


and
“Courier” as the font type and 10 as the font size.


Within the Render function, just before drawing the shapes, add code to ‘
printString
’ at
-
0.5f in the
x and 0.9f in the y, with string “Displa
ying primitive shapes”.



Exercise 3

Ensure each quarter’s shape is drawn in a different colour.


Add code that displays appropriate text messages in each quarter of the screen. Ensure each
quarter’s string is in the same colour as the respective shape.



GET2

W1


Page
3

of
3

Responding to keyboard events

If, for example, we wanted a triangle to be
re
-
drawn as a rectangle and vice versa
, every time a space bar was
pressed, then one approach is for a windows key

down event to be
handled.


To be able to handle key

down events,
a case statement within the
WndProc

function, can be used.
Currently, only an Escape
key down event

is being handled, as shown below.



case

WM_KEYDOWN:




switch
(wParam){





case

VK_ESCAPE:






PostQuitMessage(0);






break
;





default
:






break
;




}


A ‘quit’ message is posted to the Windows message queue when the Escape key is pressed, effectively
closing the program.


Exercise 4

Virtual key constants represent particular keys on a keyboard. The constant for a spacebar press is
VK_SPACE
.


Add a
private Boolean attribute to the Game class called
triangleDrawn
. Also, add a public function called
resetTriangleDrawn

which requires no parameters and returns void.


Within the Game constructor, set
the
triangleDrawn

initial value to
true
.

Create the f
unction
resetTriangleDrawn

that sets the
triangleDrawn

attribute to its opposite value


the use of
!

is the simplest
approach.


Within the render function for the triangle at the
top right
-
hand quarter of the screen
, add an if statement
that draws a tria
ngle, if
triangleDrawn

is true, else a rectangle


in the same colour as the triangle.


Within the ‘main’ program
,

add code within the
appropriate
switch

code of the
WndProc

function
, that
executes the
resetTriangleDrawn

function using the game object
.


Ad
d a text message, within the
top right
-
hand quarter of the screen
, which informs a user to press the
spacebar to change the shape within that quarter
. Check the output!


Exercise 5

Add
code that changes the objects in the remaining quarters of the screen
using different virtual key
constants. Add appropriate text messages throughout.