PDA

View Full Version : Rotate sphere and move it???



ammykks
05-26-2009, 10:42 AM
Can any one tell me how to use gluSphere function add material,light,texture properties to it? How to move as well as rotate it in motion?

vivek vyas
05-26-2009, 10:11 PM
you not posted you source code but still(apologies beginner is sign of greatness)

to make sphere
GLUQuadric *blabla;
blabla = gluNewQuadric();
gluQuadricDrawStyle (blabla, GLenum draw);
gluQuadricNormals (blabla, GLenum normal);
gluQuadricOrientation (blabla, GLenum orientation);
gluQuadricTexture (blabla, GLboolean texture);
gluSphere(blabla,double radius,32,24);
2nd parameter up to you
you can find it on GL/glu.h
for rotation
use glRotatef(angle,x,y,z);
for motion glTranslatef(x,y,z);
for Light
glEnable(GL_LIGHT);
find function in GL/gl.h
glLightf(); and related to it
but if you post your source code much help you can get

ammykks
05-27-2009, 05:18 AM
I want spinning of sphere as well as to show moving it to a place in motion. I just see the final destination of sphere after using glTranslate(). I want the movement of the sphere seen.
Pls check the program below and let me know the changes.

#include<GL/glut.h>
#include<stdio.h>

void myinit()
{
GLfloat mat_ambient[]={0.3,0.3,0.3,1.0};
GLfloat mat_diffuse[]={0.6,0.6,0.6,1.0};
GLfloat mat_specular[]={0.9,0.9,0.9,1.0};
GLfloat mat_shininess[]={100.0};

glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambi ent);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diff use);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_spe cular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_sh ininess);

GLfloat light0_ambient[]={0.5,0.5,0.5,1.0};
GLfloat light0_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat light0_specular[]={1.0,1.0,1.0,1.0};
GLfloat light0_position[]={5.0,5.0,5.0,0.0};

glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light0_specular);
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE,light0_ambi ent);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
}

void sphere()
{
glPushMatrix();
glutSolidSphere(1.0,100,100);
glPopMatrix();
}
static GLfloat theta[]={0.0,0.0,0.0};
static GLint axis=2.0;

void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
sphere();
glFlush();
glutSwapBuffers();
}

void spinsphere()
{
theta[axis]+=10.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}

void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("TEST");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutIdleFunc(spinsphere);
myinit();
glutMainLoop();
return 0;
}

matchStickMan
05-27-2009, 08:53 PM
Add this line:

static GLfloat translate[]={0.0,0.0,0.0};

just after:
static GLint axis=2;


Then inside function SpinSphere(), add

translate[0] += 0.1; // or any value you see fit
translate[1] += 0.1; // or any value you see fit
translate[2] += 0.1; // or any value you see fit


Finally inside display(), add:

void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

/////////
glTranslatef( translate[0], translate[1], translate[2] );
////////

glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);

sphere();
glFlush();
glutSwapBuffers();
}



I hope I understood your issue properly. Let me know if it works. :)

ammykks
05-28-2009, 07:34 AM
thks 4 de code...but i'm able to control the motion...how to control it?

matchStickMan
05-28-2009, 07:08 PM
Hi ammykks,

I do not really understand your issue. What do u mean you cannot control the motion?

Because if you change the values of:

translate[0] += 0.1; // or any value you see fit
translate[1] += 0.1; // or any value you see fit
translate[2] += 0.1; // or any value you see fit


You can control the motion.

What kind of motion do you want to do?

ammykks
05-29-2009, 06:01 PM
My problem is I want the sphere to move using the arrow keys to small increments in all directions so that I can use and place them where ever I want....using just one translate() will show the final position and not the movement from initial to final position...I need to show the movement from the initial to final position using arrow keys and in small increments...even using 0.1 value I cannot stop the sphere from going out of the window screen.
And lastly as I move the sphere it should as well rotate or spin...In my code the spin() function rotates the sphere in such an axis that its not noticable...therefore I require that part of the code to be modified...

pudman
05-31-2009, 06:49 AM
You are missing a key handling function (for using the arrow keys to modifying the translation increments). The idle function (spin sphere) should then update the translation values using those translation increments. You can then add a key that zeros out the values so that the sphere stops (and does not move off screen).


.In my code the spin() function rotates the sphere in such an axis that its not noticable...

You are currently rotating in all three axes. Choose one that looks best. Also, be wary of your rotation speed because it is unknown how frequently your spinsphere function will be called.

And stop opening new threads.