PDA

View Full Version : OpenGL Line Stipple question



Sam Danove
12-13-2015, 02:35 AM
Hi,everyone
I am fresh in studying opengl, and i just do the practice to draw lines in different pattens. Although the code can work, i find a question that if you draw two lines use same factor with different pattens, you will only get two lines with same pattern which is the former. Here is my original code.


void display(void)
{
int i;

glClear(GL_COLOR_BUFFER_BIT);
/* select white for all lines */
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);

glDisable(GL_LINE_STIPPLE);
glFlush();
}
What you get will only just dots. And if you change the factor of other 2 lines, you can get all patterns. Here is the code i changed.

void display(void)
{
int i;

glClear(GL_COLOR_BUFFER_BIT);
/* select white for all lines */
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(2, 0x00FF); /* dashed */
drawOneLine(150.0, 125.0, 250.0, 125.0);
glLineStipple(3, 0x1C47); /* dash/dot/dash */
drawOneLine(250.0, 125.0, 350.0, 125.0);

glDisable(GL_LINE_STIPPLE);
glFlush();
}
Can anyone help me solve the question, why and how can i fix it if i just want use one factor.
The complete code is below, you can just test it if you want.

#include "stdafx.h"
#include "gl/freeglut.h" //引用相关包
using namespace System;

#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); glVertex2f((x1),(y1)); glVertex2f((x2),(y2)); glEnd();
void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}

void display(void)
{
int i;

glClear(GL_COLOR_BUFFER_BIT);
/* select white for all lines */
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(2, 0x00FF); /* dashed */
drawOneLine(150.0, 125.0, 250.0, 125.0);
glLineStipple(3, 0x1C47); /* dash/dot/dash */
drawOneLine(250.0, 125.0, 350.0, 125.0);

glDisable(GL_LINE_STIPPLE);
glFlush();
}

void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 150);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

GClements
12-13-2015, 03:43 AM
Both versions work for me, so I assume that it's a bug in your display driver.

Sam Danove
12-13-2015, 06:08 AM
Both versions work for me, so I assume that it's a bug in your display driver.

Really? I just use win7 64bit system with vs 2010. And the device is totally normal.

And I find that when I use the first version, if I click or drag the window. The line will changes to the third pattern--dash/dot/dash.

It seems that the glutMainLoop() will redraw the graphic, and it will only save one patten if the factors are same.

I believe some one will meet the question like me.

It's annoying I can't upload any picture:(

GClements
12-13-2015, 01:59 PM
And I find that when I use the first version, if I click or drag the window. The line will changes to the third pattern--dash/dot/dash.

I can't reproduce this. I always get 3 distinct patterns.

One thing which may be a factor is the use of a single-buffered context; these aren't often used nowadays, and probably don't get much testing. To try using a double-buffered context, change GLUT_SINGLE to GLUT_DOUBLE in the glutInitDisplayMode() call, and call glutSwapBuffers() at the end of display() (after or in place of the glFlush() call).