PDA

View Full Version : How to position a sphere and click on it



illuminator
09-21-2006, 05:40 AM
Hi!

I have 2 questions I hope someone can answer me!

This is my code for the drawing/rendering:

//Draw the axes
glBegin(GL_LINES);

glColor3f(1.0f,0.0f,0.0f); //Red
glVertex3i(0,0,0);
glVertex3i(300,0,0);

glColor3f(0.0f,0.0f,1.0f); //Blue
glVertex3i(0,0,0);
glVertex3i(0,300,0);

glColor3f(0.0f,1.0f,0.0f); //Green
glVertex3i(0,0,0);
glVertex3i(0,0,300);

//Draw sphere number 1
glColor3f(1.0f,1.0f,1.0f); //White
GLUquadric *myQuad;
GLdouble radius = 3.0;
GLint slices, stacks;
myQuad=gluNewQuadric();
slices = stacks = 10;
gluSphere( myQuad , radius , slices , stacks );

//Draw sphere number 2
glColor3f(1.0f,0.0f,0.0f); //Red
myQuad=gluNewQuadric();
gluSphere( myQuad , radius , slices , stacks );

//Draw sphere number 3
glColor3f(0.0f,0.0f,1.0f); //Blue
myQuad=gluNewQuadric();
gluSphere( myQuad , radius , slices , stacks );

glEnd();Question #1: This code places all the spheres at the origin, no surprise since this is stated in the definition of the gluSphere function. But how can I set the position to whatever I want?


Question #2: How can I be notified when the user clicks on one of the spheres? I also need to know which of the spheres that were clicked?


Very greatful for any help here!


Cheers!

k_szczech
09-21-2006, 08:05 AM
Rendering objects at different locations:

glMatrixMode(GL_MODELVIEW);
glPushMatrix(); //remember current matrix
glTranslatef(x1,y1,z1);
glBegin(<type>); /* render something */ glEnd();
glPopMatrix(); //restore matrix

glPushMatrix(); //remember current matrix
glTranslatef(x2,y2,z2);
glBegin(<type>); /* render something else */ glEnd();
glPopMatrix(); //restore matrixAs for detecting which sphere was clicked there are two common ways:
-calculate it yourself using some math
-use OpenGL's selection mode - this is what I would suggest