PDA

View Full Version : Drawing Lines in OpenGL (Mac OS X)



Blakeasd
09-07-2011, 09:21 AM
Hello!
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 am using Xcode and Mac OS X) I get no errors but the lines don't draw here is my 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!

malexander
09-07-2011, 11:59 AM
You haven't set up the modelview or projection matrices. If you're looking for a simple 2D canvas, use

glOrtho( 0, 0, rectView.size.width, rectView.size.height, -1, 1);

after your viewport call.

Blakeasd
09-07-2011, 02:38 PM
I added it right after

glViewport(0, 0, rectView.size.width, rectView.size.height);

in the reshape method, but the lines still don't appear.
Any Ideas?

xiphos
09-08-2011, 12:34 AM
Lines drawing are not inside camera's frustum.
Quick check(fix),
add below lines at beginning of function drawAnObject()
glLoadIdentity();
glScalef(0.01,0.01,1); // scale down //to make vertex value < 1
glTranslatef(-rectView.size.width/2,-rectView.size.height/2,0);If you are using glOrtho( 0, 0, rectView.size.width, rectView.size.height, -1, 1);for projection.

Blakeasd
09-08-2011, 12:57 PM
It works now, thanks everyone!