📄 rotating torus.cpp
字号:
#include <math.h>
#include "windows.h"
#include "GLTools.h"
#include <glut.h> // OpenGL toolkit
#define PI 3.14159
float theta=-90.0;
float R=100;
int inner=10,outer=80;
float eyex=0,eyey=0,eyez=outer+4*inner+50;
float atx=0,aty=0,atz=0;
float r=outer+4*inner+50;
float s=5;
float rotateangley=0;
float rotateanglex=0;
float angle=5;
float startx=0,starty=0,endx=0,endy=0;
int ww,hh;
bool flag=true;
float tmp;
//float tt=0,tt2=0;
void keyboard(unsigned char key, int x, int y);
void Display(void);
void Reshape(int w, int h);
void myidle();
void drawground();
void drawsphere();
void drawwall();
void init();
void myMouse(int button,int state,int x,int y);
void myMotion(int x,int y);
GLTMatrix mShadowMatrix;
GLTVector3 vPoints[3] = {{ 0.0f, -outer-4*inner, 0.0f },
{ 10.0f, -outer-4*inner, 0.0f },
{ 5.0f, -outer-4*inner, -5.0f }};
void specialkeyboard(int key, int x, int y);
void Display(void);
void Reshape(int w, int h);
void myidle();
void drawground();
void drawsphere(int flag);
void drawwall();
void init();
/*
// Light and material Data
GLfloat fLightPos[4] = { -100.0f, 100.0f, 50.0f, 1.0f }; // Point source
GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGB|GLUT_DEPTH);
glutInitWindowPosition(0,0);
glutInitWindowSize(1000,760);
//glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("A Rotating Torus");
init();
glutDisplayFunc(Display);
glutKeyboardFunc(keyboard);
glutMouseFunc(myMouse);
glutMotionFunc(myMotion);
// glutSpecialFunc(specialkeyboard);
glutReshapeFunc(Reshape);
glutIdleFunc(myidle);
glutMainLoop();
return 0;
}
void init()
{
//define light position1
GLfloat light_position1[]={-outer,outer,outer+inner,0.0};
// GLfloat light_position1[]={0,0,0,0.0};
GLfloat light_position2[]={+outer,-outer,outer+inner,0.0};
//GLfloat light color1
GLfloat light_ambient1[]={1.0,1.0,1.0,1.0};
GLfloat light_diffuse1[]={1.0,1.0,1.0,1.0};
GLfloat light_specular1[]={1.0,1.0,1.0,1.0};
//GLfloat light color2
GLfloat light_ambient2[]={0.8,0.8,0.8,1.0};
GLfloat light_diffuse2[]={0.8,0.8,0.8,1.0};
GLfloat light_specular2[]={0.8,0.8,0.8,1.0};
// light model- global light
GLfloat lmodel_ambient[]={0.8,0.2,0.2,1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
// set light source location
glLightfv(GL_LIGHT0,GL_POSITION,light_position1);
glLightfv(GL_LIGHT1,GL_POSITION,light_position2);
// set light source color;
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient1);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse1);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular1);
// set light source color;
glLightfv(GL_LIGHT1,GL_AMBIENT,light_ambient2);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse2);
glLightfv(GL_LIGHT1,GL_SPECULAR,light_specular2);
// set material let material fits color
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
//make light source enable;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
// Calculate shadow matrix
gltMakeShadowMatrix(vPoints, light_position1, mShadowMatrix);
//clear color
glClearColor(0.0,0.0,0.0,0.0);
//Color shade mode
glShadeModel(GL_SMOOTH);
// glShadeModel(GL_FLAT);
// polygon mode
glPolygonMode(GL_FRONT,GL_FILL);
//// Anti-aliasing
// glEnable(GL_POINT_SMOOTH);
// glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
// glEnable(GL_LINE_SMOOTH);
// glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
// glEnable(GL_POLYGON_SMOOTH);
// glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
// glEnable(GLUT_MULTISAMPLE);
// depth detecting
glEnable(GL_DEPTH_TEST);
}
void Display(void)
{
glClearColor(1,1,1,1);
// glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// gluLookAt(0,-10,350,0,0,0,0,1,0);
gluLookAt(eyex,eyey,eyez,atx,aty,atz,0,1,0);
glRotatef(rotateangley,0,1,0);
glRotatef(rotateanglex,1,0,0);
// Draw shadows first
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glPushMatrix();
glMultMatrixf(mShadowMatrix);
drawsphere(0);
glPopMatrix();
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
drawground();
drawwall();
// Draw torus_sphere normally
drawsphere(1);
glutSwapBuffers();
// glFlush();
}
void drawsphere(int flag)
{
float tr;
tr=(outer+3*inner);
glRotatef(theta,0,1,0);
glPushMatrix();
glPushMatrix();
if (flag==1)
glColor3f(1.0,0,0.0);
else
glColor3f(0.5,0.5,0.5);
glutSolidTorus(inner,outer,50,80);
glPopMatrix();
glPushMatrix();
glTranslatef(outer,0,0);
glRotatef(theta,0,1,0);
glTranslatef(-outer,0,0);
glPushMatrix();
glTranslatef(tr,0,0);
glRotatef(-45,1,0,0);
if (flag==1)
glColor3f(0.0,1.0,0);
else
glColor3f(0.5,0.5,0.5);
glutSolidSphere(inner,40,40);
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
void drawground()
{
int colorflag=1;
GLfloat mat_specular1[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess1[]={80.0};
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular1);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess1);
glNormal3f(0,1,0);
for (int i=-outer-4*inner;i<outer+4*inner;i+=2*inner)
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
for (int j=-outer-4*inner;j<outer+4*inner;j+=2*inner)
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
glBegin(GL_QUADS);
glVertex3d(j,-outer-4*inner,i);
glVertex3d(j,-outer-4*inner,i+2*inner);
glVertex3d(j+2*inner,-outer-4*inner,i+2*inner);
glVertex3d(j+2*inner,-outer-4*inner,i);
glEnd();
}
}
glNormal3f(0,-1,0);
colorflag=1;
for ( int i=-outer-4*inner;i<outer+4*inner;i+=2*inner)
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
for (int j=-outer-4*inner;j<outer+4*inner;j+=2*inner)
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
glBegin(GL_QUADS);
glVertex3d(j,outer+4*inner,i);
glVertex3d(j,outer+4*inner,i+2*inner);
glVertex3d(j+2*inner,outer+4*inner,i+2*inner);
glVertex3d(j+2*inner,outer+4*inner,i);
glEnd();
}
}
}
void drawwall()
{
int i,j;
glNormal3f(1,0,0);
int colorflag=1;
//left
for (i=-outer-4*inner;i<outer+4*inner;i+=2*inner)
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
for (j=-outer-4*inner;j<outer+4*inner;j+=2*inner)
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
glBegin(GL_QUADS);
glVertex3d(-outer-4*inner,j,i);
glVertex3d(-outer-4*inner,j+2*inner,i);
glVertex3d(-outer-4*inner,j+2*inner,i+2*inner);
glVertex3d(-outer-4*inner,j,i+2*inner);
glEnd();
}
}
colorflag=1;
glNormal3f(0,-1,0);
//right
for (i=-outer-4*inner;i<=outer+4*inner-2*inner;i+=2*inner) //for z
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
for (j=-outer-4*inner;j<=outer+4*inner-2*inner;j+=2*inner) //for y
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
glBegin(GL_QUADS);
glVertex3f(outer+4*inner,j,i);
glVertex3f(outer+4*inner,j+2*inner,i);
glVertex3f(outer+4*inner,j+2*inner,i+2*inner);
glVertex3f(outer+4*inner,j,i+2*inner);
glEnd();
}
}
colorflag=1;
glNormal3f(0,0,1);
//front
for (i=-outer-4*inner;i<=outer+4*inner-2*inner;i+=2*inner) //for z
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
for (j=-outer-4*inner;j<=outer+4*inner-2*inner;j+=2*inner) //for y
{
if (colorflag>0) glColor4f(1.0,1.0,0.0,0.6);
else glColor4f(1.0,1.0,1.0,0.6);
colorflag=-colorflag;
glBegin(GL_QUADS);
glVertex3f(j,i,-outer-4*inner);
glVertex3f(j+2*inner,i,-outer-4*inner);
glVertex3f(j+2*inner,i+2*inner,-outer-4*inner);
glVertex3f(j,i+2*inner,-outer-4*inner);
glEnd();
}
}
}
void myidle()
{
theta+=0.5;
// if (theta>=360.0) theta-=360.0;
glutPostRedisplay();
}
void Reshape(GLsizei w,GLsizei h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glOrtho(-outer-6*inner,outer+6*inner,-outer-4*inner,outer+4*inner,20,2*outer+8*inner+50);
gluPerspective(90,w/h,10,2*outer+8*inner+250);
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
float dx=s/r*(atx-eyex);
float dz=s/r*(eyez-atz);
switch(key)
{
/* YOUR CODE HERE */
case 'W':
case 'w':
eyex=eyex+dx;
atx=atx+dx;
eyez=eyez-dz;
atz=atz-dz;
break;
case 'S':
case 's':
eyex=eyex-dx;
atx=atx-dx;
eyez=eyez+dz;
atz=atz+dz;
break;
case 'A':
case 'a':
tmp=eyex;
eyex=atx-(eyez-atz)*sin(angle/180*PI)-(atx-eyex)*cos(angle/180*PI);
eyez=atz+(eyez-atz)*cos(angle/180*PI)-(atx-tmp)*sin(angle/180*PI);
break;
case 'D':
case 'd':
tmp=eyex;
eyex=atx+(eyez-atz)*sin(angle/180*PI)-(atx-eyex)*cos(angle/180*PI);
eyez=atz+(eyez-atz)*cos(angle/180*PI)+(atx-tmp)*sin(angle/180*PI);
break;
}
glutPostRedisplay();
}
void myMouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
{
if (flag)
{
// get the line's start point
startx=x;
starty=hh-y;
flag=!flag;
}
if(button==GLUT_LEFT_BUTTON&&state==GLUT_UP)
{
if (!flag)
{
// get the line's end point , count lines's number
endx=x;
endy=hh-y;
flag=!flag;
rotateangley+=(endx-startx)/1024*5;
rotateanglex+=(endy-starty)/768*5;
}
glutPostRedisplay();
}
}
}
void myMotion(int x,int y)
{
endx=x;
endy=hh-y;
rotateangley+=(endx-startx)/1024*5;
rotateanglex+=(endy-starty)/768*5;
glutPostRedisplay();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -