#include <gl/glaux.h>
#include "gl/glut.h"
#include <stdio.h>
#include <math.h>
#define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
#define GL_SEPARATE_SPECULAR_COLOR 0x81FA
const int width = 800, height = 600;
const double half_width=1.0;
GLfloat xRotation = 0.0f, yRotation = 0.0f;
const GLfloat rotSpeed = 0.01f;
bool bLButtonDown = false;
bool bMButtonDown = false;
bool bRButtonDown = false;
bool bRKeyDown = false;
bool bLKeyDown = false;
GLint lastPx, lastPy;
GLuint textures=0;
GLuint textures2=0;
GLuint textures3=0;
GLfloat SclFac=2.0f, lastSclFac=1.0f;
GLfloat xTransl = 0.7f, yTransl =0.7f;
GLfloat ambientProperties[] = {0.5f, 0.5f, 0.5f, 0.5f};
GLfloat diffuseProperties[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specularProperties[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat lightPos[] = {0.0f, 0.0f, 0.0f, 0.0f};
float roll = 0.0f ;
float roll2 = 0.0f ;
float roll3=0.0f;
GLUquadricObj *q;
GLfloat xrot=0.1;
void init(){
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
q = gluNewQuadric();
gluQuadricNormals(q, GL_SMOOTH);
gluQuadricTexture(q, GL_TRUE);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
//sun
AUX_RGBImageRec *image3 = NULL;
image3 = auxDIBImageLoad("sun.bmp");
if(image3 != NULL)
{
printf("sun_success ");
glGenTextures(1,&textures3);
glBindTexture(GL_TEXTURE_2D,textures3);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image3->sizeX,image3->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image3->data);
glEnable(GL_TEXTURE_2D);
if(image3->data !=NULL)
{
delete[] image3->data;
}
delete image3;
}
//earth
AUX_RGBImageRec *image = NULL;
image = auxDIBImageLoad("earth.bmp");
if(image != NULL)
{
printf("earth_success ");
glGenTextures(1,&textures);
glBindTexture(GL_TEXTURE_2D,textures);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->sizeX,image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data);
glEnable(GL_TEXTURE_2D);
if(image->data !=NULL)
{
delete[] image->data;
}
delete image;
}
//moon
AUX_RGBImageRec *image2 = NULL;
image2 = auxDIBImageLoad("moon.bmp");
if(image2 != NULL)
{
printf("moon_success ");
glGenTextures(1,&textures2);
glBindTexture(GL_TEXTURE_2D,textures2);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image2->sizeX,image2->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image2->data);
glEnable(GL_TEXTURE_2D);
if(image2->data !=NULL)
{
delete[] image2->data;
}
}
delete image2;
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
}
void keyboard(int key, int x, int y)
{
switch(key){
case GLUT_KEY_RIGHT:
yRotation -= (GLfloat) (lastPx - x) * rotSpeed;
break;
case GLUT_KEY_LEFT:
yRotation -= -(GLfloat) (lastPx - x) * rotSpeed;
break;
case GLUT_KEY_UP:
xRotation -= -(GLfloat) (lastPy - y) * rotSpeed;
break;
case GLUT_KEY_DOWN:
xRotation -= (GLfloat) (lastPy - y) * rotSpeed;
break;
}
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (bLButtonDown = state == GLUT_DOWN) {
lastPx = x;
lastPy = y;
}
break;
case GLUT_MIDDLE_BUTTON:
if (bMButtonDown = state == GLUT_DOWN) {
lastPx = x;
lastPy = y;
}
break;
case GLUT_RIGHT_BUTTON:
if (bRButtonDown = state == GLUT_DOWN) {
lastPy = y;
lastSclFac = SclFac;
}
break;
default:
break;
}
}
void motion(int x, int y)
{
if (bLButtonDown) {
yRotation -= (GLfloat) (lastPx - x) * rotSpeed;
xRotation -= (GLfloat) (lastPy - y) * rotSpeed;
}
else if (bRButtonDown) {
GLfloat dy = y - lastPy;
if (dy > 0.0f) {
dy = sqrt(dy);
SclFac = lastSclFac * dy;
} else if (dy < 0.0f) {
dy = sqrt(-dy);
SclFac = lastSclFac / dy;
}
} else if (bMButtonDown) {
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
GLfloat halfHeight = viewport[3] >> 1;
GLfloat reci = 1.0 / SclFac;
xTransl += (GLfloat) (x - lastPx) * reci / halfHeight;
yTransl -= (GLfloat) (y - lastPy) * reci / halfHeight;
lastPx = x;
lastPy = y;
}
glutPostRedisplay();
}
void reshape(int x, int y)
{
gluLookAt(0.0,0.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble aspect =(GLdouble)x/(GLdouble)y;
gluPerspective(45.0*0.5,aspect,0.1,100.0);
glViewport(0,0,x,y);
}
void Rotate()
{
_sleep(20);
roll += 0.1;
roll2 +=-1.0;
if (roll > 360.0)
{
roll -=360.0;
}
if (roll2 > 360.0)
{
roll2 -=360.0;
}
glutPostRedisplay();
}
void update(int value)
{
xrot = 0.1f;
value = 25;
glutPostRedisplay(); //Tell GLUT that the scene has changed
glutTimerFunc(value, update, 0);//Tell GLUT to call update again in 25 milliseconds
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // 清除颜色缓存内容
glMatrixMode(GL_MODELVIEW);
/* glPushMatrix();
glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
glRotatef(yRotation, 0.0f, 1.0f, 0.0f);
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
glScalef(SclFac,SclFac,SclFac);
glTranslatef(xTransl,yTransl,0.0f);
cubic();
glPopMatrix();
*/
//sun
glPushMatrix();
glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
glRotatef(yRotation, 0.0f, 1.0f, 0.0f);
glRotatef(roll3,0.0f, 0.0f, 1.0f);
glScalef(SclFac,SclFac,SclFac);
glTranslatef(0.0f,0.0f,0.0f);
glBindTexture(GL_TEXTURE_2D,textures3);
gluSphere(q, 0.5f, 100, 100);
glPopMatrix();
//earth
glPushMatrix();
glRotatef(roll,0.0f, 0.0f, 1.0f);
glScalef(SclFac,SclFac,SclFac);
glTranslatef(xTransl,yTransl,1.0f);
glBindTexture(GL_TEXTURE_2D,textures);
gluSphere(q, 0.1f, 100, 100);
//moon
glPushMatrix();
glRotatef(roll2,0.0f, 0.0f, 1.0f);
glScalef(SclFac,SclFac,SclFac);
glTranslatef(0.05f,0.05f,0.0f);
glBindTexture(GL_TEXTURE_2D,textures2);
gluSphere(q, 0.01f, 100, 100);
glPopMatrix();
glPopMatrix();
glFlush(); // 刷新
glEnd();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutSpecialFunc(keyboard);
glutIdleFunc(display);
glutIdleFunc(Rotate);
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
OpenGL_日地月模型动画
共33个文件
h:7个
bmp:4个
obj:3个
需积分: 33 33 下载量 172 浏览量
2015-12-21
14:33:30
上传
评论
收藏 7.64MB RAR 举报
温馨提示
OpenGL实现简单的日地月旋转动画,在日地月球体表面贴上纹理,使之更加真实,加上鼠标和键盘简单操作。
资源推荐
资源详情
资源评论
收起资源包目录
OpenGL_日地月模型动画.rar (33个子文件)
OpenGL_日地月模型动画
moon.bmp 768KB
earth.bmp 3MB
HelloGlut.plg 1KB
glut32.dll 216KB
sun.bmp 3MB
Free-Wood-Texture-48.bmp 2.25MB
GL
tube.h 9KB
fgl.h 61KB
glutf90.h 4KB
fglu.h 7KB
fglut.h 10KB
glut.h 27KB
glsmap.h 5KB
HelloGlut.dsp 5KB
glut32.lib 28KB
HelloGlut.exe 232KB
HelloGlut.ncb 105KB
HelloGlut.opt 48KB
HelloGlut.dsw 543B
HelloGlut.cpp 7KB
Debug
HelloGlut.pch 4.48MB
HelloGlut.ilk 371KB
PlyInput.sbr 15KB
PlyInput.obj 10KB
Rolling-earth.obj 21KB
HelloGlut.sbr 984KB
vc60.idb 409KB
HelloGlut.exe 232KB
Rolling-earth.sbr 988KB
HelloGlut.obj 29KB
HelloGlut.bsc 1.93MB
vc60.pdb 76KB
HelloGlut.pdb 649KB
共 33 条
- 1
资源评论
DGSsession
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功