Skip to main content
GameDev.net gamedev.net
🔒 Locked

Textures not working!

Started by mdfmKoRn Dec 23, 2001 at 4:20 PM 10 replies 500+ views
Original Post
mdfmKoRn
mdfmKoRn
I'm have been trying for hours to texture a panel. So far, it loads the image okay then proceeds to draw the panel shaded red to black from the bottom up. This doesn't make any sense to me! There's no red anywhere in the image or in the code. The all four verticies are white and the image is mostly white. Here's some small clips from my code:
    
LPDIRECT3DDEVICE8       D3DDevice;
LPDIRECT3DVERTEXBUFFER8 TS_VBuffer;
LPDIRECT3DTEXTURE8      Logo;

D3DXCreateTextureFromFile( D3DDevice, "Logo.dds", &Logo );

D3DDevice->SetTexture( 0, Logo );
D3DDevice->SetStreamSource( 0, TS_VBuffer, sizeof(CUSTOMVERTEX) );
D3DDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
D3DDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
    
So, uh, can anybody help me figure out why it's refusing to render the texture? ---- Herb M. (mdfmKoRn) s3202@attbi.com Edited by - mdfmKoRn on December 23, 2001 5:21:13 PM
Alload
Alload
There''s no mistake in your code, so it must be another thing.
S1CA
S1CA
Some thoughts:

1. What are the texture coordinates set to for the primitive ? - if the texture has any black and red pixels you could simply be getting 2 pixels of the texture magnified to fill the whole primitive. Setting two texture coordinates of a triangle could do this for example.

2. What are your texture stage states set to ? - these determine how a pixel is drawn, where the colour values for a pixel come from, how textures are sampled etc

3. Have you remembered to set the D3DRS_LIGHTING renderstate to FALSE ? - If your custom vertex format doesn''t have normals and lighting is enabled (it is by default IIRC), then the operation which takes place can be umm kinda random depending on what other states are set...

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
mdfmKoRn
mdfmKoRn
Okay, this is odd. Even if I don't bother loading the texture, it still does the funny red to black shade instead of the vertex colors. It works correctly until I add the u and v floats to my CUSTOMVERTEX struct.

    
#define D3DFVF_TITLE_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1|D3DFVF_DIFFUSE)

struct TITLEVERTEX
{
float x,y,z,rhw;
float tu, tv; // The position for the texture

DWORD color; // The vertex color

};
TITLEVERTEX TitleScreen_Verticies[4];


If I remove the "float tu, tv" line then it just uses vertex shading and operates correctly. As soon as I add it, it does the goodball red to black stuff.

I didn't have the D3DRS_LIGHTING render state set but I do now, and it still doesn't work.

Here are my texture stage states:
  
D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
D3DTitleDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );



----
Herb M. (mdfmKoRn)
s3202@attbi.com


Edited by - mdfmKoRn on December 23, 2001 6:45:48 PM
S1CA
S1CA
Aha!

Flexible Vertex Formats are flexible, but not THAT flexible...

...FVFs let you choose WHICH components are present in a vertex, but not which ORDER they appear in the vertex!!!

Change your structure to this:
  
struct TITLEVERTEX
{
float x,y,z,rhw;
DWORD color; // The vertex color

float tu, tv; // The position for the texture

}
TITLEVERTEX TitleScreen_Verticies[4];



With FVFs, the texture coordinates always go at the end of the structure.

For more information on this check the DX SDK docs under:

DirectX 8.1 (C++) ->
DirectX Graphics ->
Programmers Guide ->
Using Direct3D ->
Fixed Function Vertex and Pixel Processing ->
Vertex Formats

(The book icon rather than the sub topics).



--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
mdfmKoRn
mdfmKoRn
You da man! All hail Simon!

That fixed it. I had no idea the order of members of a structure was ever significant. Leave it to a C++ newbie mistake. I knew it would be one.

Anyway, thank you!

One other thing, though. The texture is the exact same size as the primitive, but it looks a little blurry and distorted. I''m assuming correcting this has something to do with the texture filtering, if there is a way. Am I right?


