3D Robot in C++, 10 years ago :)

Few days ago I stumbled upon an archive folder related to university days. I did a quick digging and found this 3D Robot in C++, written with a help of GLUT library. #nostalgia

3D Robot in C++, written with my friend Lucic Mirko, 10+ years ago 😀
Robot has hammer or suitcase, activated with Z or X tasters.
If you right click with mouse, there will be a navigation to rotate, translate or simply move robot across the board.

At that time, we had a great professor of Computer Graphic, dr. Patricio Bulic. He provided us with first insights to C++ libraries for computer graphic and how to use them.

Looking at it after 10 years, it is a small and simple code, but at that point where we were starting out, this was very important for us.

This is a complete source code (comments are in Serbian, I didn’t want to translate anything to keep the original “source code”). I also didn’t want to cleanup dirty comment lines or anything. It is as it was.

Complete source code (with executable file and complete program) you can download at my Patreon page.

///-----------------------------------------------------------------------------
/// Elektrotehnicki fakultet
/// Banjaluka
/// 
/// Predmet : Racunarska Grafika
/// Profesor : dr Patricio Bulic
///
/// Studenti:
/// Latinovic Milan
/// Lucic Mirko
///
/// Seminarski rad : Robot
///----------------------------------------------------------------------------- 

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

#define rotiraj 1
#define transliraj 2
#define pomjerajRobota 3


float zoom = 0.0f;
float rotx = 0.0;
float roty = 0.00f;
float tx = 0;
float ty = 0;
int lastx=0;
int lasty=0;
unsigned char Buttons[3] = {0};
int k1,k2,w;

///-----------------------------------------------------------------------------
// Podesavanja robota
    float ugao=0,ugao2=0,x=0,y=0;
    float brzina=0;
    int predmet=0,granica=0,r,xr,yr,zr;
///-----------------------------------------------------------------------------


///-----------------------------------------------------------------------------
/// Funkcija za aktiviranje stavki menija
/// 
void processMenuEvents(int option) {

	switch (option) {
		case rotiraj : 
			r=1; 
            break;
		case transliraj : 
			r=0;  
            break;
		case pomjerajRobota :
            r=2; 
            break;
	}
}

