📄 opengl cylinder.txt
字号:
void Finger::SetPhase(GLfloat phs)
{
if (next!=NULL) next->SetPhase(phs);
if (phs<0.0) phs=-phs;
if (phs>2.0)
{
phase=phs-floor(phs);
phase+=(GLfloat)(((unsigned long)((floor(phs)))%2));
}
else phase=phs;
phase=(GLfloat)(fabs(sin(phase*3.141592654))*(double)threshold);
}
void Finger::draw(void)
{
GLfloat d2;
glPushMatrix();
if (next!=NULL) d2=next->get_diam();
else d2=d;
glRotatef(phase,0,0,1);
glutSolidSphere(d,10,10);
glPushMatrix();
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
gluCylinder(q,d,d2,l,10,1);
glPopMatrix();
glTranslatef(l,0,0);
if (next==NULL) glutSolidSphere(d2,10,10);
if (next!=NULL) next->draw();
glPopMatrix();
}
class Palm
{
private:
GLfloat phase,threshold;
GLUquadricObj *q;
public:
Finger *F[5];
Palm(GLfloat Threshold);
~Palm();
Finger *operator[](int f);
void SetPhase(GLfloat phs);
void draw();
};
Palm::Palm(GLfloat Threshold)
{
threshold=Threshold;
phase=0;
q=gluNewQuadric();
gluQuadricDrawStyle(q,GLU_FILL);
gluQuadricNormals(q, GLU_SMOOTH);
F[0]=new Finger(4,20-2,85);
F[0]->Add(3.8,16-3,110);
F[0]->Add(3.2,10,75);
F[0]->Add(3,0,90);
F[1]=new Finger(4,22-2,70);
F[1]->Add(3.9,20,115);
F[1]->Add(3.3,10,85);
F[1]->Add(3,1,90);
F[2]=new Finger(4,20-2,85);
F[2]->Add(3.8,16-3,110);
F[2]->Add(3.2,10,75);
F[2]->Add(3,0,90);
F[3]=new Finger(4,16-2,100);
F[3]->Add(3.3,12-3,85);
F[3]->Add(2.7,6,75);
F[3]->Add(2.5,0,90);
F[4]=new Finger(4+2,16-3,30);
F[4]->Add(3.3+2,12-2,95);
F[4]->Add(2.7+2,6,75);
F[4]->Add(2.5+1,0,90);
}
void Palm::SetPhase(GLfloat phs)
{
if (phs<0.0) phs=-phs;
if (phs>2.0)
{
phase=phs-floor(phs);
phase+=(GLfloat)(((unsigned long)((floor(phs)))%2));
}
else phase=phs;
phase=(GLfloat)(fabs(sin(phase*3.141592654))*(double)threshold);
}
Finger *Palm::operator[](int f)
{
return(F[f]);
}
void Palm::draw()
{
int i;
glPushMatrix();
glRotatef(phase,0,0,1);
glPushMatrix();
glScalef(1,0.3,1);
glPushMatrix();
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glTranslatef(0,0,-16);
gluCylinder(q,14,19.4,32,20,1);
glPopMatrix();
//glutSolidCube(16);
glPopMatrix();
glPushMatrix();
glTranslatef(16,0,0);
glScalef(0.3,0.3,1);
glutSolidSphere(19,20,20);
glPopMatrix();
//glTranslatef(22,0,-14.5);
glTranslatef(18,0,-13);
//glRotatef(8,0,1,0);
for (i=0;i<4;i++)
{
glPushMatrix();
glTranslatef(0,0,9*i);
glRotatef(8-6*i,0,1,0);
//glTranslatef(0,0,9*i);
F[i]->draw();
glPopMatrix();
}
glTranslatef(-22,2,2);
glRotatef(55,0,1,0);
glRotatef(30,0,0,1);
glScalef(1,0.8,1);
F[4]->draw();
glPopMatrix();
}
Palm *P;
void init_shapes()
{
P=new Palm(90);
}
//-------------------------------------------------------------
// This function is hooked to OpenGL toolkit library
// OpenGL calls this procedure every time it wants to
// refresh it's drawing on the screen
void display(void)
{
int i;
GLfloat Unlit[] = {0.0, 0.0, 0.0, 1.0};
GLfloat Material1Diffuse[] = {1.0, 1.0, 0.7, 1.0};
GLfloat Material2Diffuse[] = {0.5, 1.0, 0.5, 1.0};
GLfloat Material3Diffuse[] = {0.0, 0.0, 1.0, 1.0};
GLfloat Material4Diffuse[] = {0.4, 0.6, 0.0, 1.0};
GLfloat Material1Specular[] = {0.9, 0.9, 0.9, 1.0};
GLfloat Material1Shininess[] = {82.0};
GLfloat Material2Shininess[] = {150.0};
GLfloat Material3Shininess[] = {10.0};
GLfloat Material4Shininess[] = {10.0};
static GLfloat a=0.0,d=0.0,st=0.01;
// First clear the screen
a=a+2.0;
d=d+st;
if (d>2.0) st=-0.01;
else if (d<0.5) st=0.01;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMaterialfv(GL_FRONT, GL_DIFFUSE, Material1Diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, Material1Specular);
glMaterialfv(GL_FRONT, GL_SHININESS, Material1Shininess);
glPushMatrix();
glRotatef(phase/5,-1,0.4,0.6);
//glTranslatef(-30,0,0);
//glRotatef(90,1,0,0);
//F->SetPhase(phase/500.0);
for (i=0;i<5;i++)
{
P->F[i]->SetPhase(phase/500.0-(double)(i)/20.0);
//P->F[i]->SetPhase(0.5);
}
//P->F[1]->SetPhase(phase/500.0-(double)(i++)/20.0);
P->draw();
glPopMatrix();
// Ask Geometry container to draw all it's shape
//Geometry.Render(phase);
/*glPushMatrix();
glRotatef(a/4.0,0.0,0.5,1);
glMaterialfv(GL_FRONT, GL_EMISSION, Unlit);
glPushMatrix();
glTranslatef(-35,30,0);
glRotatef(a,1,1,1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, Material1Diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, Material1Specular);
glMaterialfv(GL_FRONT, GL_SHININESS, Material1Shininess);
glutSolidCube(20);
glPopMatrix();
glPushMatrix();
glTranslatef(-35,-30,0);
glRotatef(a,1,1,1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, Material2Diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, Material1Specular);
glMaterialfv(GL_FRONT, GL_SHININESS, Material2Shininess);
glutSolidSphere(20,70,70);
glPopMatrix();
glPushMatrix();
glTranslatef(35,30,0);
glRotatef(a,1,1,1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, Material3Diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, Material1Specular);
glMaterialfv(GL_FRONT, GL_SHININESS, Material3Shininess);
glutSolidTeapot(20);
glPopMatrix();
glPushMatrix();
glTranslatef(35,-30,0);
glRotatef(a,1,1,1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, Material4Diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, Material1Specular);
glMaterialfv(GL_FRONT, GL_SHININESS, Material4Shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, Material4Diffuse);
glutSolidCone(20,40,20,20);
glPopMatrix();
glPopMatrix();*/
// Swap the display buffer to show the new picture just drawn
glutSwapBuffers();
}
//-------------------------------------------------------------
// This function is hooked to OpenGL toolkit library
// openGL calls this function every time the window
// size is changed.
void reshape(int w,int h)
{
// Specify size of the window
glViewport(0,0,(GLsizei) w, (GLsizei) h);
// Select a display mode
// See the TA for more on Projection
// Select the viewport coordinate system
// The following is a 2D coordinate system
// X-Axis : -50 to 50
// Y-Axis : -50 to 50
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1.33, 1.0, 1000.0);
// This is another mode for matrix translation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-100,0,0,100,0,1,0);
}
//-------------------------------------------------------------------
// Mouse event handler
//-------------------------------------------------------------------
void mouse(int button,int state,int x,int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state==GLUT_DOWN)
{
// Right click to exit the application
glutDestroyWindow(Window);
exit(0);
}
break;
// Left click to toggle fullscreen mode
case GLUT_LEFT_BUTTON:
if (state==GLUT_DOWN && !fullscreen)
{
glutFullScreen();
fullscreen=1;
}
else if (state==GLUT_DOWN && fullscreen)
{
glutReshapeWindow(640,480);
fullscreen=0;
}
default:
break;
}
}
//-------------------------------------------------------------------
// Keyboard event handlers
//-------------------------------------------------------------------
void KeyInp(unsigned char key,int x,int y)
{
if (key=='=') phasestep++;
if (key=='-' && phasestep>0)phasestep--;
if (key==27)
{
glutDestroyWindow(Window);
exit(0);
}
}
void SpKeyInp(int key,int x,int y)
{
}
void Idle()
{
phase+=phasestep;
glutPostRedisplay();
}
//-------------------------------------------------------------------
// Function that initializes the OpenGL Environment
//-------------------------------------------------------------------
void init(void)
{
int a;
char *c2,c[]=".\\";
GLfloat Light1Position[] = {50, 50, -80.0, 0.0};
GLfloat Light2Position[] = {-80, -80, -80.0, 0.0};
GLfloat LightAmbient[] = {0.1, 0.1, 0.1, 1.0};
GLfloat LightDiffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat LightSpecular[] = {1, 1, 1, 1.0};
GLfloat Light1Shininess[] = {30.0};
GLfloat Light2Shininess[] = {90.0};
a=1;
c2=c;
glutInit(&a,&c2);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glEnable(GL_DEPTH_TEST);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow(AppTitle);
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
Window=glutGetWindow();
// Select black background
glClearColor(0.0,1.0,0.0,0.0);
// Shading mode is flat
glShadeModel(GL_SMOOTH);
// Now Enable Antialiasing
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
glLineWidth(1.1);
reshape(640,480);
glLightfv(GL_LIGHT0, GL_POSITION, Light1Position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
glLightfv(GL_LIGHT0, GL_SHININESS, Light1Shininess);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION , 0.0002);
glLightfv(GL_LIGHT1, GL_POSITION, Light2Position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular);
glLightfv(GL_LIGHT1, GL_SHININESS, Light2Shininess);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION , 0.0002);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
// Hook the functions with OpenGL
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(KeyInp);
glutSpecialFunc(SpKeyInp);
glutIdleFunc(Idle);
// Goto Fullscreen Mode.
if (fullscreen) glutFullScreen();
glEnableClientState(GL_VERTEX_ARRAY);
}
//-------------------------------------------------------------------
// This is the application entry point of the main program
//-------------------------------------------------------------------
int main(int argc, char **argv)
{
// Initialize And switch to OpenGL Mode
init_shapes();
init();
// Enter the main loop
glutMainLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -