Author Topic: Wolf3D - OpenGL cube engine.  (Read 8732 times)

unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Wolf3D - OpenGL cube engine.
« on: May 20, 2011, 06:17:22 pm »
This is the start of the OpenGL portion of what i will be demoing with GDK next month at uni.

It requires the SFML patch : http://dl.dropbox.com/u/8822351/SFML.rar and this set of files. http://dl.dropbox.com/u/8822351/Wolf3D_OpenGL.zip

Use the Wolf_SFML.bas program to design your level, click the build button and then run the WOLF3d_opengl_engine.bas file. Currently you can only move around via the use of a gamepad/joystick but i will add keyboard/mouse input asap.

I need some help in how to move around properly. I had another thread on a similar topic...http://www.qb64.net/forum/index.php?topic=3225.0

Thanks folks,

Unseen

yanom

  • Guest
Re: Wolf3D - OpenGL cube engine.
« Reply #1 on: May 20, 2011, 06:27:54 pm »
Wolf3D? Is this a remake of Wolfenstein 3D?

unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Re: Wolf3D - OpenGL cube engine.
« Reply #2 on: May 20, 2011, 06:29:20 pm »
It will be, but using OpenGL rather than raycasting.

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 5490
  • QB Forever
Re: Wolf3D - OpenGL cube engine.
« Reply #3 on: May 21, 2011, 03:06:33 am »
Thanks for sharing this. Luckily I've got a dual-stick game-pad (Logitech F310, though I mostly use it as a Dolphin Wii controller ;)) otherwise I wouldn't have been able to do much. Can't wait to try the next release with sprites etc.
Something old... Something new... Something borrowed... Something blue...

unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Re: Wolf3D - OpenGL cube engine.
« Reply #4 on: May 21, 2011, 07:37:02 am »
I admit only having Gamepad support may have been a silly mistake! Heres a version of the engine that has Keyboard input aswell. I added support for the keyboard if either window is selected but it seems the INKEY$ input does not work as smoothly as the SFML input for some reason.

Code: [Select]
'// Wolf 3D -
REM $INCLUDE:'GL_Consts.bas'
CHDIR "wolf3d\" '// Clippy Protection!
SCREEN _NEWIMAGE(800, 100, 32)
_SCREENMOVE 0, 600


GameFile$ = "Wolf3DLevel.lvl"


'// Game variables
DIM SHARED Level(1 TO 50, 1 TO 50) AS INTEGER, Sprites(1 TO 50) AS XTile, PowerUps(1 TO 50) AS XTile
'REDIM SHARED LoadTexture_Buffer(1) AS LONG
'REDIM SHARED LoadTexture_Buffer2(1) AS LONG
DIM SHARED Tiles(1 TO 15) AS LONG, Sprite(5) AS LONG

TYPE XTile
    SpriteNum AS INTEGER
    Col AS INTEGER
    Row AS INTEGER
    InitDir AS INTEGER
END TYPE

OPEN GameFile$ FOR INPUT AS #1
'// Load level and other variables from level file.
FOR i% = 1 TO 50
    LINE INPUT #1, DataLine$
    RowCnt% = 1
    FOR j% = 0 TO LEN(DataLine$)
        IF INSTR(j%, DataLine$, ",") > 0 THEN
            Level(i%, RowCnt%) = VAL(MID$(DataLine$, j%, INSTR(j%, DataLine$, ",") - j%))
            j% = INSTR(j%, DataLine$, ",")
            RowCnt% = RowCnt% + 1
        ELSE
            Level(i%, RowCnt%) = VAL(MID$(DataLine$, j%))
        END IF
    NEXT
NEXT

CLOSE #1



'//SFML init
SFML
SF_Screen_New 800, 600, "Wolf3D - OpenGL"
SF_Screen_Set_XY 0, 0
SF_Screen_Set_Color 0, 0, 0, 255
SF_Screen_CLS
SF_Screen_Display
SF_Screen_Set_Active
SF_Screen_Set_MAXFPS 60

'// GL init
glClearDepth 1
glClearColor 0, 0, 0, 0
glEnable GL_DEPTH_TEST
glEnable GL_TEXTURE_2D
glDepthMask GL_TRUE
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 60, 1, 1, 2000

'//Load image tiles
FOR i% = 1 TO 15
    TileName$ = LTRIM$(STR$(i%)) + ".jpg"
    Tiles(i%) = LoadTexture(TileName$)
NEXT


