Page 1 of 1

OpenGL: Moving through a world

Posted: Sat Jan 24, 2004 3:53 pm
by DarkDragon
Hello,

I don't understand the cos-, sin- and tanmathematics. So I want to walk through a world made in opengl. In opengl you can only rotate around the zeropoint of the world, if you can't make such a calculating-function. Can somebody help me?

[EDIT] I have tryed it, but it doesn't work correctly: http://nopaste.php.cd/7058

Re: OpenGL: Moving through a world

Posted: Sat Jan 24, 2004 7:04 pm
by traumatic
IMHO really good tutorial here:
http://nehe.gamedev.net/data/articles/a ... article=08

Nice explainations, pictures and all the formulas you need.


You'll also find some nicely commented codes (c++) on this site:
http://www.ultimategameprogramming.com/ ... nGL&page=5
but I think you don't even have to know c in order to understand this.

Re: OpenGL: Moving through a world

Posted: Sun Jan 25, 2004 12:09 am
by Blade
DarkDragon wrote:Hello,

I don't understand the cos-, sin- and tanmathematics.
Walking in a OpenGL world needs more math than some sin-cos calculations.
I wasn't very good at math lessons (many-many years ago) but I've found myself studing a lot when I decided to make my GL world-walking (using C) some years ago...

As suggested, the Ne-He site is a very good start.
Good luck

Thx

Posted: Sun Jan 25, 2004 8:51 am
by DarkDragon
Thanks :) . I will try to learn it.

Posted: Wed Jan 28, 2004 7:52 am
by DarkDragon
Hello,

I have found the answer. Look here:

Code: Select all

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_TRIANGLES = $0004
#GL_FOG = $0B60
#GL_MODELVIEW                      = $1700
#GL_PROJECTION                     = $1701



#WindowWidth = 500
#WindowHeight = 400
#WindowFlags = #PB_Window_TitleBar | #PB_Window_MaximizeGadget | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered

Global floatX.f
Global floatY.f
Global floatZ.f

Global CameraX.f, CameraY.f, CameraZ.f, CamrotX.f, CamrotY.f, CamrotZ.f, CameraSpeed.f, CameraZoom.f, hDC

CamSpeed.f = 0.025
CameraZoom = 0.5

If OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, #WindowFlags, "OpenGLCamera")

  piover180.f = (ATan(1)*4)/180
  
  pfd.PIXELFORMATDESCRIPTOR   ;OpenGL starten
  hDC = GetDC_(hWnd)
  pfd\nSize        = SizeOf(PIXELFORMATDESCRIPTOR)
  pfd\nVersion     = 1
  pfd\dwFlags      = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
  pfd\iLayerType   = #PFD_MAIN_PLANE
  pfd\iPixelType   = #PFD_TYPE_RGBA
  pfd\cColorBits   = 24
  pfd\cDepthBits   = 32
  
  pixformat = ChoosePixelFormat_(hDC, pfd)
  SetPixelFormat_(hDC, pixformat, pfd)
  hrc = wglCreateContext_(hDC)
  wglMakeCurrent_(hDC, hrc)
  
  pixformat = ChoosePixelFormat_(hDC, pfd)
  SetPixelFormat_(hDC, pixformat, pfd)
  hrc = wglCreateContext_(hDC)
  wglMakeCurrent_(hDC,hrc)
  
  SwapBuffers_(hDC)

  floatX = 0.5
  floatY = 0.25
  floatZ = -0.5

Repeat
  glTranslatef_(0, 0, -7.0)
  glMatrixMode_(#GL_MODELVIEW)
  If GetAsyncKeyState_(#VK_UP) <> 0
    CameraZ = CameraZ - Sin((CamrotY-90)*piover180)*CamSpeed
    CameraX = CameraX - Cos((CamrotY+90)*piover180)*CamSpeed
  ElseIf GetAsyncKeyState_(#VK_DOWN) <> 0
    CameraZ = CameraZ + Sin((CamrotY-90)*piover180)*CamSpeed
    CameraX = CameraX + Cos((CamrotY+90)*piover180)*CamSpeed
  EndIf
  If GetAsyncKeyState_(#VK_RIGHT) <> 0
    CamrotY - (CamSpeed*5)
  ElseIf GetAsyncKeyState_(#VK_LEFT) <> 0
    CamrotY + (CamSpeed*5)
  EndIf
  glMatrixMode_(#GL_PROJECTION) ;Höhere Sichtweite, Kamera und Zoom
  glLoadIdentity_()
  glOrtho__(-2.0+CameraZoom, 2.0-CameraZoom, -2.0+CameraZoom, 2.0-CameraZoom, 0.0, 50.0)
  glRotatef_(-CamrotY, 0.0, 1.0, 0.0)
  glTranslatef_(0, 0, -10.0)
  glMatrixMode_(#GL_MODELVIEW)
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  glLoadIdentity_()
  glRotatef_(45.0, 0, 1.0, 0.0)
  glTranslatef_(-CameraX, -CameraY, -CameraZ)
  glBegin_(#GL_TRIANGLES)
    glNormal3f_(0, 0, 1.0)
    glColor4f_(0.0, 0.0, 1.0, 1.0)
    glVertex3f_(floatX, floatY, floatZ)
    glColor4f_(0.0, 1.0, 0.0, 1.0)
    glVertex3f_(-floatX, floatY, floatZ)
    glColor4f_(1.0, 1.0, 0.0, 1.0)
    glVertex3f_(-floatX, -floatY, floatZ)
  glEnd_()
  glBegin_(#GL_TRIANGLES)
    glNormal3f_(0, 0, 1.0)
    glColor4f_(0.0, 0.0, 1.0, 1.0)
    glVertex3f_(floatX, floatY, floatZ+0.25)
    glColor4f_(0.0, 1.0, 0.0, 1.0)
    glVertex3f_(-floatX, floatY, floatZ+0.25)
    glColor4f_(1.0, 1.0, 0.0, 1.0)
    glVertex3f_(-floatX, -floatY, floatZ+0.25)
  glEnd_()
  SwapBuffers_(hDC)
  Event = WindowEvent()
  Delay(1) ;For WinME users
Until Event = #PB_Event_CloseWindow
End
EndIf
Uhm, here it looks a little bit crazy, but in my engine it works. And you can't show glu objects in this code. But maybe it will help other people with their walkthrough ;) .

Posted: Wed Jan 28, 2004 12:57 pm
by Polo
it seems there's a problem, in the glortho_ function...

Posted: Wed Jan 28, 2004 3:19 pm
by legider_pb
gluLookAtf_() is a MUCH better command for handling camera movement for your needs.

Posted: Wed Jan 28, 2004 6:54 pm
by DarkDragon
glOrtho__ => glWrapper library

gluLookAtf shows me a black screen, like all gluLookAt functions.
Can you give me the standart arguments for gluLookAtf in PureBasic?

Posted: Wed Jan 28, 2004 7:37 pm
by traumatic
DarkDragon wrote:Can you give me the standart arguments for gluLookAtf in PureBasic?
does this help?

Code: Select all

gluLookAt__(eyex, eyey, eyez,           <- where you are
            centerx, centery, centerz   <- where you look to
            upx, upy, upz)              <- up-vector
All parameters have to be doubles (well, floats...)

Posted: Thu Jan 29, 2004 10:33 am
by benny
@DarkDragon:

On my system your code just opens a blank (grey) normal window. I cant see any triangle and stuff :?:
Anyone any ideas ?

[WinXP / GeForce2 MX]

Posted: Thu Jan 29, 2004 5:11 pm
by DarkDragon
Uhm, I know the parameters, but I want the standart settings.
On my system your code just opens a blank (grey) normal window. I cant see any triangle and stuff
Anyone any ideas ?

[WinXP / GeForce2 MX]
Ohh, I have the same problem, if I use the glu objects, but it should work!

Posted: Thu Jan 29, 2004 7:25 pm
by traumatic
DarkDragon wrote:Uhm, I know the parameters, but I want the standart settings.
what do you mean by "standard settings" ?
that totally depends on the purpose.

EDIT:
try gluLookAt__(0.0,0.0,0.0, 0.0,0.0,-1.0, 0.0,1.0,0.0)
instead of glOrtho__() and remark the glTranslatef_() s.
Now you should see something...

Posted: Thu Jan 29, 2004 7:29 pm
by traumatic
DarkDragon wrote: Ohh, I have the same problem, if I use the glu objects, but it should work!
your code CAN'T work because you're trying to get the dc of nothing:

Code: Select all

hDC = GetDC_(hWnd)
you'll have to change

Code: Select all

[...]
If OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, #WindowFlags, "OpenGLCamera")
[...]
to

Code: Select all

[...]
hWnd = OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, #WindowFlags, "OpenGLCamera") 
If hWnd
[...]
in order to make it work

Posted: Fri Jan 30, 2004 6:35 am
by DarkDragon
thanks, it works!