----
Herb M. (mdfmKoRn)
s3202@attbi.com
S1CA
S1CA
The D3DTSS_MAGFILTER, D3DTSS_MINFILTER and D3DTSS_MIPFILTER TextureStageStates affect which type of filtering D3D uses to magnify, minify and mipmap a texture. POINT or NONE disables bilinear filtering.

However since it appears you''re trying to draw sprite style things, there is another thing it could be. Assuming the size of the polygons you have on screen is exactly the same size as the texture you''re trying to display, then you may not be aware of the texel->pixel mapping required. It''s to do with where in a pixel a coordinate maps to. Check the docs under:

DirectX 8.1 (C++) ->
DirectX Graphics ->
Programmers Guide ->
Using Direct3D ->
Textures ->
Texture Coordinates ->
Directly Mapping Texels to Pixels


Another cause of unexpected blurriness on sprites is setting RHW to incorrect values. (Texture coordinates get multiplied by RHW for each texel as the rasteriser draws pixels - this is for perspective correction - set it to a bad value and you get unwanted stretching/shrinking which makes your sprites look ugly).

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
DrunkenHyena
DrunkenHyena
G''day!

Another common cause of texture blurriness is bad texture size. Is your texture a power of 2 in size? If not it will be adjusted to be a power of 2 at load time. So if you''re loading a texture with a width of 31, it will load (and filter) it into a 32 width texture.


Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena</FON
mdfmKoRn
mdfmKoRn
Power of 2 or a multiple of 2?

The texture is 640x480. That''s pretty gay if I can''t load a texture that size ''cause it''s not a power of 2! Man! Please tell me that was a typo.


----
Herb M. (mdfmKoRn)
s3202@attbi.com
S1CA
S1CA
What DrunkenHyena said is right - yet another one of the things which can cause blurry textures.

Some hardware requires dimensions of a texture to be power-of-two (1,2,4,8,16,32,64,128,256,512,1024,2048...).

Some hardware even requires that the textures be square as well (i.e. 512x512 is ok, 256x512 isn''t).

And finally some hardware like the 3Dfx Voodoo cards have a maximum size of 256x256 for textures.

There are some device caps which tell you what the chip will allow (ignoring the cap is ''undefined'' sometimes you''ll get blurriness, others the texture just won''t appear etc). In particular you''re interested in the following members of the D3DCAPS8 structure:

.TextureCaps
D3DPTEXTURECAPS_POW2
D3DPTEXTURECAPS_NONPOW2CONDITIONAL
D3DPTEXTURECAPS_SQUAREONLY

.MaxTextureWidth
.MaxTextureHeight

The power of 2 thing apparently makes life easier for the hardware designers for stuff like texture memory allocation, texel caching etc. It''s not really a D3D thing, nor is it even just a PC thing - the same limitation crops up on the "next gen" consoles too (although they have even more weird things).


The best solution is to check the device caps when your program starts, and if required split non power-of-2, and non-square and large textures into a set of smaller textures. For example to load a 640x480 bitmap onto a card which only supports maximum of 256x256, square, power of two only textures, at load time chop the loaded file into 6 256x256 textures:

640 / 256 = 2.5
480 / 256 = 1.875

You could use smaller textures for the .5 extra to the right and .875 to the bottom, but its usually not worth the hassle for the amount of memory saved so simply round up any fractions to the next nearest whole. When transferring the image onto the textures, just ignore the extra unused area of the texture at the right and bottom.


Unfortunately there''s not any other, easier solution (apart from stretching say a 512x512 texture which will bring back some blurriness) - we do the above texture splitting in our game engine to load background screens.


--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
mdfmKoRn
mdfmKoRn
Okay, that works, but now my question is, how do I place multiple textures on a primitive, or should I just use 6 panels and texture each one individually with one of the sections?


----
Herb M. (mdfmKoRn)
s3202@attbi.com
S1CA
S1CA
Use 6 panels ("quads" is the more usual term).
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.