///-----------------------------------------------------------------------------
/// Funkcija za kreiranje glut menija
///
void createGLUTMenus() {

	int menu;
	menu = glutCreateMenu(processMenuEvents);
	
	// kreiranje stavki menija
	glutAddMenuEntry("Rotiranje",rotiraj);
	glutAddMenuEntry("Transliranje",transliraj);
	glutAddMenuEntry("Pomjeraj Robota",pomjerajRobota);
		
	// postavljanje menija na desno dugme misa
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

///-----------------------------------------------------------------------------
/// Postavljanje pocetne scene
///
void Init() 
{
	glEnable(GL_DEPTH_TEST);
	
	
    //
    //   Osvjetljenje
	//
    {
         GLfloat pozicija[] = {0.0, 10.0, 20.0, 1.0};
         GLfloat difuzija[] = {1.0, 0.0, 0.0, 1.0};
         GLfloat zrcalna[] = {0.5, 0.0, 0.0, 0.0};
         GLfloat ambijent[] = {0.1, 0.1, 0.1, 1.0};
         glEnable(GL_LIGHTING);
         glEnable(GL_LIGHT0);
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();
         glLightfv(GL_LIGHT0, GL_POSITION, pozicija);
         glLightfv(GL_LIGHT0, GL_DIFFUSE, difuzija);
         glLightfv(GL_LIGHT0, GL_SPECULAR, zrcalna);
         glLightfv(GL_LIGHT0, GL_AMBIENT, ambijent);
	}
	
    //
	//	 Materiali
	//
	{
         GLfloat pozicija[] = {1.0, 1.0, 1.0, 1.0};
         GLfloat difuzija[] = {0.6, 0.0, 0.0, 1.0};
         GLfloat zrcalna[] = {1.0, 1.0, 1.0, 1.0};
         GLfloat ambijent[] = {0.0, 0.0, 0.0, 1.0};
         GLfloat sijaj = 100.0;
         glMaterialfv(GL_FRONT, GL_POSITION, pozicija);
         glMaterialfv(GL_FRONT, GL_DIFFUSE, difuzija);
         glMaterialfv(GL_FRONT, GL_SPECULAR, zrcalna);
         glMaterialf(GL_FRONT, GL_SHININESS, sijaj);
	}
}    
    

///-----------------------------------------------------------------------------
/// Funkcija koja crta robota + animacija
///
void crtajRobota() {
      

// zapamti koordinatni pocetak
glPushMatrix();


    // :Pocetak crtanja
    glTranslatef(0.0,8.7,0.0);
    glPushMatrix();
            
            // Crtamo glavu sa ocima
            glPushMatrix();
                glRotatef(ugao,0,1,0);               //animacija glave
                glutSolidSphere(0.8,5,5);            //glava
                glTranslatef(0.4,0.2,0.6);
                glutSolidSphere(0.2,10,10);          //oko1
                glTranslatef(-0.8,0.0,0.0);
                glutSolidSphere(0.2,10,10);          //oko2
            glPopMatrix();
            
            // Crtamo vrat
            glTranslatef(0.0,-0.9,0.0);
            glRotatef(-90.0,1.0,0.0,0.0);
            glutSolidCone(0.5,1,20,20);              //crtaj vrat
            glRotatef(90.0,1.0,0.0,0.0);             // vracanje rotacije
            
            // Crtamo centralni dio tjela
            glTranslatef(0.0,-1.35,0.0);
            glPushMatrix();                          // animiranje centralnog dijela tjela
                glRotatef(ugao,0,0,1);
                glutSolidOctahedron();
            glPopMatrix();
            
            //crtaj tijelo kao torus
            glutSolidTorus (0.60, 0.95, 5, 10);
            
            //crtaj desnu ruku
            glTranslatef(1.1,0.9,0.0);
            glPushMatrix();                          
                glRotatef(ugao,1,0,0);               // rotiranje desnog ramena
                glutSolidSphere(0.4,100,100);        // crtaj desno rame
                glTranslatef(0.8,-0.45,0);
                glRotatef(10.0,0,0,1);               // zakrivi ruku po z

                glPushMatrix();
                    glScalef(1.0, 2.5, 1.0);
                    glutSolidSphere(0.4,100,100);    // crtaj nadlakticu
                glPopMatrix();
                
                glTranslatef(0.0,-0.8,0.0); 
                glRotatef(-ugao2,1,0,0);             // rotiraj lakat
                glutSolidSphere(0.4,100,100);
                glTranslatef(0.0,-0.7,0.0);
                
                glPushMatrix();
                    glScalef(1.0, 2.0, 1.0);
                    glutSolidSphere(0.4,100,100);    // crtaj podlakticu
                glPopMatrix();
                
            glPopMatrix();   
            
            //crtaj lijevu ruku
            glTranslatef(-2.2,0.0,0.0);
            
            glPushMatrix();
                glRotatef(-ugao,1,0,0);
                glutSolidSphere(0.4,100,100);
                glTranslatef(-0.8,-0.45,0);
                glRotatef(-10.0,0,0,1);
                
                glPushMatrix();
                    glScalef(1.0, 2.5, 1.0);
                    glutSolidSphere(0.4,100,100);
                glPopMatrix();
                
                glTranslatef(0.0,-0.8,0.0);   
                glRotatef(-ugao2,1,0,0);  
                glutSolidSphere(0.4,100,100);
                glTranslatef(0.0,-0.7,0.0);
                
                glPushMatrix();
                    glScalef(1.0, 2.0, 1.0);
                    glutSolidSphere(0.4,100,100);
                glPopMatrix();
                
                if(predmet == 1) {
                    glTranslatef(0.0,-1.3,0.0);
                    glScalef(0.8, 1.5, 2.3);
                    glutSolidCube(1.0);
                }
                if(predmet==3){
                glTranslatef(0.0,-0.8,1.3);
                
                glPushMatrix();
                    glScalef(0.3,0.3,4.0);
                    glutSolidCube(1.0);
                glPopMatrix();
                
                glTranslatef(0.0,0.0,2.0);
                glRotatef(90.0,0,1,0);
                glScalef(1.0,1.6,1.0);
                glutSolidCube(1.0);
                }
                 if(predmet==2){
                glTranslatef(0.3,-0.8,1.9);
                glRotatef(90.0,0,0,1);
                
                glPushMatrix();
                    glScalef(0.3,0.3,4.0);
                    glutSolidSphere(1.0,100,100);
                glPopMatrix();
                
                glTranslatef(0.0,0.0,-0.7);
                glRotatef(90.0,0,1,0);
                glScalef(1.0,3.0,1.0);
                glutSolidCube(0.5);
                }
              
                
            glPopMatrix();    
    
    //crtaj pojas
    glPopMatrix();
    glTranslatef(0.0,-3.5,0.0);

    glRotatef(ugao/2,0,1,0);                     // rotira pojas sa nogama
    
    glPushMatrix();
        glRotatef(45.0,0,1,0);
        
        glutSolidOctahedron();
        
    glPopMatrix();
    
    //crtaj desnu nogu
    glTranslatef(0.5,-0.6,0.0);
    
    glPushMatrix();
        glRotatef(-ugao,1,0,0);                  // rotiraj desni kuk
        glutSolidSphere(0.4,100,100);            // crtaj kuk
        glTranslatef(0.35,-1.0,0);
        glRotatef(15.0,0,0,1);                   // zakrivi nogu
            
            glPushMatrix();
                glScalef(1.5, 2.5, 1.0);
                glutSolidSphere(0.4,100,100);    // crtaj nadkoljenicu
            glPopMatrix();
            
        glTranslatef(0.0,-1.1,0.0);  
        glRotatef(ugao2,1,0,0); 
        glutSolidSphere(0.4,100,100);            // crtaj koljeno
        glTranslatef(0.0,-0.95,0.0);
            
            glPushMatrix();
                glScalef(1.2, 3.0, 1.0);
                glutSolidSphere(0.4,100,100);    // crtaj podkoljenicu
            glPopMatrix();
            
        glPushMatrix();
            glTranslatef(0.0,-1.2,0.4);
            glScalef(1.5, 1.0, 3.0);
            glutSolidCube(0.5);                  // crtaj cipelu
        glPopMatrix();
    
    glPopMatrix();    

    //crtaj lijevu nogu
    glTranslatef(-1.0,-0.0,0.0);
    
    glPushMatrix();
    
        glRotatef(ugao,1,0,0);                   
        glutSolidSphere(0.4,100,100);
        glTranslatef(-0.35,-1.0,0);
        glRotatef(-15.0,0,0,1);
            
            glPushMatrix();
                glScalef(1.5, 2.5, 1.0);
                glutSolidSphere(0.4,100,100);
            glPopMatrix();
            
        glTranslatef(0.0,-1.1,0.0);  
        glRotatef(ugao2,1,0,0);  
        glutSolidSphere(0.4,100,100);
        glTranslatef(0.0,-0.95,0.0);
            
            glPushMatrix();
                glScalef(1.2, 3.0, 1.0);
                glutSolidSphere(0.4,100,100);
            glPopMatrix();
            
        glTranslatef(0.0,-1.2,0.4);
        glScalef(1.5, 1.0, 3.0);
        glutSolidCube(0.5);
    
    glPopMatrix(); 
 
// vracamo koordinatni pocetak
glPopMatrix();     
}

///-----------------------------------------------------------------------------
/// Display funkcija
///
void display() {
	
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    
	glMatrixMode(GL_MODELVIEW);	
	glLoadIdentity();	
	 
	gluLookAt( 0, 10, 20,
               0, 4, 0,
               0, 1, 0);

	glTranslatef(0,0,-zoom);
	glTranslatef(tx,ty,0);
	glRotatef(rotx,1,0,0);
	glRotatef(roty,0,1,0);	
    
    //// Sfera za testiranje
//    glPushMatrix();
//    glTranslatef(0,10,30);
//    glutSolidSphere(2.0,100,100);
//    glPopMatrix();
	
    // crtaj grid
	glBegin(GL_LINES);
	for(int i=-20;i<=20;++i) {
		glVertex3f(i,0,-20);
		glVertex3f(i,0,20);

		glVertex3f(20,0,i);
		glVertex3f(-20,0,i);
	}
	glEnd();
	
	/* crtaj koordinantni sistem
	
      glBegin(GL_LINES);
      glColor3f(0.0, 0.0, 1.0);
      glVertex3f(0.0, 0.0, 0.0);
      glVertex3f(0.0, 0.0, 20.0);
      glColor3f(0.0, 1.0, 0.0);
      glVertex3f(0.0, 0.0, 0.0);
      glVertex3f(0.0, 20.0, 0.0);
      glColor3f(1.0, 0.0, 0.0);
      glVertex3f(0.0, 0.0, 0.0);
      glVertex3f(20.0, 0.0, 0.0);
      glEnd();
     
     */

    //crtaj robota
    glTranslatef(xr,yr,zr);    
    crtajRobota();
   
    glFlush();
	glutSwapBuffers();
	
    if(x) {
        ugao-=brzina;
        if(ugao < -granica) x=0;
    }   
    else  {
        ugao+=brzina;
        if(ugao > granica)
        x=1;
    }
    
    if(y) {
        ugao2-=brzina;
        if(ugao2 < 0) y=0;
    }   
    else {
        ugao2+=brzina;
        if(ugao2 >granica) y=1;
    }
    w=w+5;
    
    
}

///-----------------------------------------------------------------------------
/// Reshape funkcija
///
void reshape(int w, int h)
{
	// Da se izbjegne greska djeljenja sa nulom
	if(w==0) h = 1;

	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45,(float)w/h,0.1,200);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

///-----------------------------------------------------------------------------
/// Funkcija za kontrolu tastature
///
void inputKey(int key, int x, int y) {


	switch (key) {
		case GLUT_KEY_LEFT : {
             if(r==1)
            roty-=3;
             else if(r==0)
             tx--;
             else
             xr++;}
			break;
		case GLUT_KEY_RIGHT :{
             if(r==1)
             roty+=3;
             else if(r==0)
             tx++; 
             else 
             xr--;}
			break;
		case GLUT_KEY_UP :{
             if(r==1)
             rotx-=3;
             else if(r==0)
             ty++;
             else 
             zr++;} 
			break;
		case GLUT_KEY_DOWN :{
            if(r==1)
            rotx+=3;
            else if(r==0)
            ty--; 
            else
            zr--;}
			break;
	}
}

///-----------------------------------------------------------------------------
/// Proces Normal Keys funkcija
///
void processNormalKeys(unsigned char key, int x, int y) {

	if (key == 27) exit(0);
	else if ((key == 'a') || (key == 'A')) zoom+=1;
	else if ((key == 'q') || (key == 'Q')) zoom-=1;
	else if ( key == '1'){ brzina = 2; granica=20;}
	else if ( key == '2'){ brzina = 5; granica=30;}
	else if ( key == '3'){ brzina = 8; granica=40;}
	else if ( key == '4'){ brzina = 10; granica=50;}
	else if ( key == '5') brzina = 0;
	else if ( key == 'z') predmet = 3;
	else if ( key == 'x') predmet = 1;
			
	}



///-------------------------------------------------------------------------------
/// Glavna izvrsna funkcija
///
int main(int argc,char** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,100);
	glutCreateWindow("Racunarska Grafika, Seminarski Rad \"Robot\"");
		
	glutSpecialFunc(inputKey);
	glutKeyboardFunc(processNormalKeys);

	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutIdleFunc(display);
    createGLUTMenus();

	Init();
    
	glutMainLoop();
}
Leave a Comment