c++openglglutglewfreeglut

Why a dashed line is not being displayed while using glLineStipple() function?


I'm using OpenGL's glLineStipple() function to draw a dashed line. Everything is correct but no dashed line is displayed on the output screen. The output screen is totally blank.

Here's my code:

#include <Windows.h>
#include <GL\glew.h>
#include <GL\glut.h>
#include <GL\freeglut.h>


void myInit()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
void Display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_LINE_STIPPLE);
    glLineStipple(1, 0x00FF); // Pattern: 0x00FF, Factor: 1
    glBegin(GL_LINES);
    glVertex2f(-0.5f, 0.0f);
    glVertex2f(0.5f, 0.0f);
    glEnd();
    glDisable(GL_LINE_STIPPLE);
    glFlush();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);//Initialize the GLUT Library
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(700, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("LineStipple");
    myInit();
    glutDisplayFunc(Display);//Callback Function
    glutMainLoop();
    return 0;
}

Solution

  • From the glColor() documentation:

    The initial value for the current color is (1, 1, 1, 1).

    Since you're clearing to white (and white-on-white is really hard to see :)) you need to set the current color to something else before drawing the line:

    glBegin(GL_LINES);
    glColor3ub(0, 0, 0);
    glVertex2f(-0.5f, 0.0f);
    glVertex2f(0.5f, 0.0f);
    glEnd();
    

    Result:

    screenshot of stippled line