Hello Guest

Beginner Alert - where my quads at?

  • 10 Replies
  • 10051 Views
Beginner Alert - where my quads at?
« 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:
Code: [Select]

/**
* 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):
Code: [Select]
/**
* 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!
Radio for me is to bring NEW music to people who then
can make their own selection for their personal use. It's
the beauty of this medium we call radio that is often
misused for commercial reasons in my opinion."  - Armin van Buuren

*

Offline CaseyB

  • ***
  • 118
Beginner Alert - where my quads at?
« Reply #1 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?

Beginner Alert - where my quads at?
« Reply #2 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:
Code: [Select]
// 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;
Radio for me is to bring NEW music to people who then
can make their own selection for their personal use. It's
the beauty of this medium we call radio that is often
misused for commercial reasons in my opinion."  - Armin van Buuren

*

Offline CaseyB

  • ***
  • 118
Beginner Alert - where my quads at?
« Reply #3 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!
Quote
Rendering a grid using triangle strips

Beginner Alert - where my quads at?
« Reply #4 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!
Quote
Rendering 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!!
Radio for me is to bring NEW music to people who then
can make their own selection for their personal use. It's
the beauty of this medium we call radio that is often
misused for commercial reasons in my opinion."  - Armin van Buuren

Beginner Alert - where my quads at?
« Reply #5 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? :)
Radio for me is to bring NEW music to people who then
can make their own selection for their personal use. It's
the beauty of this medium we call radio that is often
misused for commercial reasons in my opinion."  - Armin van Buuren

*

Offline napier

  • ***
  • 104
    • http://potatoland.org
Beginner Alert - where my quads at?
« Reply #6 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.
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

Beginner Alert - where my quads at?
« Reply #7 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 :)
Code: [Select]
       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!
Radio for me is to bring NEW music to people who then
can make their own selection for their personal use. It's
the beauty of this medium we call radio that is often
misused for commercial reasons in my opinion."  - Armin van Buuren

*

Offline tomb

  • ***
  • 148
Beginner Alert - where my quads at?
« Reply #8 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.

Beginner Alert - where my quads at?
« Reply #9 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!
Radio for me is to bring NEW music to people who then
can make their own selection for their personal use. It's
the beauty of this medium we call radio that is often
misused for commercial reasons in my opinion."  - Armin van Buuren

Beginner Alert - where my quads at?
« Reply #10 on: January 03, 2006, 01:39:08 »
Great site for a beginner like me.  Thanks.