Public Group

How to position a sphere and click on it

This topic is 4772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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!

Share on other sites
#1
For every sphere:

glPushMatrix() //save the current matrix
glTranslatef(pox.x,pos.y,pos.z) //move sphere to pos
glPopMatrix() //restore saved matrix

#2
What you want to do is picking.

Be warned: that's an advanced feature to implement.

You could do it yourself:

Create a ray from the camera pos (from mouse position) into your world space, get the nearest sphere it hits.
This would require you to project two points from screenspace to worldspace, then test each sphere (or use some spatial information for optimization)

Or use OpenGL picking. Don't have a tutorial right now, you can find one in the "OpenGL Super Bible" or google "OpenGL picking".

Share on other sites

Sorry for the last post, writes it again.

I worked very well for sphere #2 and #3. But not for sphere #1.

The code is now like this;

//Draw sphere number 1
glPushMatrix(); //save the current matrix
glTranslatef(100.0,100.0,100.0); //move sphere to pos
glColor3f(1.0f,1.0f,1.0f); //White
GLint slices, stacks;
slices = stacks = 10;
glPopMatrix(); //restore saved matrix

//Draw sphere number 2
glPushMatrix(); //save the current matrix
glTranslatef(10.0,10.0,10.0); //move sphere to pos
glColor3f(1.0f,0.0f,0.0f); //Red
glPopMatrix(); //restore saved matrix

//Draw sphere number 3
glPushMatrix(); //save the current matrix
glTranslatef(20.0,20.0,20.0); //move sphere to pos
glColor3f(0.0f,0.0f,1.0f); //Blue
glPopMatrix(); //restore saved matrix

Again, thank you very much!

Share on other sites
What's the problem with sphere #1? Doesn't show up any more?
If yes, could be your far clip plane, try setting position to some smaller value like 50.

Share on other sites
the problem is that it is at position (0,0,0). I tried to set it to 25,25,25 also.

thx

Share on other sites
Hm, can't see a problem in the code you posted.

Did you set GL_MODELVIEW matrix mode and set up your camera position before drawing the spheres?

Addition: I just saw you are drawing your spheres between those glBegin() glEnd() calls for the axes. Better you call glEnd() after finishing the axes as OpenGL is set up for drawing lines until then.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 30
• 9
• 16
• 22