Results 1 to 2 of 2

Thread: How to position a sphere and click on it

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2006
    Location
    Norway
    Posts
    3

    How to position a sphere and click on it

    Hi!

    I have 2 questions I hope someone can answer me!

    This is my code for the drawing/rendering:
    Code :
      //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!

  2. #2
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: How to position a sphere and click on it

    Rendering objects at different locations:
    Code :
    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 matrix
    As 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

Similar Threads

  1. Position of an orbiting sphere
    By drew_jewel in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-09-2015, 01:16 PM
  2. how do i randomly position the object on left click??
    By newbiee in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-27-2014, 05:33 AM
  3. Replies: 2
    Last Post: 10-16-2012, 11:23 AM
  4. FPS game, creating a sphere/bullet on mouse click
    By PyroJing in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-26-2010, 05:35 PM
  5. Mouse click position?
    By ProfEich in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 03-11-2003, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean