Weird OpenGL thing...

jfFlash
Unregistered
 
Post: #1
So I am trying to start to use OpenGL with Objective-C, because as much as I llike flash, there are some times when it's speed issues are too frustrating, (the same with the Quartz I know)... so I subclassed NSOpenGL view... and I'm trying to make it into something recognizeable for me as a 2d programmer...

Code:
static void clearGraphics() {
        glClear(GL_COLOR_BUFFER_BIT);
    }
    static void draw() {
        clearGraphics();
        glColor3f(1.0f, 0.85f, 0.35f);
        glBegin(GL_QUADS);
        glVertex2f( 0.0, 0.0);
        glVertex2f( 10.0, 0.0);
        glVertex2f( 10.0, 10.0);
        glVertex2f( 0.0, 10.0);
        glEnd();
    }
    -(void) drawRect: (NSRect) bounds {
        //setup 2D
        glViewport(0,0,400,500);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0,400,0,500);
        glMatrixMode(GL_MODELVIEW);
        
        glClearColor(1.0,1.0,1.0,1.0);
        draw();
        glFlush();
    }

To me, this should draw a yellow rectangle in the top left corner that is ten by ten pixels... why does it come out as [Image: annoying.png]
this?
I know having the 400x500 written statically into the program is bad practice, but I can fix that once I get it working... and keep in mind I have absolutely no skill whatsoever with OpenGL, so it's probably all crappy, as I've tried to piece together the scarce 2d resources for OpenGL that I can find online.
Thanks for your help...
Quote this message in a reply
Jones
Unregistered
 
Post: #2
The OpenGL View/Window is 400 * 500 itself, I assume? (You made it to be 400 * 500 in IB, in other words...)

Quote:gluOrtho2D(0,400,0,500);

Should be:

Quote:gluOrtho2D(0,400,500,0);

For top left orientation. Remember though if your actual OpenGL view in your nib file is not 400 * 500 things will go screwy.
Quote this message in a reply
jfFlash
Unregistered
 
Post: #3
Yes, my NSOpenGLView Subclass is 400x500 (widthxheight)
And that doesn't seem to help either.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
You should probably call glLoadIdentity() after your call glMatrixMode(GL_MODELVIEW). I suggest that you leave the gluOrtho2D call the way it is and get used to the lower left being (0, 0). That's the way it's done in mathematics, and IMO that's the way it should be done in graphics. I also suggest that you place all your matrix and viewport setup calls in a separate function, since they only need to be called at the beginning or when you resize the view.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
As you get more advanced, you'll find yourself wanting to do those more often, rather than less... I virtually always have them in my display routines these days... Even glViewport is tending to end up there!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
True, I will probably need to have multiple calls once I start doing shadow maps and overlaying interfaces. However, when you don't need to call them more than once, there's no point in doing so.
Quote this message in a reply
jfFlash
Unregistered
 
Post: #7
No matter where I place glLoadIdentity, my square ends up in the middle of my window, way too big. (see screenshot) What am I doing wrong?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #8
You still need glLoadIdentity at after you call glMatrixMode for the projection matrix, I was just suggesting you also call it after calling it after calling glMatrixMode for the modelview matrix. Even though this shouldn't make any difference, you could try replacing your gluOrtho2D call with
glOrtho(0, 400, 0, 500, -1, 1)
It should be the same, but then again, I was having problems with my projection matrix on my MacBook Pro while I had no problems on my PowerMac when making a call to gluPerspective. I ended up fixing it by calling glFrustum using the exact formulas that they used. Quite strange, but worth a try.
Quote this message in a reply
jfFlash
Unregistered
 
Post: #9
Sorry it took me so long to get to this... swamped with work... okay, so I changed my gluOrtho2D call like you suggested, but now nothing appears at all. Blink
Anyway, thanks for your help, and sorry I was so slow to get back. Would posting my entire project help? Although this view subclass is the only thing in it... I could do that.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #10
OK, then, try switching the -1 and 1. (I usually have it that way myself, but the man page for gluOrtho2D says they use it the other way; they could have mistyped, though) If that doesn't work, posting the project could help.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Weird OpenGL Issues (Mach-O) Matrix 2 3,018 Jan 20, 2005 07:07 PM
Last Post: Matrix
  This thing = way too slow LongJumper 9 4,365 Dec 20, 2004 05:14 AM
Last Post: TomorrowPlusX
  Projection matrix : Strange thing ? Ole 4 2,955 Sep 13, 2003 02:38 AM
Last Post: Ole
  Doing the wall slide thing monteboyd 9 3,815 Jul 8, 2003 05:13 PM
Last Post: monteboyd
  weird thing fat-t 5 3,134 Jul 7, 2003 04:59 PM
Last Post: fat-t