Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Jun 26, 2017 12:27 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: OpenGL Positioned Lighting
PostPosted: Mon Sep 10, 2012 6:52 am 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Hello Everyone,

I am playing around with OpenGL, and I am trying to have a positioned light shining on a rotating cube. Chuckle, I must be missing something simple. Could you offer a suggestion as to what I may be doing wrong? I have put in commenting to show what I am doing (chuckle, or intending to do) :) The cube rotates, the background colour appears to work, and I am unable to have a colour change of the light that shines on one side of the cube.

The Windows 7 Real Studio 2012 r1 project can be downloaded here: http://scispec.ca/Realbasic/PositionedLight.zip

Here is my code in the OpenGLSurface1.Render Event:

OpenGL.glEnable(OpenGL.GL_LIGHTING)
OpenGL.glEnable(OpenGL.GL_LIGHT0)
OpenGL.glEnable(OpenGL.GL_LIGHT1)
OpenGL.glEnable(OpenGL.GL_NORMALIZE)
OpenGL.glEnable(OpenGL.GL_SMOOTH)

'Create the variable Ambient Colour
Dim Set_Ambient_Colour as new MemoryBlock(16)
'Set the colour
Set_Ambient_Colour.SingleValue(0) = 51/255 'red
Set_Ambient_Colour.SingleValue(4) = 51/255 'green
Set_Ambient_Colour.SingleValue(8) = 51/255 'blue
Set_Ambient_Colour.SingleValue(12) = 1.0 'Alpha

OpenGL.glLightfv(OpenGL.GL_LIGHT_MODEL_AMBIENT, OpenGL.GL_AMBIENT, Set_Ambient_Colour)

'Add Positioned Light
Dim Set_Positioned_Light_Color as new MemoryBlock(16)
'Set the colour (0.5,0.5,0.5)
Set_Ambient_Colour.SingleValue(0) = 254/255 'red
Set_Ambient_Colour.SingleValue(4) = 254/255 'green
Set_Ambient_Colour.SingleValue(8) = 254/255 'blue
Set_Ambient_Colour.SingleValue(12) = 1.0 'Alpha

Dim Set_Positioned_Light_Position as new MemoryBlock(16)
'Set the light position at (4,0,8)
Set_Ambient_Colour.SingleValue(0) = 4
Set_Ambient_Colour.SingleValue(4) = 0
Set_Ambient_Colour.SingleValue(8) = 8
Set_Ambient_Colour.SingleValue(12) = 1.0 'Alpha

'Set the positioned light colour
OpenGL.glLightfv(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, Set_Positioned_Light_Color)
OpenGL.glLightfv(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, Set_Positioned_Light_Position)

'Add Directed Light
Dim Set_Directed_Light_Color as new MemoryBlock(16)
'Set the colour (0.5,0.2,0.2)
Set_Ambient_Colour.SingleValue(0) = 122/255 'red
Set_Ambient_Colour.SingleValue(4) = 51/255 'green
Set_Ambient_Colour.SingleValue(8) = 51/255 'blue
Set_Ambient_Colour.SingleValue(12) = 1.0 'Alpha

Dim Set_Directed_Light_Position as new MemoryBlock(16)
'Set the light position at (4,0,8)
Set_Ambient_Colour.SingleValue(0) = -1
Set_Ambient_Colour.SingleValue(4) = 0.5
Set_Ambient_Colour.SingleValue(8) = 0.5
Set_Ambient_Colour.SingleValue(12) = 0 'Alpha

OpenGL.glLightfv(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, Set_Directed_Light_Color)
OpenGL.glLightfv(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION, Set_Directed_Light_Position)

'Create a rotating cube
OpenGL.glClearColor(0,0,0,1) 'White Background
OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT) 'Clear Colour buffer
OpenGL.glClear(OpenGL.GL_DEPTH_BUFFER_BIT) 'Clear Depth buffer
OpenGL.glMatrixMode(OpenGL.GL_PROJECTION) 'Select projection
OpenGL.glEnable(OpenGL.GL_COLOR_MATERIAL) 'Enable colours
OpenGL.glLoadIdentity() 'resets projection matrix

OpenGL.glEnable(OpenGL.GL_DEPTH_TEST) 'Enables 3d depth testing



