Ejemplo n.º 1
0
bool EDetail::Update	(LPCSTR name)
{
	m_sRefs				= name;
    // update link
    CEditableObject* R	= Lib.CreateEditObject(name);
    if (!R){
 		ELog.Msg		(mtError,"Can't load detail object '%s'.", name);
        return false;
    }
    if(R->SurfaceCount()!=1){
    	ELog.Msg		(mtError,"Object must contain 1 material.");
	    Lib.RemoveEditObject(R);
    	return false;
    }
	if(R->MeshCount()==0){
    	ELog.Msg		(mtError,"Object must contain 1 mesh.");
	    Lib.RemoveEditObject(R);
    	return false;
    }

    Lib.RemoveEditObject(m_pRefs);
    m_pRefs				= R;

    // fill geometry
    CEditableMesh* M	= *m_pRefs->FirstMesh();
    U16Vec inds;

    // fill vertices
    bv_bb.invalidate();
    u32 idx			= 0;
    for (u32 f_id=0; f_id<M->GetFCount(); f_id++){
        st_Face& F 	= M->GetFaces()[f_id];
    	u16 ind[3];
    	for (int k=0; k<3; k++,idx++){
            Fvector& P  = M->GetVerts()[F.pv[k].pindex];
            st_VMapPt&vm= M->GetVMRefs()[F.pv[k].vmref].pts[0];
            Fvector2& uv= M->GetVMaps()[vm.vmap_index]->getUV(vm.index);
        	ind[k]		= _AddVert	(P,uv.x,uv.y);
	        bv_bb.modify(vertices[ind[k]].P);
        }
        if (isDegenerated(ind))	continue;
        if (isEqual(inds,ind))	continue;
        inds.push_back(ind[0]);
        inds.push_back(ind[1]);
        inds.push_back(ind[2]);
    }
	number_indices 		= inds.size();
	indices				= (u16*)xr_malloc(number_indices*sizeof(u16));
    Memory.mem_copy		(indices,inds.begin(),number_indices*sizeof(u16));

	bv_bb.getsphere		(bv_sphere.P,bv_sphere.R);

    OnDeviceCreate		();

    return true;
}
Ejemplo n.º 2
0
void triangle(Vec3i t0, Vec3i t1, Vec3i t2, Vec2i uv0, Vec2i uv1, Vec2i uv2,
              float intensity, int* zbuf, TGAImage& image) {
    if (isDegenerated(t0,t1,t2)) return;
    if (t0.y > t1.y) {
        std::swap(t0, t1);
        std::swap(uv0, uv1);
    }
    if (t0.y > t2.y) {
        std::swap(t0, t2);
        std::swap(uv0, uv2);
    }
    if (t1.y > t2.y) {
        std::swap(t1, t2);
        std::swap(uv1, uv2);
    }
    int* boundbox = getBoundBox(t0, t1, t2);
    Vec2i vertex(t1.x - t0.x, t1.y - t0.y);
    Vec2i tmpUv(uv1.x - uv0.x, uv1.y - uv0.y);
    for (int x = boundbox[0]; x <= boundbox[2]; x++) {
        for (int y = boundbox[1]; y <= boundbox[3]; y++) {
            if (insideTriangle(x, y, t0, t1, t2)) {
                int idx = x + y * width;
                int z = find_z(x, y, t0, t1, t2);
                Vec2i P(x, y), v(t0.x, t0.y);
                Vec2i s01(t1.x - t0.x, t1.y - t0.y), s02(t2.x - t0.x, t2.y - t0.y), sp0(t0.x - P.x, t0.y - P.y);
                Vec3i tmp1(s01.x, s02.x, sp0.x), tmp2(s01.y, s02.y, sp0.y);
                Vec3i tmp3 = tmp1 ^ tmp2;
                Vec3f res(tmp3.x, tmp3.y, tmp3.z);
                if (res.z != 0) {
                    res = res * (1 / res.z);
                } else {
                    continue;
                }
                Vec2f uvP =  uv0 * (1 - res.x - res.y) + uv1 * res.x + uv2 * res.y;
                if (zbuf[idx] < z) {
                    zbuf[idx] =  z;
                    TGAColor color = model->getDiffusive(uvP);
                    image.set(x, y, TGAColor(color.r * intensity , color.g * intensity , color.b * intensity , 255));
                }
            }
        }
    }
    delete[] boundbox;
}