Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Sep 22, 2020 3:29 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: RB and OpenGL from Nothing to Cube!
PostPosted: Tue May 13, 2008 3:21 pm 
Offline

Joined: Mon May 12, 2008 5:25 pm
Posts: 43
Hey All,
It took me a while to figure this out and I figured I'd share. This is how you start with nothing and get a cube with OpenGL and RB.

I'm using Version 2007.

Below are instructions, you can download the project file here:
http://www.carmine.com/temp/RBOpenGLCube.rbp


1. Get the OpenGLSurface plugin:
http://www.tinrocket.com/software/openglsurface/

Put it in your plugins directory and Open up RealBasic.

2. Set Window1 width and height to be 640x480 (you can use any height, but this is nice and old school).

Drag the OpenGLSurface Control (in the list of "All Controls") onto your window and drag it to be the full size (640x480)

3. Drag a Thread onto your window.

4. Go to Window1's Event Handlers. Put this code in the event handler for Open

Thread1.Run


5. Go to Window1's Event Handlers. Put this code in the event handler for Close

Thread1.Kill
OpenGLSurface1.Close
Quit


6. Add a Property to Window1 Call it "rot" make it a "Single" set it to 0

7. Go to OpenGLSurface1's Event Handlers, put this in Open

Dim sWidth as Single = Width // Is there a better way to typecast in RB?
Dim sHeight as Single = Height

OpenGL.glMatrixMode(OpenGL.GL_PROJECTION)
OpenGL.glLoadIdentity()
OpenGL.gluPerspective(45.0, sWidth / sHeight, 0.1, 100.0)

OpenGL.glMatrixMode(OpenGL.GL_MODELVIEW)
OpenGL.glLoadIdentity()

OpenGL.glClearColor(0.0, 0.0, 0.0, 1.0) // Black Background
OpenGL.glClearDepth(1.0)

OpenGL.glEnable(OpenGL.GL_DEPTH_TEST)
OpenGL.glDepthFunc(OpenGL.GL_LEQUAL)


8. Put this code in OpenGLSurface1's Render
OpenGL.glClear(OpenGL.gl_COLOR_BUFFER_BIT + OpenGL.gl_DEPTH_BUFFER_BIT)

OpenGL.glLoadIdentity()
OpenGL.glTranslatef(0.0, 0.0, -6) // Move it back a bit so we can see all of it
OpenGL.glRotatef(rot, 1.0, 0.0, 0.0) // Rotate X
OpenGL.glRotatef(rot, 0.0, 1.0, 0.0) // Rotate Y
OpenGL.glRotatef(rot, 0.0, 0.0, 1.0) // Rotate Z

OpenGL.glBegin(OpenGL.gl_QUADS)
// Front Face
OpenGl.glColor3f(1.0, 0.0, 0.0)
OpenGL.glVertex3f(-1.0, -1.0, 1.0)
OpenGL.glVertex3f( 1.0, -1.0, 1.0)
OpenGL.glVertex3f( 1.0, 1.0, 1.0)
OpenGL.glVertex3f(-1.0, 1.0, 1.0)

// Back Face
OpenGl.glColor3f(0.0, 1.0, 0.0)
OpenGL.glVertex3f(-1.0, -1.0, -1.0)
OpenGL.glVertex3f(-1.0, 1.0, -1.0)
OpenGL.glVertex3f( 1.0, 1.0, -1.0)
OpenGL.glVertex3f( 1.0, -1.0, -1.0)

// Top Face
OpenGl.glColor3f(0.0, 0.0, 1.0)
OpenGL.glVertex3f(-1.0, 1.0, -1.0)
OpenGL.glVertex3f(-1.0, 1.0, 1.0)
OpenGL.glVertex3f( 1.0, 1.0, 1.0)
OpenGL.glVertex3f( 1.0, 1.0, -1.0)

// Bottom Face
OpenGl.glColor3f(1.0, 1.0, 0.0)
OpenGL.glVertex3f(-1.0, -1.0, -1.0)
OpenGL.glVertex3f( 1.0, -1.0, -1.0)
OpenGL.glVertex3f( 1.0, -1.0, 1.0)
OpenGL.glVertex3f(-1.0, -1.0, 1.0)

// Right face
OpenGl.glColor3f(0.0, 1.0, 1.0)
OpenGL.glVertex3f( 1.0, -1.0, -1.0)
OpenGL.glVertex3f( 1.0, 1.0, -1.0)
OpenGL.glVertex3f( 1.0, 1.0, 1.0)
OpenGL.glVertex3f( 1.0, -1.0, 1.0)

// Left Face
OpenGl.glColor3f(1.0, 0.0, 1.0)
OpenGL.glVertex3f(-1.0, -1.0, -1.0)
OpenGL.glVertex3f(-1.0, -1.0, 1.0)
OpenGL.glVertex3f(-1.0, 1.0, 1.0)
OpenGL.glVertex3f(-1.0, 1.0, -1.0)

OpenGL.glEnd()

rot = rot + 1


9. Put this in Thread1's "Run"
While true
OpenGLSurface1.Render
Wend


10. Hit RUN! Enjoy!


Top
 Profile  
Reply with quote  
 Post subject: Re: RB and OpenGL from Nothing to Cube!
PostPosted: Tue May 13, 2008 3:33 pm 
Offline

Joined: Mon May 12, 2008 5:25 pm
Posts: 43
By the way. the glColor3f command is setting the current color. The above code is setting the color for each face, but if you change the color at each vertex (point) you get some cool blending effects.

Put this in for Front Face
// Front Face
OpenGl.glColor3f(1.0, 0.0, 0.0)
OpenGL.glVertex3f(-1.0, -1.0, 1.0)
OpenGl.glColor3f(0.0, 1.0, 0.0)
OpenGL.glVertex3f( 1.0, -1.0, 1.0)
OpenGl.glColor3f(0.0, 0.0, 1.0)
OpenGL.glVertex3f( 1.0, 1.0, 1.0)
OpenGl.glColor3f(0.0, 0.0, 0.0)
OpenGL.glVertex3f(-1.0, 1.0, 1.0)


Top
 Profile  
Reply with quote  
 Post subject: Re: RB and OpenGL from Nothing to Cube!
PostPosted: Sun May 18, 2008 2:31 pm 
Offline

Joined: Fri Dec 16, 2005 10:19 pm
Posts: 493
John, the developer of the OpenGLSurface plugin, says "an update is in progress" to fix the current issue (it won't compile, at least on my Intel Mac) with rb 2008 r2. If you have this issue, you can wait for the fix, or use 2008 r1 which seems to work flawlessly.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group