Jump to content
  • Advertisement
Sign in to follow this  
furhy

How to position a sphere and click on it

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
#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 this post


Link to post
Share on other sites
Thanks for quick answer!


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
GLUquadric *myQuad;
GLdouble radius = 3.0;
GLint slices, stacks;
myQuad=gluNewQuadric();
slices = stacks = 10;
gluSphere( myQuad , radius , slices , stacks );
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
myQuad=gluNewQuadric();
gluSphere( myQuad , radius , slices , stacks );
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
myQuad=gluNewQuadric();
gluSphere( myQuad , radius , slices , stacks );
glPopMatrix(); //restore saved matrix


Again, thank you very much!

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!