PDA

View Full Version : Need help with opengl lines



moonie
06-10-2003, 06:10 AM
How come after running my program, the line never start from the first sphere and end at the second sphere? And what modification should i do to my program to make the drawing of the line connecting the 2 spheres by just pressing a key on my keyboard?
My program is wriiten like this:

#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <iostream.h>
void init(void);
void display(void);

float x1,y1,z1,x2,y2,z2;
int main(int argc, char *argv[])
{

cout << "Enter the x1 coordinate of the 1st sphere(range between -1 to 1) :";
cin >> x1;
cout << "Enter the y1 coordinate of the 1st sphere(range between -1 to 1) :";
cin >> y1;
cout << "Enter the z1 coordinate of the 1st sphere(range between -1 to 1) :";
cin >> z1;
cout << "Enter the x2 coordinate of the 2nd sphere(range between -1 to 1) :";
cin >> x2;
cout << "Enter the y2 coordinate of the 2nd sphere(range between -1 to 1) :";
cin >> y2;
cout << "Enter the z2 coordinate of the 2nd sphere(range between -1 to 1) :";
cin >> z2;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(700,700);
glutInitWindowPosition(550,300);
glutCreateWindow("Fastrak Project");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;

}

void init(void)
{
glClearColor(0.0f ,0.0f ,0.0f ,0.0f);
glClearDepth(1.0f);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)700/(GLfloat)700,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void display(void)
{
glTranslatef(0,0,-5.0f);
glRotatef(-65,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1.0,0.5,1.0);
glutWireCube(2.00);

glTranslatef(x1,y1,z1);
glColor3f(0.5,0.5,1.0);
glutSolidSphere(0.02,10,10);

glTranslatef(x2,y2,z2);
glColor3f(0.5,1.0,0.5);
glutSolidSphere(0.02,10,10);

glColor3f(1.0,1.0,0.0);
glBegin(GL_LINES);
glVertex3f(x1,y1,z1);
glVertex3f(x2,y2,z2);
glEnd();
glFlush();
glutSwapBuffers();
}

dorbie
06-10-2003, 06:56 AM
You have a matrix stack and your transformations on the stack are cumulative and persistent. A translate is relative to what's already on the stack, glTranslate* actualy creates a new translate matrix and multiplies that translate matrix with whatever matrix already exists on the stack.

After each translate you either need to consider the relative position or reset the modelview back to identity.


Look at glPushMatrix and glPopMatrix, also look at glLoadMatrix with an identity matrix or your viewing matrix. Because most people want to preserve a viewing translation and manipulate model matrixes in some tree like graph the push/pop around each model transformation can be very useful.

nexusone
06-10-2003, 10:03 AM
Try this adding glPush/Pop Matrix

void display(void)
{
// Clear before any commands
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef(0,0,-5.0f);
glRotatef(-65,1.0f,1.0f,1.0f);
glColor3d(1.0,0.5,1.0);
glutWireCube(2.00);
glPopMatrix();

glPushMatrix();
glTranslatef(x1,y1,z1);
glColor3f(0.5,0.5,1.0);
glutSolidSphere(0.02,10,10);
glPopMatrix();

glPushMatrix();
glTranslatef(x2,y2,z2);
glColor3f(0.5,1.0,0.5);
glutSolidSphere(0.02,10,10);
glPopMatrix();

glColor3f(1.0,1.0,0.0);
glBegin(GL_LINES);
glVertex3f(x1,y1,z1);
glVertex3f(x2,y2,z2);
glEnd();
glFlush();
glutSwapBuffers();
}

moonie
06-10-2003, 05:36 PM
ok thanks i manage to solve the line problem. Now i have another problem in my program. What must i add to my program to enable the drawing of the line connecting the 2 spheres possible by just pressing a key on the keyboard eg. L

nexusone
06-11-2003, 04:37 AM
Look up the glutKeyboardFunc()...

int sphere_state;


void display(void)
{

// partal code:

if (sphere_state) draw_spheres(); // When shpere_state is 1 (TRUE), draw spheres.

}

void My_keyboard_input( char key, int x, int y)
{

if (key == "s" ) sphere_state = 1; // spheres on
if (key == "a" ) sphere_state = 0; // off

}

Also you can look at my glut examples on my website: www.angelfire.com/linux/nexusone/ (http://www.angelfire.com/linux/nexusone/)

All have keyboard and some mouse examples.



Originally posted by moonie:
ok thanks i manage to solve the line problem. Now i have another problem in my program. What must i add to my program to enable the drawing of the line connecting the 2 spheres possible by just pressing a key on the keyboard eg. L



[This message has been edited by nexusone (edited 06-11-2003).]