XRot% = 0
YRot% = 0
ZRot% = 0

CamX% = -260
CamY% = 40
CamZ% = -1110

ViewX% = 0
ViewY% = 0
ViewZ% = 0

DO
    _LIMIT 120

    SF_Screen_Set_Active
    '// Dont allow the screen to be resized
    IF SF_Screen_Get_Width <> 800 THEN SF_Screen_Set_Width 800
    IF SF_Screen_Get_Height <> 600 THEN SF_Screen_Set_Height 600

    glMatrixMode GL_MODELVIEW
    glLoadIdentity

    glTranslatef CamX%, CamY%, CamZ%

    glRotatef XRot%, 1, 0, 0
    glRotatef YRot%, 0, 1, 0
    glRotatef ZRot%, 0, 0, 1

    DrawTiles

    '// Gamepad
    JoyX% = SF_GamePad_Axis_Get_X(0)
    JoyY% = SF_GamePad_Axis_Get_Y(0)
    JoyY2% = SF_GamePad_Axis_Get_R(0)
    JoyX2% = SF_GamePad_Axis_Get_U(0)

    CLS
    LOCATE 1, 1: PRINT "Joy X : "; JoyX%; "Joy Y : "; JoyY%; "Joy X2 : "; JoyX2%; "Joy Y2 : "; JoyY2%
    PRINT "Cam X : "; CamX%; "Cam Y : "; CamY%; "Cam Z : "; CamZ%
    PRINT "X Rot : "; XRot%; "Y Rot : "; YRot%; "Z Rot : "; ZRot%

    IF JoyX% > 10 THEN
        CamX% = CamX% - 2
    ELSEIF JoyX% < -10 THEN
        CamX% = CamX% + 2
    END IF

    IF JoyY% > 10 THEN
        CamZ% = CamZ% - 2
    ELSEIF JoyY% < -10 THEN
        CamZ% = CamZ% + 2
    END IF

    IF JoyX2% > 10 THEN
        YRot% = YRot% - 2
    ELSEIF JoyX2% < -10 THEN
        YRot% = YRot% + 2
    END IF

    '// Keyboard

    IF SF_Key_A THEN
        YRot% = YRot% - 2
    ELSEIF SF_Key_B THEN
        YRot% = YRot% + 2
    END IF

    IF SF_Key_Up THEN
        CamZ% = CamZ% + 2
    ELSEIF SF_Key_Down THEN
        CamZ% = CamZ% - 2
    END IF

    IF SF_Key_Left THEN
        CamX% = CamX% + 2
    ELSEIF SF_Key_Right THEN
        CamX% = CamX% - 2
    END IF


    kb$ = UCASE$(INKEY$)

    SELECT CASE kb$
        CASE CHR$(0) + CHR$(75)
            CamX% = CamX% + 2
        CASE CHR$(0) + CHR$(72)
            CamZ% = CamZ% + 2
        CASE CHR$(0) + CHR$(77)
            CamX% = CamX% - 2
        CASE CHR$(0) + CHR$(80)
            CamZ% = CamZ% - 2
        CASE "A"
            YRot% = YRot% - 2
        CASE "B"
            YRot% = YRot% + 2
    END SELECT

    SF_Screen_Display
    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT

LOOP UNTIL SF_Screen_Exit
SYSTEM
REM $INCLUDE:'sfml.bm'

SUB DrawTiles
Col% = 1
Row% = 1
z% = -200
FOR i% = Col% TO 50
    x% = -500
    FOR j% = Row% TO 50
        IF Level(i%, j%) > 0 THEN
            SelectTexture Tiles(Level(i%, j%))
            DrawCube x%, 0, z%, 120
        END IF
        x% = x% + 120
    NEXT
    z% = z% + 120
NEXT
END SUB



'// Open GL Functions

SUB DrawCube (X%, Y%, Z%, Size%)
glBegin GL_QUADS

'// Back face
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z%

'// Front face
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z% + Size%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z% + Size%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z% + Size%

'//Bottom Face
glVertex3f X%, Y% - Size%, Z%
glVertex3f X% + Size%, Y% - Size%, Z%
glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glVertex3f X%, Y% - Size%, Z% + Size%

'//Top face
glVertex3f X%, Y%, Z%
glVertex3f X% + Size%, Y%, Z%
glVertex3f X% + Size%, Y%, Z% + Size%
glVertex3f X%, Y%, Z% + Size%

