Very simple OpenGL problem

evangs
Unregistered
 
Post: #1
I've been working through the NeHe tutorials (http://nehe.gamedev.net), and I'm stumped on the 2nd lesson. I think it has to do with the way the viewport is being setup in my program. I'm not using the boilerplate code that is provided at the website. Instead, I'm using the code suggested by Apple's documentation.

Here is the code.
Code:
void drawScene()
{
    
    
    glTranslatef(-1.5f,0.0f,-0.0f);     //potential problem here
        //glTranslatef(-1.5, 0.0f, -6.0);
    glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    
    //move 3 units to the right
    glTranslatef(3.0f, 0.0f, 0.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_QUADS);
        glVertex3f(-1.0f, 1.0f, 0f);
        glVertex3f(1.0f, 1.0f, 0f);
        glVertex3f(1.0f, -1.0f, 0f);
        glVertex3f(-1.0f, -1.0f, 0f);
    glEnd();
    
    glFlush();
}

@implementation MyOpenGLView
-(void)drawRect: (NSRect) bounds
{

    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    drawScene();
}

@end

Now, I know the rendering context is setup and that stuff is being rendered. However, it seems I am "zoomed" too close to the scene as I only see part of the triangle and can't see the quad. I think the problem lies with the glTranslatef call at the start of the rendering fragment. I've tried various values for the z-axis, but I can't ever get it to display the entire scene. I see part of the scene for a z-axis value of -1 or +1 (funnily enough, I see the same part of the scene!). Any larger values and I get a black scene.

This is probably a very very simple problem but I can't seem to figure out what else I need change to get it to work right.
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #2
What you see is a combination of your projection and modelview matrices. I like to think of it as setting up the "camera" usually with a call to glOrtho or gluPerspective in the projection matrix. In Lesson02's case I think this is gluPerspective, with a 45 degree FOV and near and far clip planes of 0.1 and 100.

My guess is you are not setting up the projection matrix correctly, perhaps you are not calling gluPerspective, or are setting the far clip plane to 1.0, and everything is getting clipped when beyond that.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
evangs
Unregistered
 
Post: #3
OK, I've now got it working by taking the -reshape code from the supplied Cocoa example. Also, I've had trouble with getting the Quad to render but replacing the Quad rendering code with stuff that creates a triangle causes two triangles to be correctly rendered. Then putting the Quad rendering code back in causes the Quad to render correctly. Blink

I also make sure to clean before each build so that's pretty strange. But at least it works now and I can proceed.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
If you're using Xcode 3 (Leopard) people are having problems with their programs showing bizarre impossible behavior that's only fixed by quitting and relaunching Xcode.

Of course, you could just be confused Wink
Quote this message in a reply
evangs
Unregistered
 
Post: #5
I'm using Xcode 2.5, but it does seem that some problems can be fixed by just quitting Xcode and relaunching it. This is especially true as some of the behavior I see just cannot be explained (i.e. Quad not rendering when it should, etc).
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #6
I saw such problems on Tiger 2.5 xcode too. I would make modifications that didn't seem to hold, then quit and relaunch and sure enough the changes weren't there.

Haven't seen it on Leopard, but man was that annoying.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Member
Posts: 144
Joined: 2004.07
Post: #7
reubert Wrote:I saw such problems on Tiger 2.5 xcode too. I would make modifications that didn't seem to hold, then quit and relaunch and sure enough the changes weren't there.

Haven't seen it on Leopard, but man was that annoying.

With version 2.x I haven't noticed any funky behavior of that sort (AFAIK the only unintended behavior I've had with Xcode is crashing, and that was a 1.x thing). Having some code not stick without a relaunch sounds very odd.

If I'm one of the lucky ones, then yay.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #8
It has happened to me with 2.x. Cleaning the project wouldn't even get it to work. However, I recall modifying the code further around the spot that didn't take (maybe just as simple as adding a space and deleting it) and *then* it would pick it up. Totally bizarre...
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #9
If you're running with ZeroLink, this can happen. If you're quick to hit the build button after making a change and saving, the linker can miss that compilation unit, and zero-links the old object file. My tip is to hit Run without saving the file, and then answering Yes on the dialog button that comes up. That way, it almost never happens to me. If it happens to you, just touch the file (as AnotherJake said) and run it again.
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #10
Quote:If you're running with ZeroLink, this can happen
Wasn't the issue in my case, ZeroLink is permanently set in the off position Wink clean builds or small edits and re-saving didn't help. I was starting to suspect hard drive problems, but with XCode3 fixing it, and others mentioning the same problem I guess it is a 2.x (or tiger) thing.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #11
I've had this problem too; it is particularly annoying when you're trying to fix a bug in your code and you new code doesn't seem to fix it, so you pass on and keep looking elsewhere...
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Alpha Channel Problem Moganza 1 2,697 Jan 19, 2013 08:25 AM
Last Post: sealfin
  iPad, OpenGL ES, and XCode Instruments problem! Bandit 0 3,698 Dec 13, 2010 01:21 PM
Last Post: Bandit
  OpenGL global and local coordinate problem. mikey 4 3,649 May 26, 2009 08:48 AM
Last Post: mikey
  Simple OpenGL ES problem soulstorm 3 3,966 May 14, 2009 03:53 PM
Last Post: AnotherJake
  Opengl picking problem (zip file) papillon68 1 4,311 Mar 1, 2009 08:49 PM
Last Post: chronus