#include <stdio.h>
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define ColoredVertex(c,v) do{ glColor3fv(c); glVertex3fv(v); }while(0)
GLfloat angle = 0.0f;
#define FileName "Bliss.bmp"
static GLint ImageWidth;
static GLint ImageHeight;
static GLint PixelLength;
static GLubyte* PixelData;
void RevolveTriangle(){
// glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 绘制像素
glDrawPixels(ImageWidth, ImageHeight,
GL_BGR_EXT, GL_UNSIGNED_BYTE, PixelData);
static int list = 0;
glEnable(GL_DEPTH_TEST);
if(list == 0){ //如果显示列表不存在,则创建
GLfloat PointA[] = { 0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6}, //设置4个角的位置
PointB[] = {-0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6},
PointC[] = { 0.0f,-sqrt(6.0f)/12, sqrt(3.0f)/3},
PointD[] = { 0.0f, sqrt(6.0f)/4, 0};
GLfloat ColorR[] = {1,0,0}, //设置4个角的颜色
ColorG[] = {0,1,0},
ColorB[] = {0,0,1},
ColorY[] = {1,1,0};
list = glGenLists(1);
glNewList(list,GL_COMPILE);
glBegin(GL_TRIANGLES); //画出4个三角形
//平面BAD(左)
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorY,PointD);
//平面ABC(正)
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorB,PointC);
//平面ACD(右)
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorB,PointC);
ColoredVertex(ColorY,PointD);
//平面CBD(底)
ColoredVertex(ColorB,PointC);
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorY,PointD);
glEnd();
glEndList();
}
//已经创建了显示列表,在每次绘制正四面体时将调用它
glPushMatrix(); //入矩阵栈
glRotatef(angle,1,0.5,0); //旋转
glCallList(list); //调用列表
glPopMatrix(); //出矩阵栈
glutSwapBuffers();
}
void action(){
if(angle >= 360.0f){
angle = 0.0f;
}
angle+=2;
RevolveTriangle();
}
int main(int argc,char *argv[]){
FILE* pFile = fopen("/Users/Lion/Desktop/3.bmp", "rb");
if( pFile == 0 )
exit(0);
// 读取图象的大小信息
fseek(pFile, 0x0012, SEEK_SET);
fread(&ImageWidth, sizeof(ImageWidth), 1, pFile);
fread(&ImageHeight, sizeof(ImageHeight), 1, pFile);
// 计算像素数据长度
PixelLength = ImageWidth * 3;
while( PixelLength % 4 != 0 )
++PixelLength;
PixelLength *= ImageHeight;
// 读取像素数据
PixelData = (GLubyte*)malloc(PixelLength);
if( PixelData == 0 )
exit(0);
fseek(pFile, 54, SEEK_SET);
fread(PixelData, PixelLength, 1, pFile);
// 关闭文件
fclose(pFile);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(200,200);
glutInitWindowSize(640,480);
glutCreateWindow("Revolve Triangle");
glutDisplayFunc(&RevolveTriangle);
glutIdleFunc(&action);
glutMainLoop();
free(PixelData);
return 0;
}