'//Left face
glTexCoord2f 1, 1: glVertex3f X%, Y%, Z%
glTexCoord2f 1, 0: glVertex3f X%, Y% - Size%, Z%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z% + Size%
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z% + Size%

'//Right face
glTexCoord2f 0, 1: glVertex3f X% + Size%, Y%, Z%
glTexCoord2f 0, 0: glVertex3f X% + Size%, Y% - Size%, Z%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z% + Size%

glEnd

END SUB

'// Made by Galleon
FUNCTION LoadTexture (FileName$)
i& = _LOADIMAGE(FileName$, 32)
IF i& = -1 THEN EXIT FUNCTION
Sx = _WIDTH(i&)
Sy = _HEIGHT(i&)
DIM LoadTexture_Buffer(Sx * Sy) AS LONG
DIM LoadTexture_Buffer2(Sx * Sy) AS LONG
OldSrc& = _SOURCE
_SOURCE i&
GET (0, 0)-(Sx - 1, Sy - 1), LoadTexture_Buffer(0)
_SOURCE OldSrc&
_FREEIMAGE i&
'flip & convert BGR->RGB
p1 = 0 'source
p2 = Sx * (Sy - 1) 'dest
FOR y = 0 TO Sy - 1
    FOR x = p1 TO p1 + Sx - 1
        c = LoadTexture_Buffer(x)
        LoadTexture_Buffer2(p2) = ((c \ 65536) AND 255) + (c AND &HFF00&) + ((c AND 255&) * 65536)
        p2 = p2 + 1
    NEXT
    p1 = p1 + Sx
    p2 = p2 - Sx - Sx
NEXT
DIM h AS _UNSIGNED LONG
glGenTextures 1, h
glBindTexture GL_TEXTURE_2D, h
gluBuild2DMipmaps GL_TEXTURE_2D, GL_RGBA, Sx, Sy, GL_RGBA, GL_UNSIGNED_BYTE, LoadTexture_Buffer2(1)
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR
LoadTexture = h
END FUNCTION

'// Made by Galleon
SUB SelectTexture (h AS LONG)
glBindTexture GL_TEXTURE_2D, h
END SUB



SUB PositionCamera (EyeX%, EyeY%, EyeZ%, Rotation%)
glRotatef 1, 0, 0, Rotation%
END SUB




Sprites! Thats a long way off! :D I really could use help in getting the movement correct. I need to use the gluLookAt command but have never got it to work properly to make a First Person camera. I have read over various tutorials on it including NeHe ones(really good), but am lost! :'( How di d you (Galleon) do it in your portal game? Can i use a similar method?


Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 5490
  • QB Forever
Re: Wolf3D - OpenGL cube engine.
« Reply #5 on: May 21, 2011, 07:48:32 am »
That Portal game uses 3D raytracing, and didn't have sprites. If I did add sprites (I was going to add a particle ball) it would have been done by higher-resolution scans within bounding boxes. But that's a totally different system.

Seems to me if you want to make a Wolf clone then you only need 2D sprites, which is as simple as rotating the image to face the user. Is it the rotation that you are having trouble with?
Something old... Something new... Something borrowed... Something blue...

unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Re: Wolf3D - OpenGL cube engine.
« Reply #6 on: May 21, 2011, 09:00:35 am »
Nope, player to rotation and movement. Making the camera a first person view is what i need to/need help with figuring out. I can not seem to rotate the camera on the spot. The world rotates around a point in 3d space (i dont quite get where) I want to make it rotate around the players position, i think this is where gluLookAt comes into play, but i dont quite get how to use it.



unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Re: Wolf3D - OpenGL cube engine.
« Reply #7 on: May 21, 2011, 12:43:09 pm »
This version allows me to rotate my view from the player positition, when i walk though movement is not translated using the direction i am facing. This moveing in the direction i am facing is what i need to figure out/be taught. We all know math is not my strong suit so i appreciate any help i can get.

Code: [Select]
'// Wolf 3D -
REM $INCLUDE:'GL_Consts.bas'
CHDIR "wolf3d\" '// Clippy Protection!
SCREEN _NEWIMAGE(800, 100, 32)
_SCREENMOVE 0, 600


GameFile$ = "Wolf3DLevel.lvl"


'// Game variables
DIM SHARED Level(1 TO 50, 1 TO 50) AS INTEGER, Sprites(1 TO 50) AS XTile, PowerUps(1 TO 50) AS XTile
'REDIM SHARED LoadTexture_Buffer(1) AS LONG
'REDIM SHARED LoadTexture_Buffer2(1) AS LONG
DIM SHARED Tiles(1 TO 15) AS LONG, Sprite(5) AS LONG

TYPE XTile
    SpriteNum AS INTEGER
    Col AS INTEGER
    Row AS INTEGER
    InitDir AS INTEGER
END TYPE

OPEN GameFile$ FOR INPUT AS #1
'// Load level and other variables from level file.
FOR i% = 1 TO 50
    LINE INPUT #1, DataLine$
    RowCnt% = 1
    FOR j% = 0 TO LEN(DataLine$)
        IF INSTR(j%, DataLine$, ",") > 0 THEN
            Level(i%, RowCnt%) = VAL(MID$(DataLine$, j%, INSTR(j%, DataLine$, ",") - j%))
            j% = INSTR(j%, DataLine$, ",")
            RowCnt% = RowCnt% + 1
        ELSE
            Level(i%, RowCnt%) = VAL(MID$(DataLine$, j%))
        END IF
    NEXT
NEXT

CLOSE #1



'//SFML init
SFML
SF_Screen_New 800, 600, "Wolf3D - OpenGL"
SF_Screen_Set_XY 0, 0
SF_Screen_Set_Color 0, 0, 0, 255
SF_Screen_CLS
SF_Screen_Display
SF_Screen_Set_Active
SF_Screen_Set_MAXFPS 60

'// GL init
glClearDepth 1
glClearColor 0, 0, 0, 0
glEnable GL_DEPTH_TEST
glEnable GL_TEXTURE_2D
glDepthMask GL_TRUE
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 60, 1, 1, 2000

'//Load image tiles
FOR i% = 1 TO 15
    TileName$ = LTRIM$(STR$(i%)) + ".jpg"
    Tiles(i%) = LoadTexture(TileName$)
NEXT


XRot% = 0
YRot% = 0
ZRot% = 0

CamX% = -260
CamY% = 40
CamZ% = -1110

ViewX% = 0
ViewY% = 0
ViewZ% = 0

DO
    _LIMIT 120

    SF_Screen_Set_Active
    '// Dont allow the screen to be resized
    IF SF_Screen_Get_Width <> 800 THEN SF_Screen_Set_Width 800
    IF SF_Screen_Get_Height <> 600 THEN SF_Screen_Set_Height 600

    glMatrixMode GL_MODELVIEW
    glLoadIdentity

    glRotatef XRot%, 1, 0, 0
    glRotatef YRot%, 0, 1, 0
    glRotatef ZRot%, 0, 0, 1
    glTranslatef CamX%, CamY%, CamZ%

    DrawTiles

    '// Gamepad
    JoyX% = SF_GamePad_Axis_Get_X(0)
    JoyY% = SF_GamePad_Axis_Get_Y(0)
    JoyY2% = SF_GamePad_Axis_Get_R(0)
    JoyX2% = SF_GamePad_Axis_Get_U(0)

    CLS
    LOCATE 1, 1: PRINT "Joy X : "; JoyX%; "Joy Y : "; JoyY%; "Joy X2 : "; JoyX2%; "Joy Y2 : "; JoyY2%
    PRINT "Cam X : "; CamX%; "Cam Y : "; CamY%; "Cam Z : "; CamZ%
    PRINT "X Rot : "; XRot%; "Y Rot : "; YRot%; "Z Rot : "; ZRot%

    IF JoyX% > 10 THEN
        CamX% = CamX% - 2
    ELSEIF JoyX% < -10 THEN
        CamX% = CamX% + 2
    END IF

    IF JoyY% > 10 THEN
        CamZ% = CamZ% - 2
    ELSEIF JoyY% < -10 THEN
        CamZ% = CamZ% + 2
    END IF

    IF JoyX2% > 10 THEN
        YRot% = YRot% - 2
    ELSEIF JoyX2% < -10 THEN
        YRot% = YRot% + 2
    END IF

    '// Keyboard

    IF SF_Key_A THEN
        YRot% = YRot% - 2
    ELSEIF SF_Key_B THEN
        YRot% = YRot% + 2
    END IF

    IF SF_Key_Up THEN
        CamZ% = CamZ% + 2
    ELSEIF SF_Key_Down THEN
        CamZ% = CamZ% - 2
    END IF

    IF SF_Key_Left THEN
        CamX% = CamX% + 2
    ELSEIF SF_Key_Right THEN
        CamX% = CamX% - 2
    END IF


    kb$ = UCASE$(INKEY$)

    SELECT CASE kb$
        CASE CHR$(0) + CHR$(75)
            CamX% = CamX% + 2
        CASE CHR$(0) + CHR$(72)
            CamZ% = CamZ% + 2
        CASE CHR$(0) + CHR$(77)
            CamX% = CamX% - 2
        CASE CHR$(0) + CHR$(80)
            CamZ% = CamZ% - 2
        CASE "A"
            YRot% = YRot% - 2
        CASE "B"
            YRot% = YRot% + 2
    END SELECT

    SF_Screen_Display
    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT

