// first.cpp : Defines the entry point for the console application.
//
#include <gl/glut.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
static int shoulder1 = 0;
static int shoulder2 = 0;
static int hand = 0;
static int turn1 = 0;
static int tag = 0;
static int turn = 0;//转弯
static float forward = 0;//前进
static float elbow = 0, z = 0;
int w;
int h;
int font = (int)GLUT_BITMAP_8_BY_13;
char s[30];
int frame, timeOwn, timebase = 0;
//光照 使用光源
GLfloat lightPos[] = { 1.0f, 0.0f, -1.0f, 0.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, -1.0f };//反射光
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };//a
GLfloat ambientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };//环绕光
GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f };
GLboolean bEdgeFlag = 1;
void resetPerspectiveProjection();
void setOrthographicProjection();
//设置背景
void SetupRC(void)
{
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 20.0f);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
glMateriali(GL_FRONT, GL_SHININESS, 8);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
//场景
glBegin(GL_QUADS);
glColor3ub(0, 32, 0);
glVertex3f(8.0f, -3.0f, -4.0f);
glVertex3f(-8.0f, -3.0f, -4.0f);
glColor3ub(255, 255, 255);
glVertex3f(-8.0f, -3.0f, 4.0f);
glVertex3f(8.0f, -3.0f, 4.0f);
glEnd();
//运动
glPushMatrix();//202
glTranslatef(forward, 0.0, z);//前进
glRotatef((GLfloat)turn, 0.0, 1.0, 0.0);
//阴影
glPushMatrix();
glColor3f(0, 0, 0);
glTranslatef(0, -3, 0);
glScalef(2, 1, 3);
glutSolidSphere(1.0f, 30, 30);//
glTranslatef(0, 0, -1);
glScalef(0.5, 0.5, 0.5);
glutSolidSphere(1.0f, 30, 30);
glPopMatrix();
glColor3f(0, 255, 0);
//右腿
glTranslatef(1, -0.1, -2.0);//右1大腿
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glTranslatef(0.0, -0.5, 0.0);
glRotatef((GLfloat)elbow, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();//右小腿
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(forward, 0.0, z);//前进
glRotatef((GLfloat)turn, 0.0, 1.0, 0.0);
//Something();
glTranslatef(1, -0.1, 0.0);//右2大腿
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glTranslatef(0.0, -0.5, 0.0);
glRotatef((GLfloat)elbow, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();//右小腿
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(forward, 0.0, z);//前进
glRotatef((GLfloat)turn, 0.0, 1.0, 0.0);
//Something();
glTranslatef(1, -0.1, 2.0);//右3大腿
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glTranslatef(0.0, -0.5, 0.0);
glRotatef((GLfloat)elbow, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();//右小腿
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glPopMatrix();
//左腿
glPushMatrix();//235
glTranslatef(forward, 0.0, z);
glRotatef((GLfloat)turn, 0.0, 1.0, 0.0);
glTranslatef(-1, 0.4, -2.0);//左1大腿
glRotatef((GLfloat)shoulder1, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glTranslatef(0.0, -0.5, 0.0);//左小腿
glRotatef((GLfloat)elbow, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix();
glPushMatrix();//235
glTranslatef(forward, 0.0, z);
glRotatef((GLfloat)turn, 0.0, 1.0, 0.0);
glTranslatef(-1, 0.4, 0.0);//左2大腿
glRotatef((GLfloat)shoulder1, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glTranslatef(0.0, -0.5, 0.0);//左小腿
glRotatef((GLfloat)elbow, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix();
glPushMatrix();//235
glTranslatef(forward, 0.0, z);
glRotatef((GLfloat)turn, 0.0, 1.0, 0.0);
glTranslatef(-1, 0.4, 2);//左3大腿
glRotatef((GLfloat)shoulder1, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0); //
glPopMatrix();
glTranslatef(0.0, -0.5, 0.0);//左小腿
glRotatef((GLfloat)elbow, 1.0, 0.0, 0.0);
glTranslatef(0.0, -0.5, 0.0);
glPushMatrix();
glScalef(0.1, 1.0, 0.1);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(forward, 0.0, z);
glRotatef((GLfloat)turn, 0.0, 1.0, 0.0);
glTranslatef(0.0, 1.0, 0.0);//躯干
glPushMatrix();
glScalef(2, 1, 3);
glutWireSphere(1.0f, 30, 30);//
glPopMatrix();
glTranslatef(0, 0, -3);//头
glPushMatrix();
glScalef(0.5, 0.5, 0.5);
glutWireSphere(1.0f, 30, 30); //
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w1, int h1)
{
w = w1;
h = h1;
glViewport(0, 0, (GLsizei)w1, (GLsizei)h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w1 / (GLfloat)h1, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -8.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'w'://向后走
turn1 = turn;
forward = forward - 0.04*sin((GLfloat)turn1 / 360 * 3.14 * 2);
z = z - 0.05*cos((GLfloat)turn1 / 360 * 3.14 * 2);
if (tag == 0) {
shoulder1 = (shoulder1 + 1);
shoulder2 = (shoulder2 - 1);
if (shoulder1 >= 0) { elbow = elbow - 1.2; }
else { elbow = elbow + 1.2; }
}
else
{
shoulder1 = (shoulder1 - 1);
shoulder2 = (shoulder2 + 1);
if (shoulder1 >= 0) { elbow = elbow + 1.25; }
else { elbow = elbow - 1.2; }
}
if (shoulder1 > 30) {
tag = 1;
}
if (shoulder1 < -30) {
tag = 0;
}
glutPostRedisplay();
break;
case 's'://向前走
turn1 = turn;
forward = forward + 0.04*sin((GLfloat)turn1 / 360 * 3.14 * 2);
z = z + 0.05*cos((GLfloat)turn1 / 360 * 3.14 * 2);
if (tag == 0) {
shoulder1 = (shoulder1 - 1);
shoulder2 = (shoulder2 + 1);
}
else
{
shoulder1 = (shoulder1 + 1);
shoulder2 = (shoulder2 - 1);
}
if (shoulder1 > 30) {
tag = 0;
}
if (shoulder1 < -30) {
tag = 1;
}
glutPostRedisplay();
break;
case 'd'://右转
turn = (turn - 5) % 360;
glutPostRedisplay();
break;
case 'a'://左转
turn = (turn + 5) % 360;
glutPostRedisplay();
break;
case 't':
glLoadIdentity(); //加载单位矩阵
gluLookAt(0.0, 10.0, 0.0, 0, 0, 0, 1.0, 0.0, 0);
break;
case 27:
exit(0);
break;
default:
break;
}
}
//设置视觉角度
void setOrthographicProjection()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glScalef(1, -1, 1);
glTranslatef(0, -h, 0);
glMatrixMode(GL_MODELVIEW);
}
void resetPerspectiveProjection()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
//主函数
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("昆虫 wsad移动");
SetupRC();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
wasd移动
t切换视角
有帮助给我点个赞,或者去gitee给我stargitee