// sample.cpp
// sample program demonstrating use of the arcball.
// see arcball.h for an explanation of what the
// arcball_*** functions do.
//
// must be compiled with glut
//
// -- Bradley Smith, 5/05/2008
#include <stdio.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <cstdlib>
#include <cmath>
#include "arcball.h"
// =================
// Drawing the Scene
// =================
static float aspect_ratio = 1.0f;
static int width, height;
// scene parameters
const vec eye( 0.0f, 0.0f, -20.0f );
const vec centre( 0.0f, 0.0f, 0.0f );
const vec up( 0.0f, 1.0f, 0.0f );
float SPHERE_RADIUS = 5.0f;
const int SPHERE_LAT_SLICES = 12;
const int SPHERE_LONG_SLICES = 24;
const int NUM_STARS = 256;
static vec star[NUM_STARS];
vec coord;
GLuint texture[1];
GLUquadricObj *quadratic;
const float PI = 3.141592654f;
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("Data/earth.bmp"))
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(1, &texture[0]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
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 Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
return Status; // Return The Status
}
static void reset_view(int w, int h)
{
width = w;
height = h;
aspect_ratio = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 50.0f, aspect_ratio, 1.0f, 50.0f );
gluLookAt(
eye.x, eye.y, eye.z,
centre.x, centre.y, centre.z,
up.x, up.y, up.z );
// set up the arcball using the current projection matrix
arcball_setzoom( SPHERE_RADIUS, eye, up );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
inline float randf()
{
return ((1.0f / 127.f) * (((float)(rand() & 255)) - 128.0f)) ;
}
static void startup_scene()
{
// generate random starfield
for ( int i=0; i < NUM_STARS; ++i )
{
star[i] = vec( randf(), randf(), randf() ).unit() * 2.0f;
}
}
static void shutdown_scene()
{
// nothing to be done here
}
static void draw_stars()
{
glBegin( GL_POINTS );
glColor3f( 1.0f, 1.0f, 1.0f );
for ( int i=0; i < NUM_STARS; ++i )
{
glVertex3f( star[i].x, star[i].y, star[i].z );
}
glEnd();
}
inline vec rotate_x( vec v, float sin_ang, float cos_ang )
{
return vec(
v.x,
(v.y * cos_ang) + (v.z * sin_ang),
(v.z * cos_ang) - (v.y * sin_ang)
);
}
inline vec rotate_y( vec v, float sin_ang, float cos_ang )
{
return vec(
(v.x * cos_ang) + (v.z * sin_ang),
v.y,
(v.z * cos_ang) - (v.x * sin_ang)
);
}
static void draw_sphere()
{/*
const int NUM_FACE_COLOURS = 4;
vec FACE_COLOURS[NUM_FACE_COLOURS] = {
vec( 1.0f, 0.0f, 0.0f ),
vec( 0.0f, 1.0f, 0.0f ),
vec( 0.0f, 0.0f, 1.0f ),
vec( 1.0f, 1.0f, 0.0f )
};
const float lat_angle = PI / (float)SPHERE_LAT_SLICES;
const float long_angle = 2.0f * PI / (float)SPHERE_LONG_SLICES;
const float sin_lat = sin( lat_angle );
const float cos_lat = cos( lat_angle );
const float sin_long = sin( long_angle );
const float cos_long = cos( long_angle );
glBegin( GL_QUADS );
vec lat_0( 0.0f, SPHERE_RADIUS, 0.0 );
for ( int y = 0; y < SPHERE_LAT_SLICES; ++y )
{
vec lat_1 = rotate_x( lat_0, sin_lat, cos_lat );
vec long_0_0 = lat_0;
vec long_1_0 = lat_1;
for ( int x = 0; x < SPHERE_LONG_SLICES; ++x )
{
vec long_0_1 = rotate_y( long_0_0, sin_long, cos_long );
vec long_1_1 = rotate_y( long_1_0, sin_long, cos_long );
vec colour = FACE_COLOURS[(x+y)%NUM_FACE_COLOURS];
glColor4f( colour.x, colour.y, colour.z, 1.0f );
glVertex3f( long_0_0.x, long_0_0.y, long_0_0.z );
glVertex3f( long_1_0.x, long_1_0.y, long_1_0.z );
glVertex3f( long_1_1.x, long_1_1.y, long_1_1.z );
glVertex3f( long_0_1.x, long_0_1.y, long_0_1.z );
long_0_0 = long_0_1;
long_1_0 = long_1_1;
}
lat_0 = lat_1;
}
glEnd();*/
quadratic=gluNewQuadric(); //绘制类型
//画图
// 创建二次几何体
gluQuadricNormals(quadratic, GLU_SMOOTH); // 使用平滑法线
gluQuadricTexture(quadratic, GL_TRUE);
glEnable(GL_TEXTURE_2D); // 启用纹理映射
glBindTexture(GL_TEXTURE_2D, texture[0]);
gluSphere(quadratic,SPHERE_RADIUS,48,48);
gluQuadricTexture(quadratic,GLU_TRUE); //建立纹理坐标
gluQuadricDrawStyle(quadratic,GLU_FILL); //用面填充
glDisable(GL_TEXTURE_2D);
}
static void click_scene(int x, int y)
{
int invert_y = (height - y) - 1; // OpenGL viewport coordinates are Cartesian
arcball_start(x,invert_y);
}
static void drag_scene(int x, int y)
{
int invert_y = (height - y) - 1;
arcball_move(x,invert_y,(5/SPHERE_RADIUS));
}
static void draw_scene()
{
// stars: to simulate infinite distance,
// translate the sphere of stars to the eye
// and perform the arcball rotation around the eye
// (also disable depth test so they become the background)
glPushMatrix();
glDrawBuffer(GL_BACK); //指定在后台缓存中绘制图形
glLoadIdentity(); //初始化变换矩阵
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glShadeModel(GL_SMOOTH);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
//glDisable( GL_DEPTH_TEST );
glTranslatef( eye.x, eye.y, eye.z );
arcball_rotate();
//draw_stars();
glEnable( GL_DEPTH_TEST );
glPopMatrix();
// now render the regular scene under the arcball rotation about 0,0,0
// (generally you would want to render everything here)
arcball_rotate();
draw_sphere();
}
// ==============
// GLUT Callbacks
// ==============
static void resize(int w, int h)
{
reset_view(w,h);
}
static void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
draw_scene();
glutSwapBuffers();
}
static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
shutdown_scene();
exit(0);
break;
case '+':
SPHERE_RADIUS = SPHERE_RADIUS+1;
arcball_setzoom( SPHERE_RADIUS, eye, up );
break;
case '-':
if(SPHERE_RADIUS>1)
{
SPHERE_RADIUS = SPHERE_RADIUS-1;
arcball_setzoom( SPHERE_RADIUS, eye, up );
}
break;
default :
break;
}
glutPostRedisplay();
}
static
没有合适的资源?快使用搜索试试~ 我知道了~
opengl旋转3维图形
共21个文件
obj:2个
cpp:2个
sbr:2个
5星 · 超过95%的资源 需积分: 10 27 下载量 199 浏览量
2010-01-25
11:36:13
上传
评论
收藏 3.03MB RAR 举报
温馨提示
opengl旋转3维图形,旋转的地图 opengl旋转3维图形,旋转的地图
资源推荐
资源详情
资源评论
收起资源包目录
testearth.rar (21个子文件)
testball
testball.dsp 4KB
testball.plg 1KB
glut32.dll 232KB
arcball.h 3KB
Debug
testball.pch 4.59MB
sample.sbr 0B
vc60.pdb 84KB
testball.pdb 697KB
vc60.idb 265KB
testball.ilk 399KB
arcball.obj 39KB
sample.obj 38KB
testball.bsc 4.67MB
testball.exe 244KB
arcball.sbr 0B
testball.ncb 57KB
testball.dsw 539B
sample.cpp 9KB
arcball.cpp 7KB
testball.opt 53KB
data
earth.bmp 768KB
共 21 条
- 1
资源评论
- qinyuan_hu2013-08-20效果不错 旋转的地图 对我挺有帮助的
- 2837614942014-06-30效果不错,谢谢
- cowboy00002012-08-20效果不错可惜我需要C#的方案了
peng_2019
- 粉丝: 5
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功