October 22, 2024
Chicago 12, Melborne City, USA
C++

Raycaster is not mirroring textures on the y-axis


I am recreating 3Dsage’s raycast engine but I cannot seem to properly mirror the north and south wall textures along the y-axis. The arrow placed in the top left of the texture should continue in its direction but it seems to flip on walls facing the opposite direction?

here is my source code:

//STOPPPED VIDEO AT 7:40
//fixed rescaling error
//removed input lag and changed movement functions

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.1415926535
#define P2 PI/2
#define P3 3*PI/2
#define DR 0.0174533 //one degree in radians

typedef struct
{
        int w,a,d,s; //button state on off
}ButtonKeys; ButtonKeys Keys;

//TEXTURES
int All_Textures[]=               //all 32x32 textures
{
 //Checkerboard
 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,
 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,
 0,0,0,0,0,1,1,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,
 0,0,1,1,1,1,1,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,
 0,0,1,1,1,1,1,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,
 0,0,0,0,0,1,1,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,
 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,
 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,

 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,
 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 1,1,1,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E MAP
int mapX=8,mapY=8,mapS=64;
int map[]=
{
    1,1,1,1,1,1,1,1,
    1,0,0,0,1,0,0,1,
    1,0,1,0,1,0,0,1,
    1,0,0,0,1,0,0,1,
    1,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,
};

//MAP
void drawMap2D()
{
    int x,y,xo,yo;
    for(y=0;y<mapY;y++)
    {
        for(x=0;x<mapX;x++)
        {
            if(map[y*mapX+x]==1){glColor3f(1,1,1);}
            else{glColor3f(0,0,0);}

            xo=x*mapS; yo=y*mapS;
            glBegin(GL_QUADS);
            glVertex2i(xo +1,yo +1);
            glVertex2i(xo +1,yo+mapS -1);
            glVertex2i(xo+mapS -1,yo+mapS -1);
            glVertex2i(xo+mapS -1,yo +1);
            glEnd();
        }
    }
}

float dist (float ax,float ay, float bx, float by, float ang)
{
    return (sqrt((bx-ax)*(bx-ax)+(by-ay)*(by-ay)));
}

//PLAYER AND POSITION
float degToRad(float a) {return a*M_PI/180.0;}
float FixAng(float a) {if(a>359){a-=360;} if(a<0){a+=360;} return a;}

float px,py,pdx,pdy,pa;

void drawPlayer2D()
{
    glColor3f(1,1,0);
    glPointSize(8);
    glLineWidth(4);
    glBegin(GL_POINTS);
    glVertex2i(px,py);
    glEnd();

    glBegin(GL_LINES);
    glVertex2i(px,py);
    glVertex2i(px+pdx*5,py+pdy*5);
    glEnd();
}

//RAYCASTING SPELLS
void drawRays2D()
{
    int r,mx,my,mp,dof; float rx,ry,ra,xo,yo,disT;
    ra=pa-DR*30; if(ra<0){ra+=2*PI;} if(ra>2*PI){ra-=2*PI;}
    for(r=0;r<60;r++)
    {
        //horizontal line check <-->
        dof=0;
        float disH=1000000,hx=px,hy=py;
        float aTan=-1/tan(ra);
        //if looking up
        if(ra>PI){ry=(((int)py>>6)<<6)-0.0001; rx=(py-ry)*aTan+px; yo=-64; xo=-yo*aTan;}
        //if looking down
        if(ra<PI){ry=(((int)py>>6)<<6)+64; rx=(py-ry)*aTan+px; yo=64; xo=-yo*aTan;}
        //if looking left or right, cannot see horizontal lines
        if(ra==0 || ra==PI){rx=px; ry=py; dof=8;}
        while(dof<8)
        {
            mx=(int)(rx)>>6; my=(int)(ry)>>6; mp=my*mapX+mx;
            //stop if ray hits wall
            if(mp>0 && mp<mapX*mapY && map[mp]>0){hx=rx; hy=ry; disH=dist(px,py,hx,hy,ra); dof=8;}
            //else, if ray doesnt hit wall, see next line
            else{rx+=xo; ry+=yo; dof+=1;}
        }


        //vertical line check
        dof=0;
        float disV=1000000,vx=px,vy=py;
        float nTan=-tan(ra);
        //if looking left
        if(ra>P2 && ra<P3){rx=(((int)px>>6)<<6)-0.0001; ry=(px-rx)*nTan+py; xo=-64; yo=-xo*nTan;}
        //if looking right
        if(ra<P2 || ra>P3){rx=(((int)px>>6)<<6)+64; ry=(px-rx)*nTan+py; xo=64; yo=-xo*nTan;}
        //if looking up or down, cannot see vertical lines
        if(ra==0 || ra==PI){rx=px; ry=py; dof=8;}
        while(dof<8)
        {
            mx=(int)(rx)>>6; my=(int)(ry)>>6; mp=my*mapX+mx;
            //stop if ray hits wall
            if(mp>0 && mp<mapX*mapY && map[mp]>0){vx=rx; vy=ry; disV=dist(px,py,vx,vy,ra); dof=8;}
            //else, if ray doesnt hit wall, see next line
            else{rx+=xo; ry+=yo; dof+=1;}
        }

        //shading
        float shade=1;
        //vertical wall hit
        if(disV<disH) {shade=0.5; rx=vx; ry=vy; disT=disV; glColor3f(0.9,0,0);}
        //horizontal wall hit
        if(disH<disV) {rx=hx; ry=hy; disT=disH; glColor3f(0.7,0,0);}

        //draw rays
        glLineWidth(3);
        glBegin(GL_LINES);
        glVertex2i(px,py);
        glVertex2i(rx,ry);
        glEnd();

        //draw 3D walls
        //fix fisheye
        float ca=pa-ra; if(ca<0){ca+=2*PI;} if(ca>2*PI){ca-=2*PI;} disT=disT*cos(ca);
        //line height
        float lineH=(mapS*320)/disT;
        float ty_step=32.0/(float)lineH;
        float ty_off=0;
        if(lineH>320) {ty_off=(lineH-320)/2.0; lineH=320;}
        //offset line to center of view
        float lineOff=160-lineH/2;

        //draw walls and textures
        int y;
        float ty=ty_off*ty_step;
        float tx=(int)(rx/2.0)%32; if(ra>180){tx=31-tx;}
        for(y=0;y<lineH;y++)
        {
            float c=All_Textures[(int)(ty)*32+(int)(tx)]*shade;
            glColor3f(c,c,c);

            //draw vertical wall
            glPointSize(8);
            glBegin(GL_POINTS);
            glVertex2i(r*8+530,y+lineOff);
            glEnd();
            ty+=ty_step;
        }

        ra+=DR; if(ra<0){ra+=2*PI;} if(ra>2*PI){ra-=2*PI;}

    }
}

//DRAW WINDOW FUNCTION
void init()
{
    glClearColor(0.3,0.3,0.3,0);
    gluOrtho2D(0,1024,512,0);
    px=300; py=300; pdx=cos(pa)*5; pdy=sin(pa)*5;
}

    float frame1,frame2,fps;

//DISPLAY FUNCTION
void display()
{

    //movement
    //frames per second
    frame2=glutGet(GLUT_ELAPSED_TIME); fps=(frame2-frame1); frame1=glutGet(GLUT_ELAPSED_TIME);
    //movement keys left and right
    if(Keys.a==1){pa-=0.005*fps; if(pa<0){pa+=2*PI;} pdx=cos(pa)*5; pdy=sin(pa)*5;}
    if(Keys.d==1){pa+=0.005*fps; if(pa>2*PI){pa-=2*PI;} pdx=cos(pa)*5; pdy=sin(pa)*5;}

    //wall collision for x axis
    int xo=0; if(pdx<0){xo=-20;} else{xo=20;}
    //wall collision for y axis
    int yo=0; if(pdy<0){yo=-20;} else{yo=20;}

    int ipx=px/64.0, ipx_add_xo=(px+xo)/64.0, ipx_sub_xo=(px-xo)/64.0;
    int ipy=py/64.0, ipy_add_yo=(py+yo)/64.0, ipy_sub_yo=(py-yo)/64.0;

    //movement forward
    if(Keys.w==1)
    {
        //forward collision
        if(map[ipy*mapX + ipx_add_xo]==0){px+=pdx*0.03*fps;}
        if(map[ipy_add_yo*mapX + ipx]==0){py+=pdy*0.03*fps;}
    }

    //movement backward
    if(Keys.s==1)
    {
        //backward collision
        if(map[ipy*mapX + ipx_sub_xo]==0){px-=pdx*0.03*fps;}
        if(map[ipy_sub_yo*mapX + ipx]==0){py-=pdy*0.03*fps;}
    }
    glutPostRedisplay();

    //draw 2D map
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawMap2D();
    drawRays2D();
    drawPlayer2D();
    glutSwapBuffers();
}

//MOVEMENT FUNCTION
void ButtonDown(unsigned char key,int x,int y)                                  //keyboard button pressed down
{
    if(key=='a'){ Keys.a=1;}
    if(key=='d'){ Keys.d=1;}
    if(key=='w'){ Keys.w=1;}
    if(key=='s'){ Keys.s=1;}
    glutPostRedisplay();
}

void ButtonUp(unsigned char key,int x,int y)                                    //keyboard button pressed up
{
    if(key=='a'){ Keys.a=0;}
    if(key=='d'){ Keys.d=0;}
    if(key=='w'){ Keys.w=0;}
    if(key=='s'){ Keys.s=0;}
    glutPostRedisplay();
}

//fix rescale error
void resize(int w,int h)
{
    glutReshapeWindow(824,412);
}

//INITIALIZER
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(824,412);
    glutInitWindowPosition(100,100);
    glutCreateWindow("3DSageEngine");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(resize);
    glutKeyboardFunc(ButtonDown);
    glutKeyboardUpFunc(ButtonUp);
    glutMainLoop();

    return 0;
}

Images might not help but oh well:

1

2

The code compiles without errors but visually the arrow should continue its direction as though it could loop around the parameter and all point the same way.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video