0

I'm looking at the apple documentation for drawing OpenGL content to the screen, on their 'drawing to a window or view page' (linked below) they show that you can draw by placing data within the drawRect method within the openGL class. I understand that, but what if you have a customClass which produces some vertex data you would like to draw. How is that drawn to the screen?

// within the opengl class

-(void) drawRect: (NSRect) bounds
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 0.85f, 0.35f);
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(  0.0,  0.6, 0.0);
        glVertex3f( -0.2, -0.3, 0.0);
        glVertex3f(  0.2, -0.3 ,0.0);
    }
    glEnd();
    glFlush();
}

Ideally I would be able to do this within a draw method within my customClass, like...

// within my custom class (ideally)

-(void) draw
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 0.85f, 0.35f);
    glBegin(GL_TRIANGLES);
    {
        for(int i=0; i<[customClassVertices count]; i++)
        glVertex3f(vertices[i].x, vertices[i].y,vertices[i].z);
    }
    glEnd();
    glFlush();
}

Here Document

4
  • can you not call the draw method from within the drawRect: ? Commented Oct 1, 2013 at 9:32
  • That does work, but that would mean having references to all my objects I would like to draw within the opengl class, is this how it is done? So that would mean an array of polygons, graphs etc with the opengl class? Commented Oct 1, 2013 at 9:46
  • 2
    yep thats pretty much how its done, opengl is a state machine so that means you needs to do all your calls in a specific order, so all drawing needs to be done in one place essentially. maybe you can construct an object that manages all your other objects that need to be drawn, so you dont have to have everything quite in one place. think of it like the apps main run loop Commented Oct 1, 2013 at 9:54
  • Ok thanks, sounds good. I was wondering why I couldn't get anything to work, that sounds like a good reason why to me! Commented Oct 1, 2013 at 15:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.