LOOP UNTIL SF_Screen_Exit
SYSTEM
REM $INCLUDE:'sfml.bm'

SUB DrawTiles
Col% = 1
Row% = 1
z% = -200
FOR i% = Col% TO 50
    x% = -500
    FOR j% = Row% TO 50
        IF Level(i%, j%) > 0 THEN
            SelectTexture Tiles(Level(i%, j%))
            DrawCube x%, 0, z%, 120
        END IF
        x% = x% + 120
    NEXT
    z% = z% + 120
NEXT
END SUB



'// Open GL Functions

SUB DrawCube (X%, Y%, Z%, Size%)
glBegin GL_QUADS

'// Back face
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z%

'// Front face
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z% + Size%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z% + Size%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z% + Size%

'//Bottom Face
glVertex3f X%, Y% - Size%, Z%
glVertex3f X% + Size%, Y% - Size%, Z%
glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glVertex3f X%, Y% - Size%, Z% + Size%

'//Top face
glVertex3f X%, Y%, Z%
glVertex3f X% + Size%, Y%, Z%
glVertex3f X% + Size%, Y%, Z% + Size%
glVertex3f X%, Y%, Z% + Size%

'//Left face
glTexCoord2f 1, 1: glVertex3f X%, Y%, Z%
glTexCoord2f 1, 0: glVertex3f X%, Y% - Size%, Z%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z% + Size%
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z% + Size%

'//Right face
glTexCoord2f 0, 1: glVertex3f X% + Size%, Y%, Z%
glTexCoord2f 0, 0: glVertex3f X% + Size%, Y% - Size%, Z%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z% + Size%

glEnd

END SUB

'// Made by Galleon
FUNCTION LoadTexture (FileName$)
i& = _LOADIMAGE(FileName$, 32)
IF i& = -1 THEN EXIT FUNCTION
Sx = _WIDTH(i&)
Sy = _HEIGHT(i&)
DIM LoadTexture_Buffer(Sx * Sy) AS LONG
DIM LoadTexture_Buffer2(Sx * Sy) AS LONG
OldSrc& = _SOURCE
_SOURCE i&
GET (0, 0)-(Sx - 1, Sy - 1), LoadTexture_Buffer(0)
_SOURCE OldSrc&
_FREEIMAGE i&
'flip & convert BGR->RGB
p1 = 0 'source
p2 = Sx * (Sy - 1) 'dest
FOR y = 0 TO Sy - 1
    FOR x = p1 TO p1 + Sx - 1
        c = LoadTexture_Buffer(x)
        LoadTexture_Buffer2(p2) = ((c \ 65536) AND 255) + (c AND &HFF00&) + ((c AND 255&) * 65536)
        p2 = p2 + 1
    NEXT
    p1 = p1 + Sx
    p2 = p2 - Sx - Sx
NEXT
DIM h AS _UNSIGNED LONG
glGenTextures 1, h
glBindTexture GL_TEXTURE_2D, h
gluBuild2DMipmaps GL_TEXTURE_2D, GL_RGBA, Sx, Sy, GL_RGBA, GL_UNSIGNED_BYTE, LoadTexture_Buffer2(1)
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR
LoadTexture = h
END FUNCTION

'// Made by Galleon
SUB SelectTexture (h AS LONG)
glBindTexture GL_TEXTURE_2D, h
END SUB



SUB PositionCamera (EyeX%, EyeY%, EyeZ%, Rotation%)
glRotatef 1, 0, 0, Rotation%
END SUB




DarthWho

  • Hero Member
  • *****
  • Posts: 4039
  • Timelord of the Sith
Re: Wolf3D - OpenGL cube engine.
« Reply #8 on: May 21, 2011, 01:05:18 pm »
so you have the angle on the horizontal plane and you can increment the position in both the x and y directions correct? you also have a speed of movement correct?
so what you need is a variation of this:

