Opengl 3d movement [solved]

Advanced game related topics
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Opengl 3d movement [solved]

Post by Foz »

I'm trying to move down the Z axis and when the stars are out of range (+/- 100) a new star is placed in with a new location in relative to your current position. Except that it doesn't work.

Okay, I'll admit that I'm doing something simply idiotic. Could someone tell me what idiotic thing I'm doing is please? :mrgreen:

Here's my example - note that the subsystem needs to be set to OpenGL.

Q to move forward, A to move back, Esc to quit

Edit: ok, I've solved it now - I forgot that when you look down the Z plane, it's in the negatives, and my movement was in the positives and then when I was combining the two together... well, it didn't work! I've updated the example accordingly

Code: Select all

;{--- OpenGL Setup
CompilerIf Subsystem("OpenGL")
CompilerElse
  MessageRequester("ERROR", "Set the subsystem To OpenGL")
  End
CompilerEndIf

#GL_BLEND = $0BE2
#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_DEPTH_TEST = $0B71
#GL_LEQUAL = $0203
#GL_LINEAR = $2601
#GL_LINEAR_MIPMAP_NEAREST = $2701
#GL_LUMINANCE = $1909
#GL_CULL_FACE = $0B44
#GL_TRUE = 1
#GL_MODELVIEW = $1700
#GL_MODULATE = $2100
#GL_NICEST = $1102
#GL_PERSPECTIVE_CORRECTION_HINT = $0C50
#GL_PROJECTION = $1701
#GL_SMOOTH = $1D01

;  BeginMode 
#GL_POINTS                         = $0000
#GL_LINES                          = $0001
#GL_LINE_LOOP                      = $0002
#GL_LINE_STRIP                     = $0003
#GL_TRIANGLES                      = $0004
#GL_TRIANGLE_STRIP                 = $0005
#GL_TRIANGLE_FAN                   = $0006
#GL_QUADS                          = $0007
#GL_QUAD_STRIP                     = $0008
#GL_POLYGON                        = $0009


CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC "/usr/lib/libX11.a"+Chr('"')+" -l"+Chr('"')+"GL"
CompilerElse
  Import "Opengl32.lib"
CompilerEndIf
;{-
glBegin(a.l)
glBindTexture(a.l,b.l)
glClear(a.l)
glClearColor(a.f,b.f,c.f,d.f)
glClearDepth(a.d)
glDepthFunc(a.l)
glDepthMask(a.c)
glDisable(a.l)
glEnable(a.l)
glEnd()
glGenTextures(a.l,b.l)
glGetError()
glHint(a.l,b.l)
glLoadIdentity()
glMatrixMode(a.l)
glShadeModel(a.l)
glTexCoord2f(a.f,b.f)
glTexEnvf(a.l,b.l,c.f)
glTexParameterf(a.l,b.l,c.f)
glTexParameteri(a.l,b.l,c.i)
glTranslatef(a.f,b.f,c.f)
glVertex3f(a.f,b.f,c.f)
glVertex2f(a.f,b.f)
glViewport(a.l,b.l,c.l,d.l)

glBlendFunc(a.l,b.l) ; As "glBlendFunc@8"
glPixelStorei(a.l,b.l) ; As "glPixelStorei@8"
glColor3f(r.f, g.f, b.f)
glColor4f(r.f, g.f, b.f, a.f)
glRotatef(degrees.f, x.f, y.f, z.f)

glPushMatrix()
glPopMatrix()

glMultMatrixf(*matrix)
;}
EndImport

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC "/usr/lib/libGLU.a"
CompilerElse
  Import "Glu32.lib"
CompilerEndIf
;{-
gluBuild2DMipmaps(a.l,b.l,c.l,d.l,e.l,f.l,g.l)
gluErrorString(a.l)
gluPerspective(a.d,b.d,c.d,d.d)
gluLookAt(CameraX.f, CameraY.f, CameraZ.f, LookAtX.f, LookAtY.f, LookAtZ.f, UpX.f, UpY.f, UpZ.f)
;}
EndImport
;}


Structure POINTZ
  x.f
  y.f
  z.f
  w.f
EndStructure

Global NewList StarField.POINTZ()

For i = 1 To 500
  AddElement(StarField())
  StarField()\x = Random(200) - 100
  StarField()\y = Random(200) - 100
  StarField()\z = Random(200) - 100
  StarField()\w = 0
Next

Global Speed.f = 0

Global Position.POINTZ
Position\x = 0
Position\y = 0
Position\z = 0

Procedure InitGL()
  glClearColor(0.0, 0.0, 0.0, 1.0)
  
  glEnable(#GL_DEPTH_TEST)
  glDepthMask(#GL_TRUE);
  glDepthFunc(#GL_LEQUAL)
  glEnable(#GL_CULL_FACE)  
EndProcedure

Procedure ResizeScene(width, height)
  If height < 1 : height = 1 : EndIf
  If width < 1 : width = 1 : EndIf
  
  glViewport(0, 0, width, height)
  glMatrixMode(#GL_PROJECTION)
  glLoadIdentity()
  
  gluPerspective(45, width / height, 1,  1000)
  
  glMatrixMode(#GL_MODELVIEW)
  glLoadIdentity()
EndProcedure

Procedure DrawScene()
  glClear(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  glLoadIdentity()
  
  With Position
    \z - Speed
    glTranslatef(-\x, -\y, -\z)
  EndWith
  
  Protected.f dx, dy, dz
  ForEach StarField()
    dx = Position\x - StarField()\x
    dy = Position\y - StarField()\y
    dz = Position\z - StarField()\z
    
    StarField()\w = 1 - (Sqr(dx * dx + dy * dy + dz * dz) / 150)

    If StarField()\w < 0.0
      
      StarField()\x = Position\x + Random(200) - 100
      StarField()\y = Position\y + Random(200) - 100
      If Speed > 0
        StarField()\z = Position\z - 100
      Else
        StarField()\z = Position\z + 100
      EndIf
      StarField()\w = 0
      
    EndIf

Next
  
  glBegin(#GL_POINTS)
  ForEach StarField()
    glColor4f(StarField()\w,StarField()\w,StarField()\w,1)
    glVertex3f( StarField()\x, StarField()\y, StarField()\z )
  Next
  glEnd()

EndProcedure

InitSprite() : InitKeyboard()
OpenWindow (0, 100, 100, 512, 512, "OpenGL Window")
OpenWindowedScreen(WindowID(0), 0, 0, 512, 512, 0, 0, 0, #PB_Screen_WaitSynchronization)

InitGL()

Repeat
  TimerStart = ElapsedMilliseconds()
  
  Select WindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
  
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Escape)
    Break
  EndIf
  
  If KeyboardPushed(#PB_Key_Q)
    Speed + 0.1
    
    If Speed > 3
      Speed = 3
    EndIf
    
  EndIf
  
  If KeyboardPushed(#PB_Key_A)
    Speed - 0.1
    
    If Speed < -3
      Speed = -3
    EndIf
  EndIf
  
  DrawScene()
  
  FlipBuffers()
  ResizeScene(WindowWidth(0), WindowHeight(0))
  
  Pausing = Int((1000.0 / 30.0) - (ElapsedMilliseconds() - TimerStart))
  If Pausing < 1 : Pausing = 1 : EndIf
  Delay( Pausing )
  
  
ForEver
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Opengl 3d movement [solved]

Post by Foz »

Problem solved. First post updated accordingly
Post Reply