Archived

This topic is now archived and is closed to further replies.

How to set texture stage

This topic is 5310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to add a texture to a square. I have created the texture from a small .bmp. I have used SetTexture to specify the texture. I have copied the following texture stages from the SDK help which occur in the following order: SetTextureStage 0,D3DTSS_COLOROP, D3DTOP_MODULATE SetTextureStage 0,D3DTSS_COLORARG1, D3DTA_TEXTURE SetTextureStage 0,D3DTSS_COLORARG1, D3DTA_DIFFUSE SetTextureStage 0,D3DTSS_ALPHAOP, D3DTA_DISABLE My questions are: 1. I only have a fundamental understanding of the texture stage pipeline. As a result I am finding it difficult to graps the flag combinations used in SetTextureStage, which and how to use them. 2. I am not sure where this code should be placed. Does it come before SetStreamSource,SetVertexShader, SetRenderState or after. Presumably it comes before the use of DrawPrimitive. Once again any helpful advice would be much appreciated.

Share this post


Link to post
Share on other sites
Yo Emulti, check this out. I got to learn all I need about
texture from this site, very clear explaination:

http://www.thehavok.co.uk/scene/32bits/tutorials/directx/thebasics3/tut4texturemapping.htm

Some good tutorials there, this in particular.





Share this post


Link to post
Share on other sites
It should not matter what order you put SetRenderState, SetTextureStageState, SetStreamSource, or SetVertexShader. The first two can be before or after BeginScene. Probably the same is true of the last two.

Anyway your code is correct except that the third line should be "COLORARG2" not "COLORARG1" but that flaw is probably not even in your code, just this post.

Here''s an explanation: Basic lighting is done with gouraud shading, which means each vertex has a color value which tints the texture. If the color is red, only the red components of the texture will show up. If the color is white, the texture appears normally (fully lit). The algorithm for this is PixelColor = VertexColor * TextureColor. Technically in terms of byte values this is divided by 255 but let''s just think of the colors as floats from 0..1.

Anyway, "modulate" means "multiply" so specifying D3DTOP_MODULATE tells Direct3D that to compute the pixel color we want it to multiply two things together. Which things? That''s what the COLORARG comments are for. D3DTA_TEXTURE is the texture and D3DTA_DIFFUSE is the vertex color (aka the diffuse color). You can reverse the order if you want (ARG1=DIFFUSE, ARG2=TEXTURE), it makes no difference.

If you wanted to, you can set COLORARG1=D3DTA_TEXTURE and COLOROP=D3DTOP_SELECTARG1. This means PixelColor = TextureColor, so in other words ignore the vertex color. If you do for some reason want to ignore the vertex colors, the only way to do it is either to set them all to white or to do the SELECTARG1 thing.

Alpha arguments work exactly the same way. When you decide to implement alpha-blending, you use SELECTARG1:DIFFUSE to have a simple polygon/sprite where all areas are equally transparent, SELECTARG1:TEXTURE to use the texture''s alpha component to have some areas be transparent and others opaque (or less/more transparent) [you do this for alpha-testing], or modulating the two if you want both the texture''s alphamap and the ability to fade the sprite/polygon.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Thank you both for this information.

Yes I did refer to COLORARG1 twice by mistake. I am still getting around the concept of a vertex supplying a color, because the vertex is describing a point rather than an area. But I guess it means that the source of the color is coming from that vertex, so if I have three vertices with three different colors I should get a kind of prism color effect emnanting from each vertex.

Share this post


Link to post
Share on other sites
Ah yes I know have three vertices with different colors and I get the color merging effect I said above. It seems much easier to see what is going on using colors instead of white vertexes on black background.

Share this post


Link to post
Share on other sites
So my rendering code for a square now looks like this...

Private Sub RenderSquare()
sizeOfVertex = Len(VerticesSquare(0))
mVideoCard.SetStreamSource 0, mBuffer(1), sizeOfVertex
mVideoCard.SetVertexShader FVF_TLVERTEX
mVideoCard.SetRenderState D3DRS_LIGHTING, 0
mVideoCard.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE

mVideoCard.SetTexture 0, mTexture
''mVideoCard.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
''mVideoCard.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
''mVideoCard.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_CURRENT
''mVideoCard.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_DISABLE

mVideoCard.DrawPrimitive D3DPT_TRIANGLEFAN, 0, 2

End Sub

I have commented out the SetTextureStageState requests. I believe the image should be displayed now as just the texture from the bitmap without diffusing the vertex colors.

But at the moment I am just getting a white square with no texture. This suggests to me that texture isn''t being loaded properly or my tu and tv co-ordinates are incorrect in the vertices.

I believe tu and tv should both be set to 1. Should my z param be greater than 0...perhaps 0.1? The vertices for my square look like this:

Dim VertexSizeInBytes As Long

VertexSizeInBytes = Len(VerticesSquare(0))
With VerticesSquare(0)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 1
.tv = 1
.x = 250
.y = 250
.z = 0
End With
With VerticesSquare(1)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 1
.tv = 1
.x = 450
.y = 250
.z = 0
End With
With VerticesSquare(2)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 1
.tv = 1
.x = 450
.y = 450
.z = 0
End With
With VerticesSquare(3)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 1
.tv = 1
.x = 250
.y = 450
.z = 0
End With

''create the vertex buffer to add to the stream
Set mBuffer(1) = mVideoCard.CreateVertexBuffer( _
VertexSizeInBytes * UBound(VerticesSquare), _
0, _
FVF_TLVERTEX, _
D3DPOOL_DEFAULT)
If mBuffer(1) Is Nothing Then Exit Function

D3DVertexBuffer8SetData mBuffer(1), 0, VertexSizeInBytes * UBound(VerticesSquare), 0, VerticesSquare(0)

CreateVertices_Square = True
End Function

Share this post


Link to post
Share on other sites
At this point I realize that I have misunderstood the tu and tv values. These do of course refer to which parts of the bitmap I want to transfer to my vertex.

So I now have my bitmap displayed inside my square with tu and tv values of:

With VerticesSquare(0)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 0
.tv = 0
.x = 250
.y = 250
.z = 0
End With
With VerticesSquare(1)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 0
.tv = 1
.x = 450
.y = 250
.z = 0
End With
With VerticesSquare(2)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 1
.tv = 1
.x = 450
.y = 450
.z = 0
End With
With VerticesSquare(3)
.color = &HFFFFFFFF
.rhw = 1
.Specular = &O0
.tu = 1
.tv = 0
.x = 250
.y = 450
.z = 0
End With




"What do you make of it Spock?"

"It''s programming Jim but not as we know it."

Share this post


Link to post
Share on other sites