PDA

View Full Version : Drawing a Line in 3d space with mouse



Robert.P
03-23-2015, 12:38 PM
Trying to draw a line from the center of my cube to the mouse location. The line appears at first when the window is loaded but then disappears once the mouse is on the screen.



glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-1.0, 1.0, -1.0, 1.0, 0.5, 5.0);
glFrustum(-1.0, 1.0, -1.0, 1.0, 0.5, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-2.0f);



/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);

while(!Display.isCloseRequested())
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (1.0f, 1.0f, 1.0f);
glRotatef(rotx,1.0f,0.0f,0.0f);
glRotatef(roty,0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,transz);
glScalef(scale,scale,scale);


drawCube();


glColor3f (1.0f, 1.0f, 1.0f);
drawLine(dm.getWidth()/2,dm.getHeight()/2,Mouse.getX(),Mouse.getY());


glFlush ();
Display.update();


if (Keyboard.isKeyDown(Keyboard.KEY_UP)) rotx=0.5f;
else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) rotx=-0.5f;
else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) roty=0.5f;
else if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) roty=-0.5f;
else if (Keyboard.isKeyDown(Keyboard.KEY_BACKSLASH)) transz=-0.01f;
else if (Keyboard.isKeyDown(Keyboard.KEY_SLASH)) transz=0.01f;
else if (Keyboard.isKeyDown(Keyboard.KEY_PERIOD)) scale-=0.0001f;
else if (Keyboard.isKeyDown(Keyboard.KEY_COMMA)) scale+=0.0001f;

else {rotx=0.0f; roty=0.0f; transz=0.0f;scale=1.0f;drawLine(dm.getWidth()/2,dm.getHeight()/2,Mouse.getX(),Mouse.getY());}

glClear (GL_COLOR_BUFFER_BIT);


}
}
public static void drawLine(int x0, int y0, int x1, int y1)
{ glBegin(GL_LINE_LOOP);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
glEnd();
}