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:
Godzil
2020-03-06 09:38:39 +00:00
parent 2725b5f657
commit 518ac260e1
8 changed files with 12082 additions and 23 deletions

View File

@@ -17,10 +17,17 @@ target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest
target_sources(testMyRays PRIVATE ${TESTS_SRC})
target_link_libraries(testMyRays gtest gtest_main Threads::Threads)
add_custom_command(
TARGET testMyRays POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/triangles.obj
${CMAKE_CURRENT_BINARY_DIR}/
)
gtest_discover_tests(testMyRays
WORKING_DIRECTORY ${PROJECT_DIR}
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}"
)
)
add_executable(hw3render)
@@ -65,6 +72,15 @@ target_sources(ch13_cone PRIVATE ch13_cone.cpp)
add_executable(ch14_test)
target_sources(ch14_test PRIVATE ch14_test.cpp)
add_executable(ch15_teapot_objfile)
target_sources(ch15_teapot_objfile PRIVATE ch15_teapot_objfile.cpp)
add_custom_command(
TARGET ch15_teapot_objfile POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/external/teapot*.obj
${CMAKE_CURRENT_BINARY_DIR}/
)
add_executable(arealight_test)
target_sources(arealight_test PRIVATE arealight_test.cpp)

View File

@@ -0,0 +1,86 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Render test for OBJ File using teapots in chapter 15.
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <world.h>
#include <light.h>
#include <plane.h>
#include <material.h>
#include <colour.h>
#include <canvas.h>
#include <camera.h>
#include <objfile.h>
#include <pattern.h>
#include <strippattern.h>
#include <gradientpattern.h>
#include <checkerspattern.h>
#include <ringpattern.h>
#include <transformation.h>
int main()
{
World w = World();
/* Add lights */
Light light1 = Light(POINT_LIGHT, Point(0, 20, 2), Colour(1, 1, 1));
w.addLight(&light1);
Light light2 = Light(POINT_LIGHT, Point(0, 2, 20), Colour(1, 1, 1));
w.addLight(&light2);
/* ----------------------------- */
/* Floor */
Plane p = Plane();
CheckersPattern checkered = CheckersPattern(Colour(0.35, 0.35, 0.35), Colour(0.4, 0.4, 0.4));
p.material.pattern = &checkered;
p.material.ambient = 1;
p.material.diffuse = 0;
p.material.specular = 0;
w.addObject(&p);
Plane p2 = Plane();
p2.setTransform(translation(0, 0, -10) * rotationX(M_PI/2));
p2.material.pattern = &checkered;
p2.material.ambient = 1;
p2.material.diffuse = 0;
p2.material.specular = 0;
w.addObject(&p2);
OBJFile teapot = OBJFile("teapot-low.obj");
teapot.setTransform(rotationY(M_PI) * rotationX(-M_PI/2) * scaling(0.4, 0.4, 0.4));
teapot.material.colour = Colour(1, 0.2, 0.1);
teapot.material.ambient = 0.2;
teapot.material.specular = 0.2;
teapot.material.diffuse = 20;
w.addObject(&teapot);
/* ----------------------------- */
FILE *fpOut = fopen("teapot_worlddump.json", "wt");
if (fpOut)
{
w.dumpMe(fpOut);
fclose(fpOut);
}
/* ----------------------------- */
/* Set the camera */
Camera camera = Camera(800, 400, M_PI/2);
camera.setTransform(viewTransform(Point(0, 7, 13),
Point(0, 1, 0),
Vector(0, 1, 0)));
/* Now render it */
Canvas image = camera.render(w, 5);
image.SaveAsPNG("ch15_teapot_objfile.png");
return 0;
}

View File

