OBJFile parser seems to work.
Can render a simple scene using it. TODO: Need add way to set material to childs.
This commit is contained in:
@@ -9,21 +9,23 @@
|
||||
#ifndef DORAYME_OBJFILE_H
|
||||
#define DORAYME_OBJFILE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <tuple.h>
|
||||
#include <shape.h>
|
||||
#include <group.h>
|
||||
#include <renderstat.h>
|
||||
|
||||
class OBJFile : public Shape
|
||||
{
|
||||
private:
|
||||
uint32_t allocatedFaceGroupCount;
|
||||
Shape* *faceGroupList;
|
||||
Group* *faceGroupList;
|
||||
uint32_t faceGroupCount;
|
||||
|
||||
uint32_t allocatedVertexCount;
|
||||
Tuple* *vertexList;
|
||||
Point* *vertexList;
|
||||
uint32_t vertexCount;
|
||||
|
||||
|
||||
private:
|
||||
Intersect localIntersect(Ray r);
|
||||
Tuple localNormalAt(Tuple point);
|
||||
@@ -33,11 +35,11 @@ public:
|
||||
uint32_t ignoredLines;
|
||||
|
||||
protected:
|
||||
void addGroup(Shape *group);
|
||||
void addVertex(Tuple *vertex);
|
||||
void addGroup(Group *group);
|
||||
void addVertex(Point *vertex);
|
||||
|
||||
void parseLine(char *line);
|
||||
int execLine(int argc, char *argv[]);
|
||||
void parseLine(char *line, uint32_t currentLine);
|
||||
int execLine(int argc, char *argv[], uint32_t currentLine);
|
||||
BoundingBox bounds;
|
||||
|
||||
public:
|
||||
@@ -46,7 +48,9 @@ public:
|
||||
|
||||
int parseOBJFile(const char *content);
|
||||
|
||||
|
||||
/* OBJ file expect the first vertice to be 1 and not 0 */
|
||||
Point vertices(uint32_t i) { return *this->vertexList[i - 1]; };
|
||||
Group *groups(uint32_t i) { return this->faceGroupList[i]; };
|
||||
Intersect intersect(Ray r);
|
||||
BoundingBox getLocalBounds();
|
||||
BoundingBox getBounds();
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <objfile.h>
|
||||
#include <math_helper.h>
|
||||
#include <group.h>
|
||||
#include <triangle.h>
|
||||
|
||||
#define MIN_ALLOC (2)
|
||||
#define DEFAULT_GROUP (0)
|
||||
@@ -25,11 +26,11 @@ OBJFile::OBJFile() : Shape(SHAPE_OBJFILE), ignoredLines(0)
|
||||
stats.addOBJFile();
|
||||
|
||||
this->allocatedFaceGroupCount = MIN_ALLOC;
|
||||
this->faceGroupList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
|
||||
this->faceGroupList = (Group **)calloc(sizeof(Group **), MIN_ALLOC);
|
||||
this->faceGroupCount = 0;
|
||||
|
||||
this->allocatedVertexCount = MIN_ALLOC;
|
||||
this->vertexList = (Tuple **)calloc(sizeof(Tuple *), MIN_ALLOC);
|
||||
this->vertexList = (Point **)calloc(sizeof(Point **), MIN_ALLOC);
|
||||
this->vertexCount = 0;
|
||||
|
||||
/* There is always a default group */
|
||||
@@ -61,12 +62,12 @@ OBJFile::OBJFile(const char *filepath) : OBJFile()
|
||||
}
|
||||
}
|
||||
|
||||
void OBJFile::addGroup(Shape *group)
|
||||
void OBJFile::addGroup(Group *group)
|
||||
{
|
||||
if ((this->faceGroupCount + 1) > this->allocatedFaceGroupCount)
|
||||
{
|
||||
this->allocatedFaceGroupCount *= 2;
|
||||
this->faceGroupList = (Shape **)realloc(this->faceGroupList, sizeof(Shape **) * this->allocatedFaceGroupCount);
|
||||
this->faceGroupList = (Group **)realloc(this->faceGroupList, sizeof(Group **) * this->allocatedFaceGroupCount);
|
||||
}
|
||||
|
||||
group->parent = this;
|
||||
@@ -77,12 +78,12 @@ void OBJFile::addGroup(Shape *group)
|
||||
this->bounds | group->getBounds();
|
||||
}
|
||||
|
||||
void OBJFile::addVertex(Tuple *vertex)
|
||||
void OBJFile::addVertex(Point *vertex)
|
||||
{
|
||||
if ((this->vertexCount + 1) > this->allocatedVertexCount)
|
||||
{
|
||||
this->allocatedVertexCount *= 2;
|
||||
this->vertexList = (Tuple **)realloc(this->vertexList, sizeof(Tuple **) * this->allocatedVertexCount);
|
||||
this->vertexList = (Point **)realloc(this->vertexList, sizeof(Point **) * this->allocatedVertexCount);
|
||||
}
|
||||
|
||||
this->vertexList[this->vertexCount++] = vertex;
|
||||
@@ -94,7 +95,7 @@ Intersect OBJFile::intersect(Ray r)
|
||||
int i, j;
|
||||
if (this->faceGroupCount > 0)
|
||||
{
|
||||
if (this->bounds.intesectMe(r))
|
||||
//if (this->bounds.intesectMe(r))
|
||||
{
|
||||
for (i = 0 ; i < this->faceGroupCount ; i++)
|
||||
{
|
||||
@@ -166,7 +167,18 @@ void OBJFile::updateTransform()
|
||||
|
||||
void OBJFile::dumpMe(FILE * fp)
|
||||
{
|
||||
int i;
|
||||
fprintf(fp, "\"Type\": \"OBJFile\",\n");
|
||||
fprintf(fp, "\"Objects\": {\n");
|
||||
for(i = 0; i < this->faceGroupCount; i++)
|
||||
{
|
||||
fprintf(fp, "\"%d\": {\n", i);
|
||||
this->faceGroupList[i]->dumpMe(fp);
|
||||
fprintf(fp, "},\n");
|
||||
}
|
||||
fprintf(fp, "},\n");
|
||||
|
||||
Shape::dumpMe(fp);
|
||||
}
|
||||
|
||||
#define MAX_LINE_LENGTH (512)
|
||||
@@ -201,7 +213,7 @@ int OBJFile::parseOBJFile(const char *content)
|
||||
memset(lineBuff, 0, MAX_LINE_LENGTH);
|
||||
strncpy(lineBuff, bufferPos, lineLength);
|
||||
|
||||
this->parseLine(lineBuff);
|
||||
this->parseLine(lineBuff, currentLineNum);
|
||||
|
||||
bufferPos += lineLength + 1;
|
||||
currentLineNum++;
|
||||
@@ -212,7 +224,7 @@ int OBJFile::parseOBJFile(const char *content)
|
||||
#define MAX_ARGS (15)
|
||||
|
||||
/* Parse the line into a couple ofr argc/argv using space as argument separator */
|
||||
void OBJFile::parseLine(char *line)
|
||||
void OBJFile::parseLine(char *line, uint32_t currentLine)
|
||||
{
|
||||
char *argv[MAX_ARGS];
|
||||
uint32_t argc = 0;
|
||||
@@ -232,7 +244,11 @@ void OBJFile::parseLine(char *line)
|
||||
*next = '\0';
|
||||
linePos = next - line;
|
||||
buffer = next + 1;
|
||||
argv[argc++] = buffer;
|
||||
/* Skip empty strings as it mean multiple spaces */
|
||||
if (strlen(buffer) > 0)
|
||||
{
|
||||
argv[argc++] = buffer;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -240,14 +256,67 @@ void OBJFile::parseLine(char *line)
|
||||
}
|
||||
}
|
||||
|
||||
if (this->execLine(argc, argv))
|
||||
if (this->execLine(argc, argv, currentLine))
|
||||
{
|
||||
this->ignoredLines++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Actually execute the line */
|
||||
int OBJFile::execLine(int argc, char *argv[])
|
||||
int OBJFile::execLine(int argc, char *argv[], uint32_t currentLine)
|
||||
{
|
||||
return 1;
|
||||
int ret = 1;
|
||||
if (strncmp(argv[0], "v", 1) == 0)
|
||||
{
|
||||
/* Vertice entry */
|
||||
if (argc != 4)
|
||||
{
|
||||
printf("ERROR: Malformed file at line %d: Vertices expect 3 parameters!\n", currentLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->addVertex(new Point(atof(argv[1]), atof(argv[2]), atof(argv[3])));
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
else if (strncmp(argv[0], "f", 1) == 0)
|
||||
{
|
||||
/* Vertice entry */
|
||||
if (argc == 4)
|
||||
{
|
||||
Shape *t = new Triangle(this->vertices(atoi(argv[1])),
|
||||
this->vertices(atoi(argv[2])),
|
||||
this->vertices(atoi(argv[3])));
|
||||
this->faceGroupList[this->faceGroupCount - 1]->addObject(t);
|
||||
ret = 0;
|
||||
}
|
||||
else if (argc > 4)
|
||||
{
|
||||
int i;
|
||||
for(i = 2; i < (argc - 1); i++)
|
||||
{
|
||||
Shape *t = new Triangle(this->vertices(atoi(argv[1])),
|
||||
this->vertices(atoi(argv[i])),
|
||||
this->vertices(atoi(argv[i+1])));
|
||||
this->faceGroupList[this->faceGroupCount - 1]->addObject(t);
|
||||
}
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ERROR: Malformed file at line %d: Too few/many parameters!\n", currentLine);
|
||||
}
|
||||
}
|
||||
else if (strncmp(argv[0], "g", 1) == 0)
|
||||
{
|
||||
if (argc == 2)
|
||||
{
|
||||
this->addGroup(new Group());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ERROR: Malformed file at line %d: Too few/many parameters!\n", currentLine);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user