Revision: 14544 http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=14544 Author: blendix Date: 2008-04-25 15:46:14 +0200 (Fri, 25 Apr 2008)
Log Message: ----------- Apricot Branch ============== GLSL materials: - Added the beginning of texture stack evaluation, only a few options are supported. - Added a few lines of code to display non-node materials, with solid mode lighting to at least see something. Modified Paths: -------------- branches/apricot/source/blender/blenkernel/intern/node.c branches/apricot/source/blender/gpu/GPU_material.h branches/apricot/source/blender/gpu/intern/gpu_material.c branches/apricot/source/blender/gpu/intern/material_shaders.glsl branches/apricot/source/blender/gpu/intern/material_shaders.glsl.c branches/apricot/source/blender/nodes/intern/SHD_nodes/SHD_texture.c branches/apricot/source/blender/src/drawobject.c Modified: branches/apricot/source/blender/blenkernel/intern/node.c =================================================================== --- branches/apricot/source/blender/blenkernel/intern/node.c 2008-04-25 10:25:17 UTC (rev 14543) +++ branches/apricot/source/blender/blenkernel/intern/node.c 2008-04-25 13:46:14 UTC (rev 14544) @@ -2419,7 +2419,8 @@ gpu_from_node_stack(&node->inputs, nsin, gpuin); gpu_from_node_stack(&node->outputs, nsout, gpuout); gnode= node->typeinfo->gpufunc(mat, node, gpuin, gpuout); - data_from_gpu_stack(&node->outputs, nsout, gpuout); + if(gnode) + data_from_gpu_stack(&node->outputs, nsout, gpuout); } /* groups not supported yet .. */ } Modified: branches/apricot/source/blender/gpu/GPU_material.h =================================================================== --- branches/apricot/source/blender/gpu/GPU_material.h 2008-04-25 10:25:17 UTC (rev 14543) +++ branches/apricot/source/blender/gpu/GPU_material.h 2008-04-25 13:46:14 UTC (rev 14544) @@ -8,6 +8,7 @@ struct Image; struct ImageUser; +struct Material; struct GPUMaterial; typedef struct GPUMaterial GPUMaterial; @@ -42,5 +43,7 @@ void GPU_mat_node_output(GPUNode *node, GPUType type, char *name, GPUNodeStack *out); +GPUMaterial *GPU_material_from_blender(struct Material *ma); + #endif /*__GPU_MATERIAL__*/ Modified: branches/apricot/source/blender/gpu/intern/gpu_material.c =================================================================== --- branches/apricot/source/blender/gpu/intern/gpu_material.c 2008-04-25 10:25:17 UTC (rev 14543) +++ branches/apricot/source/blender/gpu/intern/gpu_material.c 2008-04-25 13:46:14 UTC (rev 14544) @@ -33,10 +33,13 @@ #include "MEM_guardedalloc.h" +#include "DNA_image_types.h" #include "DNA_listBase.h" -#include "DNA_image_types.h" +#include "DNA_material_types.h" +#include "DNA_texture_types.h" #include "BKE_DerivedMesh.h" +#include "BKE_utildefines.h" #include "BLI_blenlib.h" @@ -189,6 +192,8 @@ *attribs = material->attribs; } +/* Nodes */ + GPUNode *GPU_mat_node_create(GPUMaterial *material, char *name, GPUNodeStack *in, GPUNodeStack *out) { GPUNode *node; @@ -205,7 +210,7 @@ GPU_node_output(node, out[i].type, out[i].name, &out[i].buf); if (strcmp(node->name, "output") == 0) { - if (!material->outbuf) { + if (!material->outbuf && node->inputs.first) { GPUInput *inp = (GPUInput*)node->inputs.first; material->outbuf = inp->buf; } @@ -252,3 +257,192 @@ GPU_node_output(node, out->type, out->name, &out->buf); } +/* Code generation */ + +static GPUNodeBuf *texture_rgb_blend(GPUMaterial *mat, GPUNodeBuf *texbuf, GPUNodeBuf *outbuf, GPUNodeBuf *factbuf, GPUNodeBuf *facgbuf, int blendtype) +{ + GPUNodeBuf *inbuf; + GPUNode *node; + + if(blendtype == MTEX_BLEND) + node = GPU_mat_node_create(mat, "mtex_blend", NULL, NULL); + else + node = GPU_mat_node_create(mat, "mtex_blend", NULL, NULL); + + GPU_node_input(node, GPU_VEC3, "out", NULL, outbuf); + GPU_node_input(node, GPU_VEC3, "tex", NULL, texbuf); + GPU_node_input(node, GPU_FLOAT, "fact", NULL, factbuf); + GPU_node_input(node, GPU_FLOAT, "facg", NULL, facgbuf); + + GPU_node_output(node, GPU_VEC3, "in", &inbuf); + + return inbuf; +} + +static void material_tex_nodes_create(GPUMaterial *mat, Material *ma, GPUNodeBuf **colbuf_r, GPUNodeBuf **specbuf_r) +{ + MTex *mtex; + Tex *tex; + GPUNodeBuf *texcobuf, *tinbuf, *trgbbuf, *tnorbuf, *stencilbuf = NULL; + GPUNodeBuf *colbuf, *specbuf, *colfacbuf; + GPUNode *node; + float col[4], spec[4]; + int tex_nr; + + VECCOPY(col, &ma->r); + col[3]= 1.0f; + VECCOPY(spec, &ma->specr); + spec[3]= 1.0f; + + node = GPU_mat_node_create(mat, "setrgb", NULL, NULL); + GPU_mat_node_uniform(node, GPU_VEC4, col); + GPU_node_output(node, GPU_VEC4, "col", &colbuf); + + node = GPU_mat_node_create(mat, "setrgb", NULL, NULL); + GPU_mat_node_uniform(node, GPU_VEC4, spec); + GPU_node_output(node, GPU_VEC4, "spec", &specbuf); + + for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) { + /* separate tex switching */ + if(ma->septex & (1<<tex_nr)) continue; + + if(ma->mtex[tex_nr]) { + mtex= ma->mtex[tex_nr]; + + tex= mtex->tex; + if(tex==0) continue; + + /* which coords */ + if(mtex->texco==TEXCO_ORCO) { + node = GPU_mat_node_create(mat, "texco_orco", NULL, NULL); + GPU_mat_node_attribute(node, GPU_VEC3, CD_ORCO, ""); + GPU_node_output(node, GPU_VEC3, "texco", &texcobuf); + } + else if(mtex->texco==TEXCO_NORM) { + node = GPU_mat_node_create(mat, "texco_norm", NULL, NULL); + GPU_node_output(node, GPU_VEC3, "texco", &texcobuf); + } + else if(mtex->texco==TEXCO_UV) { + node = GPU_mat_node_create(mat, "texco_uv", NULL, NULL); + GPU_mat_node_attribute(node, GPU_VEC2, CD_MTFACE, mtex->uvname); + GPU_node_output(node, GPU_VEC3, "texco", &texcobuf); + } + else continue; + + node = GPU_mat_node_create(mat, "mtex_2d_mapping", NULL, NULL); + GPU_node_input(node, GPU_VEC3, "texco", NULL, texcobuf); + GPU_node_output(node, GPU_VEC3, "texco", &texcobuf); + + node = GPU_mat_node_create(mat, "mtex_mapping", NULL, NULL); + GPU_node_input(node, GPU_VEC3, "texco", NULL, texcobuf); + GPU_mat_node_uniform(node, GPU_VEC3, mtex->size); + GPU_mat_node_uniform(node, GPU_VEC3, mtex->ofs); + GPU_node_output(node, GPU_VEC3, "texco", &texcobuf); + + if(tex && tex->type == TEX_IMAGE && tex->ima) { + node = GPU_mat_node_create(mat, "mtex_image", NULL, NULL); + GPU_node_input(node, GPU_VEC3, "texco", NULL, texcobuf); + GPU_mat_node_image(node, GPU_TEX2D, tex->ima, NULL); + + GPU_node_output(node, GPU_FLOAT, "tin", &tinbuf); + GPU_node_output(node, GPU_VEC4, "trgb", &trgbbuf); + GPU_node_output(node, GPU_VEC3, "tnor", &tnorbuf); + } + else continue; + + /* texture output */ + + if(mtex->texflag & MTEX_RGBTOINT) { + node = GPU_mat_node_create(mat, "mtex_rgbtoint", NULL, NULL); + GPU_node_input(node, GPU_VEC4, "trgb", NULL, trgbbuf); + GPU_node_output(node, GPU_FLOAT, "tin", &tinbuf); + + } + if(mtex->texflag & MTEX_NEGATIVE) { + node = GPU_mat_node_create(mat, "mtex_invert", NULL, NULL); + GPU_node_input(node, GPU_VEC4, "trgb", NULL, trgbbuf); + GPU_node_output(node, GPU_VEC4, "trgb", &trgbbuf); + } + + if(mtex->texflag & MTEX_STENCIL) { + if(!stencilbuf) { + node = GPU_mat_node_create(mat, "setvalue", NULL, NULL); + GPU_node_input(node, GPU_FLOAT, "tin", NULL, tinbuf); + GPU_node_output(node, GPU_FLOAT, "stencil", &stencilbuf); + } + else { + node = GPU_mat_node_create(mat, "math_multiply", NULL, NULL); + GPU_node_input(node, GPU_FLOAT, "tin", NULL, tinbuf); + GPU_node_input(node, GPU_FLOAT, "stencil", NULL, stencilbuf); + GPU_node_output(node, GPU_FLOAT, "stencil", &stencilbuf); + } + } + + /* mapping */ + if(mtex->mapto & (MAP_COL+MAP_COLSPEC)) { + /* stencil maps on the texture control slider, not texture intensity value */ + if(stencilbuf) { + node = GPU_mat_node_create(mat, "math_multiply", NULL, NULL); + GPU_mat_node_uniform(node, GPU_FLOAT, &mtex->colfac); + GPU_node_input(node, GPU_FLOAT, "stencil", NULL, stencilbuf); + GPU_node_output(node, GPU_FLOAT, "colfac", &colfacbuf); + } + else { + node = GPU_mat_node_create(mat, "setvalue", NULL, NULL); + GPU_mat_node_uniform(node, GPU_FLOAT, &mtex->colfac); + GPU_node_output(node, GPU_FLOAT, "colfac", &colfacbuf); + } + + if(mtex->mapto & MAP_COL) + colbuf = texture_rgb_blend(mat, trgbbuf, colbuf, tinbuf, colfacbuf, mtex->blendtype); + if(mtex->mapto & MAP_COLSPEC) + specbuf = texture_rgb_blend(mat, trgbbuf, specbuf, tinbuf, colfacbuf, mtex->blendtype); + } + } + } + + *colbuf_r= colbuf; + *specbuf_r= specbuf; +} + +static void material_nodes_create(GPUMaterial *mat, Material *ma) +{ + GPUNode *node; + GPUNodeBuf *colbuf, *specbuf, *combinedbuf; + float hard; + + material_tex_nodes_create(mat, ma, &colbuf, &specbuf); + + if(ma->mode & MA_SHLESS) { + mat->outbuf = colbuf; + } + else { + node = GPU_mat_node_create(mat, "material_simple", NULL, NULL); + GPU_node_input(node, GPU_VEC4, "col", NULL, colbuf); + GPU_mat_node_uniform(node, GPU_FLOAT, &ma->ref); + GPU_node_input(node, GPU_VEC4, "spec", NULL, specbuf); + GPU_mat_node_uniform(node, GPU_FLOAT, &ma->spec); + hard= ma->har; + GPU_mat_node_uniform(node, GPU_FLOAT, &hard); + GPU_node_output(node, GPU_VEC4, "combined", &combinedbuf); + + mat->outbuf = combinedbuf; + } +} + +GPUMaterial *GPU_material_from_blender(Material *ma) +{ + GPUMaterial *mat; + + mat = GPU_material_construct_begin(); + + material_nodes_create(mat, ma); + + if(!GPU_material_construct_end(mat)) { + GPU_material_free(mat); + mat= NULL; + } + + return mat; +} + Modified: branches/apricot/source/blender/gpu/intern/material_shaders.glsl =================================================================== --- branches/apricot/source/blender/gpu/intern/material_shaders.glsl 2008-04-25 10:25:17 UTC (rev 14543) +++ branches/apricot/source/blender/gpu/intern/material_shaders.glsl 2008-04-25 13:46:14 UTC (rev 14544) @@ -384,6 +384,78 @@ void texture_image(vec3 vec, sampler2D ima, out float value, out vec4 color, out vec3 normal) { color = texture2D(ima, (vec.xy + vec2(1.0, 1.0))*0.5); + value = 1.0; normal = vec3(0.0, 0.0, 0.0); } +/************* MTEX *****************/ + +void texco_orco(vec3 attorco, out vec3 orco) +{ + orco = attorco; +} + +void texco_uv(vec2 attuv, out vec3 uv) +{ + uv = vec3(attuv*2.0 - vec2(1.0, 1.0), 0.0); +} + +void texco_norm(out vec3 normal) +{ + normal = -normalize(varnormal); +} + +void mtex_blend(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol) +{ + float facm; + + fact *= facg; + facm = 1.0-fact; + + incol = fact*texcol + facm*outcol; +} + +void mtex_rgbtoint(vec4 rgb, out float intensity) +{ + intensity = 0.35*rgb.r + 0.45*rgb.g + 0.2*rgb.b; +} + +void mtex_invert(vec4 inrgb, out vec4 outrgb) +{ + outrgb = vec4(1.0) - inrgb; +} + +void mtex_mapping(vec3 texco, vec3 size, vec3 ofs, out vec3 outtexco) +{ + outtexco.x = size.x*(texco.x - 0.5) + ofs.x + 0.5; + outtexco.y = size.y*(texco.y - 0.5) + ofs.y + 0.5; + outtexco.z = texco.z; +} + +void mtex_2d_mapping(vec3 vec, out vec3 outvec) +{ + outvec.xy = (vec.xy + vec2(1.0, 1.0))*0.5; + outvec.z = vec.z; +} + +void mtex_image(vec3 vec, sampler2D ima, out float value, out vec4 color, out vec3 normal) +{ + color = texture2D(ima, vec.xy); + value = 1.0; + normal = vec3(0.0, 0.0, 0.0); +} + +/******* MATERIAL *********/ + @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org http://lists.blender.org/mailman/listinfo/bf-blender-cvs