Welcome! Log In Create A New Profile

Advanced

Substract texture from other

Posted by copete23 
Substract texture from other
October 26, 2009 03:04PM
Hi, the porblem is this, I have two textures GX_SetTexCoordGen (GX_TEXCOORD0. ..) and GX_SetTexCoordGen (GX_TEXCOORD1. ..) and what I try to do is, subtract the second texture from the first, in TEV, but i don`t know how. Does anyone have any idea how to do it?

Thanks :)
Re: Substract texture from other
October 26, 2009 05:31PM
Each TEV stage implements a blending equation (see the TEV patent). Something like this should work, but I didn't run any tests:
/* Stage 1: read the 1st texture color and output to GX_TEVPREV */
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_FALSE, GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE0, GX_CC_ZERO, GX_CC_ZERO, GX_CC_ZERO, GX_CC_TEXC);

/* Stage 2: read the 2nd texture color, subtract it from the 1st color, output to GX_TEVPREV */
GX_SetTevOrder(GX_TEVSTAGE1, GX_TEXCOORD1, GX_TEXMAP1, GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE1, GX_TEV_SUB, GX_TB_ZERO, GX_CS_SCALE_1, GX_FALSE, GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE1, GX_CC_TEXC, GX_CC_ZERO, GX_CC_ZERO, GX_CC_CPREV);
I assumed you have different textures, but if you're only changing the texture coordinates for the same texture, you don't need to load the same texture twice. For this scenario, you need to change GX_SetTevOrder() line in stage 2 and specify GX_TEXMAP0.
Re: Substract texture from other
October 26, 2009 09:44PM
Hi Michael, something is wrong, I might be forgeting something, when I put GX_SetTevOrder (GX_TEVSTAGE1, GX_TEXCOORD1, GX_TEXMAP1, GX_COLORNULL), shows me the map 0 ... What I try to do is a emboss mapping, using two textures, the first is the original texture, and the second the displacement map (gray scale) ... better show you what I have:

GX_SetNumTexGens(2);
// The simple map 

 GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
    guLightPerspective(mv,45, (f32)rmode->viWidth/rmode->viHeight, 1.05F, 1.0F, 0.0F, 0.0F);
    guMtxTrans(mv2, 0.0F, 0.0F, 0.0F);
    guMtxConcat(mv, mv2, mv);
GX_LoadTexMtxImm(mv, GX_TEXMTX0, GX_MTX2x4);
      
// The  displacement map   
   GX_SetTexCoordGen(GX_TEXCOORD1,GX_TG_MTX2x4,GX_TG_BUMP0 , GX_TEXMTX1);
  guLightPerspective(mr,45, (f32)rmode->viWidth/rmode->viHeight, 1.05F, 1.0F, 0.0F, 0.0F);
    guMtxTrans(mr2, 0.0F, 0.0F, 0.0F);
    guMtxConcat(mr, mr2, mr); 
GX_LoadTexMtxImm(mr, GX_TEXMTX1, GX_MTX2x4);

/*Load the textures*/
GX_LoadTexObj(&texture[1], GX_TEXMAP0); //simple map
GX_LoadTexObj(&texture[2], GX_TEXMAP1);// displacement map

while(1){

Light(....);
//your code
    /* Stage 1: read the 1st texture color and output to GX_TEVPREV */
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_FALSE, GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE0, GX_CC_ZERO, GX_CC_ZERO, GX_CC_ZERO, GX_CC_TEXC);

/* Stage 2: read the 2nd texture color, subtract it from the 1st color, output to GX_TEVPREV */
GX_SetTevOrder(GX_TEVSTAGE1, GX_TEXCOORD1, GX_TEXMAP1, GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE1, GX_TEV_SUB, GX_TB_ZERO, GX_CS_SCALE_1, GX_FALSE, GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE1, GX_CC_TEXC, GX_CC_ZERO, GX_CC_ZERO, GX_CC_CPREV);

GX_SetNumTevStages(2);   
     
GX_SetTevOp(GX_TEVSTAGE0, GX_REPLACE);
 GX_SetTevOp(GX_TEVSTAGE1, GX_REPLACE);    

......other model data.......
}

It`s ok? (probably not, I'm so lost XD)
Re: Substract texture from other
October 26, 2009 10:21PM
Hi copete23,
You don't need this:
GX_SetTevOp(GX_TEVSTAGE0, GX_REPLACE);
GX_SetTevOp(GX_TEVSTAGE1, GX_REPLACE);
Take a look at the source code for GX_SetTevOp() in gx.c. This function calls GX_SetTevColorOp() and GX_SetTevColorIn(), so you're undoing what you just setup.
Re: Substract texture from other
October 27, 2009 12:47PM
Hi,

what you're looking for is something along this:

GX_SetTevColor(GX_TEVREG0,GXColor{,0,0,0}); //TEVREG0 with only alpha value set is used here to scale/bump the displacement.

GX_SetNumTexGens(3);
GX_SetTexCoordGen(GX_TEXCOORD0,GX_TG_MTX2x4,GX_TG_TEX0,GX_TEXMTX0); // tex coords for displacement texture generated from vertex texcoord data
GX_SetTexCoordGen(GX_TEXCOORD1,GX_TG_BUMP0,GX_TG_TEXCOORD0,GX_IDENTITY); // tex coords for displacement texture for emboss bump mapping
GX_SetTexCoordGen(GX_TEXCOORD2,GX_TG_MTX3x4,GX_TG_TEX0,GX_TEXMTX1) // tex coord for material texture

GX_SetNumTevStages(3);
GX_SetTevOrder(GX_TEVSTAGE0,GX_TEXCOORD0,GX_TEXMAP0,GX_COLOR0A0);
GX_SetTevColorOp(GX_TEVSTAGE0,GX_TEV_ADD,GX_TB_ZERO,GX_CS_SCALE_1,GX_FALSE,GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE0,GX_CC_ZERO,GX_CC_TEXC,GX_CC_A0,GX_CC_RASC);

GX_SetTevOrder(GX_TEVSTAGE1,GX_TEXCOORD1,GX_TEXMAP0,GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE1,GX_TEV_SUB,GX_TB_ZERO,GX_CS_SCALE_1,GX_TRUE,GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE1,GX_CC_ZERO,GX_CC_TEXC,GX_CC_A0,GX_CC_CPREV);

GX_SetTevOrder(GX_TEVSTAGE2,GX_TEXCOORD2,GX_TEXMAP1,GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE2,GX_TEV_ADD,GX_TB_ZERO,GX_CS_SCALE_1,GX_TRUE,GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE2,GX_CC_ZERO,GX_CC_TEXC,GX_CC_CPREV,GX_CC_ZERO);

Also keep in mind that you'll have scale the vertex normals by the normal matrix which you normally generate out of the model-view matix. But this time you don't inverte and transpose the matrix, only scale it and reload it with GX_LoadNrmMtxImm().

Hope this helps?

regards
shagkur
Re: Substract texture from other
October 27, 2009 05:13PM
Quote
Michael
Hi copete23,
You don't need this:
GX_SetTevOp(GX_TEVSTAGE0, GX_REPLACE);
GX_SetTevOp(GX_TEVSTAGE1, GX_REPLACE);
Take a look at the source code for GX_SetTevOp() in gx.c. This function calls GX_SetTevColorOp() and GX_SetTevColorIn(), so you're undoing what you just setup.

XD it´s true sorry.


shagkur , I tried it your example, and I have not managed to work well, in the emulator dolphin looks good, but without relief, and in the real wii, it looks all distorted (?), i don`t know why, look:


emulator:[img39.imageshack.us]
wii:[img197.imageshack.us]

Maybe I'm getting something wrong, this is what I have set:

Mtx disp;
Mtx mat;

guMtxScale(disp,1.0,1.0,1.0);
GX_LoadTexMtxImm(disp, GX_TEXMTX0, GX_MTX2x4);
GX_SetTexCoordGen(GX_TEXCOORD0,GX_TG_MTX2x4,GX_TG_TEX0,GX_TEXMTX0); // tex coords for displacement texture generated from vertex texcoord data

GX_SetTexCoordGen(GX_TEXCOORD1,GX_TG_BUMP0,GX_TG_TEXCOORD0,GX_IDENTITY); // tex coords for displacement texture for emboss bump mapping


guMtxScale(mat,1.0,1.0,1.0);
GX_LoadTexMtxImm(mat, GX_TEXMTX1, GX_MTX3x4); 
GX_SetTexCoordGen(GX_TEXCOORD2,GX_TG_MTX3x4,GX_TG_TEX0,GX_TEXMTX1); // tex coord for material texture      
    
GX_LoadTexObj(&texture[2], GX_TEXMAP0);
GX_LoadTexObj(&texture[1], GX_TEXMAP1);

while(1){

Light(....);

GXColor color={0,0,0,0};

GX_SetTevColor(GX_TEVREG0,color); //TEVREG0 with only alpha value set is used here to scale/bump the displacement.
    
GX_SetNumTevStages(3);
GX_SetTevOrder(GX_TEVSTAGE0,GX_TEXCOORD0,GX_TEXMAP0,GX_COLOR0A0);
GX_SetTevColorOp(GX_TEVSTAGE0,GX_TEV_ADD,GX_TB_ZERO,GX_CS_SCALE_1,GX_FALSE,GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE0,GX_CC_ZERO,GX_CC_TEXC,GX_CC_A0,GX_CC_RASC);

GX_SetTevOrder(GX_TEVSTAGE1,GX_TEXCOORD1,GX_TEXMAP0,GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE1,GX_TEV_SUB,GX_TB_ZERO,GX_CS_SCALE_1,GX_TRUE,GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE1,GX_CC_ZERO,GX_CC_TEXC,GX_CC_A0,GX_CC_CPREV);

GX_SetTevOrder(GX_TEVSTAGE2,GX_TEXCOORD2,GX_TEXMAP1,GX_COLORNULL);
GX_SetTevColorOp(GX_TEVSTAGE2,GX_TEV_ADD,GX_TB_ZERO,GX_CS_SCALE_1,GX_TRUE,GX_TEVPREV);
GX_SetTevColorIn(GX_TEVSTAGE2,GX_CC_ZERO,GX_CC_TEXC,GX_CC_CPREV,GX_CC_ZERO)

guMtxIdentity(model);
		guMtxRotAxisDeg(model, &cubeAxis, rquad);
		guMtxTransApply(model, model, 0.0f,0.0f,zt-4.0f);
		guMtxConcat(view,model,modelview);

		// load the modelview matrix into matrix memory
		GX_LoadPosMtxImm(modelview, GX_PNMTX0);
		
		//guMtxInverse(modelview,mvi);
		//guMtxTranspose(mvi,modelview);
	    GX_LoadNrmMtxImm(disp, GX_PNMTX0); // this is ok?

LoadCube();
Render();
}



Render(){
             GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
	     
		GX_SetColorUpdate(GX_TRUE);
		GX_SetAlphaUpdate(GX_TRUE);
		GX_CopyDisp(frameBuffer[fb],GX_TRUE);

		GX_DrawDone();

		VIDEO_SetNextFramebuffer(frameBuffer[fb]);
		if(first_frame) {
			first_frame = 0;
			VIDEO_SetBlack(FALSE);
		}
		VIDEO_Flush();
 		VIDEO_WaitVSync();
		fb ^= 1;
}



Edited 1 time(s). Last edit at 10/27/2009 05:14PM by copete23.
Re: Substract texture from other
October 28, 2009 09:53AM
Hi

Is that your complet function to setup emboss bump mapping? I'm missing GX_SetNumTexGens......
Your mesh is drawn with Normals/Binormals and Tangents? As this is needed for emboss bump mapping.

Otherwise i don't see anything which is wrong here. That's the TEV setup used for emboss bump mapping.
Could you point me to your repository so i can take a deeper look into it?

regards
shagkur
Re: Substract texture from other
October 28, 2009 03:14PM
Ops! I'm just using simple normals,

GX_Position3f32(-1.0f,1.0f,1.0f);
GX_Normal3f32(-1.0f,1.0f,1.0f);
GX_TexCoord2f32(1.0f,0.0f);
......
....
Referring to GX_SetNumTexGens, XD I've forgotten, but in my code it is.

To set binormals and tangents i do this?:

GX_SetVtxDesc(GX_VA_NBT, GX_DIRECT);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_NBT, GX_NRM_NBT, GX_F32, 0);

And to draw the polygon?

GX_Position3f32(-1.0f,1.0f,1.0f);
GX_Normal3f32(-1.0f,1.0f,1.0f); //Does it stay like this?
GX_TexCoord2f32(1.0f,0.0f);

Thanks :)
Re: Substract texture from other
October 28, 2009 05:53PM
Hi

To set binormals and tangents i do this?:

GX_SetVtxDesc(GX_VA_NBT, GX_DIRECT);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_NBT, GX_NRM_NBT, GX_F32, 0);

Yes thats correct.

And to draw the polygon?

GX_Position3f32(-1.0f,1.0f,1.0f);
GX_Normal3f32(-1.0f,1.0f,1.0f); //Does it stay like this? --> that's the NORMAL
GX_Normal3f32(-1.0f,1.0f,1.0f); //Does it stay like this? --> that's the BITANGENT
GX_Normal3f32(-1.0f,1.0f,1.0f); //Does it stay like this? --> that's the TANGENT
GX_TexCoord2f32(1.0f,0.0f);

GX_Normal3f32 has to be called 3 times, once for every normal type. Hence you'll probably have to calculate the binormal and tangent from the normal.

regards
shagkur
Re: Substract texture from other
October 28, 2009 07:34PM
Ok, I will experiment with this, thanks again shagkur :)
Sorry, only registered users may post in this forum.

Click here to login