5 New OpenGL Programs!!! :-)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

Image

Here's a demo with a proper show_sphere() function.
If you want to make it a bit more simple, you can remove all the lighting commands. (Just search the code for "light.") ;-)

Code: Select all

' Atom Show OpenGL Demo! v0.1
' (C) 2008 Innova and Kristopher Windsor
' Adjusted by Brad Fuller and Kristopher Windsor (again! :-P)

#include "GL/gl.bi"
#include "GL/glu.bi"

Const screenx = 640, screeny = 480
Const true = -1, false = 0, pihalf = Atn(1) * 2

Type camera_type
  As Double eye(1 To 3)
  As Double see(1 To 3)
  As Double up(1 To 3)
End Type

Type coord_type
  As Double x, y
End Type

Dim Shared LightDiffuse(0 To 3) As Single => {3, 3, 3, 1}
Dim Shared LightPosition(0 To 3) As Single => {0, 0, 0, 1}
Dim Shared As String Key
Dim Shared As camera_type camera

Sub start
  Screenres screenx, screeny, 32,, 2
  
  glMatrixMode GL_PROJECTION
  glLoadIdentity
  gluPerspective 35.0, screenx / screeny, .01, 8
  glMatrixMode GL_MODELVIEW
  glLoadIdentity
  
  glClearColor 0, 0, 0, 1
  glEnable GL_DEPTH_TEST 'properly handles overlapping polygons (draw order for layering)
  
  glEnable GL_LIGHT1
  glLightfv GL_LIGHT1, GL_DIFFUSE, @LightDiffuse(0)
  glEnable GL_LIGHTING
  glEnable GL_COLOR_MATERIAL
  
  Randomize Timer
End Sub

Sub show_sphere (Byval x As Double, Byval y As Double, Byval z As Double, Byval size As Double, Byval c As Integer)
  #define clor(c) glColor3b( (c And &HFF0000) Shr 17, (c And &HFF00) Shr 9, (c And &HFF) Shr 1)
  
  Static As GLUquadricObj Ptr q 'Quadratic For Drawing A Sphere
  
  If q = 0 Then q = gluNewQuadric() 'Initialize Quadratic
  
  clor(c)
  
  glTranslated(x, y, z) 'move camera so it looks like the sphere is drawn in a different place
  gluSphere(q, size, 64, 32) 'sphere
  glTranslated(-x, -y, -z) 'set camera back to normal
End Sub

Sub go
  Static As Double angle_x, angle_y, angle_z
  
  'coords are set for bird's eye view
  'top left of map is at (0, 0, 0)
  'all parts of map are 1un cubed
 
  glClear GL_DEPTH_BUFFER_BIT Or GL_COLOR_BUFFER_BIT
  glLoadIdentity
  
  LightPosition(0) = 0
  LightPosition(1) = 0
  LightPosition(2) = 1
  glLightfv GL_LIGHT1, GL_POSITION, @LightPosition(0)
 
  With camera
    .eye(1) = 6
    .eye(2) = 0
    .eye(3) = 0
    
    .see(1) = 0
    .see(2) = 0
    .see(3) = 0
    
    .up(1) = 0
    .up(2) = 0
    .up(3) = 1
   
    gluLookat .eye(1), .eye(2), .eye(3), .see(1), .see(2), .see(3), .up(1), .up(2), .up(3)
  End With
  
  'random spinning (instead of walking around the ground)
  angle_x += .4
  angle_y += .6
  angle_z += .7
  glRotatef angle_x, 1, 0, 0
  glRotatef angle_y, 0, 1, 0
  glRotatef angle_z, 0, 0, 1
  
  'ground
  If Multikey(&H39) Then 'space bar
    glBegin GL_QUADS
      glColor3d .4, .2, 0
      glNormal3f 0, 0, 1
      glVertex3d -1, -1, 0
      glVertex3d 1,  -1, 0
      glVertex3d 1,  1,  0
      glVertex3d -1, 1,  0
    glEnd
  End If
  
  'center sphere (red)
  show_sphere 0, 0, 0, .5, &HFF4444
  
  'four spheres on the ground (blue)
  show_sphere 1,  0,  0, .2, &H4444FF
  show_sphere 0,  1,  0, .2, &H4444FF
  show_sphere -1, 0,  0, .2, &H4444FF
  show_sphere 0,  -1, 0, .2, &H4444FF
  
  'four spheres above the ground (yellow)
  show_sphere 1,  0,  1, .2, &HFFFF44
  show_sphere 0,  1,  1, .2, &HFFFF44
  show_sphere -1, 0,  1, .2, &HFFFF44
  show_sphere 0,  -1, 1, .2, &HFFFF44
  
  'four spheres below the ground (green)
  show_sphere 1,  0,  -1, .2, &H44FFFF
  show_sphere 0,  1,  -1, .2, &H44FFFF
  show_sphere -1, 0,  -1, .2, &H44FFFF
  show_sphere 0,  -1, -1, .2, &H44FFFF
  
  'one really big sphere far away, above the ground (white)
  show_sphere 0,  0, 3, 2, &HFFFFFF
  
  Flip
