Jump to content
  • Advertisement
Sign in to follow this  
Togakangaroo

OpenGL help a feller out?

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

Hi guys, I am a beginer at OpenGL so please bear with me. A couple weeks you guys helped me out a lot and now I've got a new bug that I can't seem to get out of my program. Ok, here goes. What I'm doing: Writing a bunch of classes and an interface to randomly generate, manipulate and save an overhead view of a square terrain map. Although the actual process is pretty complicated at this level all that is happening is that the heights at each point x, y are stored at index GridSideSize*y + x of array Overhead[] where GridSideSize is the size of one side of the square (so Overhead is GridSideSize^2 large). What should be happening: There are several modes in which you can view the terrain, i.e. I draw it using GL TRIANGLE STRIP, GL QUAD STRIP, so on. Since the problem seems to be the same in all viewing modes I have cut out all but the most simple one. In the code below what should be happening is that a gray plane on the x-y axes should be displayed, with different colored and length lines coming out of it to represent the value (height) stored at the corresponding index of the array. I have it set up so the user can pan, zoom and rotate along the x, y and z axes by incrementing or decremnting the variables zoomfactor, xrot,yrot,zrot, viewportL, viewportB, viewportH, viewPortW. What is happening: Everything draws, zooms and pans fine. But for some reason (and even if I multiply the z value by -1) it draws in the opposite direction from the camera. This is wierd but it wouldn't be much of a problem if I could just rotate the plane. However, as I rotate over the x or y axis a strange thing happens, it will rotate fine right up to the point where the plane is exactly level with your view (coming at you, so you're getting a side view of the map) but that's when the problem starts happening. Instead of continuing to rotate in the direction and bringing the terrain map into view, it seems like either all my lines disappear and appear on the backside of the plane or that it starts rotating backwards in the opposite direction. I don't think its a logic error on my part, and think it must be a result of some aspect of OPENGL that I'm not aware of. Any help would be nice. Thanks again. The following is my display function which is where I think the problem is occuring: void RenderScene() { // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glViewport(ViewportL, ViewportB, Zoomfactor*ViewportW, Zoomfactor*ViewportH); // Save matrix state and do the rotation glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glRotatef(zRot, 0.0f, 0.0f, 1.0f); if(DisplayMode == Quads) { . . . } else if(DisplayMode == Lines) { int i, j; double h; for(i = 0; i < (GridSideSize-1);++i) { for(j=0;j<GridSideSize;++j) { h = Overhead[getIndex(j,i,GridSideSize)]; glColor3f(h/Highest, (Highest - h)/Highest, 0); glBegin(GL_LINES); glVertex3d(j-GridSideSize/2,i-GridSideSize/2,-AMP*h); glVertex3d(j-GridSideSize/2,i-GridSideSize/2,0); glEnd(); } } glColor3f(.5, .5, .5); glBegin(GL_QUADS); glVertex3d(-GridSideSize/2,-GridSideSize/2,0); glVertex3d(GridSideSize/2,-GridSideSize/2,0); glVertex3d(GridSideSize/2,GridSideSize/2,0); glVertex3d(-GridSideSize/2,GridSideSize/2,0); glEnd(); } // Restore transformations glPopMatrix(); // Flush drawing commands glutSwapBuffers(); }

Share this post


Link to post
Share on other sites
Advertisement
My immediate reactions are:
  • Are you in the right matrix mode? GL_MODELVIEW for your drawing and GL_PROJECTION when you set your projecton matrix.

  • Do you have depth testing enabled?


Enigma

Share this post


Link to post
Share on other sites
the only other time I do anything that I think can affect this is in my changesize function:

void ChangeSize(int w, int h){
GLfloat xRange = ((float)GridSideSize)/2 + BORDER;
GLfloat yRange = ((float)GridSideSize)/2 + BORDER;
GLfloat zRange = -100.0f;

// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
ViewportL = 0;
ViewportB = 0;
ViewportW = WindowW = w;
ViewportH = WindowH = h;
Zoomfactor = 1;
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-xRange, xRange, -yRange*h/w, yRange*h/w, -zRange, zRange);
else
glOrtho (-xRange*w/h, xRange*w/h, -yRange, yRange, -zRange, zRange);

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

So yeah, I do think I'm in the right matrix mode. What does depth testing do and how/when do I enable it?

Share this post


Link to post
Share on other sites
Oh, I suppose the following can have something to do with it. It runs once before the main loop

void SetUpInterface() {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(INITWPOSX,INITWPOSY) ;
glutInitWindowSize(INITWWIDTH,INITWHEIGHT) ;
glutCreateWindow("Terrain Generator");

glutReshapeFunc(InternalChangeSize);
glutSpecialFunc(InternalSpecialKeys);
glutDisplayFunc(InternalRender);
glutKeyboardFunc(InternalKeyPress);
glutMouseFunc(InternalMouseFunc);

// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
// Set drawing color to green
glColor3f(0.0f, 1.0f, 0.0f);
// Set color shading model to flat
glShadeModel(GL_FLAT);

glFrontFace(GL_CW);
}

Everything else I have a good idea what it does

Share this post


Link to post
Share on other sites
Yep, your matix modes are fine. Depth testing is a visibility test. Without depth testing if I draw a distant mountain and then a wall inbetween myself and the mountain I will end up with an image of the wall, but if I render the wall first and then the distant mountain I'll get an image of the mountain. Without depth testing OpenGL just adds everything you give it to the screen in order. If I enabled depth testing in the above example I would always get an image of the wall, regardless of the order I rendered in. Depth testing checks whether the new object is nearer* than what has already been rendered and only draws it if it is. You can enable depth testing by calling glEnable(GL_DEPTH_TEST);. This is usually done in the initialisation stage.

Enigma

*You can actually set whether it should only be nearer objects that are drawn or futher object, objects of the same depth, etc.

Share this post


Link to post
Share on other sites
well that sounded really promising, but I added glEnable(GL_DEPTH_TEST); in my setUp function and no difference. Could it have something to do with one of the other commands in there? I canabalized a lot of code off of someone elses project for the openGL stuff and some of it I'm not quite sure what it does

Share this post


Link to post
Share on other sites
I take that back, it did SOMETHING. The drawing mode I discussed above still has the same thing going on, but the other modes look better...wierd. Thank goodness for small favors though I guess.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!