?? r1.cpp
字號:
/* Steven Billington
January 13, 2003
May 26, 2003 - UPDATE
RobotOGL.cpp
rod@cprogramming.com
The following program creates a window and then
uses multiple OpenGL functions to display a
animated robot constructed of different size
cubes. To compile this code you must make the
proper library links in project--->settings.
I apologize for any amount of scatterd code or
mis-formatting that printing this may occur. Please
feel free to email me at the address above for the .cpp
file.
*/
/* These are what we refer to as Pre-processor
Directives. In order for certain functions in
C++ to operate you must include certain header
files. Each header file below contains different
functions needed throughout this program.
*/
#pragma comment(linker, "/subsystem:windows")
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
/* Here we find a few global variables. While
i don't really like to use global variables,
i found them very handy for this particular
program. These variables will control angles,
fullscreen, and the global device context.
*/
HDC g_HDC;
float angle = 0.0f;
float legAngle[2] = {0.0f, 0.0f};
float armAngle[2] = {0.0f, 0.0f};
bool fullScreen = false;
/* Function: DrawCube
Purpose: As the name would suggest, this is
the function for drawing the cubes.
*/
void DrawCube(float xPos, float yPos, float zPos)
{
glPushMatrix();
glBegin(GL_POLYGON);
/* This is the top face*/
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);
/* This is the front face*/
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);
/* This is the right face*/
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);
/* This is the left face*/
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);
/* This is the bottom face*/
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
/* This is the back face*/
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();
}
/* Function: DrawArm
Purpose: This function draws the arm
for the robot.
*/
void DrawArm(float xPos, float yPos, float zPos)
{
glPushMatrix();
/* Sets color to red*/
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(xPos, yPos, zPos);
/* Creates 1 x 4 x 1 cube*/
glScalef(1.0f, 4.0f, 1.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
/* Function: DrawHead
Purpose: This function will create the
head for the robot.
*/
void DrawHead(float xPos, float yPos, float zPos)
{
glPushMatrix();
/* Sets color to white*/
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(xPos, yPos, zPos);
/* Creates 2 x 2 x 2 cube*/
glScalef(2.0f, 2.0f, 2.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
/* Function: DrawTorso
Purpose: Function will do as suggested
and draw a torso for our robot.
*/
void DrawTorso(float xPos, float yPos, float zPos)
{
glPushMatrix();
/* Sets color to blue*/
glColor3f(0.0f, 0.0f, 1.0f);
glTranslatef(xPos, yPos, zPos);
/* Creates 3 x 5 x 1 cube*/
glScalef(3.0f, 5.0f, 1.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
/* Function: DrawLeg
Purpose: Not to sound repetitve, but as suggested
this function will draw our robots legs.
*/
void DrawLeg(float xPos, float yPos, float zPos)
{
glPushMatrix();
/* Sets color to yellow*/
glColor3f(1.0f, 1.0f, 0.0f);
glTranslatef(xPos, yPos, zPos);
/* Creates 1 x 5 x 1 cube*/
glScalef(1.0f, 5.0f, 1.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
/* Function: DrawRobot
Purpose: Function to draw our entire robot
*/
void DrawRobot(float xPos, float yPos, float zPos)
{
/* Variables for state of robots legs. True
means the leg is forward, and False means
the leg is back. The same applies to the
robots arm states.
*/
static bool leg1 = true;
static bool leg2 = false;
static bool arm1 = true;
static bool arm2 = false;
glPushMatrix();
/* This will draw our robot at the
desired coordinates.
*/
glTranslatef(xPos, yPos, zPos);
/* These three lines will draw the
various components of our robot.
*/
DrawHead(1.0f, 2.0f, 0.0f);
DrawTorso(1.5f, 0.0f, 0.0f);
glPushMatrix();
/* If the arm is moving forward we will increase
the angle; otherwise, we will decrease the
angle.
*/
if (arm1)
{
armAngle[0] = armAngle[0] + 1.0f;
}
else
{
armAngle[0] = armAngle[0] - 1.0f;
}
/* Once the arm has reached its max angle
in one direction, we want it to reverse
and change direction.
*/
if (armAngle[0] >= 15.0f)
{
arm1 = false;
}
if (armAngle[0] <= 15.0f)
{
arm1 = true;
}
/* Here we are going to move the arm away
from the torso and rotate. This will
create a walking effect.
*/
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(armAngle[0], 1.0f, 0.0f, 0.0f);
DrawArm(2.5f, 0.0f, -0.5f);
glPopMatrix();
glPushMatrix();
/* If the arm is moving forward we will increase
the angle, otherwise we will decrease the
angle
*/
if (arm2)
{
armAngle[1] = armAngle[1] + 1.0f;
}
else
{
armAngle[1] = armAngle[1] - 1.0f;
}
/* Here we are going to move the arm away
from the torso and rotate. This will
create a walking effect.
*/
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(armAngle[1], 1.0f, 0.0f, 0.0f);
DrawArm(-1.5f, 0.0f, -0.5f);
glPopMatrix();
/* Now its time to rotate the legs relative to the
robots position in the world, this is the first
leg, ie the right one.
*/
glPushMatrix();
/* If the leg is moving forward we will increase
the angle; otherwise, we will decrease the
angle.
*/
if (leg1)
{
legAngle[0] = legAngle[0] + 1.0f;
}
else
{
legAngle[0] = legAngle[0] - 1.0f;
}
/* Once the leg has reached its max angle
in one direction, we want it to reverse
and change direction.
*/
if (legAngle[0] >= 15.0f)
{
leg1 = false;
}
if (legAngle[0] <= -15.0f)
{
leg1 = true;
}
/* Here we are going to move the leg away
from the torso and rotate. This will
create a walking effect.
*/
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(legAngle[0], 1.0f, 0.0f, 0.0f);
/* Time to draw the leg.
*/
DrawLeg(-0.5f, -5.0f, -0.5f);
glPopMatrix();
/* Same as above, for the left leg.
*/
glPushMatrix();
/* If the leg is moving forward we will increase
the angle, otherwise we will decrease the
angle
*/
if (leg2)
{
legAngle[1] = legAngle[1] + 1.0f;
}
else
{
legAngle[1] = legAngle[1] - 1.0f;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -