资源简介
OpenGL读取bunny文件,并且画出来。画出斯坦福兔子
代码片段和文件信息
/*======================
opengl + bunny
========================*/
#include
#include
#include
#include
#include
#include
#include
#include “windows.h“
using namespace std;
#define POINT_MAX 40000 //max number of point
#define TRIANGLE_MAX 70000 //max number of triangle
#define max_num 999999999
struct Point
{
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat nx; //normal vector
GLfloat ny;
GLfloat nz;
int num;
};
struct Triangle
{
GLint point1;
GLint point2;
GLint point3;
GLfloat nx;
GLfloat ny;
GLfloat nz;
};
Point point[POINT_MAX];
Triangle triangle[TRIANGLE_MAX];
int NumOfPoint=0NumOfTriangle=0;
GLfloat min_x=max_nummin_y=max_nummin_z=max_nummax_x=-max_nummax_y=-max_nummax_z=-max_num;
void readfile()
{
ifstream fin(“bunny.vt“); //read file
fin>>NumOfPoint>>NumOfTriangle;
for(int i=0;i {
fin>>point[i].x>>point[i].y>>point[i].z;
point[i].num=0;
point[i].nx=0;
point[i].ny=0;
point[i].nz=0;
//calculate the min and max of the coord
if(point[i].x if(point[i].x>max_x)max_x=point[i].x;
if(point[i].y if(point[i].y>max_y)max_y=point[i].y;
if(point[i].z if(point[i].z>max_z)max_z=point[i].z;
}
for(int i=0;i {
fin>>triangle[i].point1>>triangle[i].point2>>triangle[i].point3;
GLfloat a1b1c1a2b2c2; //two vector [a1b1c1] and [a2b2c2]
a1=point[triangle[i].point1].x-point[triangle[i].point2].x;
b1=point[triangle[i].point1].y-point[triangle[i].point2].y;
c1=point[triangle[i].point1].z-point[triangle[i].point2].z;
a2=point[triangle[i].point1].x-point[triangle[i].point3].x;
b2=point[triangle[i].point1].y-point[triangle[i].point3].y;
c2=point[triangle[i].point1].z-point[triangle[i].point3].z;
//then compute triangle normal vertex
triangle[i].nx=b1*c2-b2*c1;
triangle[i].ny=a2*c1-a1*c2;
triangle[i].nz=a1*b2-a2*b1;
//calcuate three points‘ normal vertex
point[triangle[i].point1].num++;
point[triangle[i].point1].nx+=triangle[i].nx;
point[triangle[i].point1].ny+=triangle[i].ny;
point[triangle[i].point1].nz+=triangle[i].nz;
point[triangle[i].point2].num++;
point[triangle[i].point2].nx+=triangle[i].nx;
point[triangle[i].point2].ny+=triangle[i].ny;
point[triangle[i].point2].nz+=triangle[i].nz;
point[triangle[i].point3].num++;
point[triangle[i].point3].nx+=triangle[i].nx;
point[triangle[i].point3].ny+=triangle[i].ny;
point[triangle[i].point3].nz+=triangle[i].nz;
}
for(int i=0;i {
point[i].nx/=point[i].num;
point[i].ny/=point[i].num;
point[i].nz/=point[i].num;
//normalize the vertex normal
GLfloat nor=sqrt(pow(point[i].nx2)+pow(point[i].ny2)+pow(point[i].nz2));
point[i].nx/=nor;
point[i]
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-08-06 16:12 bunny\
目录 0 2015-08-06 16:06 bunny\Debug\
文件 104960 2015-08-06 16:12 bunny\Debug\opengl.exe
文件 1085248 2015-08-06 16:12 bunny\Debug\opengl.ilk
文件 1084416 2015-08-06 16:12 bunny\Debug\opengl.pdb
目录 0 2015-08-06 13:57 bunny\opengl\
文件 32309248 2015-08-06 16:12 bunny\opengl.sdf
文件 885 2015-07-24 20:10 bunny\opengl.sln
文件 21504 2015-08-06 16:12 bunny\opengl.v11.suo
文件 2285487 2003-08-21 09:44 bunny\opengl\bunny.vt
目录 0 2015-08-06 16:12 bunny\opengl\Debug\
文件 1518 2015-08-06 16:12 bunny\opengl\Debug\cl.command.1.tlog
文件 46052 2015-08-06 16:12 bunny\opengl\Debug\CL.read.1.tlog
文件 312 2015-08-06 16:12 bunny\opengl\Debug\CL.write.1.tlog
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
文件 2 2015-08-06 16:12 bunny\opengl\Debug\li
............此处省略34个文件信息
评论
共有 条评论