add 2D pixmap texture class pyrit
authorRadek Brich <radek.brich@devl.cz>
Thu, 24 Apr 2008 10:49:11 +0200
branchpyrit
changeset 81 9dbb9c8c115b
parent 80 907929fa9b59
child 82 930a2d3ecaed
add 2D pixmap texture class
TODO
ccdemos/common_sdl.h
ccdemos/spheres_shadow.cc
ccdemos/textures.cc
include/material.h
--- a/TODO	Wed Apr 23 19:35:03 2008 +0200
+++ b/TODO	Thu Apr 24 10:49:11 2008 +0200
@@ -4,7 +4,6 @@
 
 Future Plans
 ============
- * pixmap textures
  * generalization: Camera "shader" (ray generator), surface shader and maybe light & background shaders
  * namespace
  * Python binding for all classes
@@ -15,9 +14,7 @@
 New Classes?
 ============
 
-shapes.h  -- Triangle, Sphere
-scene.h   -- Ray, Light, Camera, Scene
-material.h -- Material, Texture
+scene.h   -- Scene, ...
 reader.h  -- Reader, WavefrontReader
 
 wf = new WavefrontReader()
@@ -33,7 +30,3 @@
 scene.addLight(new PointLight(pos, color))
 rt.setScene(scene)
 rt.render(w,h)
-
-
-constructors have zero or one parameter by default -- the object to copy data from
-more parameters -- the data (ray origin and direction)
--- a/ccdemos/common_sdl.h	Wed Apr 23 19:35:03 2008 +0200
+++ b/ccdemos/common_sdl.h	Thu Apr 24 10:49:11 2008 +0200
@@ -52,7 +52,7 @@
 }
 
 void loop_sdl(Raytracer &rt, Camera &cam,
-	void (*update_callback)() = NULL, void (*key_callback)(int, int) = NULL)
+	void (*update_callback)(Float*) = NULL, void (*key_callback)(int, int) = NULL)
 {
 	SDL_Surface *screen;
 	Float *render_buffer;
@@ -156,7 +156,7 @@
 		if (move != 0.0)
 			cam.move(move,0,0);
 		if (update_callback != NULL)
-			update_callback();
+			update_callback(render_buffer);
 		update(rt, screen, render_buffer);
 	}
 	free(render_buffer);
--- a/ccdemos/spheres_shadow.cc	Wed Apr 23 19:35:03 2008 +0200
+++ b/ccdemos/spheres_shadow.cc	Thu Apr 24 10:49:11 2008 +0200
@@ -9,7 +9,7 @@
 
 Float lx, ly, lz, cf;
 
-void update_callback()
+void update_callback(Float*)
 {
 	if (lx != 0.0)
 		light.pos.x += lx;
--- a/ccdemos/textures.cc	Wed Apr 23 19:35:03 2008 +0200
+++ b/ccdemos/textures.cc	Thu Apr 24 10:49:11 2008 +0200
@@ -9,7 +9,7 @@
 
 Float lx, ly, lz, cf;
 
-void update_callback()
+void update_callback(Float*)
 {
 	if (lx != 0.0)
 		light.pos.x += lx;
--- a/include/material.h	Wed Apr 23 19:35:03 2008 +0200
+++ b/include/material.h	Thu Apr 24 10:49:11 2008 +0200
@@ -223,6 +223,45 @@
 };
 
 /**
+ * pixmap for image texture
+ */
+class Pixmap
+{
+	Colour *data;
+	int w,h;
+public:
+	Pixmap(): data(NULL), w(0), h(0) {};
+	Pixmap(Float *adata, int aw, int ah):
+		data((Colour*)(adata)), w(aw), h(ah) {};
+	void setData(Float *adata, int aw, int ah)
+		{ data = (Colour*)adata; w = aw; h = ah; };
+	Colour get(int x, int y) { return data[y*w + x]; };
+	int getWidth() { return w; };
+	int getHeight() { return h; };
+};
+
+/**
+ * 2D image texture
+ */
+class ImageTexture: public Texture2D
+{
+	Pixmap *pixmap;
+public:
+	ImageTexture(TextureMap *tmap, Pixmap *image):
+		Texture2D(tmap), pixmap(image) {};
+	Colour evaluate(const Vector3 &point)
+	{
+		Float u,v;
+		map->map(point, u,v);
+		u = u - 0.5;
+		u -= floor(u);
+		v = -(v - 0.5);
+		v -= floor(v);
+		return pixmap->get(u*pixmap->getWidth(), v*pixmap->getHeight());
+	};
+};
+
+/**
  * 2D checkers texture
  */
 class CheckersTexture: public Texture2D