📄 swing.cpp
字号:
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define SWING 1
#define BAR 2
GLfloat angle;////angle of swing
int direction;///direction of swing
void CALLBACK reshape(int width,int height);
void CALLBACK draw(void);
void CALLBACK reshape(int width,int height)
{
///set the new viewpoint size
glViewport(0,0,(GLint)width,(GLint)height);
///choose the projection matrix to be the matrix
glMatrixMode(GL_PROJECTION);
////set the projection to be the identity matrix
glLoadIdentity();
///define the dimensions of the orthographtic viewing volume
glOrtho(-50.0,50.0,-50.0,50.0,-150.0,150.0);
///choose the modelview matrix to be the matrix
glMatrixMode(GL_MODELVIEW);
}
void CALLBACK draw(void)
{
///clear the RGB buffer and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/////set the modelview matrix to be the indentity matrix
glLoadIdentity();
///rotate the bar and swing
glRotatef(60.0,0.0,1.0,0.0);
///draw the bar
glCallList(BAR);
//translate and rotate the swing to make it "swing"
glTranslatef(0.0,30.0,0.0);
glRotatef(angle,1.0,0.0,0.0);
glTranslatef(0.0,-30.0,0.0);
////draw the swing
glCallList(SWING);
///flush the bufffer to force drawing of the all objects thus bar
glFlush();
///swap the buffers
auxSwapBuffers();
///make the is swing back or forth depengding upon direction
if(direction==1)
angle++;
else
angle--;
////change the direction if 40 or -40 is reached
if(angle==40.0){
direction=1;
}
if(angle==-40.0){
direction=0;
}
}
void make_bar()
{
glNewList(BAR,GL_COMPILE);
/////////Darw the pole
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
//botom
glVertex3d(-50,30,-1);
glVertex3d(50,30,-1);
glVertex3d(50,30,1);
glVertex3d(-50,30,1);
///top
glVertex3d(-50,33,-1);
glVertex3d(50,33,-1);
glVertex3d(50,33,1);
glVertex3d(-50,33,1);
////turn the color
glColor3f(1.0f,0.65f,0.0f);
///front
glVertex3d(-50,33,1);
glVertex3d(-50,30,1);
glVertex3d(50,30,1);
glVertex3d(50,33,1);
///back
glVertex3d(50,33,-1);
glVertex3d(50,30,-1);
glVertex3d(-50,30,-1);
glVertex3d(-50,33,-1);
glColor3f(1.0,0.0,0.0);
////left
glVertex3d(-50,33,-1);
glVertex3d(-50,30,-1);
glVertex3d(-50,30,1);
glVertex3d(-50,33,1);
///right
glVertex3d(50,33,1);
glVertex3d(50,30,1);
glVertex3d(50,30,-1);
glVertex3d(50,33,-1);
glEnd();
glEndList();
}
void make_swing()
{
glNewList(SWING,GL_COMPILE);
glColor3f(1.0,0.0,1.0);
glLineWidth(5.0);
/////draw the ropes
glBegin(GL_LINES);
glVertex3d(-20,30,0);
glVertex3d(-20,-30,0);
glVertex3d(20,30,0);
glVertex3d(20,-30,0);
glEnd();
glColor3f(0.0,1.0,0.0);
///draw the sides of the bar
glBegin(GL_QUAD_STRIP);
glVertex3d(20,-30,-10);
glVertex3d(20,-33,-10);
glVertex3d(-20,-30,-10);
glVertex3d(-20,-33,-10);
glVertex3d(-20,-30,10);
glVertex3d(-20,-30,10);
glVertex3d(20,-30,10);
glVertex3d(20,-33,10);
glVertex3d(20,-30,-10);
glVertex3d(20,-33,-10);
glEnd();
glColor3f(0.0,0.0,1.0);
///draw the top and bottom of the seat
glBegin(GL_QUADS);
glVertex3d(-20,-33,-10);
glVertex3d(20,-33,-10);
glVertex3d(20,-33,10);
glVertex3d(-20,-33,10);
glVertex3d(-20,-30,-10);
glVertex3d(20,-30,-10);
glVertex3d(20,-30,10);
glVertex3d(-20,-30,10);
glEnd();
glEndList();
}
void main(int argc,char **argv)
{
///set the left corner of window to be at location(0.0)
////set the window size to be 500*500
auxInitPosition(0,0,500,500);
///initialize the RGB and Depth buffers
auxInitDisplayMode(AUX_RGB | AUX_DEPTH | AUX_DOUBLE);
///open a window, name it swing
auxInitWindow("swing");
////set the clear color to black
glClearColor(0.0,0.0,0.0,0.0);
///set the shade model
glShadeModel(GL_FLAT);
/////set the polygon mode to fill
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
///enable depth testing for hidden line removal
glEnable(GL_DEPTH_TEST);
///create the display lists
make_bar();
make_swing();
angle=-39.0;
direction=1;
////assign reshape() to be the function called whenever
///a reshape event occours
auxReshapeFunc(reshape);
///assign draw() to be the function called whenever a display
////event occours,generally after a resize or expose event
auxMainLoop(draw);
auxIdleFunc(draw);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -