Software Engineering

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();
}

Feel free to use this code for any kind of educational purposes.

This code has not been published at any kind of git or svn repository. So if you want to use or utilize it in any way, please copy paste code from the code editor.

Keep in mind that this code is created 10 years ago, using library that was popular at that time. Also, most of the code is not written in English and the logic quality is at the level of students who are being educated.

milan.latinovic

Senior PHP Engineer and Enterprise Architect at apilayer GmbH. Topics of interest: Software development, PHP, Java, Python, REST API, OpenApi, MySQL, Microservices, Integrations, Interfaces, Interoperability, Processes, Solution Architecture, LDAP, Azure

Recent Posts

Code Review Best Practices: Code reviewing and being reviewed

This article is about the code review best practices. It explains code review from the… Read More

2 years ago

What are the best Practices in REST API design

API design is an important aspect of modern software development. It allows different systems to… Read More

2 years ago

Next Industrial revolution: What is ChatGPT? Will it replace jobs?

This article sheds some light related to the question will ChatGPT or AIs in general… Read More

2 years ago

What is new in PHP 8.2: What are new features, what is deprecated?

This article provides an overview of new features and deprecations in PHP 8.2. PHP 8.0… Read More

2 years ago

Automation and AI in Software Engineering

This article is about Automation and Artificial Intelligence in Software Engineering: Experiences, Challenges, and Opportunities.… Read More

4 years ago

Enumerations in PHP 8.1 – with code example and references

PHP is getting more and more features. Enumerations in PHP are one of the latest… Read More

4 years ago