Sign in to follow this  

Rendering more than 1 moving objects in a maze

This topic is 2062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey people,

I have a maze and some agents moving through it using A star search. I'm using GLUT to do the rendering. After completing the path the agent returns to its original position and the cycle continues. The problem is in the render loop:
if the number of agents = 2, at first the 2nd agent gets rendered, then it kind of vanishes so that only 1 agent is shown. This agent completes the path and then when it restarts from its original position, agent 2 shows up and then both of them move. Even when I increase the number of agents to more than 2, initially all of them show up and then not more than 2 agents are shown at a time. Here's my code:

This is the main render loop:

static void render(void)
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// gluLookAt(-20.0f,5.0f,0.0f,-20.0+cos(270.0),5.0f,0.0+sin(270.0f),0.0,1.0,0.0);

glPushMatrix();
glLoadIdentity();
//glTranslatef(-27,-10,-85); //setting the appropriate camera look at (do it in a separate function)
//glRotatef(-90.0,1,0,0);
renderScene(-27,-10,-85);
renderAgent();
glPopMatrix();

// glDrawPixels(DIM,DIM,GL_RGBA,GL_UNSIGNED_BYTE,0);
glutSwapBuffers(); //swaps the front and back buffers
}

Render scene is:
void renderScene(float x, float y, float z)
{

glPushMatrix();
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(x,y,z);
glRotatef(-90.0, 1, 0, 0);
pMaze->displayMaze();
glPopMatrix();
}

My update function: I m using glTimer:

void updateScreen(int value)
{
moveAgent();
glutPostRedisplay();
glutTimerFunc(500,updateScreen,0);
}

and this is the move agent function:
void moveAgent()
{
for(int i = 0; i < numRows; i++)
{
//reset the current position of the agent and then move it
int a = pAgents[i].Position.x;
int b = pAgents[i].Position.y;
pMaze->m_Maze[b][a].setCellValue(UNOCCUPIED);

pAgents[i].positionIndex++;

if(pAgents[i].positionIndex >= pAgents[i].PathInMaze.size())
pAgents[i].positionIndex = 0;

pAgents[i].Position.x = pAgents[i].PathInMaze.at(pAgents[i].positionIndex).getCellX();
pAgents[i].Position.y = pAgents[i].PathInMaze.at(pAgents[i].positionIndex).getCellY();

a = pAgents[i].Position.x;
b = pAgents[i].Position.y;
pMaze->m_Maze[b][a].setCellValue(OCCUPIED);

}
}

Here is the renderAgent function:
void renderAgent()
{

for(int i = 0; i < numRows; i++)
{
float x1,z1;

x1 = -3.75 + ((float)pAgents[i].Position.x * 2.50f);
z1 = -9.0 + 1.25 + 2.50f + ((float)pAgents[i].Position.y * 2.50f);

glPushMatrix();

glColor3f(1.0f,0.0f,1.0f);
glTranslatef(-27,-10,-85);
glRotatef(-90.0, 1,0,0);
// glScalef(0.5,0.5,0.5);

glBegin(GL_QUADS);
glVertex3f((x1 + 0.25f) , 0, (z1 - 1.25f));
glVertex3f((x1 - 0.25f) , 0 , (z1 - 1.25f));
glVertex3f((x1 - 0.25f) , 0 , (z1 + 1.25f));
glVertex3f((x1 + 0.25f) , 0 , (z1 + 1.25f));
glEnd();
// Bottom


glBegin(GL_QUADS);
glVertex3f((x1+0.25) ,-0, (z1+1.25)); // Top Right Of The Quad (Bottom)
glVertex3f((x1-0.25) ,-0, (z1+1.25)); // Top Left Of The Quad (Bottom)
glVertex3f((x1-0.25) ,-0, (z1-1.25)); // Bottom Left Of The Quad (Bottom)
glVertex3f((x1+0.25) ,-0, (z1-1.25)); // Bottom Right Of The Quad (Bottom)
glEnd();
// Front

glBegin(GL_QUADS);
glVertex3f((x1+0.25), 0, (z1+1.25)); // Top Right Of The Quad (Front)
glVertex3f((x1-0.25), 0, (z1+1.25)); // Top Left Of The Quad (Front)
glVertex3f((x1-0.25),-0, (z1+1.25)); // Bottom Left Of The Quad (Front)
glVertex3f((x1+0.25),-0, (z1+1.25)); // Bottom Right Of The Quad (Front)
glEnd();
// Back

glBegin(GL_QUADS);
glVertex3f((x1-0.25),0,(z1-1.25)); // Top Right Of The Quad (Back)
glVertex3f((x1+0.25),0,(z1-1.25)); // Top Left Of The Quad (Back)
glVertex3f((x1+0.25),-0,(z1-1.25)); // Bottom Left Of The Quad (Back)
glVertex3f((x1-0.25),-0,(z1-1.25)); // Bottom Right Of The Quad (Back)
glEnd();
// left of cube

glBegin(GL_QUADS);
glVertex3f((x1-0.25), 0,(z1+1.25)); // Top Right Of The Quad (Left)
glVertex3f((x1-0.25), 0,(z1-1.25)); // Top Left Of The Quad (Left)
glVertex3f((x1-0.25),-0,(z1-1.25)); // Bottom Left Of The Quad (Left)
glVertex3f((x1-0.25),-0,(z1+1.25)); // Bottom Right Of The Quad (Left)
glEnd();
// Right of cube

glBegin(GL_QUADS);
glVertex3f((x1+0.25), 0,(z1-1.25)); // Top Right Of The Quad (Right)
glVertex3f((x1+0.25), 0,(z1+1.25)); // Top Left Of The Quad (Right)
glVertex3f((x1+0.25),-0,(z1+1.25)); // Bottom Left Of The Quad (Right)
glVertex3f((x1+0.25),-0,(z1-1.25)); // Bottom Right Of The Quad (Right)
glEnd();
glPopMatrix();

}
}

So I m not sure why they get rendered only 2 at a time. If anyone can point out what is going wrong, I would be appreciate it

Share this post


Link to post
Share on other sites
They are modifying the model view matrix. I wanted to get the top view of the maze I had created. That's why I had to use push/pop translate and rotate initially in the renderScene function. And then when I wanted to position the agents which are pink colored cubes in the maze, I had to again apply push/pop translate and rotate so that they get positioned in the right place. But yes here is the initialization code.

void initGL(int *argc, char* argv[])
{
glutInit(argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

int screenWidth = getScreenWidth();
if(screenWidth == 0)
screenWidth = DIM; //default width

int screenHeight = getScreenHeight();
if(screenHeight == 0)
screenHeight = DIM;

glutInitWindowSize(screenWidth,screenHeight);
glutInitWindowPosition(20,20);
glutCreateWindow("CUDA AI EVACUATION");
glutDisplayFunc(render);
// glutIdleFunc(moveAgent);
glutTimerFunc(500,updateScreen,0);
glutKeyboardFunc(keyHandler);

glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f,1.0,0.1f,1000.0f); //might have to change it to ortho

glMatrixMode(GL_MODELVIEW);

glewInit();
if(! glewIsSupported("GL_VERSION_2_0 "))
{
fprintf(stderr,"ERROR: Support for necessary OpenGL extensions missing.");
fflush(stderr);
exit(-3);
}

//also load the texture
wallTexture = LoadWallTexture("wall.bmp");
}

Share this post


Link to post
Share on other sites

This topic is 2062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this