OpenGL in draw rect

Discussion in 'Mac Programming' started by Duke Leto, Jul 18, 2009.

  1. macrumors regular

    Joined:
    Mar 17, 2008
    #1
    I have a circle drawn in an NSOpenGL view, but the OpenGL units and the frame units are very different.

    Given a square size, how can I make a circle to fit the frame? It seems like a guessing game!

    I am very new to OpenGL, as I started to figure some things out just yesterday, so go easy on me please :D

    Almost forgot, here's the code:
    Code:
    void glCircle3i3d(GLint x, GLint y, GLint rad, GLdouble red, GLdouble green, GLdouble blue) { 
        float angle; 
        glPushMatrix(); 
        glLoadIdentity(); 
        glDisable(GL_TEXTURE_2D); 
        glLineWidth(1.0f); 
        glBegin(GL_POLYGON); 
        glColor3d(red, green, blue);
        for(int i = 0; i < 100; i++) { 
            angle = i*2*M_PI/100; 
            glVertex2f(x + (cos(angle) * rad), y + (sin(angle) * rad)); 
        } 
        glEnd(); 
        glEnable(GL_TEXTURE_2D); 
        glPopMatrix(); 
    }  
    
    - (void)drawRect:(NSRect)rect
    {
    	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    	glLoadIdentity();   // Reset the current modelview matrix
    	glCircle3i3d(0, 0, rect.size.width/2, myColor.red, myColor.green,
    myColor.blue);
    	[[self openGLContext] flushBuffer];
    }
    
    
     
  2. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #2
    Are you using orthographic or perspective projection?

    If you're using perspective projection, you won't be able to make a circle fit the frame without some very serious math.

    If this is a 2D application, you should use orthographic projection, because you can make pixel coordinates correspond easily to world coordinates. If you set up your projection matrix with
    Code:
    gluOrtho2D(0.0, viewWidth, 0.0, viewHeight);
    then world coordinates will be identical to pixel coordinates.

    OpenGL is not easy. It can take skilled programmers months or even years to master it.
     

Share This Page