I'm trying to draw a line using a GL_TRIANGLE_STRIP that will go deeper or more shallow with the z coordinate, but the the z value I'm passing into glVertex3f() doesn't seem to affect the perceived width. In the code below (a modified example from OpenGL Programming Guide), the line should start closer to the perspective (larger), and get farther (smaller).
Would you point to my mistake, or a topic in which this it is addressed?
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display(void)
{
GLdouble gran = 5.0;
GLdouble width = 7.0;
GLdouble theta = 1.047197551;
GLdouble halfPi = 3.1459/2;
GLdouble refpx = 50.0;
GLdouble refpy = 50.0;
GLdouble startr = 1.0;
GLdouble startg = 1.0;
GLdouble startb = .5;
glClear(GL_COLOR_BUFFER_BIT);
//glLoadIdentity();
//glScalef(2.0, -.5,1.0);
//glRotatef(45.0, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
for(int i = 0; i < 40; i++)
{
glColor3f(startr / i, startg / (20 - (i%20)), startb);
GLdouble x1 = refpx + width*cos(theta + halfPi);
GLdouble x2 = refpx + width*cos(theta + halfPi*3);
GLdouble y1 = refpy + width*sin(theta + halfPi);
GLdouble y2 = refpy + width*sin(theta + halfPi*3);
glVertex3f(x1,y1, (GLdouble)i/40.0);
glVertex3f(x2,y2, (GLdouble)i/40.0);
refpx += gran*cos(theta);
refpy += gran*sin(theta);
if(i % 9 == 0) {
theta += halfPi/8;
}
if(i == 20) {
theta -= halfPi/2;
}
if(i == 5) {
theta -= halfPi/2;
}
}
glEnd();
glDisable(GL_BLEND);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble) w, 0.0, (GLdouble) h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(450, 450);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
You are setting the projection matrix to just an orthographic view. This means that changes in the distance from the camera will not affect the size of the object on the screen.
In a perspective projection far things are smaller, that is not true for an orthographic projection.
So in your vertex calculations the Z coordinate is controlled by i, but not the X and Y, they are only controlled by the trigonometry, and in particular, while refpx
and refpy
change, the size of the object in the XY plane never does (because the only variation is a sin or cosine term added to the refpx
and refpy
values).
So that's why: it can't vary, because you've got nothing in the math that calculates X and Y coordinates that varies the size by distance. On its own Z will make no difference to the size on screen unless you set a perspective transform in the projection matrix. Use gluPerspective
for ease, or assemble your own camera matrix, the math is here: http://en.wikipedia.org/wiki/3D_projection#Perspective_projection