C++OpenGL畫一個3d火柴人
阿新 • • 發佈:2021-01-13
用C++OpenGL實現3D簡易人@TOC
核心程式碼:
void object()
{
// lightsphere();
glRotatef(rotating_theta, 0.0, 1.0, 0.0);//rotating along the y axis
glRotatef(theta[0], 1.0, 0.0, 0.0);//bending the body
//draw the main body including chest, waist and belly
chest();
waist();
head();
belly();
glPushMatrix();//save the current matrix
glRotatef(theta[1], -1.0, 0.0, 0.0);//bending the arm
upper_arm();//draw the left upper arm
glTranslatef(0.0, LOWER_ARM_HEIGHT, 0.0);//move the bottom os the upper arm to the top of the lower arm // glRotatef(theta[2], 1.0, 0.0, 0.0);//bending the arm lower_arm();//draw the left lower arm glPopMatrix();//load the saved matrix glPushMatrix();//save the current matrix glTranslatef(-CHEST_LENGTH / 2 - LOWER_ARM_WIDTH / 2, 0, 0.0); glRotatef(theta[1], 1.0, 0.0, 0.0);//bending the arm upper_arm();//right arm glTranslatef(0.0, LOWER_ARM_HEIGHT, 0.0); //glRotatef(theta[2], 1.0, 0.0, 0.0);//bending the arm lower_arm();//right arm glPopMatrix();//load the saved matrix glPushMatrix(); glTranslatef(BELLY_LENGTH / 2 - UPPER_LEG_LENGTH / 2, -CHEST_HEIGHT / 2 - WAIST_HEIGHT - BELLY_HEIGHT, 0.0);//move the leg to the bottom left of the belly glRotatef(theta[3], 1.0, 0.0, 0.0);//bending the leg upper_leg();//left leg glTranslatef(0.0, -UPPER_LEG_HEIGHT, 0.0); glRotatef(theta[4], 1.0, 0.0, 0.0);//bending the leg lower_leg();//left leg glTranslatef(0.0, -FOOT_HEIGHT / 2 - LOWER_LEG_HEIGHT, FOOT_WIDTH / 2 - LOWER_LEG_WIDTH / 2); foot(); glPopMatrix(); glPushMatrix(); glTranslatef(-BELLY_LENGTH / 2 + UPPER_LEG_LENGTH / 2, -CHEST_HEIGHT / 2 - WAIST_HEIGHT - BELLY_HEIGHT, 0.0); glRotatef(-theta[3], 1.0, 0.0, 0.0);//bending the leg upper_leg(); glTranslatef(0.0, -UPPER_LEG_HEIGHT, 0.0); glRotatef(theta[4], 1.0, 0.0, 0.0);//bending the leg lower_leg(); glTranslatef(0.0, -FOOT_HEIGHT / 2 - LOWER_LEG_HEIGHT, FOOT_WIDTH / 2 - LOWER_LEG_WIDTH / 2); foot(); glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear the window with the color set glMatrixMode(GL_MODELVIEW);//set the modelview matrix as the current matrix glLoadIdentity();//replaces the current matrix with the identity matrix gluLookAt(1, 1, 1, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glTranslatef(0.0, 10, 0.0);//move the robot along the positive direction of y axis with 10 glColor3f(0.0, 0.0, 1.0);//use blue for the body of the robot glTranslatef(translate_distance, 0.0, 0.0);//translating the robot object(); glutSwapBuffers();
}