Author Topic: OpenGL heirarchal transforms....  (Read 3584 times)

0 Members and 1 Guest are viewing this topic.

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
OpenGL heirarchal transforms....
« on: June 05, 2006 »
Way cool!!!!  Line cube is now faster and cooler. :*)


Code: [Select]
'Line cube n GL?
'Easier than doing it in software. :*)
'Heirarchal transforms roxoors.
'Relsoft 2006
'Richard Eric M. Lope
'http://rel.betterwebber.com

Option Explicit
'$Static
'$INCLUDE: "\gl\gl.bi"
'$INCLUDE: "\gl\glu.bi"
'$INCLUDE: "\gl\glfw.bi"
'$INCLUDE: "\gl\glext.bi"

const SCR_WIDTH = 640                   'Width of the screen
const SCR_HEIGHT = 480                  'height of the screen
const BITSPP = 32                       '32 bits per pixel format

const GL_TEXT_WID = 512
const GL_TEXT_HEI = 512

const PI = 3.141593
const TWOPI = (2 * PI)



Type DisplayType
    W As Uinteger
    H As Uinteger
    R_BITS As Uinteger
    G_BITS As Uinteger
    B_BITS As Uinteger
    A_BITS As Uinteger
    D_BITS As Uinteger
    S_BITS As Uinteger
    MODE As Uinteger
    GlVer As Zstring Ptr
    As Single FOVy, Aspect, zNear, zFar
End Type



declare Sub Init_GL_Window( Display As DisplayType )
declare SUB Init_GL_SCREEN()
declare SUB init_cube()


Dim Display As DisplayType
Init_GL_Window Display
Init_GL_SCREEN()


dim shared as glUint CubeDL

init_cube()

dim i as integer
dim theta as integer
dim elapsed_time as single
dim as single ti, t, dt
dim as single time_start
time_start =  glfwGetTime()
Do
   
    elapsed_time = glfwGetTime()
    ti = glfwGetTime() * 0.001208
    t = ti * 180.5 * (sin(elapsed_time/3150) + 360 * cos(elapsed_time/4150) + 90 * sin(elapsed_time/3140))
    dt = 0.80130 + 2.0124 * (sin(elapsed_time/40) * sin(elapsed_time/240) * cos(elapsed_time/540))
    GlClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
       
    GlMatrixMode GL_MODELVIEW
    GlLoadIdentity
    glPolygonMode(GL_FRONT, GL_LINE)
    glPolygonMode(GL_BACK, GL_LINE)
    glEnable (GL_BLEND)
    glBlendFunc(GL_ONE, GL_ONE)
    glDisable (GL_DEPTH_TEST)
   
    glTranslatef (0.0,0.0,-5.0)   
    glPushMatrix       
        glTranslatef (sin(elapsed_time)* 1,cos(elapsed_time)* 1,cos(elapsed_time)+ sin(elapsed_time)* 1)       
        theta = (theta + 1) mod 360
        dim as single scale = 0.01
        'glRotatef (theta, 1,0,0)
        glRotatef (theta, 0,1,0)
        glRotatef (theta, 0,0,1)
        glScalef (scale,scale,scale)
        for i = 0 to 250
            glPushMatrix
            'theta += 1
            'glRotatef (sin((elapsed_time+theta)/100) * 180, 1,0,0)
            'glRotatef (sin((elapsed_time+theta)/120) * 90, 0,1,0)
            'glRotatef (sin((elapsed_time+theta)/150) * 360, 0,0,1)
            t = t + dt
            glRotatef ((t), 1,0,0)
            glRotatef ((t), 0,1,0)
            glRotatef ((t), 0,0,1)
            'glScalef (0.9,0.9,0.9)
            glCallList(CubeDL)
            glPopMatrix
            glScalef (1.0187,1.0187,1.0187)
        next i
    glPopMatrix   
    do
    loop until glfwGetTime() - time_start >= (1/60)
    time_start =  glfwGetTime()
   
    GlfwSwapBuffers
Loop Until glfwGetKey( GLFW_KEY_ESC )


GlfwTerminate()



