Real Software Forums

OpenGL Positioned Lighting
Page 1 of 1

Author:  eugenedakin [ Mon Sep 10, 2012 6:52 am ]
Post subject:  OpenGL Positioned Lighting

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:

Here is my code in the OpenGLSurface1.Render Event:


'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.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.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.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.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.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.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.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]


Thanks for your helpful suggestions :)


Windows 7, Real Studio 2012 r1

Author:  doofus [ Tue Sep 11, 2012 6:12 am ]
Post subject:  Re: OpenGL Positioned Lighting

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.light(0).position(30, 0, 0)


g.xform.rotate(AnAngle, 0.5, 1, 0.5)

End Sub

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

Author:  eugenedakin [ Tue Sep 11, 2012 9:19 pm ]
Post subject:  Re: OpenGL Positioned Lighting

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.



Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group