OpenGL example for starters

Just starting out? Need help? Post your questions and find answers here.
peter
New User
New User
Posts: 8
Joined: Thu Apr 08, 2004 7:56 pm

OpenGL example for starters

Post by peter »

I made an OpenGL program for noobies with perspective.
The OpenGl-Cube example with Purebasic doesn't have perspective view.
I'm just a noob myself so don't ask advanced questions.

You need Glwrapper, put it in the userlibs. You can get it here.
viewtopic.php?t=9116&highlight=glwrapper

Perhaps fred should consider to add glwrapper to purebasic,
and replace the cube example with this code.

Tell me what you think.

Code: Select all

;OPENGL TEST FOR NOOBIES by Peter van Zwol
;I am just a noob learning openGL
;I threw this together using others code and learning from NEHE opengl
tutorials.
;I made this because the opengl cube program provided with PB doesn't have any perspective.
;This looks realy ugly and commands like translatef_ don't work without it, 

;Make sure you get the opengl wrapper of traumatic.
;It replaces some procedures from Opengl as well as GLut wich use
64 bit floats. Place the glwrapper in the userlibs directory.
;Note that all purebasic opengl commands end witth one _
;The wrapper commands end with two _. For example: glutperspective__ 



;This is a file wich contains all the constants you need it in your 
;program directory, it can be found in the sourcesadvanced\openglcube ;directory
IncludeFile "opengl.pbi"   

;rottri is a variable holding the triangle rotation variable
Global rottri.f
;hdc contains the window handle, it is used by OpenGL
Global hdc.w 

Procedure WindowGL()
;This procedure sets up a OpenGL window. I don't know how it works. Maybe sometime i want to learn it.
;For now this thingy works perfectly.
pfd.PIXELFORMATDESCRIPTOR
hWnd = OpenWindow(0, 0, 0, 800, 600, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "OpenGL Test")
hdc = GetDC_(hWnd)

pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion = 1
pfd\dwFlags = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType = #PFD_MAIN_PLANE
pfd\iPixelType = #PFD_TYPE_RGBA
pfd\cColorBits = 24
pfd\cDepthBits = 16

pixformat = ChoosePixelFormat_(hdc, pfd)
SetPixelFormat_(hdc, pixformat, pfd)
hrc = wglCreateContext_(hdc)
wglMakeCurrent_(hdc,hrc)
EndProcedure

Procedure InitGL()
;This procedure sets up the rendering environment. The projection matrix must be set to perspective.
;It's just the matrix wich calculates a 3d vector to a 2d screen.
glMatrixMode_(#GL_PROJECTION) ;pick projection matrix
glLoadIdentity_()             ;reset the matrix to the identity matrix
gluPerspective__(45,640/480,0.1,100)  ;add perspective to the matrix ;   ( This is a function from glwrapper)
glClearColor_(0.0, 0.0, 0.0, 0.0)   ;Set the clearcolor to black
glShadeModel_(#GL_SMOOTH)         ;enable smooth shading
glEnable_(#GL_DEPTH_TEST)         ;enable zbuffering, it calculates which polygon/object is deeper into the screen    
EndProcedure

Procedure defineobject()
;Here we gonna define the object. For triangles we have to define three ;vertexes
;between these three vertexes a triangle is drawn. This objevct consists of ;four triangles

glBegin_(#GL_TRIANGLES)

;Front Face of Pyramid
glColor3f_(1.0,0.0,0.0);      Red (Just RGB colors but now from 0 to 1)
glVertex3f_( 0.0, 1.0, 0.0);  Top Of Triangle (Front)
glColor3f_(1.0,1.0,1.0)
glVertex3f_(-1.0,-1.0, 0.0);  Left Of Triangle (Front)
glVertex3f_( 1.0,-1.0, 1.0);  Right Of Triangle (Front)

;Right Face of Pyramid
glColor3f_(0.0,1.0,0.0); 
glVertex3f_( 0.0, 1.0, 0.0);  Top Of Triangle (Right)
glColor3f_(1.0,1.0,1.0)
glVertex3f_(1.0,-1.0, 1.0);   Left Of Triangle (Right)
glVertex3f_(1.0,-1.0, -1.0);  Right Of Triangle (Right)

;left Face of Pyramid
glColor3f_(0.0,0.0,1.0); 
glVertex3f_( 0.0, 1.0, 0.0);  Top Of Triangle (Back)
glColor3f_(1.0,1.0,1.0); 
glVertex3f_(1.0,-1.0, -1.0);  Left Of Triangle (Back)
glVertex3f_(-1.0,-1.0, 0.0);  Right Of Triangle (Back)

;ground face of pyramid
glColor3f_(0.5,0.5,0.5); 
glVertex3f_( 1.0, -1.0, 1.0); Top Of Triangle (Left)
glVertex3f_(-1.0,-1.0,0.0);   Left Of Triangle (Left)
glVertex3f_(1.0,-1.0, -1.0);  Right Of Triangle (Left)
glEnd_(); // Done Drawing The Pyramid
EndProcedure

Procedure DrawScene(hdc_)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glMatrixMode_(#GL_MODELVIEW)      ;pick the matrix containing modeldata
glLoadIdentity_()                 ;reset it
glTranslatef_(-1.5, 0.0, -4.0)    ;move object 1.5 units left and 4 units into screen
glRotatef_(rottri,0.0,0.0,1.0)    ;rotate around z axis.
defineobject()

glLoadIdentity_()                 ;reset matrix for the following object
glTranslatef_(0.0, 0.0, -16.0)    ;rotate around y axis.
glRotatef_(rottri,0.0,1.0,0.0)    
defineobject()

glLoadIdentity_()                 ;reset matrix for the following object
glTranslatef_(2.0, 0.0, -10.0)    ;move object 2 right and 10 deep
glRotatef_(rottri,1.0,0.0,0.0)    ;rotate around x axis.
defineobject()

rottri = rottri + 0.6 ;Increase The Rotation Variable For The Triangle

EndProcedure

WindowGL()
InitGL()

While Quit = 0

EventID = WindowEvent()
Select EventID
Case #PB_EventCloseWindow
Quit = 1
EndSelect

DrawScene(hDC)          ;draw our stuff.
SwapBuffers_(hDC)       ;swap the buffers for smooth animation

Wend