Add spherical mapping for UV Textures.
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <material.h>
|
||||
#include <uv_pattern.h>
|
||||
#include <uv_checkers.h>
|
||||
#include <texturemap.h>
|
||||
|
||||
#ifdef ENABLE_LUA_SUPPORT
|
||||
extern "C" {
|
||||
@@ -273,4 +274,81 @@ TEST(PatternTest, Checkers_pattern_in_2D)
|
||||
ASSERT_EQ(checkers.uvPatternAt(0.0, 0.5), white);
|
||||
ASSERT_EQ(checkers.uvPatternAt(0.5, 0.5), black);
|
||||
ASSERT_EQ(checkers.uvPatternAt(1.0, 1.0), black);
|
||||
}
|
||||
|
||||
TEST(PatternTest, Using_a_spherical_mapping_on_a_3d_point)
|
||||
{
|
||||
Point testList[] = {
|
||||
Point( 0, 0, -1),
|
||||
Point( 1, 0, 0),
|
||||
Point( 0, 0, 1),
|
||||
Point(-1, 0, 0),
|
||||
Point( 0, 1, 0),
|
||||
Point( 0, -1, 0),
|
||||
Point(sqrt(2)/2, sqrt(2)/2, 0),
|
||||
};
|
||||
|
||||
double testResults[][2] {
|
||||
{0.0, 0.5},
|
||||
{0.25, 0.5},
|
||||
{0.5, 0.5},
|
||||
{0.75, 0.5},
|
||||
{0.5, 1.0},
|
||||
{0.5, 0.0},
|
||||
{0.25, 0.75},
|
||||
};
|
||||
|
||||
int testCount = sizeof(testList)/sizeof((testList)[0]);
|
||||
int i;
|
||||
|
||||
TextureMap tm = TextureMap(SPHERICAL_MAP, nullptr);
|
||||
|
||||
for(i = 0; i < testCount; i++)
|
||||
{
|
||||
double u, v;
|
||||
tm.sphericalMap(testList[i], u, v);
|
||||
ASSERT_TRUE(double_equal(u, testResults[i][0]));
|
||||
ASSERT_TRUE(double_equal(v, testResults[i][1]));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PatternTest, Using_a_texture_map_with_a_spherical_map)
|
||||
{
|
||||
Point testList[] = {
|
||||
Point( 0.4315, 0.4670, 0.7719),
|
||||
Point( -0.9654, 0.2252, -0.0534),
|
||||
Point( 0.1039, 0.7090, 0.6975),
|
||||
Point( -0.4986, -0.7856, -0.3663),
|
||||
Point( -0.0317, -0.9395, 0.3411),
|
||||
Point( 0.4809, -0.7721, 0.4154),
|
||||
Point( 0.0285, -0.9612, -0.2745),
|
||||
Point( -0.5734, -0.2162, -0.7903),
|
||||
Point( 0.7688, -0.1470, 0.6223),
|
||||
Point( -0.7652, 0.2175, 0.6060),
|
||||
};
|
||||
|
||||
Colour testResults[] {
|
||||
white,
|
||||
black,
|
||||
white,
|
||||
black,
|
||||
black,
|
||||
black,
|
||||
black,
|
||||
white,
|
||||
black,
|
||||
black,
|
||||
};
|
||||
|
||||
int testCount = sizeof(testList)/sizeof((testList)[0]);
|
||||
int i;
|
||||
|
||||
UVCheckers uvpat = UVCheckers(16, 8, black, white);
|
||||
TextureMap tm = TextureMap(SPHERICAL_MAP, &uvpat);
|
||||
|
||||
for(i = 0; i < testCount; i++)
|
||||
{
|
||||
Colour ret = tm.patternAt(testList[i]);
|
||||
EXPECT_EQ(ret, testResults[i]);
|
||||
}
|
||||
}
|
||||
54
tests/uvmap_checkeredsphere.cpp
Normal file
54
tests/uvmap_checkeredsphere.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Render test for chapter 10
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#include <world.h>
|
||||
#include <light.h>
|
||||
#include <sphere.h>
|
||||
#include <plane.h>
|
||||
#include <material.h>
|
||||
#include <colour.h>
|
||||
#include <canvas.h>
|
||||
#include <camera.h>
|
||||
|
||||
#include <pattern.h>
|
||||
#include <texturemap.h>
|
||||
#include <uv_checkers.h>
|
||||
|
||||
#include <transformation.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
World w = World();
|
||||
|
||||
Light light = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
|
||||
w.addLight(&light);
|
||||
|
||||
Sphere sp = Sphere();
|
||||
UVCheckers checkers = UVCheckers(20, 10, Colour(0, 0.5, 0), Colour(1, 1, 1));
|
||||
TextureMap tm = TextureMap(SPHERICAL_MAP, &checkers);
|
||||
sp.material.pattern = &tm;
|
||||
sp.material.ambient = 0.1;
|
||||
sp.material.specular = 0.4;
|
||||
sp.material.shininess = 10;
|
||||
sp.material.diffuse = 0.6;
|
||||
|
||||
w.addObject(&sp);
|
||||
|
||||
/* Set the camera */
|
||||
Camera camera = Camera(400, 400, 0.5);
|
||||
camera.setTransform(viewTransform(Point(0, 0, -5),
|
||||
Point(0, 0, 0),
|
||||
Vector(0, 1, 0)));
|
||||
|
||||
/* Now render it */
|
||||
Canvas image = camera.render(w);
|
||||
|
||||
image.SaveAsPNG("uvmap_checkeredsphere.png");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user