Revision: 14547 http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=14547 Author: blendix Date: 2008-04-25 18:58:32 +0200 (Fri, 25 Apr 2008)
Log Message: ----------- Apricot Branch ============== GLSL materials: - Basic support for normal maps, though it looks quite flat with the current solid mode lighting. Modified Paths: -------------- branches/apricot/source/blender/blenkernel/BKE_DerivedMesh.h branches/apricot/source/blender/blenkernel/intern/DerivedMesh.c branches/apricot/source/blender/blenkernel/intern/cdderivedmesh.c branches/apricot/source/blender/blenkernel/intern/customdata.c branches/apricot/source/blender/blenkernel/intern/node.c branches/apricot/source/blender/blenkernel/intern/subsurf_ccg.c branches/apricot/source/blender/gpu/intern/gpu_codegen.c branches/apricot/source/blender/gpu/intern/gpu_extensions.c 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/makesdna/DNA_customdata_types.h branches/apricot/source/blender/python/api2_2x/Mesh.c branches/apricot/source/blender/src/buttons_shading.c branches/apricot/source/blender/src/previewrender.c Modified: branches/apricot/source/blender/blenkernel/BKE_DerivedMesh.h =================================================================== --- branches/apricot/source/blender/blenkernel/BKE_DerivedMesh.h 2008-04-25 16:35:52 UTC (rev 14546) +++ branches/apricot/source/blender/blenkernel/BKE_DerivedMesh.h 2008-04-25 16:58:32 UTC (rev 14547) @@ -470,9 +470,14 @@ struct { float (*array)[3]; int emOffset, glIndex; + } tang; + + struct { + float (*array)[3]; + int emOffset, glIndex; } orco; - int tottface, totmcol, totorco; + int tottface, totmcol, tottang, totorco; } DMVertexAttribs; void DM_vertex_attributes_from_gpu(DerivedMesh *dm, Modified: branches/apricot/source/blender/blenkernel/intern/DerivedMesh.c =================================================================== --- branches/apricot/source/blender/blenkernel/intern/DerivedMesh.c 2008-04-25 16:35:52 UTC (rev 14546) +++ branches/apricot/source/blender/blenkernel/intern/DerivedMesh.c 2008-04-25 16:58:32 UTC (rev 14547) @@ -59,6 +59,7 @@ #include "BLI_edgehash.h" #include "BLI_editVert.h" #include "BLI_linklist.h" +#include "BLI_memarena.h" #include "BKE_cdderivedmesh.h" #include "BKE_customdata.h" @@ -938,6 +939,10 @@ MCol *cp = (MCol*)((char*)efa->data + attribs.mcol[b].emOffset); \ glVertexAttrib4ubv(attribs.mcol[b].glIndex, (GLubyte*)(cp+vert)); \ } \ + if(attribs.tottang) { \ + float *tang = attribs.tang.array[i*4 + vert]; \ + glVertexAttrib3fv(attribs.tang.glIndex, tang); \ + } \ } for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) { @@ -3056,9 +3061,129 @@ /* ******************* GLSL ******************** */ +static void DM_add_tangent_layer(DerivedMesh *dm) +{ + /* mesh vars */ + MTFace *mtface, *tf; + MFace *mface, *mf; + MVert *mvert, *v1, *v2, *v3, *v4; + MemArena *arena= NULL; + VertexTangent **vtangents= NULL; + float (*orco)[3]= NULL, (*tangent)[3]; + float *uv1, *uv2, *uv3, *uv4, *vtang; + float fno[3], tang[3], uv[4][2]; + int i, j, len, mf_vi[4], totvert, totface; + + if(CustomData_get_layer_index(&dm->faceData, CD_TANGENT) != -1) + return; + + /* check we have all the needed layers */ + totvert= dm->getNumVerts(dm); + totface= dm->getNumFaces(dm); + + mvert= dm->getVertArray(dm); + mface= dm->getFaceArray(dm); + mtface= dm->getFaceDataArray(dm, CD_MTFACE); + + if(!mtface) { + orco= dm->getVertDataArray(dm, CD_ORCO); + if(!orco) + return; + } + + /* create tangent layer */ + DM_add_face_layer(dm, CD_TANGENT, CD_CALLOC, NULL); + tangent= DM_get_face_data_layer(dm, CD_TANGENT); + + /* allocate some space */ + arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE); + BLI_memarena_use_calloc(arena); + vtangents= MEM_callocN(sizeof(VertexTangent*)*totvert, "VertexTangent"); + + /* sum tangents at connected vertices */ + for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++) { + v1= &mvert[mf->v1]; + v2= &mvert[mf->v2]; + v3= &mvert[mf->v3]; + + if (mf->v4) { + v4= &mvert[mf->v4]; + CalcNormFloat4(v1->co, v2->co, v3->co, v4->co, fno); + } + else { + v4= NULL; + CalcNormFloat(v1->co, v2->co, v3->co, fno); + } + + if(mtface) { + uv1= tf->uv[0]; + uv2= tf->uv[1]; + uv3= tf->uv[2]; + uv4= tf->uv[3]; + } + else { + uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3]; + spheremap(orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2], &uv[0][0], &uv[0][1]); + spheremap(orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2], &uv[1][0], &uv[1][1]); + spheremap(orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2], &uv[2][0], &uv[2][1]); + if(v4) + spheremap(orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2], &uv[3][0], &uv[3][1]); + } + + tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, fno, tang); + sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1); + sum_or_add_vertex_tangent(arena, &vtangents[mf->v2], tang, uv2); + sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3); + + if(mf->v4) { + v4= &mvert[mf->v4]; + + tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, fno, tang); + sum_or_add_vertex_tangent(arena, &vtangents[mf->v1], tang, uv1); + sum_or_add_vertex_tangent(arena, &vtangents[mf->v3], tang, uv3); + sum_or_add_vertex_tangent(arena, &vtangents[mf->v4], tang, uv4); + } + } + + /* write tangent to layer */ + for(i=0, tf=mtface, mf=mface; i < totface; mf++, tf++, i++, tangent+=4) { + len= (mf->v4)? 4 : 3; + + if(mtface) { + uv1= tf->uv[0]; + uv2= tf->uv[1]; + uv3= tf->uv[2]; + uv4= tf->uv[3]; + } + else { + uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3]; + spheremap(orco[mf->v1][0], orco[mf->v1][1], orco[mf->v1][2], &uv[0][0], &uv[0][1]); + spheremap(orco[mf->v2][0], orco[mf->v2][1], orco[mf->v2][2], &uv[1][0], &uv[1][1]); + spheremap(orco[mf->v3][0], orco[mf->v3][1], orco[mf->v3][2], &uv[2][0], &uv[2][1]); + if(len==4) + spheremap(orco[mf->v4][0], orco[mf->v4][1], orco[mf->v4][2], &uv[3][0], &uv[3][1]); + } + + mf_vi[0]= mf->v1; + mf_vi[1]= mf->v2; + mf_vi[2]= mf->v3; + mf_vi[3]= mf->v4; + + for(j=0; j<len; j++) { + vtang= find_vertex_tangent(vtangents[mf_vi[j]], mtface ? tf->uv[j] : uv[j]); + + VECCOPY(tangent[j], vtang); + Normalize(tangent[j]); + } + } + + BLI_memarena_free(arena); + MEM_freeN(vtangents); +} + void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs, DMVertexAttribs *attribs) { - CustomData *vdata, *fdata; + CustomData *vdata, *fdata, *tfdata = NULL; int a, b, layer; /* From the layers requested by the GLSL shader, figure out which ones are @@ -3072,41 +3197,61 @@ /* ugly hack, editmesh derivedmesh doesn't copy face data, this way we * can use offsets instead */ if(dm->release == emDM_release) - fdata = &((EditMeshDerivedMesh*)dm)->em->fdata; + tfdata = &((EditMeshDerivedMesh*)dm)->em->fdata; + else + tfdata = fdata; + /* add a tangent layer if necessary */ + for(b = 0; b < gattribs->totlayer; b++) + if(gattribs->layer[b].type == CD_TANGENT) + if(CustomData_get_layer_index(fdata, CD_TANGENT) == -1) + DM_add_tangent_layer(dm); + for(b = 0; b < gattribs->totlayer; b++) { if(gattribs->layer[b].type == CD_MTFACE) { /* uv coordinates */ if(gattribs->layer[b].name[0]) - layer = CustomData_get_named_layer_index(fdata, CD_MTFACE, + layer = CustomData_get_named_layer_index(tfdata, CD_MTFACE, gattribs->layer[b].name); else - layer = CustomData_get_active_layer_index(fdata, CD_MTFACE); + layer = CustomData_get_active_layer_index(tfdata, CD_MTFACE); if(layer != -1) { a = attribs->tottface++; - attribs->tface[a].array = fdata->layers[layer].data; - attribs->tface[a].emOffset = fdata->layers[layer].offset; + attribs->tface[a].array = tfdata->layers[layer].data; + attribs->tface[a].emOffset = tfdata->layers[layer].offset; attribs->tface[a].glIndex = gattribs->layer[b].glindex; } } else if(gattribs->layer[b].type == CD_MCOL) { /* vertex colors */ if(gattribs->layer[b].name[0]) - layer = CustomData_get_named_layer_index(fdata, CD_MCOL, + layer = CustomData_get_named_layer_index(tfdata, CD_MCOL, gattribs->layer[b].name); else - layer = CustomData_get_active_layer_index(fdata, CD_MCOL); + layer = CustomData_get_active_layer_index(tfdata, CD_MCOL); if(layer != -1) { a = attribs->totmcol++; - attribs->mcol[a].array = fdata->layers[layer].data; - attribs->mcol[a].emOffset = fdata->layers[layer].offset; + attribs->mcol[a].array = tfdata->layers[layer].data; + attribs->mcol[a].emOffset = tfdata->layers[layer].offset; attribs->mcol[a].glIndex = gattribs->layer[b].glindex; } } + else if(gattribs->layer[b].type == CD_TANGENT) { + /* tangents */ + layer = CustomData_get_layer_index(fdata, CD_TANGENT); + + if(layer != -1) { + attribs->tottang = 1; + + attribs->tang.array = fdata->layers[layer].data; + attribs->tang.emOffset = fdata->layers[layer].offset; + attribs->tang.glIndex = gattribs->layer[b].glindex; + } + } else if(gattribs->layer[b].type == CD_ORCO) { /* original coordinates */ layer = CustomData_get_layer_index(vdata, CD_ORCO); Modified: branches/apricot/source/blender/blenkernel/intern/cdderivedmesh.c =================================================================== --- branches/apricot/source/blender/blenkernel/intern/cdderivedmesh.c 2008-04-25 16:35:52 UTC (rev 14546) +++ branches/apricot/source/blender/blenkernel/intern/cdderivedmesh.c 2008-04-25 16:58:32 UTC (rev 14547) @@ -635,6 +635,10 @@ MCol *cp = &attribs.mcol[b].array[a*4 + vert]; \ glVertexAttrib4ubv(attribs.mcol[b].glIndex, (GLubyte*)cp); \ } \ + if(attribs.tottang) { \ + float *tang = attribs.tang.array[a*4 + vert]; \ + glVertexAttrib3fv(attribs.tang.glIndex, tang); \ + } \ if(smoothnormal) \ glNormal3sv(mvert[index].no); \ glVertex3fv(mvert[index].co); \ Modified: branches/apricot/source/blender/blenkernel/intern/customdata.c =================================================================== --- branches/apricot/source/blender/blenkernel/intern/customdata.c 2008-04-25 16:35:52 UTC (rev 14546) +++ branches/apricot/source/blender/blenkernel/intern/customdata.c 2008-04-25 16:58:32 UTC (rev 14547) @@ -454,13 +454,14 @@ {sizeof(MStringProperty), "MStringProperty",1,"String",NULL,NULL,NULL,NULL}, {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVTex", layerCopy_origspace_face, NULL, layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face}, - {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL} + {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL}, + {sizeof(float)*3*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL} }; const char *LAYERTYPENAMES[CD_NUMTYPES] = { "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags","CDMFloatProperty", - "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco"}; + "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco", "CDTangent"}; const CustomDataMask CD_MASK_BAREMESH = CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE; @@ -474,7 +475,7 @@ const CustomDataMask CD_MASK_DERIVEDMESH = CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_ORIGINDEX | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | @@ 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