Sign in to follow this  
joshyboy13

floor-of-game-help

Recommended Posts

hello all, I have been programming for 4 years in C++, but im a relative |\|00|3 to OGL. I am starting my first game project and a have a problem. I could do the floor with hundreds of glBegin(); / glEnd(); functions, but i want to do it the cool way. Does any one know how to make like a whole repeated floor using for loops? any help would be greatly appriciatted. sincerly, C++/OGL programmer

Share this post


Link to post
Share on other sites
Well, to start with, what sort of floor do you intend to have?

Firstly, is this in 2D or 3d, and if the former, from what perspective?

The same tile repeated over and over? One of a selection of tiles? Arbitrary (but flat) polygon shapes? Arbitrary (but flat) shapes, potentially curved? A heightmap? Something else entirely? (If the last, please specify.)

Next, will a lot of the floor be visible at once, or will the only small sections be visible at a time (such as in a series of small rooms connected by short passageways that change direction, thus limiting how far the player can see most of the time)?

Finally, good luck with your project! I hope that the result is what you hope for, and more. ^_^

Share this post


Link to post
Share on other sites
well the first level has the same floor over and over and over again. it is a flat terrain (for now). It is a 3-d game, using perspective projection. I have heard that you can use
for loops to repeat floor code. Do you have any knowlage on this?

P.S. i have tried something like this:


for(int b = -100; b < 100; b++)
{
for(int a = -100; a < 100; a++)
{
glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex3f(b,-0.3,-a);
glTexCoord2f(0,0); glVertex3f(b,-0.3,-a-5);
glTexCoord2f(1,0); glVertex3f(b+5,-0.3,-a-5);
glTexCoord2f(1,1); glVertex3f(b+5,-0.3,-a);
glEnd();
}
}








but it wont work right(the game runs really slow and the textures look weird)

[Edited by - joshyboy13 on April 5, 2007 10:12:02 PM]

Share this post


Link to post
Share on other sites
Well, first of all, I would recommend compiling the entire floor into a display list, to make it more efficient at render time.

However, more relevantly to your question, this should, if I recall correctly, be more or less correct (excluding actual assignment of declared variables):

//The size of the floor in the x and z dimensions, measured in numbers of tiles
int num_tiles_x;
int num_tiles_z;

//The size of a particular tile, presuming them to be square.
//Non-square rectangular tiles simply call for two such values and a little more
// care in the loops
float size_of_tile;

//The height at which the floor should be drawn
float floorheight;

//Bind your texture here, or somewhere earlier

//iterate over one axis, then the other
for(int i = 0; i < num_tiles_x; i++)
{
for(int j = 0; j < num_tiles_z; j++)
{
//I presume that you want your floor centred at 0,0, which is why
// I subtract tilesize*num_tiles_<insert axis here>/2.0 from the coordinates
// used.
float x1 = i*tilesize - tilesize*num_tiles_x/2.0;
float x2 = (i+1)*tilesize - tilesize*num_tiles_x/2.0;
float z1 = j*tilesize - tilesize*num_tiles_x/2.0;
float z2 = (j+1)*tilesize - tilesize*num_tiles_x/2.0;

glBegin(GL_QUADS);
//Create texture coordinates similarly to the method used for
// vertices below; I presume that your normal will
// be the same in all cases

//Note too that I have presumed a clockwise forward face -
// simply change the order of the calls if forward faces
// are expected to be counterclockwise
glVertex3f(x1, floorheight, z1);
glVertex3f(x2, floorheight, z1);
glVertex3f(x2, floorheight, z2);
glVertex3f(x1, floorheight, z2);
glEnd();
}
}



There may be some errors in the above illustration, but hopefully if so it still demonstrates the principle.

Of course, it would probably be more efficient to render that as a series of triangle- or quad strips, which would have the glBegin and glEnd calls outside of the inner loop.

If you have any questions, feel free to ask - I hope that this helps. ^_^

Share this post


Link to post
Share on other sites
Quote:
Original post by Thaumaturge
Of course, it would probably be more efficient to render that as a series of triangle- or quad strips, which would have the glBegin and glEnd calls outside of the inner loop.

While strips would indeed be faster, there's no reason that the glBegin/glEnd calls can't go outside of the loops. The only things that can't go inside of glBegin/glEnd calls are certain other OpenGL calls (the list of those GL calls that can go inside of a pair of glBegin/glEnds is listed here). The only other thing to keep in mind is that some types require a certain multiple of calls to glVertex*. (It's all explained more clearly in the link.)

