[Bf-blender-cvs] [dde6460] cycles-ptex-06: Displacement mod works now

Nicholas Bishop noreply at git.blender.org
Thu Jan 15 20:13:10 CET 2015


Commit: dde6460a79e813acfd96d0e409fa510953a3c4a1
Author: Nicholas Bishop
Date:   Thu Jan 8 15:42:41 2015 +0100
Branches: cycles-ptex-06
https://developer.blender.org/rBdde6460a79e813acfd96d0e409fa510953a3c4a1

Displacement mod works now

===================================================================

M	source/blender/modifiers/intern/MOD_util.c
M	source/blender/render/CMakeLists.txt
A	source/blender/render/intern/include/ptex_reader.h
A	source/blender/render/intern/source/ptex_reader.cpp
M	source/blender/render/intern/source/render_texture.c

===================================================================

diff --git a/source/blender/modifiers/intern/MOD_util.c b/source/blender/modifiers/intern/MOD_util.c
index 62a7dde..10acdd5 100644
--- a/source/blender/modifiers/intern/MOD_util.c
+++ b/source/blender/modifiers/intern/MOD_util.c
@@ -87,7 +87,14 @@ void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
 
 	/* UVs need special handling, since they come from faces */
 	if (texmapping == MOD_DISP_MAP_UV) {
-		if (CustomData_has_layer(&dm->loopData, CD_MLOOPUV)) {
+		CustomData *ldata = &dm->loopData;
+		CustomData *pdata = &dm->polyData;
+		const bool has_uvs = CustomData_has_layer(ldata, CD_MLOOPUV);
+		const bool has_ptex = (CustomData_has_layer(ldata, CD_PTEX_UV) &&
+		                       CustomData_has_layer(pdata,
+		                                            CD_PTEX_QUAD_FACE_ID));
+
+		if (has_uvs || has_ptex) {
 			MPoly *mpoly = dm->getPolyArray(dm);
 			MPoly *mp;
 			MLoop *mloop = dm->getLoopArray(dm);
@@ -96,9 +103,13 @@ void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
 			int numPolys = dm->getNumPolys(dm);
 			char uvname[MAX_CUSTOMDATA_LAYER_NAME];
 			MLoopUV *mloop_uv;
+			MLoopUV *ptex_uv;
+			int *ptex_face_ids;
 
-			CustomData_validate_layer_name(&dm->loopData, CD_MLOOPUV, dmd->uvlayer_name, uvname);
-			mloop_uv = CustomData_get_layer_named(&dm->loopData, CD_MLOOPUV, uvname);
+			CustomData_validate_layer_name(ldata, CD_MLOOPUV, dmd->uvlayer_name, uvname);
+			mloop_uv = CustomData_get_layer_named(ldata, CD_MLOOPUV, uvname);
+			ptex_uv = CustomData_get_layer(ldata, CD_PTEX_UV);
+			ptex_face_ids = CustomData_get_layer(pdata, CD_PTEX_QUAD_FACE_ID);
 
 			/* verts are given the UV from the first face that uses them */
 			for (i = 0, mp = mpoly; i < numPolys; ++i, ++mp) {
@@ -109,9 +120,19 @@ void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
 					unsigned int vidx = mloop[lidx].v;
 
 					if (done[vidx] == 0) {
-						/* remap UVs from [0, 1] to [-1, 1] */
-						texco[vidx][0] = (mloop_uv[lidx].uv[0] * 2.0f) - 1.0f;
-						texco[vidx][1] = (mloop_uv[lidx].uv[1] * 2.0f) - 1.0f;
+						// TODO(bishop): shouldn't add conditional in
+						// inner loop probably
+						if (has_ptex) {
+							texco[vidx][0] = ptex_uv[lidx].uv[0];
+							texco[vidx][1] = ptex_uv[lidx].uv[1];
+							/* TODO(bishop): for now just pack ID as float */
+							texco[vidx][2] = (float)(ptex_face_ids[i]);
+						}
+						else {
+							/* remap UVs from [0, 1] to [-1, 1] */
+							texco[vidx][0] = (mloop_uv[lidx].uv[0] * 2.0f) - 1.0f;
+							texco[vidx][1] = (mloop_uv[lidx].uv[1] * 2.0f) - 1.0f;
+						}
 						done[vidx] = 1;
 					}
 
diff --git a/source/blender/render/CMakeLists.txt b/source/blender/render/CMakeLists.txt
index e516c95..505675e 100644
--- a/source/blender/render/CMakeLists.txt
+++ b/source/blender/render/CMakeLists.txt
@@ -65,6 +65,7 @@ set(SRC
 	intern/source/pixelblending.c
 	intern/source/pixelshading.c
 	intern/source/pointdensity.c
+	intern/source/ptex_reader.cpp
 	intern/source/rayshade.c
 	intern/source/rendercore.c
 	intern/source/render_result.c
@@ -94,6 +95,7 @@ set(SRC
 	intern/include/pixelblending.h
 	intern/include/pixelshading.h
 	intern/include/pointdensity.h
+	intern/include/ptex_reader.h
 	intern/include/raycounter.h
 	intern/include/rayobject.h
 	intern/include/rayintersection.h
@@ -166,6 +168,9 @@ if(WITH_CYCLES AND WITH_CYCLES_DEBUG)
 	add_definitions(-DWITH_CYCLES_DEBUG)
 endif()
 
+# TODO
+include_directories("/home/nicholasbishop/ptex/src/ptex")
+
 if(APPLE)
 	# SSE math is enabled by default on x86_64
 	if(CMAKE_OSX_ARCHITECTURES MATCHES "i386")
diff --git a/source/blender/render/intern/include/ptex_reader.h b/source/blender/render/intern/include/ptex_reader.h
new file mode 100644
index 0000000..7741ab3
--- /dev/null
+++ b/source/blender/render/intern/include/ptex_reader.h
@@ -0,0 +1,17 @@
+#ifndef __PTEX_READER_H__
+#define __PTEX_READER_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+	struct Tex;
+	struct TexResult;
+
+	// All TODO, just simplest thing that can work for now...
+
+	bool read_ptex(struct Tex *tex, int face_id, float uv[2], struct TexResult *texres);
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/source/blender/render/intern/source/ptex_reader.cpp b/source/blender/render/intern/source/ptex_reader.cpp
new file mode 100644
index 0000000..faeb228
--- /dev/null
+++ b/source/blender/render/intern/source/ptex_reader.cpp
@@ -0,0 +1,77 @@
+extern "C" {
+#include "DNA_texture_types.h"
+
+#include "BLI_path_util.h"
+#include "BLI_string.h"
+
+#include "BKE_global.h"
+#include "BKE_main.h"
+
+#include "RE_shader_ext.h"
+}
+
+#include <cstring>
+
+// TODO, silencing warning
+#define NEW_API 0
+#include <Ptexture.h>
+
+#include "ptex_reader.h"
+
+/* TODO(bishop): partially a copy of the "quick hack" from Cycles OSL
+ * code */
+bool read_ptex(Tex *tex, int face_id, float uv[2], TexResult *result) {
+	memset(result, 0, sizeof(*result));
+
+	float dudx = 0.0f;
+	float dvdx = 0.0f;
+	float dudy = 0.0f;
+	float dvdy = 0.0f;
+
+	// TODO: very very todo...
+	const size_t maxmem = 16384 * 1024;
+	static PtexCache *ptex_cache = PtexCache::create(0, maxmem);
+
+	if (!tex || !tex->ima) {
+		return false;
+	}
+
+	char path[FILE_MAX];
+	BLI_strncpy(path, tex->ima->name, FILE_MAX);
+	BLI_path_abs(path, ID_BLEND_PATH(G.main, &tex->ima->id));
+
+	Ptex::String error;
+	PtexPtr<PtexTexture> r(ptex_cache->get(path, error));
+
+	if(!r) {
+		//std::cerr << error.c_str() << std::endl;
+		return false;
+	}
+
+	if (face_id < 0 || face_id >= r->numFaces()) {
+		return false;
+	}
+
+	bool mipmaplerp = false;
+	float sharpness = 1.0f;
+	PtexFilter::Options opts(PtexFilter::f_bicubic, mipmaplerp, sharpness);
+	PtexPtr<PtexFilter> f(PtexFilter::getFilter(r, opts));
+
+	int nchannels = r->numChannels();
+	if (nchannels > 4) {
+		nchannels = 4;
+	}
+
+	float data[4] = {0, 0, 0, 0};
+	int first_channel = 0;
+	f->eval(data, first_channel, nchannels, face_id,
+			uv[0], uv[1],
+			dudx, dvdx, dudy, dvdy);
+
+	result->tr = data[0];
+	result->tg = data[1];
+	result->tb = data[2];
+	result->ta = data[3];
+
+	return true;
+}
diff --git a/source/blender/render/intern/source/render_texture.c b/source/blender/render/intern/source/render_texture.c
index b1e00c4..b991268 100644
--- a/source/blender/render/intern/source/render_texture.c
+++ b/source/blender/render/intern/source/render_texture.c
@@ -36,6 +36,8 @@
 #include "BLI_math.h"
 #include "BLI_noise.h"
 #include "BLI_rand.h"
+#include "BLI_path_util.h"
+#include "BLI_string.h"
 #include "BLI_utildefines.h"
 
 #include "DNA_anim_types.h"
@@ -66,6 +68,7 @@
 
 #include "envmap.h"
 #include "pointdensity.h"
+#include "ptex_reader.h"
 #include "voxeldata.h"
 #include "render_types.h"
 #include "shading.h"
@@ -1141,7 +1144,17 @@ static int multitex(Tex *tex, float texvec[3], float dxt[3], float dyt[3], int o
 				retval = texnoise(tex, texres, thread);
 				break;
 			case TEX_IMAGE:
-				if (osatex) retval = imagewraposa(tex, tex->ima, NULL, texvec, dxt, dyt, texres, pool);
+				// TODO
+				if (tex->ima && BLI_testextensie(tex->ima->name, "ptx")) {
+					// TODO(bishop)
+					const int face_id = (int)texvec[2];
+					if (!read_ptex(tex, face_id, texvec, texres)) {
+						// TODO(bishop): handle error?
+					}
+					// TODO?
+					retval = 1;
+				}
+				else if (osatex) retval = imagewraposa(tex, tex->ima, NULL, texvec, dxt, dyt, texres, pool);
 				else        retval = imagewrap(tex, tex->ima, NULL, texvec, texres, pool);
 				if (tex->ima) {
 					BKE_image_tag_time(tex->ima);
@@ -1269,6 +1282,9 @@ static int multitex_nodes_intern(Tex *tex, float texvec[3], float dxt[3], float
 			}
 			
 			do_2d_mapping(&localmtex, texvec_l, NULL, NULL, dxt_l, dyt_l);
+			// TODO
+			copy_v3_v3(texvec_l, texvec);
+			texvec_l[1] = 1 - texvec_l[1];
 			rgbnor = multitex(tex, texvec_l, dxt_l, dyt_l, osatex, texres, thread, which_output, pool);
 
 			{




More information about the Bf-blender-cvs mailing list