End Sub

Sub main
  start
  Do
    go
    Sleep 10
    Key = Inkey
  Loop Until Key = Chr(27)
End Sub

main
xD
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Post by integer »

WOW! That is incredible.
What I mean is that I am amazed at the simplicity of the program.
I have had an very difficult time grasping the aspects of the functions.

With that code, I can finally understand what is happening.
I eliminated the white and red orbs, and
"glue" the desired neutrons (green) and protons (blue) objects
together to get the visual aid.

Thank you.

That (probably) has saved me a week end of trying to get it right.
I was jousting at windmills, and you gave me a fly swatter to kill the bugs.
That really helped.
Thanks.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

Your welcome, Don Quixote! ;-)

This particular program was easy since it just rotates everything with glRotatef. To "properly" do this by adjusting the camera (gluLookat) as I did in my other programs (especially Car Highway) takes a bit better grasp of 3D.
It would take a small reconfiguration if you wanted the camera to look at a point other than (0, 0, 0). xD
h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Post by h4tt3n »

I'd like to know about the camera thing. Would you care to post a link to that game of yours?
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

h4tt3n wrote:I'd like to know about the camera thing. Would you care to post a link to that game of yours?
It's on the first page of this thread. :-P

In the programs I have posted here, I specify the camera position, and the point for the camera to look at. If the camera appears to be moving or rotating, that's because it is.
In this sphere demo, I did that, too, but the camera is actually in a fixed position, and the rotation is achieved without it (through the Rotate commands).
While this works well, it was essentially the only way in QB with wire frame GFX. I could rotate the camera, but not move it, which limited the possibilities. So for example, if you want to have most of the spheres spinning around the origin, but have one of them not rotating, you will have to use a slightly different camera / rotation method. ;-)
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Post by relsoft »

KristopherWindsor wrote:
h4tt3n wrote:I'd like to know about the camera thing. Would you care to post a link to that game of yours?
It's on the first page of this thread. :-P

In the programs I have posted here, I specify the camera position, and the point for the camera to look at. If the camera appears to be moving or rotating, that's because it is.
In this sphere demo, I did that, too, but the camera is actually in a fixed position, and the rotation is achieved without it (through the Rotate commands).
While this works well, it was essentially the only way in QB with wire frame GFX. I could rotate the camera, but not move it, which limited the possibilities. So for example, if you want to have most of the spheres spinning around the origin, but have one of them not rotating, you will have to use a slightly different camera / rotation method. ;-)
Err, a lookat function is easily done in QB. That is if you know how to directly transform with matrices.

http://www.phatcode.net/articles.php?id=216
Post Reply