OpenGL Robot Project using List

The following section will deal with the creation of a basic robot display application in opengl library in C++.

The following section also deals with the concept of the generating lists and their uses in the making of various curves.

These lists are related with the initialization of various objects or parts of another objects and using whenever they are required by calling glCallList() function of opengl library.

The working of the following application is based on the use keyboard interaction function in opengl.

These keyboard functions will be called using opengl library function glKeyboardFunc().

This following program also uses esc key for the interaction between the user and the application and also to close the application.

The following program will use basic functions of opengl library and various c++ functions to declare and define functions and variables.

Function used in OpenGL Robot Project

The functions used in the program namely: main(), reshape(), init(), display() and keyboard().

init():

This function is used to create various parts of robot and to initialize them using the opengl library functions.

For Example:

head = glGenLists(1);
body = glGenLists(1);
hand = glGenLists(1);
foot = glGenLists(1);
robot = glGenLists(1);

The following above mentioned code specifies that all the parts of the body like head body hand foot and the complete robot are genereated using list using the function glGenList().

The following above step have just specified that we will be making a list for the above mentioned parts.

For Example:

//Generating List for Head
glNewList(head, GL_COMPILE);
glPushMatrix();
glScalef(2.0f, 2.0f, 1.0f);
glutSolidCube(1.0);
glPopMatrix();
glEndList();

This line of code explains that a solid cube will be inserted or we say initialized with the list of head type.
Hence wherever we used list named head it will be making a solid cube of size ‘1.0’ and will be transformed as shown.
Similarly all types of other list will also be genereated for other parts of the robot.

display():

The definition of this function is as follows:

void display()

which means it accepts nothing as argument and returns nothing.

This function is used to draw all the parts of the robot which were initialized using the list and will be drawn on the display screen using a calling function in opengl library function glCallList().

This function also uses opengl library function named gluLookAt() which is used to change the viewport of the display window as according to the user.

This function is keyboard interaction based means it will be changed whenever the user tries to press some appropriate keys from the keyboard.

Which is further explained in the keyboard() function and its operation or changes when the user tries to press any keyboard key.

For Example:

gluLookAt(vx,vy, vz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

This above line explains that the eye cordinate in the above function is variable and which further will be changed with the keyboard keys.

This function also contains a user interacted rotate function which will dynamically change accordingly with the use of a user defined idle()

function.

This function is using the glPopMatrix() function to end the statement of the glPushMatrix() function to specify that the drawing of all the points is done.

This function is using glCallList() function which is used to call the already defined and initialized object drawing.

The syntax of this function takes one argument which is name of the already generated list above in the program.

For Example:

 // Making Foots
glPushMatrix();
glTranslatef(0.0f, 1.0f, 0.0f);
//Making and Rotating Left Foot
glPushMatrix();
glTranslatef(1.0f, 0.0f, 0.0f);
glRotatef(LeftFootRotate, 1.0, 0.0, 0.0);
//Calling callList function to draw Left Foot
glCallList(foot);
glPopMatrix();

This above code is used to make left foot of the robot in the specified position which is enabled by the translate function glTranslatef(). It is also conformed using the opengl library function glRotatef() function.

Finally with the use of glCallList() function it is calling the foot list and making it left foot configuration.

All the other parts of the robot will be made using the same configuration and making list.

We only have to keep in mind that what will be used to make a particular object or any part of the robot.

idle():

The definition of this function is as follows:

void idle();

means that it does not accept value argument and does not return any value.

This function is using some variables for the movement of the parts of the robot.

This function is ideally called within some specified time and whatever is contained inside of this function is evaluated.

This function is generally related with the movement or say the rotation of different parts and even the whole robot.

For Example:
 //Rotating Hands Left
if (isLeftHandMove)
LeftHandRotate = LeftHandRotate + 0.05;
else
LeftHandRotate = LeftHandRotate - 0.05;
if (LeftHandRotate >= 15.0)
isLeftHandMove = false;
else if (LeftHandRotate <= -15.0)
isLeftHandMove = true;

This above line of code is rotating left hand of the robot using conditional statements.

This above line is also using the boolean variable isLeftHandRotate variable to check the current state of the left hand rotate.

Similarly many conditional statements will be used for making all the parts of the robot to rotate with some specific variables.

keyboard():

This function is used for the user interation with the keyboard when the user have to enter something through the keyboard.
This function will be used when the user wants to close the window with the help of a appropriate key on the keyboard.
The user is allowed to use esc key of the keyboard.

For Example:

if (key == 27){}

This function is also related with the changing the number of connection between any two control points and they may be increasing or decreasing by pressing the appriopriate key on the keyboard.

For Example:
if (key == 'y')
{
       vy += 0.5;
}
if (key == 'Y')
{
vy -= 0.5;
}
 if (key == 'x')
{
vx += 0.5;
}
 if (key == 'X')
{
vx -= 0.5;
}
 if (key == 'z')
{
vz += 0.5;
}
 if (key == 'Z')
{
vz -= 0.5;
}
if (connection < 0) connection = 0;

glutPostRedisplay();

This line of code specifies that if ‘y’ or ‘Y’ are pressed then the viewport of the display window is changed according to the y axis.
This line of code specifies that if ‘x’ or ‘x’ are pressed then the viewport of the display window is changed according to the x axis.
This line of code specifies that if ‘z’ or ‘Z’ are pressed then the viewport of the display window is changed according to the z axis.
Everytime any of these keys is pressed it increaments or decrements some constants with the variable (global declared)

There is also a conditional statement which keeps track of the connection value if its value is negative then the connection variable is set to zero.

main():

The main function is as follows:

 cout << "Press x or X to change the viewport in x direction.\n";
cout << "Press y or Y to change the viewport in y direction.\n";
cout << "Press z or Z to change the viewport in z direction.\n";
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;

This main() is used to set the width and height of the display window initially using the library function glutInitWindowSize()  and is also used to set the initial position of the display window using the library function glutInitWindowPosition().

Many help strings are displayed on the console window for the keyboard interaction by the user.

This function also uses keyboard interaction function which is called by the library function glutKeyBoardFunc().

Application of the Program:

This program can be further implemented and can be converted to a simulator for observing various activities and performing the same.

Complete source code of Animated OpenGL Robot

//Header Files
#include <GL/glut.h>
#include
using namespace std;
//Global Variables
GLuint hand,foot,body,head,robot;
double vx = 0.50, vy = 0, vz = 10.0;
//Variables for checking the movement of the parts of the robot.
GLfloat spin = 0.0,rightFootRotate = -15.0,LeftFootRotate = 15.0,RightHandRotate = 15.0,LeftHandRotate = -15.0;

void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
	
	head = glGenLists(1);
	body = glGenLists(1);
	hand = glGenLists(1);
	foot = glGenLists(1);
	robot = glGenLists(1);

	//Generating List for Head
	glNewList(head, GL_COMPILE);
	glPushMatrix();
	glScalef(2.0f, 2.0f, 1.0f);
	glutSolidCube(1.0);
	glPopMatrix();
	glEndList();

	//Generating List for Body
	glNewList(body, GL_COMPILE);
	glPushMatrix();
	glScalef(2.5f, 5.5f, 1.0f);
	glutSolidCube(1.0);
	glPopMatrix();
	glEndList();

	//Generating List for Hand
	glNewList(hand, GL_COMPILE);
	glPushMatrix();
	glTranslatef(0.0f, -1.5f, 0.0f);
	glScalef(1.0f, 3.0f, 1.0f);
	glutSolidCube(1.0);
	glPopMatrix();
	glEndList();

	//Generating List for Foot
	glNewList(foot, GL_COMPILE);
	glPushMatrix();
	glTranslatef(0.0f, -5.0f, 0.0f);
	glScalef(1.0f, 5.0f, 1.0f);
	glutSolidCube(1.0);
	glPopMatrix();
	glEndList();
}

//Display Function 
//For Drawing the parts of Robot
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
	glLoadIdentity();  /*clear the matrix */

	/*viewing transformation*/
	gluLookAt(vx,vy, vz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

	//Rotating the object.
	glRotatef(spin, 0.0, 1.0, 0.0);
	
	// Making Foots

	glPushMatrix();
	glTranslatef(0.0f, 1.0f, 0.0f);
		//Making and Rotating Left Foot
		glPushMatrix();
		glTranslatef(1.0f, 0.0f, 0.0f);
		glRotatef(LeftFootRotate, 1.0, 0.0, 0.0);
		//Calling callList function to draw Left Foot
		glCallList(foot);
		glPopMatrix();

		//Making and Rotating Right Foot
		glPushMatrix();
		glTranslatef(-1.0f, 0.0f, 0.0f);
		glRotatef(rightFootRotate, 1.0, 0.0, 0.0);
		//Calling callList function to draw Right Foot
		glCallList(foot);
		glPopMatrix();	
	glPopMatrix();
	
	// Making Hands
	glColor3f(1.0, 0.0, 0.0);

	glPushMatrix();
	glTranslatef(0.0f, 4.5f, 0.0f);
		//Making and Rotating Left Hand
		glPushMatrix();
		glTranslatef(2.0f, 0.0f, 0.0f);
		glRotatef(LeftHandRotate, 1.0, 0.0, 0.0);
		//Calling callList function to draw Left Hand
		glCallList(hand);
		glPopMatrix();

		//Making and Rotating Right Hand
		glPushMatrix();
		glTranslatef(-2.0f, 0.0f, 0.0f);
		glRotatef(RightHandRotate, 1.0, 0.0, 0.0);
		//Calling callList function to draw Right Hand
		glCallList(hand);
		glPopMatrix();
	glPopMatrix();

	// Body
	glColor3f(0.0, 0.0, 1.0);
	glPushMatrix();
	glTranslatef(0.0f, 2.0f, 0.0f);
	//Calling callList function to draw Body
	glCallList(body);
	glPopMatrix();

	// Head
	glColor3f(0.0, 1.0, 0.0);
	glPushMatrix();
	glTranslatef(0.0f, 5.75f, 0.0f);
	//Calling callList function to draw Head
	glCallList(head);
	glPopMatrix();


	glFlush();
}

//Idle Function
void idle()
{
	static bool isLeftHandMove = true,isRightHandMove = false,isLeftFootMove = false,isRightFootMove = true;

	//Rotating Body
	spin = spin + 0.05;
	if (spin> 360.0)
		spin = 0.5;

	//Rotating Hands Left 
	if (isLeftHandMove)
		LeftHandRotate = LeftHandRotate + 0.05;
	else
		LeftHandRotate = LeftHandRotate - 0.05;

	if (LeftHandRotate >= 15.0)
		isLeftHandMove = false;
	else if (LeftHandRotate <= -15.0) isLeftHandMove = true; //Rotating Right Hand if (isRightHandMove) RightHandRotate = RightHandRotate + 0.05; else RightHandRotate = RightHandRotate + 0.05; if (RightHandRotate >= 15.0)
		isRightHandMove = false;
	else if (RightHandRotate <= -15.0) isRightHandMove = true; //Rotating Left Foot if (isLeftFootMove) LeftFootRotate = LeftFootRotate + 0.05; else LeftFootRotate = LeftFootRotate - 0.05; if (LeftFootRotate >= 15.0)
		isLeftFootMove = false;
	else if (LeftFootRotate <= -15.0) isLeftFootMove = true; //Rotating Right Foot if (isRightFootMove) rightFootRotate = rightFootRotate + 0.05; else rightFootRotate = rightFootRotate - 0.05; if (rightFootRotate >= 15.0)
		isRightFootMove = false;
	else if (rightFootRotate <= -15.0)
		isRightFootMove = true;


	glutPostRedisplay();
}

void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-10.0, 10.0, -10.0, 10.0, -20.0, 20.0);
	glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
	if (key == 'y')
	{
		vy += 0.5;
	}
	if (key == 'Y')
	{
		vy -= 0.5;
	}
	if (key == 'x')
	{
		vx += 0.5;
	}
	if (key == 'X')
	{
		vx -= 0.5;
	}
	if (key == 'z')
	{
		vz += 0.5;
	}
	if (key == 'Z')
	{
		vz -= 0.5;
	}
	
}
//Main() function
int main(int argc, char** argv)
{
	cout << "Press x or X to change the viewport in x direction.\n";
	cout << "Press y or Y to change the viewport in y direction.\n";
	cout << "Press z or Z to change the viewport in z direction.\n";
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutIdleFunc(idle);
	glutKeyboardFunc(keyboard);
	glutMainLoop();
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *