#include <gl/glut.h> //引用相关包
#include <windows.h>
#include <gl/glaux.h>
#include <stdio.h>
#pragma comment(lib, "openGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
GLfloat xrot; // X 旋转量
GLfloat yrot; // Y 旋转量
GLfloat zrot; // Z 旋转量
GLuint texture[1]; // 存储一个纹理
GLfloat xRot = 0.0f;
GLfloat yRot = 0.0f;
static int year = 0, day = 0;
AUX_RGBImageRec *LoadBMP(CHAR *Filename) // 载入位图图象
{
FILE *File=NULL; // 文件句柄
if (!Filename) // 确保文件名已提供
{
return NULL; // 如果没提供,返回 NULL
}
File=fopen(Filename,"r"); // 尝试打开文件
if (File) // 文件存在么?
{
fclose(File); // 关闭句柄
return auxDIBImageLoadA(Filename); // 载入位图并返回指针
}
return NULL; // 如果载入失败,返回 NULL
}
int LoadGLTextures() // 载入位图(调用上面的代码)并转换成纹理
{
int Status=FALSE; // 状态指示器
AUX_RGBImageRec *TextureImage[1]; // 创建纹理的存储空间
memset(TextureImage,0,sizeof(void *)*1); // 将指针设为 NULL
// 载入位图,检查有无错误,如果位图没找到则退出
if (TextureImage[0]=LoadBMP("pengchao.bmp"))
{
Status=TRUE; // 将 Status 设为 TRUE
glGenTextures(1, &texture[0]); // 创建纹理
// 使用来自位图数据生成 的典型纹理
glBindTexture(GL_TEXTURE_2D, texture[0]);
// 生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // 线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // 线形滤波
}
if (TextureImage[0]) // 纹理是否存在
{
if (TextureImage[0]->data) // 纹理图像是否存在
{
free(TextureImage[0]->data); // 释放纹理图像占用的内存
}
free(TextureImage[0]); // 释放图像结构
}
return Status; // 返回 Status
}
void DrawCube(void) // 从这里开始进行所有的绘制
{
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // 重置当前的模型观察矩阵
glTranslatef(0.0f,0.0f,-5.0f); // 移入屏幕 5 个单位
glRotatef(xRot,1.0f,0.0f,0.0f); // 绕X轴旋转
glRotatef(yRot,0.0f,1.0f,0.0f); // 绕Y轴旋转
glRotatef(zrot,0.0f,0.0f,1.0f); // 绕Z轴旋转
glBindTexture(GL_TEXTURE_2D, texture[0]); // 选择纹理
glBegin(GL_QUADS);
// 前面
glNormal3f(0,0,1);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // 纹理和四边形的左下
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // 纹理和四边形的右下
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // 纹理和四边形的右上
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // 纹理和四边形的左上
// 后面
glNormal3f(0,0,-1);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // 纹理和四边形的右下
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // 纹理和四边形的右上
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // 纹理和四边形的左上
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // 纹理和四边形的左下
// 顶面
glNormal3f(0,1,0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // 纹理和四边形的左上
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // 纹理和四边形的左下
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // 纹理和四边形的右下
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // 纹理和四边形的右上
// 底面
glNormal3f(0,-1,0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // 纹理和四边形的右上
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // 纹理和四边形的左上
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // 纹理和四边形的左下
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // 纹理和四边形的右下
// 右面
glNormal3f(1,0,0);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // 纹理和四边形的右下
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // 纹理和四边形的右上
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // 纹理和四边形的左上
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // 纹理和四边形的左下
// 左面
glNormal3f(-1,0,0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // 纹理和四边形的左下
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // 纹理和四边形的右下
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // 纹理和四边形的右上
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // 纹理和四边形的左上
glEnd();
}
void SetupRC()
{
glMatrixMode(GL_PROJECTION);
GLfloat mat_ambient[]= { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat mat_diffuse[]= { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat mat_shininess[] = { 50.0f };
////////////////////////
//GLfloat light_diffuse[]= { 0.0f, 0.0f, 1.0f, 1.0f};
//GLfloat position[] = { 3.0f, 3.0f, 3.0f, 1.0f };
GLfloat light_ambient[]={1.0f,1.0f,1.0f,1.0f};
GLfloat light_diffuse[]={1.0f,1.0f,0.0f,1.0f};
GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat position[] = { -10.0f, 0.0f, 10.0f, 1.0f };
GLfloat spot_direction[]={ 1.0f, 1.0f, -1.0f};
GLfloat position1[] = { -10.0f, 0.0f, 10.0f, 1.0f };
////////////////////
//定义材质属性
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS,mat_shininess);
//////////////////////
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, position);
glLightf (GL_LIGHT0, GL_SPOT_CUTOFF, 120.0);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,spot_direction);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse);
glLightfv (GL_LIGHT1, GL_POSITION, position1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
//glClearColor(1.0, 1.0, 1.0, 1.0);
}
void Sphere(void)
{
//glClear (GL_COLOR_BUFFER_BIT);
//glColor3f (1.0, 1.0, 1.0);
glPushMatrix();
glPushMatrix();
glRotatef ((GLfloat) year, 0.0, 1.5, 0.0);
glTranslatef (-3.0, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glColor3f(1.0,1.0,0.0);
glutSolidSphere(0.5, 15, 12); /* draw sun */
glPopMatrix();
glRotatef ((GLfloat) year, 0.0, 1.5, 0.0);
glTranslatef (3.0, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glColor3f(1.0,1.0,0.0);
glutWireSphere(0.2, 10, 8); /* draw smaller planet */
glPopMatrix();
day = (day + 10) % 360;
year = (year + 1) % 360;
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清楚颜色数据和深度数据(清屏)
SetupRC();
DrawCube();
Sphere();
//RenderScene();
// gluLookAt(xRot,yRot,0.0,0.0,0.0,0.0,0.0,0.0,0.0);
// glPopMatrix();
glutSwapBuffers(); //交换缓冲区。显示图形
}
//初始化
void init (void)
{
glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //清理颜色,为黑色,(也可认为是背景颜色)
glCullFace(GL_BACK); //背面裁剪(背面不可见)
glEnable(GL_CULL_FACE); //启用裁剪
glEnable(GL_TEXTURE_2D);
LoadGLTextures(); //载入纹理贴图
}
//当窗口大小改变时,会调用这个函数
void reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h); //设置视口
glMatrixMode(GL_PROJECTION); //设置矩阵模式为投影变换矩阵,
glLoadIdentity(); //变为单位矩阵
gluPerspective(80, (GLfloat)w / h, 1, 40); //设置投影矩阵
//gluPerspective(40.0f, (GLfloat) w/(GLfloat) h, 1.0f, 20.0f);
glMatrixMode(GL_MODELVIEW); //设置矩阵模式为视图矩阵(模型)
- 1
- 2
前往页