LWJGL Forum

Programming => OpenGL => Topic started by: dekoffie on December 01, 2005, 19:24:42

Title: Beginner Alert - where my quads at?
Post by: dekoffie on December 01, 2005, 19:24:42
Hello there,

first of all, I'm completly new to openGL, so I'm suspecting the following problem is probably pretty stupid but I just don't seem to find the result.
I followed a lot of tutorials over at NeHe, got them all working just fine and I believe I understand as good as everything that is explained there.
Seeing how I am especialy curious about terrain rendering, I picked up this tutorial here: http://home.planet.nl/~monstrous/terrain.html and started off trying to write my own little "terrain engine": I'm only up to the 2nd algorythm explained on that page, and I'm already stuck;

When I draw my terrain, my display just gives me a black window. Now, I'm thinking it has something to do with perspective and the coordinates system, which I probably don't understand as well I should at the moment. (at least, until now I thought I did :) )

So here's my code for the initialisation of openGl and the actual rendering method:

/**
* Initialise openGL
**/
private void initGL()
{
   GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
   GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
   GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
   GL11.glClearDepth(1.0f); // Depth Buffer Setup
   GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
       GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
       
       GL11.glMatrixMode(GL11.GL_PROJECTION);
       GL11.glLoadIdentity();

       // Calculate The Aspect Ratio Of The Window
       GLU.gluPerspective(
         45.0f,
         (float) Display.getDisplayMode().getWidth() / (float) Display.getDisplayMode().getHeight(),
         0.1f,
         1000.0f);
       GL11.glMatrixMode(GL11.GL_MODELVIEW);

       GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
}

/**
* render()
*
* render our scene
*/
public void render()
{
       GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
       GL11.glLoadIdentity();

       GL11.glTranslatef(0.0f, 0.0f, -5.0f);
       GL11.glColor3f(0.0f, 0.5f, 0.1f); // set color
       
// start rendering
GL11.glBegin(GL11.GL_QUADS);
for(float x = 0.0f; x < AREA_SIZE-1; x++)
{
for(float z = 0.0f; z < AREA_SIZE-1; z++)
{
GL11.glVertex3f(F * (x+1), F * height_map[(int)x+1][(int)z], F*z);
GL11.glVertex3f(F * x, F * height_map[(int)x][(int)z], F*z);
GL11.glVertex3f(F * x, F * height_map[(int)x][(int)z+1], F*(z+1));
GL11.glVertex3f(F * (x+1), F * height_map[(int)x+1][(int)z+1], F*(z+1));
}
       }
GL11.glEnd();
}


The height_map array is filled with this method (as taken from the tutorial above):
/**
* generateMap()
*
* create a random height map
*/
private void generateMap()
{
float h;
int x, y;

for(x = 0; x < AREA_SIZE; x++)
{
for(y = 0; y < AREA_SIZE; y++)
{
h = (float) (10.0 * Math.sin(x/24.0) + 7.0 * Math.cos((y-50.0)/18.0));
height_map[x][y] = h;
}
}
}


So, could any of you see what I'm doing wrong here? :)

Thanks in advance for any tips!
Title: Beginner Alert - where my quads at?
Post by: CaseyB on December 01, 2005, 19:51:41
I take it that your variable F is just a constant multiplier?  What do you have it defined as?
Title: Beginner Alert - where my quads at?
Post by: dekoffie on December 01, 2005, 20:52:12
Quote from: "CaseyB"I take it that your variable F is just a constant multiplier?  What do you have it defined as?
it's defined as follows:
// world scale of one height map cell
private final float F = 16.0f;
// w and h of our map
private final int AREA_SIZE = 128;
Title: Beginner Alert - where my quads at?
Post by: CaseyB on December 01, 2005, 22:22:43
I think that the quads you're defining may not be planar.  This is always a concern when using quads instead of triangles.  You may be better off to modify your drawing algorythm and use GL_TRIANGLE_STRIP instead of GL_QUADS.

-=EDIT=-
In fact, I just looked at the tutorial you are linking to and they do just that!
QuoteRendering a grid using triangle strips
Title: Beginner Alert - where my quads at?
Post by: dekoffie on December 01, 2005, 22:32:50
Quote from: "CaseyB"I think that the quads you're defining may not be planar.  This is always a concern when using quads instead of triangles.  You may be better off to modify your drawing algorythm and use GL_TRIANGLE_STRIP instead of GL_QUADS.

-=EDIT=-
In fact, I just looked at the tutorial you are linking to and they do just that!
QuoteRendering a grid using triangle strips
Thanks for your explanation, and yes I do know that the tutorial was about to do that :) But still, I'd like to try everything out, to try to make myself sure that I will fully understand all that follows.

So, I hope someone would still be able to know how to fix the above code, if not, well I'll just continue like you said! Thanks a bunch CaseyB!!
Title: Beginner Alert - where my quads at?
Post by: dekoffie on December 02, 2005, 10:31:08
Well I changed my code to use triangle strips, exact same method as in the tutorial; still I don't get anything on my screen :(

I am pretty sure the program is doing calculations and drawing it, because sometimes I see some different colors for a split second on the screen (it looks kinda like when you're computer crashes and messes up your entire display and you can't see anything on it) and the mouse cursor changes also.

Any ideas? :)
Title: Beginner Alert - where my quads at?
Post by: napier on December 02, 2005, 16:46:52
Have you tried changing your viewpoint?   I don't see a gluLookAt() call in your code, so I'm guessing your viewpoint is at 0,0,0 by default which may not be a good vantage point to actually see the terrain you're generating.
Title: Beginner Alert - where my quads at?
Post by: dekoffie on December 03, 2005, 19:38:06
Quote from: "napier"Have you tried changing your viewpoint?   I don't see a gluLookAt() call in your code, so I'm guessing your viewpoint is at 0,0,0 by default which may not be a good vantage point to actually see the terrain you're generating.
Thanks a bunch napier! That did the trick!
I can now see my rendered triangle trips, y@y :)

One question though which I hope you'll be wanting to answer; how exactly does gluLookAt work? I found a good position to look over my terrain, but I only got to it by trying a bunch of different numbers actualy :)
       GLU.gluLookAt(
        0.0f, 0.0f, 0.0f, // eye yield (=viewpoint?)
        (AREA_SIZE / 2), 15.0f, (AREA_SIZE / 2), // center
        0.0f, 1.0f, 0.0f // upside vector
       );


And now I 'd like to make my camera move throughout the terrain by using the arrows (for starters :) ); now I did already have some code (learned from NeHe site) but this doesn't use gluLookAt anywhere. I just rotate and translate the scene around the user view. So, does gluLookAt come into play anywhere here?

thanks for the help!
Title: Beginner Alert - where my quads at?
Post by: tomb on December 03, 2005, 19:46:54
Here is a link to the OpenGL documentation:
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/

If you look in the glu folder you'll find some information about lookat.
Title: Beginner Alert - where my quads at?
Post by: dekoffie on December 04, 2005, 13:39:13
Quote from: "tomb"Here is a link to the OpenGL documentation:
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/

If you look in the glu folder you'll find some information about lookat.
Thank you!
Title: Beginner Alert - where my quads at?
Post by: ilazarte on January 03, 2006, 01:39:08
Great site for a beginner like me.  Thanks.