You are not logged in or registered. Please login or register to use the full functionality of this board...
SIGN IN Join Our Community For FREE


OpenGL Spot Lights
09-02-2017, 04:15 AM
Post: #1
 (Print Post)
OpenGL Spot Lights
Here's a simple demo to create directional/spot light in OpenGL.
Code Snippet: [Select]
_TITLE "Spot Lights"
SCREEN _NEWIMAGE(800, 600, 32)
DO: LOOP UNTIL _SCREENEXISTS

DIM SHARED glAllow

DECLARE LIBRARY
   SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
   SUB glutSolidSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
END DECLARE

TYPE vec3
   x AS SINGLE
   y AS SINGLE
   z AS SINGLE
END TYPE

DIM SHARED redLight AS vec3, greenLight AS vec3, blueLight AS vec3

glAllow = -1


DO
   _LIMIT 40
LOOP UNTIL INKEY$ = CHR$(27)


SUB _GL () STATIC
   IF NOT glAllow THEN EXIT SUB 'we are not ready yet
   'setup
   IF NOT glSetup THEN
       _glViewport 0, 0, _WIDTH, _HEIGHT
       aspect# = _WIDTH / _HEIGHT
       glSetup = -1
   END IF

   _glEnable _GL_DEPTH_TEST

   'enable lights
   _glEnable _GL_LIGHTING
   'red light
   _glEnable _GL_LIGHT0
   _glLightfv _GL_LIGHT0, _GL_AMBIENT, glVec3(.2, 0, 0)
   _glLightfv _GL_LIGHT0, _GL_DIFFUSE, glVec3(.8, 0, 0)
   _glLightfv _GL_LIGHT0, _GL_SPECULAR, glVec3(.4, 0, 0)
   _glLightfv _GL_LIGHT0, _GL_SPOT_CUTOFF, glVec1(30)
   _glLightfv _GL_LIGHT0, _GL_SPOT_EXPONENT, glVec1(120)
   _glLightfv _GL_LIGHT0, _GL_SPOT_DIRECTION, glVec3(0, 0, -1)
   _glLightfv _GL_LIGHT0, _GL_POSITION, glVec4(redLight.x, redLight.y, redLight.z, 1.0)

   'green light
   _glEnable _GL_LIGHT1
   _glLightfv _GL_LIGHT1, _GL_AMBIENT, glVec3(0, .2, 0)
   _glLightfv _GL_LIGHT1, _GL_DIFFUSE, glVec3(0, .8, 0)
   _glLightfv _GL_LIGHT1, _GL_SPECULAR, glVec3(0, .4, 0)
   _glLightfv _GL_LIGHT1, _GL_SPOT_CUTOFF, glVec1(30)
   _glLightfv _GL_LIGHT1, _GL_SPOT_EXPONENT, glVec1(120)
   _glLightfv _GL_LIGHT1, _GL_SPOT_DIRECTION, glVec3(0, 0, -1)
   _glLightfv _GL_LIGHT1, _GL_POSITION, glVec4(greenLight.x, greenLight.y, greenLight.z, 1.0)

   'blue light
   _glEnable _GL_LIGHT2
   _glLightfv _GL_LIGHT2, _GL_AMBIENT, glVec3(0, 0, .2)
   _glLightfv _GL_LIGHT2, _GL_DIFFUSE, glVec3(0, 0, .8)
   _glLightfv _GL_LIGHT2, _GL_SPECULAR, glVec3(0, 0, .4)
   _glLightfv _GL_LIGHT2, _GL_SPOT_CUTOFF, glVec1(30)
   _glLightfv _GL_LIGHT2, _GL_SPOT_EXPONENT, glVec1(120)
   _glLightfv _GL_LIGHT2, _GL_SPOT_DIRECTION, glVec3(0, 0, -1)
   _glLightfv _GL_LIGHT2, _GL_POSITION, glVec4(blueLight.x, blueLight.y, blueLight.z, 1.0)
   
   _glShadeModel _GL_SMOOTH


   _glClear _GL_COLOR_BUFFER_BIT

   _glMatrixMode _GL_PROJECTION
   _glLoadIdentity

   _gluPerspective 45.0, aspect#, 1.0, 1000.0

   _glMatrixMode _GL_MODELVIEW
   _glLoadIdentity


   gluLookAt 0, -1, 2.5, 0, 0, 0, 0, 1, 0

   _glColor3f 1, 1, 1
   'Let's define the material to use.
   _glMaterialfv _GL_FRONT_AND_BACK, _GL_AMBIENT, glVec3(.2, .2, .2)
   _glMaterialfv _GL_FRONT_AND_BACK, _GL_DIFFUSE, glVec3(.8, .8, .8)
   _glMaterialfv _GL_FRONT_AND_BACK, _GL_SPECULAR, glVec3(.4, .4, .4)
   _glMaterialfv _GL_FRONT_AND_BACK, _GL_SPECULAR, glVec3(.4, .4, .4)
   
   drawPlane 2, 2
   drawLights redLight, 1, 0, 0
   drawLights greenLight, 0, 1, 0
   drawLights blueLight, 0, 0, 1
   

   _glFlush
   
   'updating the position of light in circular/eclipse motion.
   redLight.x = COS(clock#) * .5
   redLight.y = SIN(clock#) * .5
   redLight.z = 1
   greenLight.x = COS(clock#) * 1.2
   greenLight.y = SIN(clock#) * .5
   greenLight.z = 1
   blueLight.x = COS(clock#) * .5
   blueLight.y = SIN(clock#) * 1.2
   blueLight.z = 1
   clock# = clock# + .01
END SUB

FUNCTION glVec1%& (x)
   STATIC internal_vec1
   internal_vec1 = x
   glVec1%& = _OFFSET(internal_vec1)
END FUNCTION

FUNCTION glVec2%& (x, y)
   STATIC internal_vec2(2)
   internal_vec2(0) = x
   internal_vec2(1) = y
   glVec2%& = _OFFSET(internal_vec2())
END FUNCTION

FUNCTION glVec3%& (x, y, z)
   STATIC internal_vec3(2)
   internal_vec3(0) = x
   internal_vec3(1) = y
   internal_vec3(2) = z
   glVec3%& = _OFFSET(internal_vec3())
END FUNCTION

FUNCTION glVec4%& (x, y, z, w)
   STATIC internal_vec4(3)
   internal_vec4(0) = x
   internal_vec4(1) = y
   internal_vec4(2) = z
   internal_vec4(3) = w
   glVec4%& = _OFFSET(internal_vec4())
END FUNCTION

SUB drawPlane (w, h)
   _glNormal3f 0, 0, 1
   FOR y = -h / 2 TO h / 2 STEP .1
       _glBegin _GL_TRIANGLE_STRIP
       FOR x = -w / 2 TO w / 2 STEP .1
           _glVertex2f x, y
           _glVertex2f x, y + .1
       NEXT
       _glEnd
   NEXT
END SUB

SUB drawLights (w AS vec3, r!, g!, b!)
   _glDisable _GL_LIGHTING
   _glPushMatrix
   _glColor3f r!, g!, b!
   _glTranslatef w.x, w.y, w.z
   glutSolidSphere .02, 10, 10
   _glPopMatrix
   _glEnable _GL_LIGHTING
END SUB


Attached File(s) Image(s)
   
Find all posts by this user
Like Post
09-02-2017, 06:42 AM
Post: #2
 (Print Post)
RE: OpenGL Spot Lights
Really cool. Nicely done.

J

May your journey be free of incident.

Live long and prosper.
Find all posts by this user
Like Post
09-02-2017, 11:54 AM
Post: #3
 (Print Post)
RE: OpenGL Spot Lights
Hi Ashish, 

A marvelous effect!

I have been meaning to compliment you with the 3D models, table, jar, taxi cab...

The GL stuff is a bit overwhelming to understand but your examples are very inspiring!

For the 3D models do you start with 2D picture?

And for this demo, are those spotlights using collision detection?

I wonder if the spotlights could be made to look more like sun or star, something shining?

B += _
Find all posts by this user
Like Post
09-03-2017, 01:01 AM
Post: #4
 (Print Post)
RE: OpenGL Spot Lights
I agree... brilliant examples...

The 'obj' files are created using a 3D model editor like Blender. The 'texture' is like the 'skin' of a 3D object that has been 'flattened' into a 2D image. The editor combines the two. I could be wrong. I am no expert with Blender. The "bust' or 'head' image is the 3D image without a texture... I managed to create an object that I imported into PlatinumArts Sandbox. (a wooden crate and a vase). A lengthy process but quite rewarding. I digress. Sorry.

I am not familiar with GL (Open or otherwise... lol) and find it beyond my grasp. Never-the-less, still a bunch of excellent examples...

J

May your journey be free of incident.

Live long and prosper.
Find all posts by this user
Like Post
09-03-2017, 01:58 AM
Post: #5
 (Print Post)
RE: OpenGL Spot Lights
@bplus
Quote:
For the 3D models do you start with 2D picture?
Absolutely not. I've just understood the structure of an OBJ file from Wikipedia - Wavefront OBJ file.
An obj contain vertex, texture coordinates, vertex normals, materials definition which can be loaded into program & with the help of those data, we can render them.
Quote:
And for this demo, are those spotlights using collision detection?
No. They are just rotating around in the front of the plane.
Quote:
I wonder if the spotlights could be made to look more like sun or star, something shining?
The colored sphere representing the location of the spot light can be made to look like shining with the help of GLSL shader. Sadly, QB64 uses quite old version of OpenGL which doesn't support GLSL shader. I believe that upgrading QB64 to newer version of OpenGL is not a difficult task.
@johnno56
Quote:
The 'obj' files are created using a 3D model editor like Blender. The 'texture' is like the 'skin' of a 3D object that has been 'flattened' into a 2D image. The editor combines the two. I could be wrong.
No, You are correct.
Find all posts by this user
Like Post



Forum Jump:


User(s) browsing this thread: 1 Guest(s)




QB64 Member Project - RGB Color Wheel
QB64 Member Project - Othello
QB64 Member Project - Rotating Background
QB64 Member Project - Dakapo
QB64 Member Project - Touche
QB64 Member Project - Score 4
QB64 Member Project - Input
QB64 Member Project - Sabotage
QB64 Member Project - Color Rotating Text
QB64 Member Project - Line Thickness
QB64 Member Project - Kings Court
QB64 Member Project - Point Blank
QB64 Member Project - Dreamy Clock
QB64 Member Project - STxAxTIC 3D World
QB64 Member Project - Amazon
QB64 Member Project - Foursight
QB64 Member Project - Isolation
QB64 Member Project - Color Triangles
QB64 Member Project - MAPTRIANGLE
QB64 Member Project - Pivet version one
QB64 Member Project - Quarto
QB64 Member Project - Swirl
QB64 Member Project - Full Color LED Sign
QB64 Member Project - Kobolts Monopoly
QB64 Member Project - Exit
QB64 Member Project - Algeria Weather
QB64 Member Project - Spiro Roses
QB64 Member Project - Blokus
QB64 Member Project - OpenGL Triangles
QB64 Member Project - Kings Vallery version two
QB64 Member Project - Martin Fractals version four
QB64 Member Project - Pivot version two
QB64 Member Project - Qubic
QB64 Member Project - Martin Fractals version two
QB64 Member Project - 9 Board
QB64 Member Project - Martin Fractals version one
QB64 Member Project - Inside Moves
QB64 Member Project - Bowditch curve
QB64 Member Project - Martin Fractals version three
QB64 Member Project - Splatter
QB64 Member Project - Domain
QB64 Member Project - Basic Dithering
QB64 Member Project - Red Scrolling LED Sign
QB64 Member Project - ARB Checkers
QB64 Member Project - Rubix's Magic
QB64 Member Project - Overboard
QB64 Member Project - Connect Four
QB64 Member Project - Spinning Color Wheel
QB64 Member Project - Kings Valley verion one