model car 3D

I have a 3D car model programmed on the code block. 4 wheels do not spin and when I move cars, the whole road also translates. I want my wheels to turn when I move. I want my road to be fixed, and just move the car, shade my car. Have a look at the code above.

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <math.h>
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
//static GLfloat spin=15.0;
//static GLfloat scale=0.0f;


GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 10.0 };
GLfloat light_position1[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat light_position2[] = { -1.0, -1.0, -1.0, 0.0 };

GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat red_light[] = {9.,0.,0.,1.};
GLfloat blue_light[] = {0.,0.,1.,1.};
GLfloat white_ambient[] = { 0.1, 0.1, 0.1, 1.0 };





//int toot=0;

void init(void)
{

 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

    glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
    glLightfv(GL_LIGHT1, GL_POSITION, light_position2);

    glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
    glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);

    glLightfv(GL_LIGHT0, GL_DIFFUSE, red_light);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, blue_light);
    glLightfv(GL_LIGHT0, GL_SPECULAR, red_light);
    glLightfv(GL_LIGHT1, GL_SPECULAR, blue_light);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_ambient);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glEnable(GL_DEPTH_TEST);
}

void Display(void)
{   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


 glMatrixMode(GL_MODELVIEW);



    glPushMatrix();
   glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);


    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-40.0f,0.0f,15.0f);
        glVertex3f(40.0f,0.0f,15.0f);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(40.0f,-15.0f,15.0f);
        glVertex3f(-40.0f,-15.0f,15.0f);
    glEnd();


    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(40.0f,0.0f,-15.0f);
        glVertex3f(40.0f,0.0f,15.0f);
        glVertex3f(40.0f,-15.0f,15.0f);
        glVertex3f(40.0f,-15.0f,-15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-40.0f,0.0f,-15.0f);
        glVertex3f(-40.0f,0.0f,15.0f);
        glVertex3f(-40.0f,-15.0f,15.0f);
        glVertex3f(-40.0f,-15.0f,-15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-40.0f,0.0f,-15.0f);
        glVertex3f(40.0f,0.0f,-15.0f);
        glColor3f(1, 1.0, 1.0);
        glVertex3f(40.0f,-15.0f,-15.0f);
        glVertex3f(-40.0f,-15.0f,-15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-40.0f,0.0f,15.0f);
        glVertex3f(-40.0f,0.0f,-15.0f);
        glVertex3f(40.0f,0.0f,-15.0f);
        glVertex3f(40.0f,0.0f,15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-40.0f,-15.0f,15.0f);
        glVertex3f(-40.0f,-15.0f,-15.0f);
        glVertex3f(40.0f,-15.0f,-15.0f);
        glVertex3f(40.0f,-15.0f,15.0f);
    glEnd();

    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-20.0f,0.0f,15.0f);
        glVertex3f(-10.0f,10.0f,15.0f);
        glVertex3f(20.0f,10.0f,15.0f);
        glVertex3f(25.0f,0.0f,15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-20.0f,0.0f,-15.0f);
        glVertex3f(-10.0f,10.0f,-15.0f);
        glVertex3f(20.0f,10.0f,-15.0f);
        glVertex3f(25.0f,0.0f,-15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-10.0f,10.0f,15.0f);
        glVertex3f(-10.0f,10.0f,-15.0f);
        glVertex3f(20.0f,10.0f,-15.0f);
        glVertex3f(20.0f,10.0f,15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-10.0f,10.0f,15.0f);
        glVertex3f(-20.0f,0.0f,15.0f);
        glVertex3f(-20.0f,0.0f,-15.0f);
        glVertex3f(-10.0f,10.0f,-15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(20.0f,10.0f,15.0f);
        glVertex3f(20.0f,10.0f,-15.0f);
        glVertex3f(25.0f,0.0f,-15.0f);
        glVertex3f(25.0f,0.0f,15.0f);
    glEnd();
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex3f(-30.0f,-15.0f,15.0f);
        glVertex3f(-30.0f,-15.0f,-15.0f);
        glVertex3f(30.0f,-15.0f,-15.0f);
        glVertex3f(30.0f,-15.0f,15.0f);
    glEnd();


    glBegin(GL_3D);
    glColor3f(1, 1, 1);
    glTranslated(-20.0f,-15.0f,15.0f);
    glutSolidTorus(2,5,5,100);



    glTranslated(0.0f,0.0f,-30.0f);
    glutSolidTorus(2,5,5,100);
    glTranslated(45.0f,0.0f,0.0f);
    glutSolidTorus(2,5,5,100);
    glTranslated(0.0f,0.0f,30.0f);
    glutSolidTorus(2,5,5,100);
    glEnd();




    glPushMatrix();
    glColor3f(3.0,4.0,5.0);
    //glRotatef(45,0,0,1);
    glScalef(10,0.3,3);

    glutWireCube(-20);


    glPopMatrix();


    glPopMatrix();
   glutSwapBuffers();

     glFlush();


}






void SpecialKeys(unsigned char key, int x, int y)
{
    switch(key)

    {
     case 'd':
          glTranslatef(-10,0.0,0.0);
          //glRotatef(spin,0,0,0);

                   //toot--;

            //Display();
            //glutIdleFunc(spinDisplay);

             glutPostRedisplay();

            break;
     case 'a':
          glTranslatef(+10,0.0,0.0);
            //toot++;
            //Display();

             glutPostRedisplay();

            break;
     case 't':
         xRot-= 10.0f;
          glutPostRedisplay();
     case 'y':
         yRot+=10.0f;
          glutPostRedisplay();

    }

}



int main(int argc, char* argv[])
{   glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("oto");
     init();
    //glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();
    glOrtho (-100, 100, -100, 100,-100, 100);
    //gluLookAt (-100, 100.0, -100.0, 100.0, -100.0, 0.0, 5.0, 1.0, 0.0);

    glutKeyboardFunc(SpecialKeys);
    glutDisplayFunc(Display);


    //gluLookAt(0., 0., 0., 1., -1., 1., 0., 1., 0.);

    glutMainLoop();
    return 0;
}

I fooled around with your code to see if I could answer your questions. You have done quite a bit of work on your own (otherwise I wouldn’t have bothered). The reason your car AND road are translating is that you are not explicitly applying the translation to the car. There is a way to do that. In fact you aren’t really applying your translation (via the ‘d’ key) to anything. By pure luck it is being applied to EVERYTHING (i.e. car and road). The code below shows how to apply the translation to the car only using a global variable and ordering the commands correctly. Note that at the beginning of my Display function I initialize all matrices using glLoadIdentity. You use PushMatrix which can get you into trouble. Did you notice that your car is sitting inside your rode? In my version it sits on top of the road surface. Also, I am rendering the road as a solid, illuminated, surface instead of a wireframe block. I know that I have not answered all of your questions. But this is a good start.

[ATTACH=CONFIG]1790[/ATTACH]

// -------------------------------------------------------------------------------------------------
// ---------------  Code posted to OpenGL forum in May 2018 and altered by Carmine. ----------------

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <GLUT/glut.h>

#define Vrt glVertex3s

int proj = 1, new_mouse = 0, mouse_rot = 1, mouse_trn = 0;

float turn = 50.0, tipp = 30.0, twst = 0.0, x_click = 0, y_click = 0, xtm = 0.0, ytm = 0.0,
      scl = 1.0, xcr = 0.0;

GLfloat lit1_spc[] = {0.20, 0.20,  0.15, 1.00},
        lit1_dfs[] = {0.70, 0.70,  0.65, 1.00},
		lit1_amb[] = {0.25, 0.25,  0.20, 1.00},
		lit1_pos[] = {0.20, 2.50,  5.20, 1.00};

GLfloat lit2_spc[] = { 0.00,  0.00,  0.00, 1.00},
        lit2_dfs[] = { 0.30,  0.30,  0.35, 1.00},
		lit2_amb[] = { 0.20,  0.20,  0.25, 1.00},
		lit2_pos[] = { 1.50, -2.00, -4.00, 0.01};

//-----------------------------------------   Keybord   --------------------------------------------

