实验课用咯
#include
#include
#include
static int axis = 2;
float theta[3] = {0.0, 0.0, 0.0};
GLubyte image[64][64][3];
int aa[1333][777][4],width=512,height=512;
GLfloat ap[512][512][3];
int PI=3.1415926 ;
int www=666, hhh=666;
double r=5;
GLfloat speed = 1 ,rx,ry,rz,dx=8,dy=1,dz=1;
void show(){//绘制物体
//非正交投影
glColor3f(1.0,1.0,1.0);
double i,j,ii,jj,k,x,y,z,dt=1,dr=10;
i=-r,j=r,ii=-r,jj=r;
//绘制正方体
for(double q=i;q
glTexCoord2f((q+r)/r/4,(w+r)/r/4);
glVertex3f(q,w,r);
glTexCoord2f((q+r)/r/4,(w+r+dt)/r/4);
glVertex3f(q,w+dt,r);
glTexCoord2f((q+r+dt)/r/4,(w+r+dt)/r/4);
glVertex3f(q+dt,w+dt,r);
glTexCoord2f((q+r+dt)/r/4,(w+r)/r/4);
glVertex3f(q+dt,w,r);
glEnd();
}
for(double q=i;q
glTexCoord2f((q+r)/r/4,(w+r)/r/4);
glVertex3f(q,w,-r);
glTexCoord2f((q+r)/r/4,(w+r+dt)/r/4);
glVertex3f(q,w+dt,-r);
glTexCoord2f((q+r+dt)/r/4,(w+r+dt)/r/4);
glVertex3f(q+dt,w+dt,-r);
glTexCoord2f((q+r+dt)/r/4,(w+r)/r/4);
glVertex3f(q+dt,w,-r);
glEnd();
}
for(double q=i;q
glTexCoord2f((w+r)/r/4,(q+r)/r/4);
glVertex3f(-r,q,w);
glTexCoord2f((w+r+dt)/r/4,(q+r)/r/4);
glVertex3f(-r,q,w+dt);
glTexCoord2f((w+r+dt)/r/4,(q+r+dt)/r/4);
glVertex3f(-r,q+dt,w+dt);
glTexCoord2f((w+r)/r/4,(q+r+dt)/r/4);
glVertex3f(-r,q+dt,w);
glEnd();
}
//上
//glColor3f(1.0,0.0,0.4);
for(double q=i;q
glBegin(GL_POLYGON);//上
glTexCoord2f((q+r)/r/4+0.5,(w+r)/r/4+0.5);
glVertex3f(q,r,w);
glTexCoord2f((q+r)/r/4+0.5,(w+r+dt)/r/4+0.5);
glVertex3f(q,r,w+dt);
glTexCoord2f((q+r+dt)/r/4+0.5,(w+r+dt)/r/4+0.5);
glVertex3f(q+dt,r,w+dt);
glTexCoord2f((q+r+dt)/r/4+0.5,(w+r)/r/4+0.5);
glVertex3f(q+dt,r,w);
glEnd();
}
for(double q=i;q
glTexCoord2f((q+r)/r/4,(w+r)/r/4+0.5);
glVertex3f(q,-r,w);
glTexCoord2f((q+r)/r/4,(w+r+dt)/r/4+0.5);
glVertex3f(q,-r,w+dt);
glTexCoord2f((q+r+dt)/r/4,(w+r+dt)/r/4+0.5);
glVertex3f(q+dt,-r,w+dt);
glTexCoord2f((q+r+dt)/r/4,(w+r)/r/4+0.5);
glVertex3f(q+dt,-r,w);
glEnd();
}
//绘制球
glTranslatef(-2,-3,-3);
glColor3f(0.1,0.2,0.7);
glutSolidSphere(1.4,40,40);
//绘制圆锥
glRotatef(-90,1,0,0);
glTranslatef(1,-3,-2);
glColor3f(0.4,0.7,0.8);
glutSolidCone(1,3, 10,10);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//必须要做,清理缓存
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) www/(GLfloat) hhh, 1.0, 20.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
gluLookAt(dx,dy,dz, 0, 0, 0, 0, 1, 0);
show();
glutSwapBuffers();
}
void reshape (GLsizei w, GLsizei h)
{
float aspectRatio;
//设置视口
glViewport(0, 0, w, h);
//设置投影(取景)变换方式
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
aspectRatio=(GLfloat)w/(GLfloat)h;
gluPerspective(60, aspectRatio, 1, 30);
//设置模型变换和视图变换方式
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
}
void keyboard (unsigned char key, int x, int y)//定义键盘操作
{
switch ( key )
{
//控制位移的键
case 'u': case 'U':
dx+=speed;
break;
case 'j': case 'J':
dx-=speed;
break;
case 'i': case 'I':
dy-=speed;
break;
case 'k': case 'K':
dy+=speed;
break;
case 'o': case 'O':
dz+=speed;
break;
case 'l': case 'L':
dz-=speed;
break;
}
glutPostRedisplay();
}
void SetupLights()
{
GLfloat ambientLight[] ={0.2f, 0.2f, 0.2f, 1.0f};//环境光
GLfloat diffuseLight[] ={0.9f, 0.9f, 0.9f, 1.0f};//漫反射
GLfloat specularLight[] ={1.0f, 1.0f, 1.0f, 1.0f};//镜面光
GLfloat lightPos[] ={-4.0f, -4.0f, 4.0f, 1.0f};//光源位置
glEnable(GL_LIGHTING); //启用光照
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); //设置环境光源
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); //设置漫反射光源
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); //设置镜面光源
glLightfv(GL_LIGHT0, GL_POSITION, lightPos); //设置灯光位置
glEnable(GL_LIGHT0); //打开第一个灯光
glEnable(GL_COLOR_MATERIAL); //启用材质的颜色跟踪
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); //指定材料着色的面
glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight); //指定材料对镜面光的反应
glMateriali(GL_FRONT, GL_SHININESS, 100); //指定反射系数
}
void init()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_RGB, GL_FLOAT, ap);//设置纹理参数
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
//读入纹理
freopen("F.txt","r",stdin);
for(int q=1;q<=512;q++)
for(int e=1;e<=3;e++)
for(int w=1;w<=512;w++)
{
scanf("%d",&aa[height-q+1][w][e]);
if(q<=height&&w<=width)
ap[height-q][w-1][e-1]=aa[height-q+1][w][e]*1.0/255;
if(e!=3||w!=width)scanf(",");
}
int co=0;
printf("%d %d %d", aa[1][1][1],aa[1280][720][2],aa[131][123][3]);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("room");
glutKeyboardFunc(keyboard);//键盘改变视点
SetupLights();//灯光
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}