Sign in to follow this  
dannydy

Coding Question

Recommended Posts

Could anyone help me regrard the function of the special key. i have been change it to other key,but still not working,it just rotate in one direction only,even i hit down key,it also rotate some way,just increasing the speed only. Could anyone help me what's the problem. thanks #include<windows.h> #include<gl.h> #include<glu.h> #include<glut.h> #include<math.h> //rotation amounts static GLfloat xRot=0.0f; static GLfloat yRot=0.0f; //called to draw scene void DisplayScene(void) { //angle of revolution around the nucleus static float fElect1=-100.0f; //clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //red nucleus glColor3ub(0,255,255); glutWireSphere(25.0f,15,15); glRotatef(1.0f,0.0f,1.0f,1.0f); //yellow electrions glColor3ub(255,0,0); //first election orbit //save viewing transformation glPushMatrix(); //rotate by angle of revolution glRotatef(fElect1,0.0f,1.0f,0.0f); //traslate out from orgin to orbit distance glTranslatef(90.0f,0.0f,0.0f); //draw the election glutSolidSphere(6.0f,15,15); //restore the wiewing transformation glPopMatrix(); //second electron orbit glColor3ub(0,0,255); glPushMatrix(); glRotatef(3.0f,0.0f,0.0f,1.0f); glRotatef(fElect1,0.0f,0.0f,0.0f); glTranslatef(-70.0f,0.0f,0.0f); glutSolidSphere(10.0f,15,15); glPopMatrix(); //third electron orbit glColor3ub(255,0,255); glPushMatrix(); glRotatef(360.0f,-45.0f,0.0f,1.0f); glRotatef(fElect1,0.0f,1.0f,0.0f); glTranslatef(0.0f,0.0f,10.0f); glutSolidSphere(12.0f,15,15); glPopMatrix(); //increament the angle of revolution fElect1+=10.0f; if(fElect1>360.0f) fElect1=0.0f; //show the image glutSwapBuffers(); } //this function does any needed initialization on the //follow context void MyInit() { glEnable(GL_DEPTH_TEST);//hidden surface removal //black background glClearColor(0.0F,0.0F,0.0F,1.0F); } void SpecialKeys(int key,int x, int y) { if(key==GLUT_KEY_UP) xRot-=10.0f; if(key==GLUT_KEY_DOWN) yRot+=10.0f; if(key==GLUT_KEY_LEFT) yRot-=10.0f; if(key==GLUT_KEY_RIGHT) yRot+=10.0f; //refresh the window glutPostRedisplay(); } void TimerFunc(int value) { glutPostRedisplay(); glutTimerFunc(10,TimerFunc, 1); } void ChangeSize(int w, int h) { GLfloat fAspect; //prevent a divide by zero if(h==0) h=0; //set Viewport to window dimensions glViewport(0,0,w,h); //reset coordinates system glMatrixMode(GL_PROJECTION); glLoadIdentity(); fAspect= (float)w/(float)h; gluPerspective(45.0,fAspect, 1.0, 500.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f,0.0f,-250.0f); } int main(int argc, char*argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("SAMPLE 9:ATOM"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(DisplayScene); glutTimerFunc(500,TimerFunc,1); MyInit(); glutMainLoop(); return 0; }

Share this post


Link to post
Share on other sites
If you knew how to use a debugger then you would have found the answer to your question in less time than it took to write your post. Don't expect anyone to debug your code for you.

For your information, place a code breakpoint at the start of the SpecialKeys function (in DevStudio, highlight the line and press F9 - a red dot appears to the left), run the program (F5) and press the down key, the debugger will then be activated, step through the code a line at a time (F10) and see what happens. You can open windows that show the status of local variables and a window to show the status of globals (the watch window).

If you're not using DevStudio then I suggest that you do - it's debugging facilities are top notch. You can get the express version free from the MS website.

Skizz

P.S. In future, use the [ source ] or [ code] tags when posting code. You're more likely to get help if you do (since it's easier to read!)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this