NewXPosition = OldXPosition + Velocity * SIN(angle)
NewYPosition = OldYPosition + Velocity * COS(angle)

FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

BTC: 1DGmy7rBZ15Y1nFJXkoE8BkvmMu6DxSMM4
LTC: LRNzAapRvQEuuEGwuLTG1f6nuHaf7tqkn7

unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Re: Wolf3D - OpenGL cube engine.
« Reply #9 on: May 21, 2011, 01:10:00 pm »
I can move in x and z planes. y is static, i expect it's something like you suggest but me math is weak. ???

DarthWho

  • Hero Member
  • *****
  • Posts: 4039
  • Timelord of the Sith
Re: Wolf3D - OpenGL cube engine.
« Reply #10 on: May 21, 2011, 01:50:21 pm »
sorry I do specialize in marine physics and for us x and y are the horizontal coordinates with z being the just switch y and z in this instance.
FastMath 1.1.0 released: http://dl.dropbox.com/u/12359848/fastmath.h

BTC: 1DGmy7rBZ15Y1nFJXkoE8BkvmMu6DxSMM4
LTC: LRNzAapRvQEuuEGwuLTG1f6nuHaf7tqkn7

SkyCharger001

  • Hero Member
  • *****
  • Posts: 2412
Re: Wolf3D - OpenGL cube engine.
« Reply #11 on: May 21, 2011, 02:01:35 pm »
what you need is a global- to camera-space translation routine.
this takes 'physical' coordinates and outputs coordinates relative to the camera.

phase 1: subtract camera-coordinates from actual coordinates of all objects.
phase 2: rotate all objects other than camera around center of world opposite to camera-angle.

unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Re: Wolf3D - OpenGL cube engine.
« Reply #12 on: May 21, 2011, 02:58:30 pm »
I nearly have it working using darths method. At certain rotations moving forwards is impossible, also some rotations cause movement to happen in a differant direction to the button being pushed.

CLick the SFML window to use the keyboard..

Arrow keys move and A and B rotate.
Code: [Select]
'// Wolf 3D -
REM $INCLUDE:'GL_Consts.bas'
CHDIR "wolf3d\" '// Clippy Protection!
SCREEN _NEWIMAGE(800, 100, 32)
_SCREENMOVE 0, 600


GameFile$ = "Wolf3DLevel.lvl"


'// Game variables
DIM SHARED Level(1 TO 50, 1 TO 50) AS INTEGER, Sprites(1 TO 50) AS XTile, PowerUps(1 TO 50) AS XTile
'REDIM SHARED LoadTexture_Buffer(1) AS LONG
'REDIM SHARED LoadTexture_Buffer2(1) AS LONG
DIM SHARED Tiles(1 TO 15) AS LONG, Sprite(5) AS LONG

TYPE XTile
    SpriteNum AS INTEGER
    Col AS INTEGER
    Row AS INTEGER
    InitDir AS INTEGER
END TYPE

OPEN GameFile$ FOR INPUT AS #1
'// Load level and other variables from level file.
FOR i% = 1 TO 50
    LINE INPUT #1, DataLine$
    RowCnt% = 1
    FOR j% = 0 TO LEN(DataLine$)
        IF INSTR(j%, DataLine$, ",") > 0 THEN
            Level(i%, RowCnt%) = VAL(MID$(DataLine$, j%, INSTR(j%, DataLine$, ",") - j%))
            j% = INSTR(j%, DataLine$, ",")
            RowCnt% = RowCnt% + 1
        ELSE
            Level(i%, RowCnt%) = VAL(MID$(DataLine$, j%))
        END IF
    NEXT
NEXT

CLOSE #1



'//SFML init
SFML
SF_Screen_New 800, 600, "Wolf3D - OpenGL"
SF_Screen_Set_XY 0, 0
SF_Screen_Set_Color 0, 0, 0, 255
SF_Screen_CLS
SF_Screen_Display
SF_Screen_Set_Active
SF_Screen_Set_MAXFPS 60

'// GL init
glClearDepth 1
glClearColor 0, 0, 0, 0
glEnable GL_DEPTH_TEST
glEnable GL_TEXTURE_2D
glDepthMask GL_TRUE
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 60, 1, 1, 2000

'//Load image tiles
FOR i% = 1 TO 15
    TileName$ = LTRIM$(STR$(i%)) + ".jpg"
    Tiles(i%) = LoadTexture(TileName$)
NEXT


XRot% = 0
YRot% = 0
ZRot% = 0

CamX% = -260
CamY% = 40
CamZ% = -1110

ViewX% = 0
ViewY% = 0
ViewZ% = 0

DO
    _LIMIT 120

    SF_Screen_Set_Active
    '// Dont allow the screen to be resized
    IF SF_Screen_Get_Width <> 800 THEN SF_Screen_Set_Width 800
    IF SF_Screen_Get_Height <> 600 THEN SF_Screen_Set_Height 600

    glMatrixMode GL_MODELVIEW
    glLoadIdentity

    glRotatef XRot%, 1, 0, 0
    glRotatef YRot%, 0, 1, 0
    glRotatef ZRot%, 0, 0, 1
    glTranslatef CamX%, CamY%, CamZ%

    DrawTiles

    '// Gamepad
    JoyX% = SF_GamePad_Axis_Get_X(0)
    JoyY% = SF_GamePad_Axis_Get_Y(0)
    JoyY2% = SF_GamePad_Axis_Get_R(0)
    JoyX2% = SF_GamePad_Axis_Get_U(0)

    CLS
    LOCATE 1, 1: PRINT "Joy X : "; JoyX%; "Joy Y : "; JoyY%; "Joy X2 : "; JoyX2%; "Joy Y2 : "; JoyY2%
    PRINT "Cam X : "; CamX%; "Cam Y : "; CamY%; "Cam Z : "; CamZ%
    PRINT "X Rot : "; XRot%; "Y Rot : "; YRot%; "Z Rot : "; ZRot%

    IF JoyX% > 10 THEN
        CamX% = CamX% - 2
    ELSEIF JoyX% < -10 THEN
        CamX% = CamX% + 2
    END IF

    IF JoyY% > 10 THEN
        CamZ% = CamZ% - (2 * COS(YRot% * ((4 * ATN(1)) / 180)))
    ELSEIF JoyY% < -10 THEN
        CamZ% = CamZ% + (2 * COS(YRot% * ((4 * ATN(1)) / 180)))
    END IF

    IF JoyX2% > 10 THEN
        IF YRot% > 0 THEN YRot% = YRot% - 2 ELSE YRot% = 360
    ELSEIF JoyX2% < -10 THEN
        IF YRot% < 360 THEN YRot% = YRot% + 2 ELSE YRot% = 0
    END IF

    '// Keyboard

    IF SF_Key_A THEN
        IF YRot% > 0 THEN YRot% = YRot% - 2 ELSE YRot% = 360
    ELSEIF SF_Key_B THEN
        IF YRot% < 360 THEN YRot% = YRot% + 2 ELSE YRot% = 0
    END IF

    IF SF_Key_Up THEN
        CamZ% = CamZ% + (2 * COS(YRot% * ((4 * ATN(1)) / 180)))
    ELSEIF SF_Key_Down THEN
        CamZ% = CamZ% - (2 * COS(YRot% * ((4 * ATN(1)) / 180)))
    END IF

    IF SF_Key_Left THEN
        CamX% = CamX% + 2
    ELSEIF SF_Key_Right THEN
        CamX% = CamX% - 2
    END IF

    SF_Screen_Display
    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT

LOOP UNTIL SF_Screen_Exit
SYSTEM
REM $INCLUDE:'sfml.bm'

SUB DrawTiles
Col% = 1
Row% = 1
z% = -200
FOR i% = Col% TO 50
    x% = -500
    FOR j% = Row% TO 50
        IF Level(i%, j%) > 0 THEN
            SelectTexture Tiles(Level(i%, j%))
            DrawCube x%, 0, z%, 120
        END IF
        x% = x% + 120
    NEXT
    z% = z% + 120
NEXT
END SUB



'// Open GL Functions

SUB DrawCube (X%, Y%, Z%, Size%)
glBegin GL_QUADS

'// Back face
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z%

'// Front face
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z% + Size%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z% + Size%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z% + Size%

'//Bottom Face
glVertex3f X%, Y% - Size%, Z%
glVertex3f X% + Size%, Y% - Size%, Z%
glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glVertex3f X%, Y% - Size%, Z% + Size%

'//Top face
glVertex3f X%, Y%, Z%
glVertex3f X% + Size%, Y%, Z%
glVertex3f X% + Size%, Y%, Z% + Size%
glVertex3f X%, Y%, Z% + Size%