sub init_cube()
  dim as single col = 0.03
  CubeDL =glGenLists(1)
  glNewList(CubeDL, GL_COMPILE)
  glBegin(GL_QUADS)
    ' Front Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0,  1.0)
    ' Back Face
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    ' Top Face
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    ' Bottom Face
    glColor3f(col, col,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    ' Right face
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    ' Left Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
  glEnd()
  glEndList()

end sub





Sub Init_GL_Window( Display As DisplayType )
    Display.W     = 640
    Display.H     = 480
    Display.R_BITS= 8
    Display.G_BITS= 8
    Display.B_BITS= 8
    Display.A_BITS= 8
    Display.D_BITS= 32
    Display.S_BITS= 8
    Display.MODE  = GLFW_WINDOW
    Display.MODE  = GLFW_FULLSCREEN
   
    If glfwInit() Then
        'Successful!
    Else   
        Print "Failed to initialize GLFW!"
        Sleep 1000
        End
    End If
   
   
    If  glfwOpenWindow( _
    Display.W     , _
    Display.H     , _
    Display.R_BITS, _
    Display.G_BITS, _
    Display.B_BITS, _
    Display.A_BITS, _
    Display.D_BITS, _
    Display.S_BITS, _
    Display.MODE  ) _
    Then
    GlfwSwapInterval 1
    Display.GlVer = glGetString(GL_VERSION)
Else
    GlfwTerminate()
    End
End If

End Sub


SUB Init_GL_SCREEN()

   'screen information
   dim w as integer, h as integer
    'OpenGL params for gluerspective
   dim FOVy as double            'Field of view angle in Y
   dim Aspect as double          'Aspect of screen
   dim znear as double           'z-near clip distance
   dim zfar as double            'z-far clip distance

    'using screen info w and h as params
   glViewport 0, 0, SCR_WIDTH,SCR_HEIGHT

   'Set current Mode to projection(ie: 3d)
   glMatrixMode GL_PROJECTION

    'Load identity matrix to projection matrix
   glLoadIdentity

    'Set gluPerspective params
    FOVy = 80/2                                     '45 deg fovy
    Aspect = SCR_WIDTH / SCR_HEIGHT                                  'aspect = x/y
    znear = 1                                       'Near clip
    zfar = 200                                      'far clip

    'use glu Perspective to set our 3d frustum dimension up
   gluPerspective FOVy, aspect, znear, zfar

    'Modelview mode
    'ie. Matrix that does things to anything we draw
    'as in lines, points, tris, etc.
   glMatrixMode GL_MODELVIEW
   'load identity(clean) matrix to modelview
   glLoadIdentity

   glShadeModel GL_SMOOTH                 'set shading to smooth(try GL_FLAT)
   glClearColor 0.0, 0.0, 0.0, 1.0        'set Clear color to BLACK
   glClearDepth 1.0                       'Set Depth buffer to 1(z-Buffer)
   glEnable GL_DEPTH_TEST                 'Enable Depth Testing so that our z-buffer works

   'compare each incoming pixel z value with the z value present in the depth buffer
   'LEQUAL means than pixel is drawn if the incoming z value is less than
    'or equal to the stored z value
   glDepthFunc GL_LEQUAL

    'have one or more material parameters track the current color
    'Material is your 3d model
   glEnable GL_COLOR_MATERIAL


    'Enable Texturing
    glEnable GL_TEXTURE_2D
   

    'Set blending parameters
    glBlendFunc GL_SRC_ALPHA, GL_ONE


   'Tell openGL that we want the best possible perspective transform
   glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
   
   'Enable Backface culling
   'GlEnable GL_CULL_FACE
   glPolygonMode(GL_FRONT, GL_FILL)
   
   
        'lighting
  dim as glFloat mat_ambient(0 to 3) =  { 0.5, 0.5, 1.0, 0.15 }
  dim as glFloat mat_specular(0 to 3) = {  1.0, 1.0, 1.0, 0.15 } 
  dim as glFloat mat_shininess = 10.0
  dim as glFloat light_position(0 to 3)= { -7.0, 8.0, -6.0, 1.0  }
  dim as glFloat light_ambient(0 to 3)= { 0.5, 0.5, 0.5, 1.0 }
  dim as glFloat mat_twister(0 to 3) = { 0.75, 0.75, 0.0, 1.0 }
  dim as GLfloat fBrightLight(0 to 3) = { 1.0, 1.0, 1.0, 1.0 }
  glMaterialfv(GL_FRONT, GL_AMBIENT, @mat_ambient(0))
  glMaterialfv(GL_FRONT, GL_SPECULAR, @mat_specular(0))
  glMaterialfv(GL_FRONT, GL_SHININESS, @mat_shininess)
  glMaterialfv(GL_FRONT, GL_DIFFUSE,@mat_twister(0))
  glMaterialfv(GL_FRONT, GL_SPECULAR, @fBrightLight(0))


  glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR)
  glLightfv(GL_LIGHT0,GL_AMBIENT, @light_ambient(0))
  glLightfv(GL_LIGHT0,GL_DIFFUSE, @light_ambient(0))
  glLightfv(GL_LIGHT0, GL_POSITION, @light_position(0))
  'glEnable(GL_LIGHTING)
  'glEnable(GL_LIGHT0)
END SUB


 O0
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OpenGL heirarchal transforms....
« Reply #1 on: June 05, 2006 »
Thats way smart, nice one Relsoft dude.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: OpenGL heirarchal transforms....
« Reply #2 on: June 05, 2006 »
Thanks!  I thought of exploiting the way opengl works and this is one way I know of. ;*)
 :||
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17427
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: OpenGL heirarchal transforms....
« Reply #3 on: June 05, 2006 »
It was fast before but it's way smoother now.
Damn, it's nice.
Shockwave ^ Codigos
Challenge Trophies Won: