Exemplo n.º 1
0
static void draw_mesh_text(Scene *scene, Object *ob, int glsl)
{
	Mesh *me = ob->data;
	DerivedMesh *ddm;
	MPoly *mp, *mface  = me->mpoly;
	MTexPoly *mtpoly   = me->mtpoly;
	MLoopUV *mloopuv   = me->mloopuv;
	MLoopUV *luv;
	MLoopCol *mloopcol = me->mloopcol;  /* why does mcol exist? */
	MLoopCol *lcol;

	bProperty *prop = BKE_bproperty_object_get(ob, "Text");
	GPUVertexAttribs gattribs;
	int a, totpoly = me->totpoly;

	/* fake values to pass to GPU_render_text() */
	MCol  tmp_mcol[4]  = {{0}};
	MCol *tmp_mcol_pt  = mloopcol ? tmp_mcol : NULL;
	MTFace tmp_tf      = {{{0}}};

	/* don't draw without tfaces */
	if (!mtpoly || !mloopuv)
		return;

	/* don't draw when editing */
	if (ob->mode & OB_MODE_EDIT)
		return;
	else if (ob == OBACT)
		if (BKE_paint_select_elem_test(ob))
			return;

	ddm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);

	for (a = 0, mp = mface; a < totpoly; a++, mtpoly++, mp++) {
		short matnr = mp->mat_nr;
		int mf_smooth = mp->flag & ME_SMOOTH;
		Material *mat = (me->mat) ? me->mat[matnr] : NULL;
		int mode = mat ? mat->game.flag : GEMAT_INVISIBLE;


		if (!(mode & GEMAT_INVISIBLE) && (mode & GEMAT_TEXT) && mp->totloop >= 3) {
			/* get the polygon as a tri/quad */
			int mp_vi[4];
			float v1[3], v2[3], v3[3], v4[3];
			char string[MAX_PROPSTRING];
			int characters, i, glattrib = -1, badtex = 0;


			/* TEXFACE */
			ME_MTEXFACE_CPY(&tmp_tf, mtpoly);

			if (glsl) {
				GPU_enable_material(matnr + 1, &gattribs);

				for (i = 0; i < gattribs.totlayer; i++) {
					if (gattribs.layer[i].type == CD_MTFACE) {
						glattrib = gattribs.layer[i].glindex;
						break;
					}
				}
			}
			else {
				badtex = set_draw_settings_cached(0, &tmp_tf, mat, Gtexdraw);
				if (badtex) {
					continue;
				}
			}

			mp_vi[0] = me->mloop[mp->loopstart + 0].v;
			mp_vi[1] = me->mloop[mp->loopstart + 1].v;
			mp_vi[2] = me->mloop[mp->loopstart + 2].v;
			mp_vi[3] = (mp->totloop >= 4) ? me->mloop[mp->loopstart + 3].v : 0;

			/* UV */
			luv = &mloopuv[mp->loopstart];
			copy_v2_v2(tmp_tf.uv[0], luv->uv); luv++;
			copy_v2_v2(tmp_tf.uv[1], luv->uv); luv++;
			copy_v2_v2(tmp_tf.uv[2], luv->uv); luv++;
			if (mp->totloop >= 4) {
				copy_v2_v2(tmp_tf.uv[3], luv->uv);
			}

			/* COLOR */
			if (mloopcol) {
				unsigned int totloop_clamp = min_ii(4, mp->totloop);
				unsigned int j;
				lcol = &mloopcol[mp->loopstart];

				for (j = 0; j < totloop_clamp; j++, lcol++) {
					MESH_MLOOPCOL_TO_MCOL(lcol, &tmp_mcol[j]);
				}
			}

			/* LOCATION */
			ddm->getVertCo(ddm, mp_vi[0], v1);
			ddm->getVertCo(ddm, mp_vi[1], v2);
			ddm->getVertCo(ddm, mp_vi[2], v3);
			if (mp->totloop >= 4) {
				ddm->getVertCo(ddm, mp_vi[3], v4);
			}



			/* The BM_FONT handling is in the gpu module, shared with the
			 * game engine, was duplicated previously */

			BKE_bproperty_set_valstr(prop, string);
			characters = strlen(string);
			
			if (!BKE_image_has_ibuf(mtpoly->tpage, NULL))
				characters = 0;

			if (!mf_smooth) {
				float nor[3];

				normal_tri_v3(nor, v1, v2, v3);

				glNormal3fv(nor);
			}

			GPU_render_text(&tmp_tf, mode, string, characters,
			                (unsigned int *)tmp_mcol_pt, v1, v2, v3, (mp->totloop >= 4 ? v4 : NULL), glattrib);
		}
	}

	ddm->release(ddm);
}
Exemplo n.º 2
0
/**
 * Copies data from loop/poly to face (assumes all triangles or quads, as generated by most subdivisions)
 */