'//Left face
glTexCoord2f 1, 1: glVertex3f X%, Y%, Z%
glTexCoord2f 1, 0: glVertex3f X%, Y% - Size%, Z%
glTexCoord2f 0, 0: glVertex3f X%, Y% - Size%, Z% + Size%
glTexCoord2f 0, 1: glVertex3f X%, Y%, Z% + Size%

'//Right face
glTexCoord2f 0, 1: glVertex3f X% + Size%, Y%, Z%
glTexCoord2f 0, 0: glVertex3f X% + Size%, Y% - Size%, Z%
glTexCoord2f 1, 0: glVertex3f X% + Size%, Y% - Size%, Z% + Size%
glTexCoord2f 1, 1: glVertex3f X% + Size%, Y%, Z% + Size%

glEnd

END SUB

'// Made by Galleon
FUNCTION LoadTexture (FileName$)
i& = _LOADIMAGE(FileName$, 32)
IF i& = -1 THEN EXIT FUNCTION
Sx = _WIDTH(i&)
Sy = _HEIGHT(i&)
DIM LoadTexture_Buffer(Sx * Sy) AS LONG
DIM LoadTexture_Buffer2(Sx * Sy) AS LONG
OldSrc& = _SOURCE
_SOURCE i&
GET (0, 0)-(Sx - 1, Sy - 1), LoadTexture_Buffer(0)
_SOURCE OldSrc&
_FREEIMAGE i&
'flip & convert BGR->RGB
p1 = 0 'source
p2 = Sx * (Sy - 1) 'dest
FOR y = 0 TO Sy - 1
    FOR x = p1 TO p1 + Sx - 1
        c = LoadTexture_Buffer(x)
        LoadTexture_Buffer2(p2) = ((c \ 65536) AND 255) + (c AND &HFF00&) + ((c AND 255&) * 65536)
        p2 = p2 + 1
    NEXT
    p1 = p1 + Sx
    p2 = p2 - Sx - Sx
NEXT
DIM h AS _UNSIGNED LONG
glGenTextures 1, h
glBindTexture GL_TEXTURE_2D, h
gluBuild2DMipmaps GL_TEXTURE_2D, GL_RGBA, Sx, Sy, GL_RGBA, GL_UNSIGNED_BYTE, LoadTexture_Buffer2(1)
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR
LoadTexture = h
END FUNCTION

'// Made by Galleon
SUB SelectTexture (h AS LONG)
glBindTexture GL_TEXTURE_2D, h
END SUB



SUB PositionCamera (EyeX%, EyeY%, EyeZ%, Rotation%)
glRotatef 1, 0, 0, Rotation%
END SUB




unseenmachine

  • Hero Member
  • *****
  • Posts: 3663
  • Make the Game not the ENGINE!!!
Re: Wolf3D - Movement solved!
« Reply #13 on: May 21, 2011, 05:37:06 pm »
I converted it from here...http://www.swiftless.com/tutorials/opengl/camera.html

Here's the code exceprt that makes it work...any way to make it faster? MATHBOY! ;)

Code: [Select]
radHelp# = (4 * ATN(1)) / 180

    IF JoyX2% < -15 THEN
        IF YRot% > 0 THEN YRot% = YRot% - 1 ELSE YRot% = 360
    ELSEIF JoyX2% > 15 THEN
        IF YRot% < 360 THEN YRot% = YRot% + 1 ELSE YRot% = 0
    END IF

    IF JoyY% > 15 THEN
        CamX% = CamX% + SIN(YRot% * radHelp#)
        CamZ% = CamZ% - COS(YRot% * radHelp#)
        CamY% = CamY% - SIN(XRot% * radHelp#)
    ELSEIF JoyY% < -15 THEN
        CamX% = CamX% - SIN(YRot% * radHelp#)
        CamZ% = CamZ% + COS(YRot% * radHelp#)
        CamY% = CamY% + SIN(XRot% * radHelp#)
    END IF

Thanks for all the help and suggestions though folks. I can crack on with more of this now.

John

stylin

  • Guest
Re: Wolf3D - OpenGL cube engine.
« Reply #14 on: May 21, 2011, 05:47:51 pm »
Congrats unseenmachine. Regarding speed,

1. Always use constants for compile-time values, like your radHelp# = (4 * ATN(1)) / 180 degrees-to-radians conversion variable. Constants are filled in with numeric literals when compiled, and avoid allocation/initialization/assignment/dereference of variables.
2. Always look for places to pre-calculate, such as YRot% * radHelp#.
3. Don't worry about optimizing this code at all, and instead focus on the rest of your engine. :)