Thread: Need Help with OpenGL

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Unhappy Need Help with OpenGL

    Hi,
    I'm starting to create a car model with OpenGL and I have to rotate four wheels but I have a problem with that. This is my code:
    Code:
    // proba352.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    #include <gl/glut.h>
    static int a=0;
    //float b=0;
    
    GLfloat sideCar[] = {0.05, 0.09, 0.19,  
    					0.04, 0.19, 0.19,
    					0.09, 0.22, 0.19,
    					0.12, 0.225, 0.19,
    					0.19, 0.23, 0.19,
    					0.30, 0.34, 0.19,
    					0.56, 0.34, 0.19,
    					0.69, 0.19, 0.19,
    					0.68, 0.09, 0.19, 
    	
    					0.05, 0.09, -0.19, 
    					0.04, 0.19, -0.19,
    					0.09, 0.22, -0.19,
    					0.12, 0.225, -0.19,
    					0.19, 0.23, -0.19,
    					0.30, 0.34, -0.19,
    					0.56, 0.34, -0.19,
    					0.69, 0.19, -0.19,
    					0.68, 0.09, -0.19}; 
    				
    	
    GLubyte sideVerticesL[] = {8,7,6,5,4,3,2,1,0};
    GLubyte sideVerticesR[] = {9,10,11,12,13,14,15,16,17};
    GLubyte midVertices[] = {0,9,10,1,
    					1,10,11,2,
    					2,11,12,3,
    					3,12,13,4,
    					4,13,14,5,
    					5,14,15,6,
    					6,15,16,7,
    					7,16,17,8};
    GLubyte sideVertices[]= {10,16,17,9,
    						7,1,0,8	};
    
    
    
    void init(void)
    {
    	
    	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    	glClearColor(0.0, 0.0, 0.9, 1.0);
    	
    }
    
    
    
    void display(void) {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
    	
    	
    	glEnableClientState(GL_VERTEX_ARRAY);
    	glColor3f(1.0, 0.0,0.0);
    	
    	for (int i=0; i<20; i++)
    	{a+=8;//b+=2;
    	glPushMatrix();
    		glPushMatrix();		
    			glVertexPointer(3, GL_FLOAT, 0, sideCar);
    			glDrawElements(GL_POLYGON, 9, GL_UNSIGNED_BYTE, sideVerticesL);
    			glDrawElements(GL_POLYGON, 9, GL_UNSIGNED_BYTE, sideVerticesR);
    			glFrontFace(GL_CW);
    			glDrawElements(GL_QUADS, 32, GL_UNSIGNED_BYTE, midVertices);
    			glDrawElements(GL_QUADS, 8, GL_UNSIGNED_BYTE, sideVertices);
    			
    		glPopMatrix();
    //glTranslatef(0,0,5);
    			
    			glTranslatef(-4,-2.05,5.2);
    			glRotatef((GLfloat) a,0.0,0.0,1.0);
    			glTranslatef(4,2.05,-5.2);
    			glPushMatrix();
    			glutWireTorus(0.09,0.18,18,8);
    			glPopMatrix();
    
    			glTranslatef(-4.5,-3.5,9);
    			glRotatef((GLfloat) a,0,0,1);
    			glTranslatef(4.5,3.5,-9);
    			glPushMatrix();
    			glutWireTorus(0.2,0.4,18,8);
    			glPopMatrix();
    
    
    		//glTranslatef(0.8,-0.3,2);
    	/*glPushMatrix();
    	glTranslatef(-2.5,-2.3,7.2);//glRotatef(a,0,0,1);//glTranslatef(4.4,2.3,-1.2);
    			glutWireTorus(0.09,0.18,18,8);
    			
    		glTranslatef(-2,-1,3);//glRotatef(a,0,0,1);//glTranslatef(1.6,0,0);
    		glutWireTorus(0.09,0.18,18,8);
    			
    				
    		glPopMatrix();*/
    	glPopMatrix();
    
    	glutSwapBuffers();
    	}
    	
    }
    
    
    void Resize(int width, int height)
    {
    	if(height==0) height=1;
    	glViewport(0,0,width,height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(45.0,(GLdouble)width/height,1.0,200.0);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	gluLookAt(2.0,1.0,-2.5,0.0,0.0,0.0,0.0,1.0,0.0);
    }
    
    void keyboard(unsigned char key,int x, int y)
    {
    	switch (key)
    	{
    	case 'a':
    		a = (a+5)%360;
    		glutPostRedisplay();
    		break;
    	case 'A':
    		a = (a-5)%360;
    		glutPostRedisplay();
    		break;
    	default:
    		break;
    	}
    }
    int main(int argc, char** argv) 
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    	glutInitWindowSize(600, 600);
    	glutInitWindowPosition(100, 100);
    	glutCreateWindow("Car");
    	init();
    	glutDisplayFunc(display);
    	glutReshapeFunc(Resize);
    	glutKeyboardFunc(keyboard);
    	glEnable(GL_DEPTH_TEST);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(-5.0,5.0,-5.0,5.0,-5.0,5.0);
    	glRotatef(5.0,1.0,0.0,0.0);
    	glMatrixMode(GL_MODELVIEW);
    	glutMainLoop();
    	return 0;
    }
    Pls, can someone told me how to rotate the wheels.
    Thank you.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    it seems the variable you are using to rotate isn't consistent.. you use a int then sometimes tell it to be a (GLfloat) and sometimes don't.... why not just name it a GLfloat to begin with?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    No, it's not that. They rotated, but not in straight line, but in arc

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    if that is the case it seems you need to look at your glTranslatef.. this will cause you to rotate around whatever point you are translating to.. in a sense the Translation becomes the "pivot point" of your object.. imagine if the pivot point wasn't the center of the object it would start to create elliptical rotations such as that of a planet. Earth rotates around the sun making the sun its pivot point. It also rotates around itself at a point which is not perfectly the center of the planet which creates its wobbles. If you rotate around the center of your wheel it will give the impression of rotating in place. If you rotate on say .02f from the center you will gain a small ellipse and so on. Hope this helps ;o).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-02-2010, 01:26 PM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM