Jump to content
  • Advertisement
Sign in to follow this  
brandonman

Terrain for fps

This topic is 4111 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

I recently started on an fps and have a camera from the apron camera tutorial 3. I got that working and I decided to add some terrain in. I decided to start with a single 1.0f wide strip of terrain and work up from that. I wrote a function for it and call it when drawing, and no terrain shows. Here's the code, at least, the function and the DrawglScene. function:
void Drawterrainsquare()
{
     for(float squares = -500; squares <= 500; squares+=1)
     {
        float ay = rand()%2;
        float by = rand()%2;
        float cy = rand()%2;
        float dy = rand()%2;
        glLoadIdentity();
        glBegin(GL_QUADS);
        glColor3f(0.1f,1.0f,0.2f);
        glVertex3f(0.0f, ay, 10.0f);
        glVertex3f(10.0f, by, 10.0f);
        glVertex3f(10.0f, cy, 0.0f);
        glVertex3f(0.0f, dy, 0.0f);
        glEnd();
        ay = by;
        dy = cy;
        by = rand()%2;
        cy = rand()%2;
        }
        }

and the DrawglScene:
int DrawGLScene(GLvoid)							
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(objCamera.mPos.x,  objCamera.mPos.y,  objCamera.mPos.z,	
			  objCamera.mView.x, objCamera.mView.y, objCamera.mView.z,	
			  objCamera.mUp.x,   objCamera.mUp.y,   objCamera.mUp.z);	
    Draw_Grid();
    Drawterrainsquare();
    glColor3f(1.0f,0.5f,0.0f);	
    glBegin(GL_QUADS);
    glVertex3f(1.0f,1.0f,1.0f);
    glVertex3f(-1.0f,1.0f,-1.0f);
    glVertex3f(-1.0f,1.0f,1.0f);
    glVertex3f(1.0f,1.0f,1.0f);
    glEnd();

	return TRUE;							
}

[Edited by - brandonman on June 23, 2007 12:55:03 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hi,

you never actually used 'squares' inside your for loop in the terrain drawing routine. This way it will only draw many quads ontop of eachother.

On a side note: I strongely advise you to use more variables. Having '500' for instance hardcoded is a very bad idea.

Good luck on the tutorials.


-CProgrammer

Share this post


Link to post
Share on other sites
I got a strip to show up but it was constantly changing the y so I tried writing to a 2d array and now I get a Stick People FPS.exe has encountered a problem and needs to close. We are sorry for the incnvenience. blah blah blah, and than it has the send error report or not. The DrawglScene is the same, so here's the new function for the terrain:

void Drawterrainsquare()
{
if(locationinarrayy < halfamount)
{
if(pass == 1)
{

for(float squares = -halfamount; squares <= halfamount; squares+=1)
{
glBegin(GL_QUADS);
glColor3f(0.1f,1.0f,0.2f);
glVertex3f(squares, ay, squares+1);
drawn[locationinarrayx][locationinarrayy] = ay;
locationinarrayx++;
glVertex3f(squares+1, by, squares+1);
drawn[locationinarrayx][locationinarrayy] = by;
glVertex3f(squares+1, cy, squares);
drawn[locationinarrayx][locationinarrayy] = cy;
glVertex3f(squares, dy, squares);
drawn[locationinarrayx][locationinarrayy] = dy;
glEnd();
ay = by;
dy = cy;
by = rand()%6;
cy = rand()%6;
if(squares > halfamount)
{
locationinarrayy++;
squares = -halfamount;
}
}
}
}
if(pass > 1)
{
int squares2 = -halfamount;
glBegin(GL_QUADS);
glColor3f(0.1f,1.0f,0.2f);
glVertex3f(squares2,drawn[locationinarrayx][locationinarrayy],squares2+1);
locationinarrayx++;
glVertex3f(squares2+1,drawn[locationinarrayx][locationinarrayy],squares2+1);
locationinarrayx++;
glVertex3f(squares2+1,drawn[locationinarrayx][locationinarrayy],squares2);
glVertex3f(squares2,drawn[locationinarrayx][locationinarrayy,squares2],squares2);
squares2++;

}
}


thanks for the help.

Share this post


Link to post
Share on other sites
a little advice:
- define variable side - number of tiles on both sides of terrain mesh
- generate heightmap - height at each point of terrain; array of side*side floats
- make a for loop, which will render all quads based on your data

// define side (it should be 'unsigned int', but i will use 'int', coz it's shorter)
int side;

// you have heightmap of side*side (dont forget to allocate and initialize it)
float *hmap;


// and then in rendering function, you will have this:
for (int z=0; z < side-1; z++) // render all rows
{
for (int x=0; x < side-1; x++) // in each row, render all quads
{
glBegin(GL_QUADS);
// now render quad at position [x, z] using height(y) from hmap
glVertex3f(x , hmap[(x )+(z )*side], z );
glVertex3f(x+1, hmap[(x+1)+(z )*side], z );
glVertex3f(x+1, hmap[(x+1)+(z+1)*side], z+1);
glVertex3f(x , hmap[(x )+(z+1)*side], z+1);
glEnd();
}
}


it's just to give you a basic idea.. understand it, make it work, and then you can improve it..
also, try google..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!