#include "OBJLoader.h"
static char* stralloc(const char *string)
{
char *copy;
copy = (char*) malloc(strlen(string) + 1);
if (copy == NULL)
return NULL;
strcpy(copy, string);
return copy;
}
static char* DirName(char* path)
{
char* dir;
char* s;
dir = stralloc(path);
s = strrchr(dir, '\\');
if (s)
s[1] = '\0';
else
dir[0] = '\0';
return dir;
}
COBJLoader::COBJLoader()
{
}
COBJLoader::~COBJLoader()
{
GLuint i;
GLMOBJgroup* group;
if (model->pathname) free(model->pathname);
if (model->mtllibname) free(model->mtllibname);
if (model->vertices) free(model->vertices);
if (model->normals) free(model->normals);
if (model->texcoords) free(model->texcoords);
if (model->facetnorms) free(model->facetnorms);
if (model->triangles) free(model->triangles);
if (model->materials) {
for (i = 0; i < model->nummaterials; i++)
free(model->materials[i].name);
}
free(model->materials);
while(model->groups) {
group = model->groups;
model->groups = model->groups->next;
free(group->name);
free(group->triangles);
free(group);
}
//free(model);
}
void COBJLoader::Init(char* filename)
{
model = (GLMOBJmodel*)malloc(sizeof(GLMOBJmodel));
model->pathname = stralloc(filename);
model->mtllibname = NULL;
model->numvertices = 0;
model->vertices = NULL;
model->numnormals = 0;
model->normals = NULL;
model->numtexcoords = 0;
model->texcoords = NULL;
model->numfacetnorms = 0;
model->facetnorms = NULL;
model->numtriangles = 0;
model->triangles = NULL;
model->nummaterials = 0;
model->materials = NULL;
model->numgroups = 0;
model->groups = NULL;
model->position[0] = 0.0;
model->position[1] = 0.0;
model->position[2] = 0.0;
ImportOBJFile(filename);
}
bool COBJLoader::ImportOBJFile(char* filename)
{
GLuint numvertices;
/* number of vertices in model */
GLuint numnormals;
/* number of normals in model */
GLuint numtexcoords;
/* number of texcoords in model */
GLuint numtriangles;
/* number of triangles in model */
GLMOBJgroup* group;
/* current group */
GLfloat* vertices;
/* array of vertices */
GLfloat* normals;
/* array of normals */
GLfloat* texcoords;
/* array of texture coordinates */
GLuint material;
/* current material */
GLfloat tmpYcoord;
unsigned v, n, t;
char buf[128];
numvertices = numnormals = numtexcoords = numtriangles = 0;
FILE* file;
file=fopen(filename,"r");
if(!file)
{
exit(1);
}
while(fscanf(file, "%s", buf) != EOF) {
//每一次读一行
switch(buf[0]) {
case '#':
/* comment */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
case 'v': /* v, vn, vt */
switch(buf[1]) {
case '\0': /* vertex */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
numvertices++;
break;
case 'n': /* normal */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
numnormals++;
break;
case 't': /* texcoord */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
numtexcoords++;
break;
default:
exit(1);
break;
}
break;
case 'm':
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
model->mtllibname =stralloc(buf);
ReadMTLFile(buf);
break;
case 'u':
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
case 'g': /* group */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", buf);
group = AddGroup(buf);
break;
case 'f': /* face */
v = n = t = 0;
fscanf(file, "%s", buf);
/* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
if (strstr(buf, "//")) {
/* v//n */
sscanf(buf, "%d//%d", &v, &n);
fscanf(file, "%d//%d", &v, &n);
fscanf(file, "%d//%d", &v, &n);
numtriangles++;
group->numtriangles++;
while(fscanf(file, "%d//%d", &v, &n) > 0) {
numtriangles++;
group->numtriangles++;
}
} else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
/* v/t/n */
fscanf(file, "%d/%d/%d", &v, &t, &n);
fscanf(file, "%d/%d/%d", &v, &t, &n);
numtriangles++;
group->numtriangles++;
while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) {
numtriangles++;
group->numtriangles++;
}
} else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
/* v/t */
fscanf(file, "%d/%d", &v, &t);
fscanf(file, "%d/%d", &v, &t);
numtriangles++;
group->numtriangles++;
while(fscanf(file, "%d/%d", &v, &t) > 0) {
numtriangles++;
group->numtriangles++;
}
} else {
/* v */
fscanf(file, "%d", &v);
fscanf(file, "%d", &v);
numtriangles++;
group->numtriangles++;
while(fscanf(file, "%d", &v) > 0) {
numtriangles++;
group->numtriangles++;
}
}
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
/* set the stats in the model structure */
model->numvertices = numvertices;
model->numnormals = numnormals;
model->numtexcoords = numtexcoords;
model->numtriangles = numtriangles;
group = model->groups;
while(group) {
group->triangles = (GLuint*)malloc(sizeof(GLuint) * group->numtriangles);
group->numtriangles = 0;
group = group->next;
}
model->vertices = (GLfloat*)malloc(sizeof(GLfloat) *
3 * (model->numvertices + 1));
model->triangles = (GLMOBJtriangle*)malloc(sizeof(GLMOBJtriangle) *
model->numtriangles);
if (model->numnormals) {
model->normals = (GLfloat*)malloc(sizeof(GLfloat) *
3 * (model->numnormals + 1));
}
if (model->numtexcoords) {
model->texcoords = (GLfloat*)malloc(sizeof(GLfloat) *
2 * (model->numtexcoords + 1));
}
/* rewind to beginning of file and read in the data this pass */
rewind(file);
/* set the pointer shortcuts */
vertices = model->vertices;
normals = model->normals;
texcoords = model->texcoords;
/* on the second pass through the file, read all the data into the
allocated arrays */
numvertices = numnormals = numtexcoords = 1;
numtriangles = 0;
material = 0;
while(fscanf(file, "%s", buf) != EOF) {
switch(buf[0]) {
case '#': /* comment */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
case 'v': /* v, vn, vt */
switch(buf[1]) {
case '\0': /* vertex */
fscanf(file, "%f %f %f",
&vertices[3 * numvertices + 0],
&vertices[3 * numvertices + 1],
&vertices[3 * numvertices + 2]);
numvertices++;
break;
case 'n': /* normal */
fscanf(file, "%f %f %f",
&normals[3 * numnormals + 0],
&normals[3 * numnormals + 1],
&normals[3 * numnormals + 2]);
numnormals++;
break;
case 't': /* texcoord */
fscanf(file, "%f %f",
&texcoords[2 * numtexcoords + 0],
&tmpYcoord);
texcoords[2 * numtexcoords + 1] = -tmpYcoord;
numtexcoords++;
break;
}
break;
case 'u':
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
group->material = material = FindMaterial(buf);
break;
case 'g': /* group */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", buf);
group = FindGroup(buf);
group->material = material;
break;
case 'f': /* face */
v = n = t = 0;
fscanf(file, "%s", buf);
/* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
if (strstr(buf, "//")) {
/* v//n */
sscanf(buf, "%d//%d", &v, &n);
model->triangles[numtriangles].vindices[0] = v;
model->triangles[numtriangles].nindices[0] = n;
fscanf(file, "%d//%d", &v, &n);
model->triangl
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
在OPENGL环境下导入OBJ文件 (113个子文件)
cyborg.bmp 768KB
terrain.BMP 768KB
detail.BMP 768KB
top.BMP 192KB
cloud.BMP 192KB
back.BMP 96KB
left.BMP 96KB
right.BMP 96KB
front.BMP 96KB
OBJLoader.cpp 20KB
3DSLoader.cpp 17KB
TGALoader.cpp 11KB
GLFrame.cpp 10KB
GLWindow.cpp 9KB
Terrain.cpp 8KB
3DSFile.cpp 8KB
Camera.cpp 6KB
SkyBox.cpp 4KB
Font.cpp 4KB
CBMPLoader.cpp 3KB
Vector.cpp 2KB
stdafx.cpp 67B
3DSLoader.exe 695KB
3DSLoader.vcxproj.filters 3KB
3DSLoader.h 5KB
GLFrame.h 4KB
OBJLoader.h 3KB
3DSFile.h 3KB
GLWindow.h 2KB
Terrain.h 2KB
Camera.h 2KB
Vector.h 1KB
TGALoader.h 1KB
CBMPLoader.h 1KB
stdafx.h 1022B
SkyBox.h 1004B
Font.h 769B
tga.h 578B
UpgradeLog.htm 26KB
BuildLog.htm 3KB
vc110.idb 947KB
3DSLoader.ilk 1.44MB
Desktop_.ini 9B
preview.jpg 54KB
3DSLoader.lastbuildstate 46B
3DSLoader.log 25KB
3DSLoader.Build.CppClean.log 1KB
vanille_obj.mtl 1KB
nude_obj.mtl 760B
3DSLoader.ncb 491KB
vanille_obj.obj 2.48MB
nude_obj.obj 1.25MB
3DSLoader.obj 226KB
3DSFile.obj 88KB
OBJLoader.obj 62KB
Terrain.obj 48KB
GLFrame.obj 46KB
SkyBox.obj 46KB
Camera.obj 44KB
GLWindow.obj 40KB
TGALoader.obj 39KB
Vector.obj 36KB
Font.obj 33KB
CBMPLoader.obj 29KB
stdafx.obj 24KB
3DSLoader.pdb 2.78MB
vc110.pdb 284KB
terrain.raw 1024KB
3DSLoader.sdf 30.38MB
3DSLoader.sln 907B
3DSLoader.sln 879B
3DSLoader.v11.suo 72KB
3DSLoader.v11.suo 10KB
3DSLoader.suo 10KB
c595_00.tga 6MB
c595_02.tga 6MB
c005_06.tga 6MB
c005_04.tga 3MB
c005_17.tga 3MB
c595_04.tga 3MB
c005_15.tga 3MB
c005_03.tga 3MB
c595_03.tga 3MB
c005_05.tga 1.5MB
c005_16.tga 768KB
c005_14.tga 768KB
c005_09.tga 768KB
c005_02.tga 512KB
c005_01.tga 512KB
c595_01.tga 384KB
c005_07.tga 384KB
c595_06.tga 384KB
c005_12.tga 256KB
c005_10.tga 192KB
c005_08.tga 192KB
c595_05.tga 192KB
box.tga 188KB
c005_00.tga 128KB
c005_13.tga 96KB
c005_11.tga 48KB
共 113 条
- 1
- 2
资源评论
c1aaaaaaa
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功