Basic OpenGL for the Newbie, Part 1

BlitzMax Forums/BlitzMax Tutorials/Basic OpenGL for the Newbie, Part 1

Setting up an OpenGL window

Type (or paste) the following code into the editor
bglCreateContext(800,600,32,0,BGL_BACKBUFFER|BGL_DEPTHBUFFER|BGL_FULLSCREEN)
init()

While Not KeyDown(KEY_ESCAPE)
display()
bglSwapBuffers
Wend 
End

Function init()
glClearColor(0.0, 0.0, 0.0, 0.0)
glColor3f(0.0, 0.0, 1.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, 800/600, 0.1, 100.0)
End Function

Function display()
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glBegin(GL_TRIANGLES)
	glVertex3f( 0.0, 1.0,-10.0)
	glVertex3f(-1.0,-1.0,-10.0)
	glVertex3f( 1.0,-1.0,-10.0)
glEnd()
End Function

Now compile and run it, hopefully you should see a small window containing a blue triangle. It may not do much at the moment but we have just made our first OpenGL program. You can close the window down by pressing the ESCAPE key.

Looking through the first few lines of code, you should be able to get a general idea of how we set everything up.

bglCreateContext(800,600,32,0,BGL_BACKBUFFER|BGL_DEPTHBUFFER|BGL_FULLSCREEN)

This is the all important line, it resets our screen to a resolution of 800 by 600 pixels - with a colour depth of 32 bits. Notice the 3 flag statements I've used 'BGL_BACKBUFFER, BGL DEPTHBUFFER and lastly BGL_FULLSCREEN - I'm sure you can guess what the last one does !. The first 2 inform our graphics card that we want to use a double buffered screen for a smoother display, the Depthbuffer is also initialised - this is an area of memory that is set aside by our graphics card for working out the all important 'distance' maths calculations - dont worry too much about what these commands do at this stage.

Have a play with the resolutions and colour depth variable to see the difference a higher res makes to the image displayed.
BUT you will also have to change this line of code...

gluPerspective(45.0, 800/600, 0.1, 100.0)

This line sets up the 3d display on the screen, making sure that program uses the correct perspective. Notice how it currently passes a value of 1.333 ( 800 / 600 ), This is the offset distance ratio between the with and height. Make sure you change this to be equal to the width and height of your required screen resolution. It is possible to distort the screen by playing with this value.. widescreen, fish eyes.. whatever !.