Permalink
Browse files

Merge pull request #1 from Xaseron/master

Solid and Image Texture Mapping merge
  • Loading branch information...
2 parents 4f1019a + e96c6f1 commit a6a4432ca5aa75a6bbec2f8d0a302d8440080fe3 @freme committed Feb 6, 2012
View
@@ -0,0 +1 @@
+build/
View
@@ -1,42 +1,35 @@
cmake_minimum_required(VERSION 2.6)
-SET(SRC
- image.cc
- vector3.cc
- onb.cc
- )
-
-SET(FLS
- rgb.h
- image.h
- vector3.h
- onb.h
- dynarray.h
- rng.h
- ray.h
- shape.h
- triangle.h
- sphere.h
- )
PROJECT(RayBeam)
# -ansi needed for uint64_t numbers
-ADD_DEFINITIONS("-g -Wall -pedantic -arch i386")
-SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -arch i386")
+ADD_DEFINITIONS("-g -Wall -pedantic")
+#SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
ADD_EXECUTABLE(test_image image.cc test_image.cc)
+ADD_EXECUTABLE(test_vector2 test_vector2.cc)
ADD_EXECUTABLE(test_vector3 vector3.cc test_vector3.cc)
ADD_EXECUTABLE(test_onb vector3.cc onb.cc test_onb.cc)
ADD_EXECUTABLE(test_dynarray test_dynarray.cc)
ADD_EXECUTABLE(test_rng test_rng.cc)
ADD_EXECUTABLE(test_ray vector3.cc test_ray.cc)
ADD_EXECUTABLE(test_surfaces triangle.cc sphere.cc test_surfaces.cc)
+ADD_EXECUTABLE(test_solid_noise solid_noise.cc test_solid_noise.cc)
+
+ADD_EXECUTABLE(test_noise_texture image.cc solid_noise.cc noise_texture.cc test_noise_texture.cc)
+ADD_EXECUTABLE(test_wood_texture image.cc solid_noise.cc wood_texture.cc test_wood_texture.cc)
+ADD_EXECUTABLE(test_turbulence_texture image.cc solid_noise.cc turbulence_texture.cc test_turbulence_texture.cc)
+ADD_EXECUTABLE(test_simple_texture image.cc test_simple_texture.cc)
+ADD_EXECUTABLE(test_marble_texture image.cc solid_noise.cc marble_texture.cc test_marble_texture.cc)
+
+ADD_EXECUTABLE(test_uvsphere image.cc solid_noise.cc marble_texture.cc wood_texture.cc uvsphere.cc test_uvsphere.cc)
+ADD_EXECUTABLE(test_image_texture image.cc image_texture.cc uvsphere.cc test_image_texture.cc)
ADD_EXECUTABLE(simple_raytrace image.cc triangle.cc sphere.cc simple_raytrace.cc)
-SET(CMAKE_C_COMPILER llvm-gcc)
-SET(CMAKE_CXX_COMPILER llvm-g++)
+SET(CMAKE_C_COMPILER clang)
+SET(CMAKE_CXX_COMPILER clang++)
# not sure if the next one even exists
SET(CMAKE_LINKER llvm-ld)
SET(CMAKE_C_LINK_EXECUTABLE llvm-ld)
View
@@ -0,0 +1,91 @@
+// simple_raytrace.cc
+
+#include <iostream>
+#include "vector3.h"
+#include "rgb.h"
+#include "image.h"
+#include "shape.h"
+#include "uvsphere.h"
+#include "image_texture.h"
+#include "wood_texture.h"
+#include "marble_texture.h"
+#include "simple_texture.h"
+#include "sys/time.h"
+using namespace std;
+
+int main(int argc,char **argv) {
+ if(argc != 4) {
+ cout << "Wrong argument size. Supply TextureNumber, size, output file" << endl;
+ cout << "Simple Texture 0, WoodTexture 1, MarbleTexture 2, ImageTexture 3" << endl;
+ exit(1);
+ }
+
+ int temp = atoi(argv[2]);
+ if(temp < 0) {
+ cout << "Negative Size!" << endl;
+ exit(1);
+ }
+
+
+ HitRecord rec;
+ bool is_a_hit;
+ float tmax;
+ int size = temp;
+ struct timeval t1, t2;
+ // negative z so we can use right-handed coords
+ Vector3 dir(0,0,-1); // direction of viewing rays
+
+ Image im(size, size);
+
+ gettimeofday(&t1, 0);
+
+ //input Texture
+ string texName;
+ Texture * tex;
+ switch(atoi(argv[1])){
+ case 0: tex = new SimpleTexture(rgb(1.0,0.0,0.0)); texName = "SimpleTexture"; break;
+ case 1: tex = new WoodTexture(0.0015f); texName = "WoodTexture"; break;
+ case 2: tex = new MarbleTexture(0.0015f, 5.0f, 8); texName = "MarbleTexture"; break;
+ case 3: tex = new ImageTexture("worldmap.ppm"); texName = "ImageTexture"; break;
+ default:
+ cout << "Wrong TextureNumber" << endl;
+ cout << "Simple Texture 0, WoodTexture 1, MarbleTexture 2, ImageTexture 3" << endl;
+ exit(1);
+ };
+
+ // scene geometry
+ UVSphere sphere(Vector3(size/2,size/2,size*-2), size*47/100, tex);
+ cout << "Rendering Sphere of Size " << size << " with " << texName << endl;
+
+ // loop over pixels
+ for (int i = 0; i < size; i++)
+ for (int j = 0; j < size; j++) {
+ tmax = 100000.0f;
+ is_a_hit = false;
+ Ray r(Vector3(i,j,0), dir);
+
+ if (sphere.hit(r, .00001f, tmax, 0.0f, rec)) {
+ tmax = rec.t;
+ is_a_hit = true;
+ }
+ if (is_a_hit) {
+ rgb color = tex->value(rec.uv, rec.hit_p);
+ im.set(i, j, color);
+ }
+ else
+ im.set(i, j, rgb(.2, .2, .2));
+ }
+
+ gettimeofday(&t2, 0);
+ int us_start, us_end;
+ us_start = t1.tv_sec*1000000+t1.tv_usec;
+ us_end = t2.tv_sec*1000000+t2.tv_usec;
+ printf("Wallclock time %f seconds\n",(float)(us_end-us_start)/1000000.0);
+
+
+ ofstream out;
+ out.open(argv[3]);
+ im.writePPM(out);
+ out.close();
+}
+
View
@@ -2,6 +2,7 @@
#include "image.h"
#include <iostream>
+#include <cstdlib>
using namespace std;
Image::Image()
View
@@ -25,6 +25,11 @@ class Image
void writePPM(std::ostream& out);
void readPPM(std::string file_name);
+ int width() { return nx; }
+ int height() { return ny; }
+
+ rgb getPixel(int x, int y) {return raster[x][y];}
+
private:
rgb** raster;
int nx; // width
View
@@ -0,0 +1,30 @@
+// image_texture.cc
+
+#include "image_texture.h"
+#include "image.h"
+
+ImageTexture::ImageTexture(char* filename){
+ image = new Image();
+ image->readPPM(filename);
+}
+
+rgb ImageTexture::value(const Vector2& uv, const Vector3& p) const {
+ float u = uv.x() - int(uv.x());
+ float v = uv.y() - int(uv.y());
+ u *= (image->width()-3);
+ v *= (image->height()-3);
+
+ int iu = (int)u;
+ int iv = (int)v;
+
+ float tu = u-iu;
+ float tv = v-iv;
+
+ rgb c =
+ image->getPixel(iu ,iv )*(1-tu)*(1-tv) +
+ image->getPixel(iu+1,iv )* tu *(1-tv) +
+ image->getPixel(iu ,iv+1)*(1-tu)* tv +
+ image->getPixel(iu+1,iv+1)* tu * tv;
+
+ return c;
+}
View
@@ -0,0 +1,19 @@
+//image_texture.h
+
+#ifndef _IMAGE_TEXTURE_H_
+#define _IMAGE_TEXTURE_H_ 1
+
+#include "texture.h"
+#include "image.h"
+
+class Image;
+
+class ImageTexture : public Texture {
+ public:
+ ImageTexture(char* file_name);
+ virtual rgb value(const Vector2& uv, const Vector3& p) const;
+
+ Image* image;
+};
+
+#endif // _IMAGE_TEXTURE_H_
View
@@ -0,0 +1,15 @@
+// marble_texture.cc
+
+#include "marble_texture.h"
+
+rgb MarbleTexture::value(const Vector2& uv, const Vector3& p) const {
+ float temp = scale * noise.turbulence(freq*p, octaves);
+ float t = 2.0f*fabs(sin(freq*p.x() + temp));
+
+ if(t < 1.0f)
+ return (c1*t + (1.0f - t)*c2);
+ else {
+ t -= 1.0f;
+ return (c0*t + (1.0f - t)*c1);
+ }
+}
View
@@ -0,0 +1,37 @@
+// marble_texture.h
+
+#ifndef _MARBLE_TEXTURE_H_
+#define _MARBLE_TEXTURE_H_ 1
+
+#include <math.h>
+#include "texture.h"
+#include "solid_noise.h"
+
+class MarbleTexture : public Texture {
+ public:
+ MarbleTexture(float stripes_per_unit, float _scale = 0.5f, int _octaves = 8){
+ freq = M_PI * stripes_per_unit;
+ scale = _scale;
+ octaves = _octaves;
+ c0 = rgb(0.8,0.8,0.8);
+ c1 = rgb(0.4,0.2,0.1);
+ c2 = rgb(0.06,0.04,0.02);
+ }
+
+ MarbleTexture(const rgb& _c0, const rgb& _c1, const rgb& _c2,
+ float stripes_per_unit, float _scale = 3.0f, int _octaves = 8)
+ : c0(_c0),c1(_c1),c2(_c2) {
+ freq = M_PI * stripes_per_unit;
+ scale = _scale;
+ octaves = _octaves;
+ }
+
+ virtual rgb value(const Vector2& uv, const Vector3& p) const;
+
+ float freq, scale;
+ int octaves;
+ rgb c0, c1, c2;
+ SolidNoise noise;
+};
+
+#endif // _MARBLE_TEXTURE_H_
View
@@ -0,0 +1,8 @@
+// noise_texture.cc
+
+#include "noise_texture.h"
+
+rgb NoiseTexture::value(const Vector2& uv, const Vector3& p) const {
+ float t = (1.0f + solid_noise.noise(p * scale)) / 2.0f;
+ return t*c0 + (1.0f - t)*c1;
+}
View
@@ -0,0 +1,29 @@
+//noise_texture.h
+
+#ifndef _NOISE_TEXTURE_H_
+#define _NOISE_TEXTURE_H_ 1
+
+#include <math.h>
+#include "texture.h"
+#include "rgb.h"
+#include "solid_noise.h"
+
+class NoiseTexture : public Texture {
+ public:
+ NoiseTexture(float _scale = 1.0f){
+ scale = _scale;
+ c0 = rgb(1.0,1.0,1.0);
+ c1 = rgb(0.0,0.0,0.0);
+ }
+
+ NoiseTexture(const rgb& _c0, const rgb& _c1, float _scale = 1.0f)
+ : c0(_c0), c1(_c1), scale(_scale) {}
+
+ virtual rgb value(const Vector2& uv, const Vector3& p) const;
+
+ rgb c0, c1;
+ float scale;
+ SolidNoise solid_noise;
+};
+
+#endif // _NOISE_TEXTURE_H_
View
6 rng.h
@@ -8,11 +8,11 @@
class RNG {
public:
- RNG(uint64_t _seed = 7564231ULL)
+ RNG(uint64_t _seed = 7564231UL)
{
seed = _seed;
- mult = 62089911ULL;
- llong_max = 4294967295ULL;
+ mult = 62089911UL;
+ llong_max = 4294967295UL;
float_max = 4294967295.0f;
}
float operator()();
View
@@ -6,6 +6,7 @@
#include "ray.h"
//#include "vector3.h"
#include "rgb.h"
+#include "texture.h"
// this shouldn't be needed
//class Ray;
@@ -15,6 +16,9 @@ struct HitRecord {
float t;
Vector3 normal;
rgb color;
+ Vector2 uv;
+ Vector3 hit_p;
+ Texture* hit_tex;
};
class Shape {
View
@@ -25,11 +25,11 @@ int main() {
Vector3(0, 100, -1000),
Vector3(450, 20, -1000),
rgb(.8, .2, .2)));
- Image im(500, 500);
+ Image im(5000, 500);
// loop over pixels
- for (int i = 0; i < 500; i++)
- for (int j = 0; j < 500; j++) {
+ for (int i = 0; i < 5000; i++)
+ for (int j = 0; j < 5000; j++) {
tmax = 100000.0f;
is_a_hit = false;
Ray r(Vector3(i,j,0), dir);
View
@@ -0,0 +1,18 @@
+// simple_texture.h
+
+#ifndef _SIMPLE_TEXTURE_H_
+#define _SIMPLE_TEXTURE_H_ 1
+
+#include "texture.h"
+
+class SimpleTexture : public Texture {
+ public:
+ SimpleTexture(rgb c) { color = c; }
+ virtual rgb value(const Vector2& uv, const Vector3& p) const {
+ return color;
+ }
+
+ rgb color;
+};
+
+#endif // _SIMPLE_TEXTURE_H_
Oops, something went wrong.

0 comments on commit a6a4432

Please sign in to comment.