static void loops_to_customdata_corners(DerivedMesh *output)
{
	int i, numPolys, numTex, numCol, hasPCol;

	MPoly *polys;
	
	//if (!CustomData_get_layer_n(&output->loopData, CD_MLOOPCOL, n)) {
		// Not sure how this works with "n" ? 
	//	CustomData_add_layer(&output->loopData, CD_MLOOPCOL, CD_CALLOC, NULL, output->numVertData);
	//}

	numCol = CustomData_number_of_layers(&output->loopData, CD_MLOOPCOL);
	numTex = CustomData_number_of_layers(&output->loopData, CD_MLOOPUV);
	hasPCol = CustomData_has_layer(&output->loopData, CD_PREVIEW_MLOOPCOL);
	
	polys = output->getPolyArray(output);
	numPolys = output->getNumPolys(output);

	for (i = 0; i < numTex; i++) {
		int k;
		MTexPoly *texpoly = CustomData_get_layer_n(&output->polyData, CD_MTEXPOLY, i);
		MLoopUV *mloopuv = CustomData_get_layer_n(&output->loopData, CD_MLOOPUV, i);
		MTFace *texface = CustomData_get_layer_n(&output->faceData, CD_MTFACE, i);
		if (!texface) {
			texface = CustomData_add_layer(&output->faceData, CD_MTFACE, CD_CALLOC, NULL, numPolys);
		}

		for (k = 0; k < numPolys; k++) {
			int j;
			MPoly *poly = &polys[k];
			MLoopUV *ml = &mloopuv[poly->loopstart];
			
			ME_MTEXFACE_CPY(&texface[k], &texpoly[k]);
			for (j = 0; j < poly->totloop; j++) {
				copy_v2_v2(texface[k].uv[j], ml[j].uv);
			}
		}
	}

	for (i = 0; i < numCol; i++) {
		int k;
		MLoopCol *mloopcol = CustomData_get_layer_n(&output->loopData, CD_MLOOPCOL, i);
		MCol *mcol = CustomData_get_layer_n(&output->faceData, CD_MCOL, i);
		if (!mcol) {
			mcol = CustomData_add_layer(&output->faceData, CD_MCOL, CD_CALLOC, NULL, numPolys * 4);
		}
		for (k = 0; k < numPolys; k++) {
			int j;
			MPoly *poly = &polys[k];
			MLoopCol *ml = &mloopcol[poly->loopstart];
			for (j = 0; j < poly->totloop; j++) {
				MESH_MLOOPCOL_TO_MCOL(&ml[j], &mcol[j]);
			}
			mcol += 4;
		}
	}
	
	if (hasPCol) {
		int k;
		MLoopCol *mloopcol = CustomData_get_layer(&output->loopData, CD_PREVIEW_MLOOPCOL);
		MCol *mcol = CustomData_get_layer(&output->faceData, CD_PREVIEW_MCOL);
		if (!mcol) {
			mcol = CustomData_add_layer(&output->faceData, CD_MCOL, CD_CALLOC, NULL, numPolys * 4);
		}
		for (k = 0; k < numPolys; k++) {
			int j;
			MPoly *poly = &polys[k];
			MLoopCol *ml = &mloopcol[poly->loopstart];
			for (j = 0; j < poly->totloop; j++) {
				MESH_MLOOPCOL_TO_MCOL(&ml[j], &mcol[j]);
			}
			mcol += 4;
		}
	}
}