Changing the order alone (not even bothering with display lists or strips ( -- yet -- )) is likely to speed things up a lot. Multiple unnecessary begin/end calls are actually most likely the cause of the slowness - state changes are costly.

OP: What, exactly, is wrong with the textures? A fuller explanation (or even a screenshot!) would go a long way in helping us figure out what's wrong with it. Best of luck!
-jouley

Share this post


Link to post
Share on other sites
Jouley, you are, of course, correct about the placement of the begin and end calls - that was a silly mistake on my part. ^^;

However, as to your code, joshyboy13, the first thing that occurred to me was that you seem to be creating tiles of size 5, but then only incrementing your next tile's position by one, which should, it seems to me, create tiles that overlap. In your code I would suggest either incrementing your loop variables by 5 or multiplying by five when constructing your tiles, as I have using my variable "tilesize" above.

As to the speed, as I said, I would recommend placing your code into a display list and calling that in your render code, and generating the tiles as quad strips.

To give a rough example of display list usage:

//This variable holds the identifier of our display list
int list;

//this method is called once, when you set everything up,
// once you have your OpenGL context set up.
void some_initialisation_method()
{
//Generate an unused display list ID. This function can generate
// multiple lists at once, returning the first in the sequence,
// as I recall.
list = glGenLists(1);

//Start a new display list, as we might use glBegin(), and tell OpenGL
// that we only want to prepare it for now, not yet display it.
glNewList(list, GL_COMPILE);

//binding of textures and specification of vertices, complete with
//glBegin()'s and glEnd()'s, as we had before, goes in here.

//end our display list
glEndList();
}

//The method that is called when you draw the screen
void draw()
{
//Do whatever setting up, transformations and drawing
// that you might want to do before drawing the floor.

//draw the floor
glCallList(list);
}




Share this post


Link to post
Share on other sites
replace
for(int b = -100; b < 100; b++)
{
for(int a = -100; a < 100; a++)
{
glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex3f(b,-0.3,-a);
glTexCoord2f(0,0); glVertex3f(b,-0.3,-a-5);
glTexCoord2f(1,0); glVertex3f(b+5,-0.3,-a-5);
glTexCoord2f(1,1); glVertex3f(b+5,-0.3,-a);
glEnd();
}
}

with

glBegin(GL_QUADS);
glTexCoord2f(0,100); glVertex3f(0,-0.3,0);
glTexCoord2f(0,0); glVertex3f(999,-0.3,0);
glTexCoord2f(100,0); glVertex3f(999,-0.3,999);
glTexCoord2f(100,100); glVertex3f(0,-0.3,999);
glEnd();

much much quicker ( though IIRC u can get visual errors by tiling with a two higher number )
if its still slow (less than 100fps) then u will need to use mipmapping

Share this post


Link to post
Share on other sites
thank you everone, i have now made meh floor. I have just learned some lihgting techneiques(from the NeHe tut's ;). And the effect I want is that The whole room is lit up by ambient light (the room is huge). However when I do the lighting, only the small area where the light is located is lit up and the rest is shadows. when the lighting isnt enabled the whole room is lit up. I want it so that the whole room is lit up like light isnt enabled and the location where
the light is positioned is lit up.I want plain bright white light. Any suggestions?

Share this post


Link to post
Share on other sites
My pleasure, on my part. ^_^

As to your light, in your call to glLight you should be using four values when you set your light's position: an x, y and z value, and a fourth value. This fourth value should dtermine whether you are specifying a point source (which seems to be what you have currently) and a distant source (which seems to be what you want.)

I'm guessing that you currently have '1' in that fourth position; if so, simply change it to '0', and you should get what you want. ^_^

Share this post


Link to post
Share on other sites
ok im really sorry to bother you guys again, but i have another problem. I turned the forth value of the lighting to 1, and it only kind of worked. when I am faced forward everything is dark. when I am faced backward every thing is light like I wanted. I tried to put on light in the front and one light in the back but it was still the same.Do you have any ideas on how to make everything light up?

Share this post


Link to post
Share on other sites
I'm sorry that I saw this so late... ^^;

I'm guessing, but are you perhaps setting up your lighting position before you set up your camera (and thus transform to the camera's position and orientation)? If so, try placing the lighting code after the camera setup is done. If I recall correctly, this should keep the lights facing the same direction regardless of player orientation.a

Remember that your lights are part of your world, and should therefore be transformed with it; if, on the other hand, they should move or rotate with the player, then place their function calls before whichever transformation they are supposed to follow - in other words, if they should rotate with the player, then they should be specified before the player's rotation transformation is made.

Share this post


Link to post
Share on other sites

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