#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <GL/gl.h>
#include "glut.h"
#include "glext.h"
#include <GL/glaux.h>
#define SideLen 1.513060f
#define PI 3.1415926536
void myinit(void);
void display(void);
void reshape(GLsizei w, GLsizei h);
void LoadTextures();
void DrawGeometry();
void ParametricEval(float u, float v, GLfloat *P, GLfloat *N);
void LoadBunny();
void LoadPlane();
void SetupRC();
static float rot;
int VexLineNum;
int IndLineNum;
GLfloat *vertices;
GLuint *Indices;
GLfloat *normals;
GLfloat *facenormals;
void myinit(void)
{
rot = 0.0;
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
SetupRC();
LoadBunny();
//LoadPlane();
}
void SetupRC()
{
GLfloat fLightPos[4] = { -100.0f, 100.0f, 50.0f, 1.0f }; // Point source
GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
// Grayish background
glClearColor(fLowLight[0], fLowLight[1], fLowLight[2], fLowLight[3]);
// Cull backs of polygons
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
// Setup light parameters
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight);
glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
void SetupRC1()
{
// Light values and coordinates
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = {1.0f, 0.0f, 0.0f, 1.0f};
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside of jet
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
//
glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
glMateriali(GL_FRONT, GL_SHININESS, 128);
// Light blue background
glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
}
void reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( w<h)
glOrtho(-4.0, 4.0, -4.0*(GLfloat)h/(GLfloat)w, 4.0*(GLfloat)h/(GLfloat)w,-4.0, 4.0);
else
glOrtho(-4.0*(GLfloat)w/(GLfloat)h, 4.0*(GLfloat)w/(GLfloat)h, -4.0, 4.0,-4.0, 4.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void DrawGeometry()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glDrawElements(GL_TRIANGLES, 3*IndLineNum, GL_UNSIGNED_INT, Indices);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void display(void)
{
static int curTime = 0;
glClearColor(.25, .25, .25, 1.);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glPushMatrix();
glPushMatrix();
glPushMatrix();
glPushMatrix();
glPushMatrix();
//front
glTranslatef(-2.5*SideLen, 0, 0);
DrawGeometry();
glPopMatrix();
//back
glTranslatef(-1.5*SideLen, 0, 0);
glRotatef(-90, 0, 1, 0);
DrawGeometry();
glPopMatrix();
//left
glTranslatef(-0.5*SideLen, 0, 0);
glRotatef(180, 0, 1, 0);
DrawGeometry();
glPopMatrix();
//right
glTranslatef(0.5*SideLen, 0, 0);
glRotatef(90, 0, 1, 0);
DrawGeometry();
glPopMatrix();
//top
glTranslatef(1.5*SideLen, 0, 0);
glRotatef(90, 1, 0, 0);
DrawGeometry();
glPopMatrix();
//bottom
glTranslatef(2.5*SideLen, 0, 0);
glRotatef(-90, 1, 0, 0);
DrawGeometry();
glPopMatrix();
//glScaled(1, 1, 1);
//glutSolidTeapot(1.5);
glPopMatrix();
_sleep(2);
glutSwapBuffers();
++curTime;
// Force another display(), so that the object keeps moving
glutPostRedisplay();
rot += 1.0;
}
void keyboard ( unsigned char key, int x, int y )
{
switch ( key ) {
case 27:
exit ( 0 );
break;
case 'd':
display();
break;
default:
break;
}
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow(argv[0]);
myinit();
glutReshapeFunc(reshape);
glutKeyboardFunc( keyboard );
glutDisplayFunc(display);
glutMainLoop();
}
void LoadBunny()
{
int i,j;
FILE *fp;
fp = fopen("bunny.x","r");
if(!fp)
{
printf("can not find model file\n");
exit(1);
}
fscanf(fp, "%d;\n", &VexLineNum);
vertices = (GLfloat *) malloc(VexLineNum * 3 * sizeof(GLfloat));
for( i=0; i< VexLineNum; i++)
{
fscanf(fp, "%f;%f;%f;\n", vertices+3*i, vertices+3*i+1, vertices+3*i+2);
*(vertices+3*i) *=10;
*(vertices+3*i+1) *=10;
*(vertices+3*i+2) *=10;
}
fscanf(fp, "%d;\n", &IndLineNum);
Indices = (GLuint *) malloc(IndLineNum * 3 * sizeof(GLuint));
for( i=0; i< IndLineNum; i++)
{
fscanf(fp, "%d;%u,%u,%u;\n", &j, Indices+3*i, Indices+3*i+1, Indices+3*i+2);
}
float a1,a2,a3, b1,b2,b3, c1,c2,c3,c;
int *FaceInPoint;
facenormals = (GLfloat *) malloc(IndLineNum * 3 * sizeof(GLfloat));
normals = (GLfloat *) malloc(VexLineNum * 3 * sizeof(GLfloat));
FaceInPoint = (int *) malloc(VexLineNum * sizeof(int));
memset(normals, 0, VexLineNum * 3 * sizeof(GLfloat));
memset(FaceInPoint, 0, VexLineNum * sizeof(int));
for(i=0; i< IndLineNum; i++)
{
a1=*(vertices + 3*(*(Indices+3*i))) - *(vertices + 3*(*(Indices+3*i+1)));
a2=*(vertices + 3*(*(Indices+3*i)) + 1) - *(vertices + 3*(*(Indices+3*i+1)) +1);
a3=*(vertices + 3*(*(Indices+3*i)) + 2) - *(vertices + 3*(*(Indices+3*i+1)) +2);
b1=*(vertices + 3*(*(Indices+3*i+1))) - *(vertices + 3*(*(Indices+3*i+2)));
b2=*(vertices + 3*(*(Indices+3*i+1)) + 1) - *(vertices + 3*(*(Indices+3*i+2)) +1);
b3=*(vertices + 3*(*(Indices+3*i+1)) + 2) - *(vertices + 3*(*(Indices+3*i+2)) +2);
c1 = (a2*b3 - a3*b2);
c2 = (a3*b1 - a1*b3);
c3 = (a1*b2 - a2*b1);
c = sqrt(c1*c1 + c2*c2 +c3*c3);
*(facenormals + 3*i) = c1/c;
*(facenormals + 3*i +1) = c2/c;
*(facenormals + 3*i +2) = c3/c;
*(normals + 3*(*(Indices+3*i))) += c1/c;
*(normals + 3*(*(Indices+3*i)) + 1) += c2/c;
*(normals + 3*(*(Indices+3*i)) + 2) += c3/c;
*(normals + 3*(*(Indices+3*i+1))) += 2 * c1/c;
*(normals + 3*(*(Indices+3*i+1)) + 1) += 2 * c2/c;
*(normals + 3*(*(Indices+3*i+1)) + 2) += 2 * c3/c;
*(normals + 3*(*(Indices+3*i+2))) += c1/c;
*(normals + 3*(*(Indices+3*i+2)) + 1) += c2/c;
*(normals + 3*(*(Indices+3*i+2)) + 2) += c3/c;
*(FaceInPoint + *(Indices+3*i)) +=1;
*(FaceInPoint + *(Indices+3*i+1)) +=2;
*(FaceInPoint + *(Indices+3*i+2)) +=1;
}
for( i=0; i< 3 * VexLineNum; i++)
{
if(*(FaceInPoint + i/3) > 0)
*(normals + i) /= *(FaceInPoint+ i/3);
}
free(FaceInPoint);
}
没有合适的资源?快使用搜索试试~ 我知道了~
OpenGL绘制的兔子
共41个文件
x:7个
bak:3个
cpp:3个
5星 · 超过95%的资源 需积分: 50 52 下载量 170 浏览量
2009-01-13
21:41:43
上传
评论 3
收藏 2.97MB RAR 举报
温馨提示
这里采用OpenGL,不是在MFC环境下画的图,从多个侧面进行绘画,且有一定的光照效果,很逼真。
资源推荐
资源详情
资源评论
收起资源包目录
arraytest.rar (41个子文件)
arraytest
glut.h 21KB
test.opt 48KB
arraytest.plg 909B
arraytest.dsp 4KB
glext.h 326KB
f22.x 800KB
test.plg 1KB
test.ncb 33KB
Data
4.bmp 257KB
Thumbs.db 28KB
test.cpp 8KB
复件 test.cpp 8KB
4gfly.txt.bak 237KB
1.tri 308KB
4gfly.x 917KB
Debug
test.exe 196KB
vc60.pdb 76KB
vc60.idb 225KB
test.pdb 505KB
test.ilk 241KB
arraytest.pdb 401KB
test.pch 3.84MB
arraytest.pch 3.84MB
arraytest.ilk 246KB
test.obj 27KB
arraytest.exe 196KB
bunny.txt.bak 29KB
arraytest.opt 48KB
1.x 388B
glut test.cpp 2KB
bunny.x 29KB
1.txt.bak 491B
f35a.tri 148KB
test.dsp 3KB
zhendi.x 186KB
arraytest.dsw 541B
test.dsw 533B
arraytest.ncb 49KB
box.tri 932B
f16.x 32KB
2.x 33KB
共 41 条
- 1
资源评论
- 普通网友2014-10-06兔子有一点模糊,代码详尽,很不错
- vivian3033032013-06-15代码详尽,框架清晰,有一定的实用性。
- u0132404662013-12-27代码详尽,框架清晰,有一定的实用性。
- Anoddity2013-12-15运行没成功
- 雨_水2013-12-18还可以 从多个侧面显示了兔子 有一定的参考意义 不过兔子的模型好像不完整。
zck08
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功