@@ -9,10 +9,11 @@
#include <objfile.h>
#include <math.h>
#include <gtest/gtest.h>
#include <triangle.h>
TEST(OBJFileTest, Ignoring_unrecognised_lines)
{
const char gibberish[] = "There was a young lane named Bright\n"
const char gibberish[] = "There was a young lady named Bright\n"
"who traveled much faster than light\n"
"She set out one day\n"
"in a relative way\n"
@@ -22,4 +23,99 @@ TEST(OBJFileTest, Ignoring_unrecognised_lines)
parser.parseOBJFile(gibberish);
ASSERT_EQ(parser.ignoredLines, 5);
}
TEST(OBJFileTest, Vertex_record)
{
const char file[] = "v -1 1 0\n"
"v -1.0000 0.5000 0.0000\n"
"v 1 0 0\n"
"v 1 1 0\n";
OBJFile parser = OBJFile();
parser.parseOBJFile(file);
ASSERT_EQ(parser.vertices(1), Point(-1, 1, 0));
ASSERT_EQ(parser.vertices(2), Point(-1, 0.5, 0));
ASSERT_EQ(parser.vertices(3), Point(1, 0, 0));
ASSERT_EQ(parser.vertices(4), Point(1, 1, 0));
}
TEST(OBJFileTest, Parsing_triangle_faces)
{
const char file[] = "v -1 1 0\n"
"v -1 0 0\n"
"v 1 0 0\n"
"v 1 1 0\n"
"\n"
"f 1 2 3\n"
"f 1 3 4\n";
OBJFile parser = OBJFile();
parser.parseOBJFile(file);
Group *g0 = parser.groups(0);
Triangle *t1 = (Triangle *)(*g0)[0];
Triangle *t2 = (Triangle *)(*g0)[1];
ASSERT_EQ(t1->p1, parser.vertices(1));
ASSERT_EQ(t1->p2, parser.vertices(2));
ASSERT_EQ(t1->p3, parser.vertices(3));
ASSERT_EQ(t2->p1, parser.vertices(1));
ASSERT_EQ(t2->p2, parser.vertices(3));
ASSERT_EQ(t2->p3, parser.vertices(4));
}
TEST(OBJFileTest, Triangulating_polygons)
{
const char file[] = "v -1 1 0\n"
"v -1 0 0\n"
"v 1 0 0\n"
"v 1 1 0\n"
"v 0 2 0\n"
"\n"
"f 1 2 3 4 5\n";
OBJFile parser = OBJFile();
parser.parseOBJFile(file);
Group *g0 = parser.groups(0);
Triangle *t1 = (Triangle *)(*g0)[0];
Triangle *t2 = (Triangle *)(*g0)[1];
Triangle *t3 = (Triangle *)(*g0)[2];
ASSERT_EQ(t1->p1, parser.vertices(1));
ASSERT_EQ(t1->p2, parser.vertices(2));
ASSERT_EQ(t1->p3, parser.vertices(3));
ASSERT_EQ(t2->p1, parser.vertices(1));
ASSERT_EQ(t2->p2, parser.vertices(3));
ASSERT_EQ(t2->p3, parser.vertices(4));
ASSERT_EQ(t3->p1, parser.vertices(1));
ASSERT_EQ(t3->p2, parser.vertices(4));
ASSERT_EQ(t3->p3, parser.vertices(5));
}
TEST(OBJFileTest, Triangle_in_groups)
{
OBJFile parser = OBJFile("triangles.obj");
/* TODO: Add group name search/test */
Group *g1 = parser.groups(1);
Group *g2 = parser.groups(2);
Triangle *t1 = (Triangle *)(*g1)[0];
Triangle *t2 = (Triangle *)(*g2)[0];
ASSERT_EQ(t1->p1, parser.vertices(1));
ASSERT_EQ(t1->p2, parser.vertices(2));
ASSERT_EQ(t1->p3, parser.vertices(3));
ASSERT_EQ(t2->p1, parser.vertices(1));
ASSERT_EQ(t2->p2, parser.vertices(3));
ASSERT_EQ(t2->p3, parser.vertices(4));
}

10
tests/triangles.obj Normal file
View File

@@ -0,0 +1,10 @@
v -1 1 0
v -1 0 0
v 1 0 0
v 1 1 0
g FirstGroup
f 1 2 3
g SecondGroup
f 1 3 4