void Keybord (unsigned char ch, int x, int y)
{
    printf ("   Keyboard -> '%c' pressed.
", ch);

    switch (ch)  {
       case 'p' :  proj      = !proj;        break;
       case 'r' :  mouse_trn = 0;
                   mouse_rot = 1;            break;
       case 't' :  mouse_trn = 1;
                   mouse_rot = 0;            break;
       case 'z' :  turn = 0.0; tipp = 0.0;
                   xtm  = 0.0; ytm  = 0.0;
                   scl  = 1.0;               break;
       case '-' :  scl *= 0.92;              break;
       case '+' :  scl *= 1.06;              break;
       case 'd' :  xcr += 2.00;              break;
       case 'D' :  xcr -= 2.00;              break;

       case  27 :  exit (0);                 break;
    }

    glutPostRedisplay();
}

//--+----4----+----3----+----2----+----1----+----|----+----1----+----2----+----3----+----4----+----5
//------------------------------------------   Mouse   ---------------------------------------------

void Mouse (int button, int state, int x, int y)
{
    x_click = x;
    y_click = y;

    new_mouse = 1;
}

//--+----4----+----3----+----2----+----1----+----|----+----1----+----2----+----3----+----4----+----5
//------------------------------------------   Motion   --------------------------------------------

void Motion (int x, int y)
{
    float turn_del, tipp_del, x_del, y_del;

    static int c = 0, x_sav, y_sav;
    static float turn_save = 0.0, tipp_save = 0.0;

    // printf ("   Motion:  %5d ->   x = %5d, y = %5d.
", ++c, x, y);

    if (new_mouse)  {
       new_mouse = 0;
       x_sav = x_click;
       y_sav = y_click;
    }

    if (mouse_rot)  {

       turn_del = x - x_sav;
       turn += turn_del;

       tipp_del = y - y_sav;
       tipp += tipp_del;

    }  else if (mouse_trn)  {

       x_del = float(x - x_sav) * 0.01;
       xtm  += x_del;

       y_del = float(y - y_sav) * 0.01;
       ytm  -= y_del;

    }

    x_sav = x;
    y_sav = y;

    glutPostRedisplay();
}

//---9----8----7----6----5----4----3----2----1---++---1----2----3----4----5----6----7----8----9---\\
//---------------------------------------   Init_Lights   ------------------------------------------

void Init_Lights (void)
{
    glLightfv (GL_LIGHT1, GL_SPECULAR, lit1_spc);
    glLightfv (GL_LIGHT1, GL_DIFFUSE , lit1_dfs);
    glLightfv (GL_LIGHT1, GL_AMBIENT , lit1_amb);
    glLightfv (GL_LIGHT1, GL_POSITION, lit1_pos);

    glLightfv (GL_LIGHT2, GL_SPECULAR, lit2_spc);
    glLightfv (GL_LIGHT2, GL_DIFFUSE , lit2_dfs);
    glLightfv (GL_LIGHT2, GL_AMBIENT , lit2_amb);
    glLightfv (GL_LIGHT2, GL_POSITION, lit2_pos);

    glEnable (GL_LIGHT1);
    glEnable (GL_LIGHT2);

    glEnable (GL_AUTO_NORMAL);
    glEnable (GL_NORMALIZE  );
}

//----------------------------------------   Draw_Road   -------------------------------------------

void Draw_Road (void)
{
    glColor3f (0.30,0.25,0.30);
    glEnable (GL_LIGHTING);

    glPushMatrix();
       glTranslatef (0.0, -25.0, 0.0);
       glScalef     (10.0, 0.3,3.0);
       glutSolidCube (-20.0f);
    glPopMatrix();
}

//---------------------------------------   Render_Wheel   -----------------------------------------

void Render_Wheel (int xt, int yt, int zt)
{
    glPushMatrix();
       glTranslatef   (xt, yt, zt);
       glutSolidTorus (2,5,12,36);
    glPopMatrix();
}

//-----------------------------------------   Draw_Car   -------------------------------------------

void Draw_Car (void)
{
    glDisable (GL_LIGHTING);

    glBegin (GL_QUADS);
       glColor3f (0.3, 0.6, 0.6);
       Vrt(-40,  0, 15); Vrt( 40,  0, 15); Vrt( 40,-15, 15); Vrt(-40,-15, 15);  // left
       Vrt(-40,  0,-15); Vrt( 40,  0,-15); Vrt( 40,-15,-15); Vrt(-40,-15,-15);  // right
       glColor3f (0.4, 0.6, 0.6);
       Vrt( 40,  0,-15); Vrt( 40,  0, 15); Vrt( 40,-15, 15); Vrt( 40,-15,-15);  // back
       Vrt(-40,  0,-15); Vrt(-40,  0, 15); Vrt(-40,-15, 15); Vrt(-40,-15,-15);  // front
       glColor3f (0.4, 0.6, 0.8);
       Vrt(-40,  0, 15); Vrt(-40,  0,-15); Vrt( 40,  0,-15); Vrt( 40,  0, 15);  // trunks
       Vrt(-10, 10, 15); Vrt(-10, 10,-15); Vrt( 20, 10,-15); Vrt( 20, 10, 15);  // roof
       glColor3f (0.7, 0.7, 0.5);
       Vrt(-20,  0, 15); Vrt(-10, 10, 15); Vrt( 20, 10, 15); Vrt( 25,  0, 15);  // window l
       Vrt(-20,  0,-15); Vrt(-10, 10,-15); Vrt( 20, 10,-15); Vrt( 25,  0,-15);  // window r
       glColor3f (0.7, 0.6, 0.5);
       Vrt(-10, 10, 15); Vrt(-20,  0, 15); Vrt(-20,  0,-15); Vrt(-10, 10,-15);  // windshield
       Vrt( 20, 10, 15); Vrt( 20, 10,-15); Vrt( 25,  0,-15); Vrt( 25,  0, 15);  // rear window
       glColor3f (0.4, 0.5, 0.5);
       Vrt(-40,-15, 15); Vrt(-40,-15,-15); Vrt( 40,-15,-15); Vrt( 40,-15, 15);  // bottom
    glEnd ();


    // Render wheels using torus's with lighting on.

     glEnable     (GL_LIGHTING);
     glColor3f    (0.3, 0.3, 0.3);
     Render_Wheel (-20,-15, 15);
     Render_Wheel ( 20,-15, 15);
     Render_Wheel (-20,-15,-15);
     Render_Wheel ( 20,-15,-15);
}

//------------------------------------------   Poster's Display   ----------------------------------

void Display (void)
{
    static float x = 2.0;

    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode   (GL_PROJECTION);
    glLoadIdentity ();

    if (proj == 0)  glOrtho        (-x, x, -x, x, 0.0, 10.0);
    else            gluPerspective ( 60.0,  1.0,  1.0, 10.0);

    glMatrixMode   (GL_MODELVIEW);
    glLoadIdentity ();

    glTranslatef (xtm, ytm, -4.0);       // Place objects between near and far clipping planes.
    glRotatef    (tipp,  1, 0, 0);       // Rotate around screen horizontal axis.
    glRotatef    (turn,  0, 1, 0);       // Rotate around world    vertical axis.
    glLightfv (GL_LIGHT1, GL_POSITION, lit1_pos);
    glLightfv (GL_LIGHT2, GL_POSITION, lit2_pos);
    glScalef     (scl, scl, scl );       // Scale world relative to its origin.
    glScalef     (0.02, 0.02, 0.02);

    Draw_Road ();
    glTranslatef (-xcr, 0.0, 0.0);
    Draw_Car  ();

    glutSwapBuffers();
}

//-----------------------------------------   Init_GL   --------------------------------------------

void Init_GL (void)
{
    Init_Lights ();

    glEnable (GL_DEPTH_TEST);
    glEnable (GL_LIGHTING  );
    glEnable (GL_COLOR_MATERIAL);

    glColorMaterial (GL_AMBIENT_AND_DIFFUSE, GL_FRONT);
    glColorMaterial (GL_SPECULAR           , GL_FRONT);

    glClearColor (0.2, 0.3, 0.3, 1.0);
}

//----------------------------------------   Init_Glut   -------------------------------------------

int Init_Glut (void)
{
    glutInitDisplayMode    (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition (800, 200);
    glutInitWindowSize     (800, 800);
    glutCreateWindow       ("Carmine's Car On Road");

    glutKeyboardFunc (Keybord);
    glutDisplayFunc  (Display);
    glutMouseFunc    (Mouse  );
    glutMotionFunc   (Motion );
}

//------------------------------------------   main   ----------------------------------------------

int main (int argc, char **argv)
{
    glutInit (&argc, argv);

    Init_Glut ();
    Init_GL   ();

    glutMainLoop ();

    return (1);
}

// -------------------------------------------------------------------------------------------------


What are you fooling me, I do not know what you mean. Thank you, that project is great. At school nobody taught me that knowledge, I developed more fun (Motion), func (shade) …

I am not trying to fool you. I am trying to help you. The code I posted is a combination of your code (reformatted a little bit) and my code. All of the coordinates for the car are yours. I assigned colors to different parts of the car to make it easier to look at. Lighting is NOT applied to the car, but it is applied to the tires and the road. Do you understand why? Do you understand the changes you would have to make to apply lighting to the car? Do you understand why only the car moves in my code, while in your code everything moves when the ‘d’ key is pushed? What class or classes were you taking at school to learn about this stuff?

I also understand 1 your code section. The d image of the image of my can be pushmatrix but popmatrix not found.
I were requested to find a image in java, same as any images. C ++ it does not know many, it has many hardware, to switch to Java