[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54041] trunk/blender/source/blender: Code cleanup: move render baking code into own file.

Brecht Van Lommel brechtvanlommel at pandora.be
Wed Jan 23 14:13:16 CET 2013


Revision: 54041
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54041
Author:   blendix
Date:     2013-01-23 13:13:16 +0000 (Wed, 23 Jan 2013)
Log Message:
-----------
Code cleanup: move render baking code into own file.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_depsgraph.h
    trunk/blender/source/blender/render/CMakeLists.txt
    trunk/blender/source/blender/render/intern/source/rendercore.c

Added Paths:
-----------
    trunk/blender/source/blender/render/intern/source/bake.c

Modified: trunk/blender/source/blender/blenkernel/BKE_depsgraph.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_depsgraph.h	2013-01-23 13:13:10 UTC (rev 54040)
+++ trunk/blender/source/blender/blenkernel/BKE_depsgraph.h	2013-01-23 13:13:16 UTC (rev 54041)
@@ -36,13 +36,14 @@
 
 // #define DEPS_DEBUG
 
+struct DagForest;
+struct DagNode;
+struct DagNodeQueue;
+struct GHash;
 struct ID;
 struct Main;
+struct Object;
 struct Scene;
-struct DagNodeQueue;
-struct DagForest;
-struct DagNode;
-struct GHash;
 
 /* **** DAG relation types *** */
 

Modified: trunk/blender/source/blender/render/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/render/CMakeLists.txt	2013-01-23 13:13:10 UTC (rev 54040)
+++ trunk/blender/source/blender/render/CMakeLists.txt	2013-01-23 13:13:16 UTC (rev 54041)
@@ -54,6 +54,7 @@
 	intern/raytrace/rayobject_qbvh.cpp
 	intern/raytrace/rayobject_rtbuild.cpp
 	intern/raytrace/rayobject_vbvh.cpp
+	intern/source/bake.c
 	intern/source/convertblender.c
 	intern/source/envmap.c
 	intern/source/external_engine.c

Added: trunk/blender/source/blender/render/intern/source/bake.c
===================================================================
--- trunk/blender/source/blender/render/intern/source/bake.c	                        (rev 0)
+++ trunk/blender/source/blender/render/intern/source/bake.c	2013-01-23 13:13:16 UTC (rev 54041)
@@ -0,0 +1,1114 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * Contributors: 2004/2005/2006 Blender Foundation, full recode
+ * Contributors: Vertex color baking, Copyright 2011 AutoCRC
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/render/intern/source/bake.c
+ *  \ingroup render
+ */
+
+
+/* system includes */
+#include <stdio.h>
+#include <string.h>
+
+/* External modules: */
+#include "MEM_guardedalloc.h"
+
+#include "BLI_math.h"
+#include "BLI_blenlib.h"
+#include "BLI_threads.h"
+#include "BLI_utildefines.h"
+
+#include "DNA_image_types.h"
+#include "DNA_material_types.h"
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+
+#include "BKE_customdata.h"
+#include "BKE_depsgraph.h"
+#include "BKE_global.h"
+#include "BKE_image.h"
+#include "BKE_main.h"
+#include "BKE_node.h"
+#include "BKE_scene.h"
+
+#include "IMB_imbuf_types.h"
+#include "IMB_imbuf.h"
+#include "IMB_colormanagement.h"
+
+/* local include */
+#include "rayintersection.h"
+#include "rayobject.h"
+#include "render_types.h"
+#include "renderdatabase.h"
+#include "shading.h"
+#include "zbuf.h"
+
+#include "PIL_time.h"
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+/* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
+/* only to be used here in this file, it's for speed */
+extern struct Render R;
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+
+/* ************************* bake ************************ */
+
+
+typedef struct BakeShade {
+	ShadeSample ssamp;
+	ObjectInstanceRen *obi;
+	VlakRen *vlr;
+	
+	ZSpan *zspan;
+	Image *ima;
+	ImBuf *ibuf;
+	
+	int rectx, recty, quad, type, vdone, ready;
+
+	float dir[3];
+	Object *actob;
+
+	/* Output: vertex color or image data. If vcol is not NULL, rect and
+	 * rect_float should be NULL. */
+	MPoly *mpoly;
+	MLoop *mloop;
+	MLoopCol *vcol;
+	
+	unsigned int *rect;
+	float *rect_float;
+
+	/* displacement buffer used for normalization with unknown maximal distance */
+	int use_displacement_buffer;
+	float *displacement_buffer;
+	float displacement_min, displacement_max;
+	
+	int use_mask;
+	char *rect_mask; /* bake pixel mask */
+
+	float dxco[3], dyco[3];
+
+	short *do_update;
+
+	struct ColorSpace *rect_colorspace;
+} BakeShade;
+
+static void bake_set_shade_input(ObjectInstanceRen *obi, VlakRen *vlr, ShadeInput *shi, int quad, int UNUSED(isect), int x, int y, float u, float v)
+{
+	if (quad) 
+		shade_input_set_triangle_i(shi, obi, vlr, 0, 2, 3);
+	else
+		shade_input_set_triangle_i(shi, obi, vlr, 0, 1, 2);
+		
+	/* cache for shadow */
+	shi->samplenr= R.shadowsamplenr[shi->thread]++;
+
+	shi->mask= 0xFFFF; /* all samples */
+	
+	shi->u= -u;
+	shi->v= -v;
+	shi->xs= x;
+	shi->ys= y;
+	
+	shade_input_set_uv(shi);
+	shade_input_set_normals(shi);
+
+	/* no normal flip */
+	if (shi->flippednor)
+		shade_input_flip_normals(shi);
+
+	/* set up view vector to look right at the surface (note that the normal
+	 * is negated in the renderer so it does not need to be done here) */
+	shi->view[0]= shi->vn[0];
+	shi->view[1]= shi->vn[1];
+	shi->view[2]= shi->vn[2];
+}
+
+static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(quad), int x, int y, float UNUSED(u), float UNUSED(v), float *tvn, float *ttang)
+{
+	BakeShade *bs= handle;
+	ShadeSample *ssamp= &bs->ssamp;
+	ShadeResult shr;
+	VlakRen *vlr= shi->vlr;
+	
+	shade_input_init_material(shi);
+	
+	if (bs->type==RE_BAKE_AO) {
+		ambient_occlusion(shi);
+
+		if (R.r.bake_flag & R_BAKE_NORMALIZE) {
+			copy_v3_v3(shr.combined, shi->ao);
+		}
+		else {
+			zero_v3(shr.combined);
+			environment_lighting_apply(shi, &shr);
+		}
+	}
+	else {
+		if (bs->type==RE_BAKE_SHADOW) /* Why do shadows set the color anyhow?, ignore material color for baking */
+			shi->r = shi->g = shi->b = 1.0f;
+	
+		shade_input_set_shade_texco(shi);
+		
+		/* only do AO for a full bake (and obviously AO bakes)
+		 * AO for light bakes is a leftover and might not be needed */
+		if ( ELEM3(bs->type, RE_BAKE_ALL, RE_BAKE_AO, RE_BAKE_LIGHT))
+			shade_samples_do_AO(ssamp);
+		
+		if (shi->mat->nodetree && shi->mat->use_nodes) {
+			ntreeShaderExecTree(shi->mat->nodetree, shi, &shr);
+			shi->mat= vlr->mat;		/* shi->mat is being set in nodetree */
+		}
+		else
+			shade_material_loop(shi, &shr);
+		
+		if (bs->type==RE_BAKE_NORMALS) {
+			float nor[3];
+
+			copy_v3_v3(nor, shi->vn);
+
+			if (R.r.bake_normal_space == R_BAKE_SPACE_CAMERA) {
+				/* pass */
+			}
+			else if (R.r.bake_normal_space == R_BAKE_SPACE_TANGENT) {
+				float mat[3][3], imat[3][3];
+
+				/* bitangent */
+				if (tvn && ttang) {
+					copy_v3_v3(mat[0], ttang);
+					cross_v3_v3v3(mat[1], tvn, ttang);
+					mul_v3_fl(mat[1], ttang[3]);
+					copy_v3_v3(mat[2], tvn);
+				}
+				else {
+					copy_v3_v3(mat[0], shi->nmaptang);
+					cross_v3_v3v3(mat[1], shi->nmapnorm, shi->nmaptang);
+					mul_v3_fl(mat[1], shi->nmaptang[3]);
+					copy_v3_v3(mat[2], shi->nmapnorm);
+				}
+
+				invert_m3_m3(imat, mat);
+				mul_m3_v3(imat, nor);
+			}
+			else if (R.r.bake_normal_space == R_BAKE_SPACE_OBJECT)
+				mul_mat3_m4_v3(ob->imat_ren, nor); /* ob->imat_ren includes viewinv! */
+			else if (R.r.bake_normal_space == R_BAKE_SPACE_WORLD)
+				mul_mat3_m4_v3(R.viewinv, nor);
+
+			normalize_v3(nor); /* in case object has scaling */
+
+			/* The invert of the red channel is to make
+			 * the normal map compliant with the outside world.
+			 * It needs to be done because in Blender
+			 * the normal used in the renderer points inward. It is generated
+			 * this way in calc_vertexnormals(). Should this ever change
+			 * this negate must be removed. */
+			shr.combined[0]= (-nor[0])/2.0f + 0.5f;
+			shr.combined[1]= nor[1]/2.0f + 0.5f;
+			shr.combined[2]= nor[2]/2.0f + 0.5f;
+		}
+		else if (bs->type==RE_BAKE_TEXTURE) {
+			shr.combined[0]= shi->r;
+			shr.combined[1]= shi->g;
+			shr.combined[2]= shi->b;
+			shr.alpha = shi->alpha;
+		}
+		else if (bs->type==RE_BAKE_SHADOW) {
+			copy_v3_v3(shr.combined, shr.shad);
+			shr.alpha = shi->alpha;
+		}
+		else if (bs->type==RE_BAKE_SPEC_COLOR) {
+			shr.combined[0]= shi->specr;
+			shr.combined[1]= shi->specg;
+			shr.combined[2]= shi->specb;
+			shr.alpha = 1.0f;
+		}
+		else if (bs->type==RE_BAKE_SPEC_INTENSITY) {
+			shr.combined[0]=
+			shr.combined[1]=
+			shr.combined[2]= shi->spec;
+			shr.alpha = 1.0f;
+		}
+		else if (bs->type==RE_BAKE_MIRROR_COLOR) {
+			shr.combined[0]= shi->mirr;
+			shr.combined[1]= shi->mirg;
+			shr.combined[2]= shi->mirb;
+			shr.alpha = 1.0f;
+		}
+		else if (bs->type==RE_BAKE_MIRROR_INTENSITY) {
+			shr.combined[0]=
+			shr.combined[1]=
+			shr.combined[2]= shi->ray_mirror;
+			shr.alpha = 1.0f;
+		}
+		else if (bs->type==RE_BAKE_ALPHA) {
+			shr.combined[0]=
+			shr.combined[1]=
+			shr.combined[2]= shi->alpha;
+			shr.alpha = 1.0f;
+		}
+		else if (bs->type==RE_BAKE_EMIT) {
+			shr.combined[0]=
+			shr.combined[1]=
+			shr.combined[2]= shi->emit;
+			shr.alpha = 1.0f;
+		}
+	}
+	
+	if (bs->rect_float && !bs->vcol) {
+		float *col= bs->rect_float + 4*(bs->rectx*y + x);
+		copy_v3_v3(col, shr.combined);
+		if (bs->type==RE_BAKE_ALL || bs->type==RE_BAKE_TEXTURE) {
+			col[3]= shr.alpha;
+		}
+		else {
+			col[3]= 1.0;
+		}
+	}
+	else {
+		/* Target is char (LDR). */
+		unsigned char col[4];
+
+		if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE)) {
+			float rgb[3];
+
+			copy_v3_v3(rgb, shr.combined);
+			if (R.scene_color_manage) {
+				/* Vertex colors have no way to specify color space, so they
+				 * default to sRGB. */
+				if (!bs->vcol)
+					IMB_colormanagement_scene_linear_to_colorspace_v3(rgb, bs->rect_colorspace);
+				else
+					linearrgb_to_srgb_v3_v3(rgb, rgb);
+			}
+			rgb_float_to_uchar(col, rgb);
+		}
+		else {
+			rgb_float_to_uchar(col, shr.combined);
+		}
+		
+		if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE)) {
+			col[3]= FTOCHAR(shr.alpha);
+		}
+		else {
+			col[3]= 255;
+		}
+
+		if (bs->vcol) {
+			/* Vertex color baking. Vcol has no useful alpha channel (it exists
+			 * but is used only for vertex painting). */
+			bs->vcol->r = col[0];
+			bs->vcol->g = col[1];
+			bs->vcol->b = col[2];
+		}
+		else {
+			unsigned char *imcol= (unsigned char *)(bs->rect + bs->rectx*y + x);

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list