Sign in to follow this  
White Scorpion

Texture not shown

Recommended Posts

White Scorpion    148
Hi everyone ! I have tried a DirectX tutorial on NeXe. I'm on the Texturing tutorial. The code compiles fine, but the image isn't shown. I get a red rectangle instead of my image. I've tried multiple different images but it didn't work neither. Anyone have an idea ? Here's the code:
IDirect3D8* Direct3D ;
IDirect3DDevice8* Device ;
IDirect3DTexture8* Texture ;
HRESULT Error ;

// Vertex FVF
const DWORD D3DFVF_VERTEX = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 ;
Vertex QuadFace[4] ;

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ) ;

BOOL Direct3DInit( )
{
	// Create D3D
	Direct3D = Direct3DCreate8( D3D_SDK_VERSION ) ;
	if( Direct3D == NULL )
		return FALSE ;

	// structure to hold infos on the display mode
	D3DDISPLAYMODE displayMode ;
	Error = Direct3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &displayMode ) ;
	if( FAILED( Error ) )
		return FALSE ;

	// Used to explain to Direct3D how it will present things on the screen
	D3DPRESENT_PARAMETERS presentParameters ;
	ZeroMemory( &presentParameters, sizeof( D3DPRESENT_PARAMETERS ) ) ;

	presentParameters.Windowed = TRUE ; // Not fullscreen
	presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD ;
	presentParameters.BackBufferFormat = displayMode.Format ;
	presentParameters.BackBufferHeight = displayMode.Height ;
	presentParameters.BackBufferWidth = displayMode.Width ;

	// Create the device
	Error = Direct3D->CreateDevice( D3DADAPTER_DEFAULT,		// Default display adapter
									D3DDEVTYPE_HAL,			// Hardware acceleration
									WindowInfos::Window,
									D3DCREATE_SOFTWARE_VERTEXPROCESSING,
									&presentParameters,
									&Device ) ;
	if( FAILED( Error ) )
		return FALSE ;

	D3DXMATRIX Matrix ;
	D3DXMatrixPerspectiveFovLH( &Matrix, 
								D3DX_PI/6, // 60°
								WindowInfos::Width / WindowInfos::Height,
								1.0,
								100.0 ) ;
	Device->SetTransform( D3DTS_PROJECTION, &( D3DMATRIX )Matrix ) ;
	D3DXMatrixIdentity( &Matrix ) ;
	Device->SetTransform( D3DTS_WORLD, &( D3DMATRIX )Matrix ) ;
	D3DXMatrixTranslation( &Matrix, 0.0, 0.0, 20.0 ) ;
	Device->SetTransform( D3DTS_VIEW, &( D3DMATRIX )Matrix ) ;

	QuadFace[0].Create( 0, 0, 0, 1, 0, 0, 0, 1 ) ;
	QuadFace[1].Create( 0, 1, 0, 0, 1, 0, 1, 1 ) ;
	QuadFace[3].Create( 2, 1, 0, 0, 0, 1, 1, 0 ) ;
	QuadFace[2].Create( 2, 0, 0, 1, 1, 0, 0, 0 ) ;

	// Blender between the vertex color and the texture color
	Device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ) ;
	Device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ) ;
	Device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ) ;

	// Turn off the alpha channel
	Device->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE ) ;

	// We don't need lighting
	Device->SetRenderState( D3DRS_LIGHTING, FALSE ) ;
	D3DXCreateTextureFromFile( Device, "C:\\Documents and Settings\\Alex\\Mes documents\\signature.bmp", &Texture ) ;
	return TRUE ;
}

void DrawScene( )
{
	// Clear the screen
	Device->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 ) ;

	// Make it possible to draw stuff
	Device->BeginScene( ) ;

		Device->SetVertexShader( D3DFVF_VERTEX ) ;
		Device->SetTexture( 0, Texture ) ; // 0th texture
		Device->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, QuadFace, sizeof( Vertex ) ) ;

	// Clean the stuff needed by BeginScene( )
	Device->EndScene( ) ;

	// Put the back buffer onto the screen
	Device->Present( NULL, NULL, NULL, NULL ) ;
}

Share this post


Link to post
Share on other sites
Supernat02    604
Are you loading the image? Looking at your file path, you have "Mes documents" where you might have meant "Mesh documents". Also, how did you define QuadFace? I see you initializing 8 values for each face...I don't see where diffuse fits in. I'll understand a little better maybe after seeing the defintion, but by QuadFace, do you mean QuadVertex?

Chris

Share this post


Link to post
Share on other sites
White Scorpion    148
lol "Mes Documents" stands for "My Documents" I'm a francophone, that's why.

Here's the Vertex class:
#include <cstdio>

#ifndef __VERTEX_H__
#define __VERTEX_H__

class Vertex
{
public:
void Create( float _x, float _y, float _z, float r, float g, float b )
{
x = _x ;
y = _y ;
z = _z ;
Color = D3DCOLOR_XRGB( ( BYTE ) r * 255, ( BYTE ) g * 255, ( BYTE ) b * 255 ) ;
}
void Create( float _x, float _y, float _z, float r, float g, float b, float u, float v )
{
x = _x ;
y = _y ;
z = _z ;
Color = D3DCOLOR_XRGB( ( BYTE ) r * 255, ( BYTE ) g * 255, ( BYTE ) b * 255 ) ;
U = u ;
V = v ;
}
private:
float x, y, z ;
float U, V ;
DWORD Color ;
} ;

#endif

As for QuadFace ... well it was meant to be a prism at the start so it would have been the face but then I changed my idea and it became only a rectangle, it kept its name... I'll change it.

Share this post


Link to post
Share on other sites
Supernat02    604
Haha, I'm sorry. I was just shooting for anything there. It looks like your structure data is not in the correct order. The fixed pipeline relies on the data to be in a specific order.

Position
RHW
Blending weight data
Vertex normal
Vertex point size
Diffuse color
Specular color
Texture coordinate set 1
...
Texture coordinate set 8

Always make your structure in this order, using only the data you need, when using the fixed pipeline, unless you change the declaration with code.

Note also that when you cast r, g, and b into BYTEs, you will lose the float value of them. i.e. if you sent 0.5, 0.5, 0.5 for gray, you'd end up with black. Always use parenthesis when in doubt. (BYTE)(r*255).

Chris

Share this post


Link to post
Share on other sites
Supernat02    604
Yep, what happens is that you specify a bit-packed DWORD (the FVF) with exactly what you're sending to the video card. So, by default, a specific order in the structure for your vertex is setup already, as defined in the SDK. The shader, as an analogy not literally, cases each bit in the FVF in that pre-defined order and transfers the data in one at a time if its bit is set. That's the easy way to think about it.

You can change the declaration to a different order, by using SetVertexDeclaration in DX9. You might find that useful if you need to pass in 9 floats in a row for instance, because you've written your own vertex shader.

Chris

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this