Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
I am following the along with the OpenGL Redbook and one example describes how to draw lines. The book uses the aux library to draw, but I prefer to use Cocoa for my GUI. I get no errors but the lines don't draw here is my code:
Code:
#import "LineView.h"
#import <OpenGL/gl.h>
#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); \
glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();

@implementation LineView

static void drawAnObject(){

    int i;
    // Draw all lines in white
    glColor3f(1.0, 1.0, 1.0);
    
    //In 1st row, 3 lines, each with a different stipple
    glEnable(GL_LINE_STIPPLE);
    glLineStipple(1, 0x0101); //Dotted
    drawOneLine(50.0, 125.0, 150.0, 125.0);
    glLineStipple (1, 0x00FF);   /*  dashed   */
    drawOneLine (150.0, 125.0, 250.0, 125.0);
    glLineStipple (1, 0x1C47);   /*  dash/dot/dash   */
    drawOneLine (250.0, 125.0, 350.0, 125.0);
    
    /*  in 2nd row, 3 wide lines, each with different stipple */
    glLineWidth (5.0);
    glLineStipple (1, 0x0101);
    drawOneLine (50.0, 100.0, 150.0, 100.0);
    glLineStipple (1, 0x00FF);
    drawOneLine (150.0, 100.0, 250.0, 100.0);
    glLineStipple (1, 0x1C47);
    drawOneLine (250.0, 100.0, 350.0, 100.0);
    glLineWidth (1.0);
    
    /*  in 3rd row, 6 lines, with dash/dot/dash stipple,   */
    /*  as part of a single connected line strip         */
    glLineStipple (1, 0x1C47);
    glBegin (GL_LINE_STRIP);
    for (i = 0; i < 7; i++)
        glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0);
    glEnd ();
    
    /*  in 4th row, 6 independent lines,   */
    /*  with dash/dot/dash stipple         */
    for (i = 0; i < 6; i++) {
        drawOneLine (50.0 + ((GLfloat) i * 50.0), 
                     50.0, 50.0 + ((GLfloat)(i+1) * 50.0), 50.0);
    }
    
    /*  in 5th row, 1 line, with dash/dot/dash stipple   */
    /*  and repeat factor of 5         */
    glLineStipple (5, 0x1C47);
    drawOneLine (50.0, 25.0, 350.0, 25.0);
    glFlush ();
}





- (void) drawRect: (NSRect) bounds{

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(GL_FLAT);
    drawAnObject();
    

}

-(void)reshape{
    
    NSRect rectView = [self bounds];
    glViewport(0, 0, rectView.size.width, rectView.size.height);
    
    
    
}

@end

What's causing the lines not to draw?
Thanks!
 
It looks like all the lines are outside the default view volume. Renormalize your lines so they lie within [-1,1] in each dimension. If you want a coordinate system aligned with screen pixels, you will have to setup a camera transform.
 
I am not sure how to do that. It's my second day with OpenGL...Do I change the parameters in the drawOneLine method?
Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.