/* Recursive subdivision of cube (Chapter 6). Three display
modes: wire frame, constant, and interpolative shading */
/*Program also illustrates defining materials and light sources
in myiit() */
/* mode 0 = wire frame, mode 1 = constant shading,
mode 3 = interpolative shading */
#include<windows.h>
#include <GL/glut.h>
#include <math.h>
#include <GL/SOIL.h>
#include <stdio.h>
#define M_PI 3.14159
typedef float point[4];
/* initial tetrahedron */
point v[] = { { 0.0, 0.0, 1.0 }, { 0.0, 0.942809, -0.33333 },
{ -0.816497, -0.471405, -0.333333 }, { 0.816497, -0.471405, -0.333333 } };
int n;
int mode;
int projectStyle = 1;
int winWidth = 500, winHeight = 500;
bool redrawContinue = false;
float lastPos[3] = { 0.0F, 0.0F, 0.0F };
float angle = 0.0;
GLfloat axis[3] = { 0.0, 0.0, 0.1 };
int curx, cury;
int startX, startY;
void trackball_ptov(int x, int y, int width, int height, float v[3])
{
float d, a;
v[0] = (2.0F*x - width) / width;
v[1] = (height - 2.0F*y) / height;
d = (float)sqrt(v[0] * v[0] + v[1] * v[1]);
v[2] = (float)cos((M_PI / 2.0F) * ((d < 1.0F) ? d : 1.0F));
a = 1.0F / (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] *= a;
v[1] *= a;
v[2] *= a;
}
void startMotion(int x, int y)
{
redrawContinue = false;
startX = x; startY = y;
curx = x; cury = y;
trackball_ptov(x, y, winWidth, winHeight, lastPos);
}
void stopMotion(int x, int y)
{
redrawContinue = true;
}
void triangle(point a, point b, point c)
/* display one triangle using a line loop for wire frame, a single
normal for constant shading, or three normals for interpolative shading */
{
if (mode == 0) glBegin(GL_LINE_LOOP); else glBegin(GL_POLYGON);
if (mode == 1) glNormal3fv(a);
if (mode == 2) glNormal3fv(a);
glVertex3fv(a);
if (mode == 2) glNormal3fv(b);
glVertex3fv(b);
if (mode == 2) glNormal3fv(c);
glVertex3fv(c);
glEnd();
}
void normal(point p)
{
/* normalize a vector */
double sqrt();
float d = 0.0;
int i;
for (i = 0; i<3; i++) d += p[i] * p[i];
//d = sqrt(d);
d = sqrtf(d);
if (d>0.0) for (i = 0; i<3; i++) p[i] /= d;
}
void divide_triangle(point a, point b, point c, int m)
{
/* triangle subdivision using vertex numbers
righthand rule applied to create outward pointing faces */
point v1, v2, v3;
int j;
if (m>0)
{
for (j = 0; j<3; j++) v1[j] = a[j] + b[j];
normal(v1);
for (j = 0; j<3; j++) v2[j] = a[j] + c[j];
normal(v2);
for (j = 0; j<3; j++) v3[j] = b[j] + c[j];
normal(v3);
divide_triangle(a, v1, v2, m - 1);
divide_triangle(c, v2, v3, m - 1);
divide_triangle(b, v3, v1, m - 1);
divide_triangle(v1, v3, v2, m - 1);
}
else(triangle(a, b, c)); /* draw triangle at end of recursion */
}
void tetrahedron(int m)
{
/* Apply triangle subdivision to faces of tetrahedron */
divide_triangle(v[0], v[1], v[2], m);
divide_triangle(v[3], v[2], v[1], m);
divide_triangle(v[0], v[3], v[1], m);
divide_triangle(v[0], v[2], v[3], m);
}
void Perspective(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glFrustum(-2.0, 2.0, -2.0 * (GLfloat)h / (GLfloat)w,
2.0 * (GLfloat)h / (GLfloat)w, 2, 10);
else
glFrustum(-2.0 * (GLfloat)w / (GLfloat)h,
2.0 * (GLfloat)w / (GLfloat)h, -2.0, 2.0, 2, 10);
glutPostRedisplay();
winWidth = w;
winHeight = h;
}
void Orthographic(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat)h / (GLfloat)w,
2.0 * (GLfloat)h / (GLfloat)w, 2, 10);
else
glOrtho(-2.0 * (GLfloat)w / (GLfloat)h,
2.0 * (GLfloat)w / (GLfloat)h, -2.0, 2.0, 2, 10);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
winWidth = w;
winHeight = h;
}
float CompositeTransMatrix[4][4] = { { 1.0, 0.0, 0.0, 0.0 },{ 0.0, 1.0, 0.0, 0.0 },{ 0.0, 0.0, 1.0, 0.0 },{ 0.0, 0.0, 0.0, 1.0 } };
float *p_CompositeTransMatrix = *CompositeTransMatrix;
float Mlookup[4][4] = { { 1.0, 0.0, 0.0, 0.0 },{ 0.0, 1.0, 0.0, 0.0 },{ 0.0, 0.0, 1.0, 0.0 },{ 0.0, 0.0, 0.0, 1.0 } };
float *p_Mlookup = *Mlookup;
void display(void)
{
/* Displays all three modes, side by side */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, p_Mlookup);
glLoadIdentity();
glRotatef(angle, axis[0], axis[1], axis[2]);
glMultMatrixf(p_CompositeTransMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX, p_CompositeTransMatrix);
glLoadMatrixf(p_Mlookup);
glMultMatrixf(p_CompositeTransMatrix);
mode = 1;
glTranslatef(-2.0, 0.0, 0.0);
tetrahedron(n);
mode = 2;
glTranslatef(4.0, 0.0, 0.0);
// tetrahedron(n);
glBegin(GL_QUADS);
glNormal3f( 0.0F, 0.0F, 1.0F);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f,-0.5f, 0.5f);
glVertex3f( 0.5f,-0.5f, 0.5f);
//1----------------------------
glNormal3f( 0.0F, 0.0F,-1.0F);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f, 0.5f,-0.5f);
glVertex3f( 0.5f, 0.5f,-0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);
//2----------------------------
glNormal3f( 0.0F, 1.0F, 0.0F);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f,-0.5f);
glVertex3f(-0.5f, 0.5f,-0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
//3----------------------------
glNormal3f( 0.0F,-1.0F, 0.0F);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);
glVertex3f( 0.5f,-0.5f, 0.5f);
glVertex3f(-0.5f,-0.5f, 0.5f);
//4----------------------------
glNormal3f( 1.0F, 0.0F, 0.0F);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f,-0.5f, 0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);
glVertex3f( 0.5f, 0.5f,-0.5f);
//5----------------------------
glNormal3f(-1.0F, 0.0F, 0.0F);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f,-0.5f);
//6----------------------------*/
glEnd();
glFlush();
}
void myMouse(int Botton, int State, int MouseX, int MouseY)
{
if (Botton == GLUT_LEFT_BUTTON)
{
switch (State)
{
case GLUT_DOWN:
startMotion(MouseX, MouseY);
break;
case GLUT_UP:
stopMotion(MouseX, MouseY);
break;
}
}
}
void mouseMotion(int x, int y)
{
float curPos[3], dx, dy, dz;
trackball_ptov(x, y, winWidth, winHeight, curPos);
dx = curPos[0] - lastPos[0];
dy = curPos[1] - lastPos[1];
dz = curPos[2] - lastPos[2];
if (dx || dy || dz) {
angle = 90.0F * sqrt(dx*dx + dy*dy + dz*dz);
axis[0] = lastPos[1] * curPos[2] - lastPos[2] * curPos[1];
axis[1] = lastPos[2] * curPos[0] - lastPos[0] * curPos[2];
axis[2] = lastPos[0] * curPos[1] - lastPos[1] * curPos[0];
lastPos[0] = curPos[0];
lastPos[1] = curPos[1];
lastPos[2] = curPos[2];
}
glutPostRedisplay();
}
void Draw_menu(int index)
{
switch (index)
{
case (1) :
{
Orthographic(winWidth, winHeight);
projectStyle = 1;
glutPostRedisplay();
break;
}
case (2) :
{
Perspective(winWidth, winHeight);
projectStyle = 2;
glutPostRedisplay();
break;
}
}
}
void KeyFunction(unsigned char key, int x, int y)
{
if(key == 'q' || key == 'Q')
exit(0);
if (key == 48) {
n = 0;
glutPostRedisplay();
}
if (key == 49) {
n = 1;
glutPostRedisplay();
}
if (key == 50) {
n = 2;
glutPostRedisplay();
}
if (key == 51) {
n = 3;
glutPostRedisplay();
}
if (key == 52) {
n = 4;
glutPostRedisplay();
}
if (key == 53) {
n = 5;
glutPostRedisplay();
}
if (key == 54) {
n = 6;
glutPostRedisplay();
}
if (key == 55) {
没有合适的资源?快使用搜索试试~ 我知道了~
3D物体建模和渲染的跟踪球交互设计和实现
共16个文件
pdb:2个
plg:1个
idb:1个
需积分: 9 11 下载量 129 浏览量
2018-01-03
14:36:57
上传
评论
收藏 1.55MB ZIP 举报
温馨提示
综合应用计算机图形学老师所教授的理论知识,本实验采用openGL编程,实现一个基于3D物体建模和渲染的跟踪球交互应用程序,应用程序主要实现以下功能: ① 画出两个图形,其中一个是可以递归细分的球面,一个是立方体; ① 在WC坐标原点处添加一个光源,物体表面将会有光照的颜色; ② 除此之外,程序能够给物体表面贴图,因此物体表面还混有纹理贴图的颜色; 程序还应能够对物体进行交互,包括: ③ 键盘交互,能够用键盘的0~8控制球面的递归细分的次数。用键盘+,-控制物体透明程度,A在0..1之间变化,其中初始为不透明; ④ 鼠标交互,程序采用模拟跟踪球交互技术实现景物观察,通过控制鼠标左键、中键、右键进而控制物体的拖动旋转、转动、投影变换。
资源推荐
资源详情
资源评论
收起资源包目录
CGraphicFinal1.zip (16个子文件)
CGraphicFinal1
CGraphicFinal1.dsp 4KB
Debug
vc60.pdb 76KB
CGraphicFinal1.ilk 244KB
vc60.idb 145KB
CGraphicFinal1.pdb 481KB
testFinal1.obj 35KB
CGraphicFinal1.exe 204KB
CGraphicFinal1.pch 3.52MB
CGraphicFinal1.opt 48KB
png1.bmp 791KB
libSOIL.a 78KB
CGraphicFinal1.ncb 49KB
CGraphicFinal1.dsw 536B
SOIL.h 15KB
CGraphicFinal1.plg 1KB
testFinal1.cpp 10KB
共 16 条
- 1
资源评论
蜗居Liu
- 粉丝: 12
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功