OpenGL.glPushMatrix()
OpenGL.glRotated(AnAngle,0.5,1,0.5) 'Rotate cube
OpenGL.glScaled(0.25,0.25,0.25) ' scale at 25%

'Prepare to Draw a cube
OpenGL.glBegin OpenGL.GL_QUADS
OpenGL.glColor3d(1,1,0)' Yellow

'Give coordinates of the four sides (x,y,z) axis
OpenGL.glNormal3d(0,1,0)
OpenGL.glVertex3d(1,1,-1) 'Top right [top]
OpenGL.glVertex3d(-1,1,-1) 'Top left [top]
OpenGL.glVertex3d(-1,1,1) 'Bottom left [top]
OpenGL.glVertex3d(1,1,1) 'Bottom right [top]

OpenGL.glNormal3d(0,-1,0)
OpenGL.glVertex3d(1,-1,1) 'Top right [bottom]
OpenGL.glVertex3d(-1,-1,1) 'Top left [bottom]
OpenGL.glVertex3d(-1,-1,-1) 'Bottom left [bottom]
OpenGL.glVertex3d(1,-1,-1) 'Bottom right [bottom]

OpenGL.glNormal3d(0,0,1)
OpenGL.glVertex3d(1,1,1) 'Top right [front]
OpenGL.glVertex3d(-1,1,1) 'Top left [front]
OpenGL.glVertex3d(-1,-1,1) 'Bottom left [front]
OpenGL.glVertex3d(1,-1,1) 'Bottom right [front]

OpenGL.glNormal3d(0,0,-1)
OpenGL.glVertex3d(1,-1,-1) 'Top right [back]
OpenGL.glVertex3d(-1,-1,-1) 'Top left [back]
OpenGL.glVertex3d(-1,1,-1) 'Bottom left [back]
OpenGL.glVertex3d(1,1,-1) 'Bottom right [back]

OpenGL.glNormal3d(-1,0,0)
OpenGL.glVertex3d(-1,1,1) 'Top right [Left]
OpenGL.glVertex3d(-1,1,-1) 'Top left [Left]
OpenGL.glVertex3d(-1,-1,-1) 'Bottom left [Left]
OpenGL.glVertex3d(-1,-1,1) 'Bottom right [Left]

OpenGL.glNormal3d(1,0,0)
OpenGL.glVertex3d(1,1,-1) 'Top right [Right]
OpenGL.glVertex3d(1,1,1) 'Top left [Right]
OpenGL.glVertex3d(1,-1,1) 'Bottom left [Right]
OpenGL.glVertex3d(1,-1,-1) 'Bottom right [Right]

OpenGL.glEnd
OpenGL.glPopMatrix()




Thanks for your helpful suggestions :)

Sincerely,

Eugene
Windows 7, Real Studio 2012 r1


Top
 Profile  
Reply with quote  
 Post subject: Re: OpenGL Positioned Lighting
PostPosted: Tue Sep 11, 2012 6:12 am 
Offline
User avatar

Joined: Thu Sep 10, 2009 2:50 am
Posts: 418
Location: Santa Cruz, CA, USA
Oh man, the thickness of the "OpenGL." is hiding the obvious problem: Set_Ambient_Colour is repeated in place of Set_Positioned_Light_Color, etc. Fix those names and it works!

I'd suggest to make a set of methods to wrap even the simplest opengl action and write against that. It's easier to autocomplete, read and remember and when its working you can always inline replace the OpenGL module code. This is what your code looks like rewritten suchly...

Sub Paint(g As ezgl)

g.lightsOn
g.lightModel.ambient(&c0000FF)
g.light(0).enable
g.light(0).diffuse(&cFFFFFF)
g.light(0).position(30, 0, 0)

g.material.diffuse(&cFFFFFF)

g.pushMatrix
g.xform.rotate(AnAngle, 0.5, 1, 0.5)
g.xform.scale(0.25)
g.draw.cube
g.popMatrix

End Sub


Also, to make it a directional light specify the spotDirection vector. and usually spotExponent and an attenuation.


Top
 Profile  
Reply with quote  
 Post subject: Re: OpenGL Positioned Lighting
PostPosted: Tue Sep 11, 2012 9:19 pm 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Excellent help, thank you!

Chuckle, this type of error happens when too much time is spent in front of the screen without a break :) You provided great tips that makes the code more readable.

Thanks for your great comments, it works great.

Sincerely,

Eugene


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