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

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
Paul Reed
  • 113
  • 14
  • can you not call the draw method from within the drawRect: ? – Fonix Oct 01 '13 at 09: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? – Paul Reed Oct 01 '13 at 09: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 – Fonix Oct 01 '13 at 09: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! – Paul Reed Oct 01 '13 at 15:58

0 Answers0