Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Drawing Lines in OpenGL (Mac OS X)

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2011
    Location
    Knoxville
    Posts
    7

    Drawing Lines in OpenGL (Mac OS X)

    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:
    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!

  2. #2
    Junior Member Regular Contributor malexander's Avatar
    Join Date
    Aug 2009
    Location
    Ontario
    Posts
    249

    Re: Drawing Lines in OpenGL (Mac OS X)

    You haven't set up the modelview or projection matrices. If you're looking for a simple 2D canvas, use
    Code :
    glOrtho( 0, 0, rectView.size.width, rectView.size.height, -1, 1);
    after your viewport call.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2011
    Location
    Knoxville
    Posts
    7

    Re: Drawing Lines in OpenGL (Mac OS X)

    I added it right after
    Code :
        glViewport(0, 0, rectView.size.width, rectView.size.height);
    in the reshape method, but the lines still don't appear.
    Any Ideas?

  4. #4
    Junior Member Newbie xiphos's Avatar
    Join Date
    Jun 2011
    Posts
    11

    Re: Drawing Lines in OpenGL (Mac OS X)

    Lines drawing are not inside camera's frustum.
    Quick check(fix),
    add below lines at beginning of function drawAnObject()
    Code :
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2011
    Location
    Knoxville
    Posts
    7

    Re: Drawing Lines in OpenGL (Mac OS X)

    It works now, thanks everyone!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •