Untitled
raw download clone
TEXT
views 19
,
size 6250 b
// lab4.cpp : Defines the entry point for the console application.
//




#include "stdafx.h"
#include "glew\glew.h"
#include "freeglut\freeglut.h"




const char BACKWARD_STATE = 0;
const char FORWARD_STATE = 0;
const char LEFT = 0;
const char RIGHT = 1;
char legStates[2];
char armStates[2];
float legAngles[2];
float armAngles[2];
float angle;
void DrawCube(float xPos, float yPos, float zPos)
void DrawArm(float xPos, float yPos, float zPos)
void DrawHead(float xPos, float yPos, float zPos)
void DrawTorso(float xPos, float yPos, float zPos)
void DrawLeg(float xPos, float yPos, float zPos)
void DrawRobot(float xPos, float yPos, float zPos)
void Prepare();
void Display();
void Init();
void Reshape(int Width, int Height);
void Idle();
void DrawCube(float xPos, float yPos, float zPos)
{
	glPushMatrix();
	glTranslatef(xPos, yPos, zPos);
	glBegin(GL_QUADS);
	glNormal3d(0, 1, 0);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, -1.0f);
	glVertex3f(-1.0f, 0.0f, -1.0f);
	glVertex3f(-1.0f, 0.0f, 0.0f);
	// ve mat truoc
	glNormal3d(0, 0, 1);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(-1.0f, 0.0f, 0.0f);
	glVertex3f(-1.0f, -1.0f, 0.0f);
	glVertex3f(0.0f, -1.0f, 0.0f);
	//ve mat ben phai 
	glNormal3d(1, 0, 0);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, -1.0f, 0.0f);
	glVertex3f(0.0f, -1.0f, -1.0f);
	glVertex3f(0.0f, 0.0f, -1.0f);
	//ve mat ben trai 
	glNormal3d(-1, 0, 0);
	glVertex3f(-1.0f, 0.0f, 0.0f);
	glVertex3f(-1.0f, 0.0f, -1.0f);
	glVertex3f(-1.0f, -1.0f, -1.0f);
	glVertex3f(-1.0f, -1.0f, 0.0f);
	//ve mat ben duoi 
	glNormal3d(0,-1, 0);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, -1.0f, 0.0f);
	glVertex3f(-1.0f, -1.0f, -1.0f);
	glVertex3f(-1.0f, -1.0f, -1.0f);
	//ve mat ben sau 
	glNormal3d(0, 0, -1);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(-1.0f, 0.0f, -1.0f);
	glVertex3f(-1.0f, -1.0f, -1.0f);
	glVertex3f(0.0f, -1.0f, -1.0f);
	glEnd();
	glPopMatrix();




}
void DrawArm(float xPos, float yPos, float zPos)
{
	glPushMatrix();
	glColor3f(1.0f, 0.0f, 0.0f);
	glTranslatef(xPos, yPos, zPos);
	glScalef(1.0f, 4.0f, 1.0f);
	Drawcube(0.0f, 0.0f, 0.0f);
	glPopMatrix();
}
void DrawHead(float xPos, float yPos, float zPos)
{
	glPushMatrix();
	glColor3f(1.0f, 1.0f, 1.0f);
	glTranslatef(xPos, yPos, zPos);
	glScalef(2.0f, 2.0f, 2.0f);
	Drawcube(0.0f, 0.0f, 0.0f);
	glPopMatrix();
}
void DrawTorso(float xPos, float yPos, float zPos)
{
	glPushMatrix();
	glColor3f(0.0f, 0.0f, 1.0f);
	glTranslatef(xPos, yPos, zPos);
	glScalef(3.0f, 5.0f, 2.0f);
	Drawcube(0.0f, 0.0f, 0.0f);
	glPopMatrix();
}
void Drawleg(float xPos, float yPos, float zPos)
{
	glPushMatrix();
	glColor3f(1.0f, 1.0f, 0.0f);
	glTranslatef(xPos, yPos, zPos);
	glScalef(1.0f, 5.0f, 1.0f);
	Drawcube(0.0f, 0.0f, 0.0f);
	glPopMatrix();
}
void DrawRobot(float xPos, float yPos, float zPos)
{
	glPushMatrix();
	glTranslatef(xPos, yPos, zPos);
	DrawHead(1.0f, 2.0f, 0.0f);
	DrawTorso(1.5f, 0.0f, 0.0f);
	glPushMatrix();
	//
	glTranslatef(0.0f, -0.5f, 0.0f);
	glRotatef(armAngles[LEFT], 1.0f, 0.0f, 0.0f);
	DrawArm(2.5f, 0.0f, -0.5f);
	glPopMatrix();
	//
	glPushMatrix();
	glTranslatef(0.0f, -0.5f, 0.0f);
	glRotatef(armAngles[RIGHT], 1.0f, 0.0f, 0.0f);
	DrawLeg(-0.5f, -5.0f, -0.5f);
	glPopMatrix();
	//
	glPushMatrix();
	glTranslatef(0.0f, -0.5f, 0.0f);
	glRotatef(armAngles[LEFT], 1.0f, 0.0f, 0.0f);
	DrawArm(-0.5f, -5.0f, -0.5f);
	glPopMatrix();
	//
	glPushMatrix();
	glTranslatef(0.0f, -0.5f, 0.0f);
	glRotatef(armAngles[RIGHT], 1.0f, 0.0f, 0.0f);
	DrawLeg(-1.5f, -5.0f, -0.5f);
	glPopMatrix();
}
void Prepare()
{
	for (char side = 0; side < 2; size++) {
		if (armStates[size] == FORWARD_STATE)
			armAngles[side] += 0.1f;
		else
			armAngles[size] -= 0.1f;
		if (armAngles[size] >= 15.0f)
			armAngles[side] = BACKWARD_STATE;
		else if (armAngles[size] <= -15.0f)
			armStates[size] = FORWARD_STATE;




		//
		if (legStates[size] == FORWARD_STATE)
			legAngles[size] += 0.1f;
		else
			legAngles[size] -= 0.1f;




		//
		if (legAngles[size] >= 15.0f)
			legStates[size] = BACKWARD_STATE;
		else if (legAngles[size] <= -15.0f)
			legAngles[size] = FORWARD_STATE;
	}
}
void Display() {
	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	angle = angle + 0.05f;
	if (angle >= 360.0f)
		angle = 0.0f;
	glPushMatrix();
		glRotatef(angle, 0.0f, 1.0f, 0.f);
		Prepare();
		DrawRobot(0.0f, 0.0f, 0.0f);
		glPopMatrix();
	glFlush();
	glutSwapBuffers();
	
}
void Init()
{
	glEnable(GL_DEAPTH_TEST);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	GLfloat ambientLight[] = { 0.2f,0.2f,0.2f,1.0f };
	GLfloat diffuseLight[] = { 1.0f,1.0f,1.0f,1.0f };
	GLfloat lightPos[] = { 25.0f,25.0f,25.0f,0.0f };
	GLfloat spotDir[] = { 0.0f,0.0f,0.0f,0.0f };




	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDir);
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	glCLearColor(0.0, 0.0, 0.0, 0.0);
	//
	angle = 0.0f
		armAngles[LEFT] = 0.0;
	armAngles[RIGHT] = 0.0;
	legAngles[LEFT] = 0.0;
	legAngles[RIGHT] = 0.0;
	armStates[LEFT] = FORWARD_STATE;
	armStates[RIGHT] = BACKWARD_STATE;
	legAngles[LEFT] = FORWARD_STATE;
	legAngles[RIGHT] = BACKWARD_STATE;
}
void Reshape(int Width, int Height) {
	glViewport(0, 0, (GLsizei)Width, (GLsizei)Height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (GLfloat) Width / (GLfloat)Height, 1.0, 200.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(5.0, 5.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void Idle()
{
	glutPostRedisplay();
}
int _tmain(int argc,char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUTRGB | GLUT_DEPTH);
	glutIntWindowPosition(80, 80);
	glutIntWindowSize(800, 600);
	glutCreateWindow("Robot_01");
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutIdleFunc(Idle);
	Init();
	glutMainLoop();
	return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.