Archived

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

CountOfMonteChristo

Light mapping / texture blending

Recommended Posts

In my ongoing struggle to learn DirectX, I came across texture blending, or more specifically, light maps. Now, this book I'm learning from, tells me to go about this thusly:
  
        g_pD3DDevice->SetTexture(0, g_pD3DTexture);	
	g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);


        g_pD3DDevice->SetTexture(1, g_pD3DTexture2);
	g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
	g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
   
g_pD3DTexture is the base texture, and Texture2 is the light map texture (currently, a white filled circle on a black background). What this supposedly does, if I'm correct, is set the base texture to stage 0, and the light map to stage 1. It then sets argument 1 of stage 0 to the texture set for stage 0 (the base texture), and the operator to SELECTARG1, which would just give the base texture as a result, without any operation. Then, with stage 1, argument 1 gets set to the light map texture, and argument 2 to the result of stage 1, i.e. the base texture again. The operator is set to modulate, so the result should be Light Map X Base Map = blended texture, which should be my base texture being bright where the circle is. However, the results are different: Instead of having nice multitextured primitives, colored by the red and blue point lights in my scene, all primitives are completely black, as if I had enabled lighting but not given any vertex normals. When I remove the SetTextureStageState and SetTexture calls for the light map/stage 1, I get my base texture painted on the primitive, but all lights are either disabled or have no effect, as if I had set the Render State to disable lighting. So the texture is just visible normally, not colored red/blue/purple by my point lights. I have no idea why this is happening, and I can't figure out what I'm doing wrong. I'm using a GeForce3 card, so it can't be that multitexturing is disabled... Not for just two stages at least. Anyone have an idea? [edited by - Bas Paap on July 29, 2002 11:35:01 AM]

Share this post


Link to post
Share on other sites
Have you got alpha blending turned on?

It''s always safest to SetTextureStage( x, D3DTSS_ALPHAOP, D3DTOP_DISABLE ) for each stage you use.

Share this post


Link to post
Share on other sites
Hmm, I hadn''t, but I added

g_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );


to the code, and it didn''t make any difference. I have absolutely no idea why it doesn''t work. Here''s my code:



  
#include <d3d8.h>
#include <d3dx8.h>
#include <mmsystem.h>

//-----------------------------------------------------------------------------

// Globale variabelen

//-----------------------------------------------------------------------------

LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER8 g_pD3DVertexBuffer = NULL;
LPDIRECT3DINDEXBUFFER8 g_pD3DIndexBuffer = NULL;
LPDIRECT3DTEXTURE8 g_pD3DTexture = NULL;
LPDIRECT3DTEXTURE8 g_pD3DTexture2 = NULL;

// Structure voor custom Vertex Format:


struct CUSTOMVERTEX
{
FLOAT x,y,z; // Ongetransformeerde vertex coördinaten

FLOAT nx,ny,nz; // Normaal

FLOAT tu, tv;
};

// Custom FVF:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)


//-----------------------------------------------------------------------------

// InitD3D()

// Initialiseert Direct3D

//-----------------------------------------------------------------------------

HRESULT InitD3D( HWND hWnd )
{

// het D3D object maken

if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Huidige display format pakken:

D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
return E_FAIL;


// Present parameters invullen:

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

// Het D3D Device maken:

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pD3DDevice ) ) )
{
return E_FAIL;
}



// Device state instellen:


//(allemaal redundant: Default values.)

//g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );

g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
//g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);



if (FAILED(D3DXCreateTextureFromFile( g_pD3DDevice, "textuur2.bmp", &g_pD3DTexture)))
{
return E_FAIL;
}

if (FAILED(D3DXCreateTextureFromFile( g_pD3DDevice, "lightmap.bmp", &g_pD3DTexture2)))
{
return E_FAIL;
}

return S_OK;
}
// Einde InitD3D()



//-----------------------------------------------------------------------------

// InitVertexBuffer()

// Initialiseert de vertex buffer

//-----------------------------------------------------------------------------

HRESULT InitVertexBuffer()
{

//Eerst de vertexbuffer maken:


// Array vullen met vertex info

CUSTOMVERTEX vertices[] =
{
{ 1, 1, -1, 0,0,-1, 1,0 }, //Voorkant kubus

{ 1, -1, -1, 0,0,-1, 1,1},
{ -1, -1, -1, 0,0,-1, 0,1},
{ -1, 1, -1, 0,0,-1, 0,0 },

{ -1, 1, 1, 0,0,1, 1,0}, //Achterkant kubus

{ -1, -1, 1, 0,0,1, 1,1},
{ 1, -1, 1, 0,0,1, 0,1},
{ 1, 1, 1, 0,0,1, 0,0},

{ -1, 1, -1, -1,0,0, 1,0}, //Linkerkant kubus

{ -1, -1, -1, -1,0,0, 1,1},
{ -1, -1, 1, -1,0,0, 0,1},
{ -1, 1, 1, -1,0,0, 0,0},

{ 1, 1, 1, 1,0,0, 1,0}, //Rechterkant kubus

{ 1, -1, 1, 1,0,0, 1,1},
{ 1, -1, -1, 1,0,0, 0,1},
{ 1, 1, -1, 1,0,0, 0,0},

{ 1, 1, 1, 0,1,0, 1,0}, //Bovenkant kubus

{ 1, 1, -1, 0,1,0, 1,1},
{ -1, 1, -1, 0,1,0, 0,1},
{ -1, 1, 1, 0,1,0, 0,0},

{ 1, -1, -1, 0,-1,0, 1,0}, //Onderkant kubus

{ 1, -1, 1, 0,-1,0, 1,1},
{ -1, -1, 1, 0,-1,0, 0,1},
{ -1, -1, -1, 0,-1,0, 0,0},
};

// Vertexbuffer creeren: Grootte die buffer nodig heeft, gebruik, FVF,

// geheugen en pointer naar de buffer zelf.

if (FAILED(g_pD3DDevice->CreateVertexBuffer(24 * sizeof(CUSTOMVERTEX),
D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&g_pD3DVertexBuffer)))
{

return E_FAIL;
}

// Stuk geheugen waarnaar geschreven zal worden maken.

// Daarna de vertexbuffer locken: Offset en aantal

// (beiden 0 om de hele buffer te kiezen), pointer

// naar stuk geheugen, flags over geheugengebruik.

// Vervolgens vertexdata naar stuk geheugen kopieren,

// en de buffer weer unlocken.


VOID* pVertices;
if (FAILED(g_pD3DVertexBuffer->Lock(0,0, (BYTE**)&pVertices, 0)))
return E_FAIL;
memcpy(pVertices, vertices, sizeof(vertices));
g_pD3DVertexBuffer->Unlock();

// Nu de indexbuffer:

// Array vullen met index info


WORD indices[] = {
0,1,2, 0,2,3,
4,5,6, 4,6,7,
8,9,10, 8,10,11,
12,13,14, 12,14,15,

16,17,18, 16,18,19,
20,21,22, 20,22,23,

};

if (FAILED(g_pD3DDevice->CreateIndexBuffer(sizeof(WORD) * 36, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pD3DIndexBuffer)))
return E_FAIL;

WORD *pIndex;

g_pD3DIndexBuffer->Lock(0, 0, (BYTE **)&pIndex, 0);
memcpy(pIndex, indices, sizeof(indices));
g_pD3DIndexBuffer->Unlock();

g_pD3DDevice->SetIndices(g_pD3DIndexBuffer, 0);

return S_OK;
}
// Einde InitVertexBuffer()





//-----------------------------------------------------------------------------

// Cleanup()

// Alle objecten releasen

//-----------------------------------------------------------------------------

VOID Cleanup()
{

if( g_pD3DTexture2 != NULL )
g_pD3DTexture2->Release();

if( g_pD3DTexture != NULL )
g_pD3DTexture->Release();

if( g_pD3DIndexBuffer != NULL )
g_pD3DIndexBuffer->Release();

if( g_pD3DVertexBuffer != NULL )
g_pD3DVertexBuffer->Release();

if( g_pD3DDevice != NULL )
g_pD3DDevice->Release();

if( g_pD3D != NULL )
g_pD3D->Release();
}
// Einde Cleanup()




//-----------------------------------------------------------------------------

// Render()

// Rendert de scene

//-----------------------------------------------------------------------------

VOID Render()
{
// Clear de backbuffer en vul hem met blauw.

g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,127,0), 1.0f, 0 );

// Materiaal instellen

D3DMATERIAL8 mat;
ZeroMemory(&mat, sizeof(D3DMATERIAL8));
mat.Diffuse.r = 5.0f;
mat.Diffuse.g = 5.0f;
mat.Diffuse.b = 5.0f;
mat.Diffuse.a = 1.0f;
mat.Specular.r = 0.0f;
mat.Specular.g = 0.0f;
mat.Specular.b = 0.0f;
mat.Specular.a = 0.0f;
mat.Power = 1.0f;

g_pD3DDevice->SetMaterial(&mat);


// Rode puntlichtbron instellen

D3DLIGHT8 light;
ZeroMemory(&light, sizeof(D3DLIGHT8));
light.Type = D3DLIGHT_POINT;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 0.0f;
light.Diffuse.b = 0.0f;
light.Diffuse.a = 0.0f;
light.Specular.r = 1.0f;
light.Specular.g = 1.0f;
light.Specular.b = 1.0f;
light.Specular.a = 1.0f;
light.Ambient.r = 1.0f;
light.Ambient.g = 1.0f;
light.Ambient.b = 1.0f;
light.Ambient.a = 1.0f;
light.Position.x = -1.0f;
light.Position.y = 1.0f;
light.Position.z = -2.0f;
light.Range = 1000.0f;
light.Attenuation1 = 1.0f;

g_pD3DDevice->SetLight(0, &light);


// Blauw licht instellen

light.Diffuse.r = 0.0f;
light.Diffuse.b = 1.0f;
light.Position.x = 2.0f;
light.Position.y = -1.0f;
light.Position.z = -2.0f;

g_pD3DDevice->SetLight(1, &light);


// Allebei aanzetten

g_pD3DDevice->LightEnable(1, TRUE);
g_pD3DDevice->LightEnable(0, TRUE);


// Matrices:

D3DXMATRIX matrix;
D3DXMATRIX rotationMatrix;

// De rotationmatrix laat dingen in het rond tollen

D3DXMatrixIdentity(&rotationMatrix);
D3DXMatrixRotationX(&matrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&rotationMatrix, &rotationMatrix, &matrix);
D3DXMatrixRotationY(&matrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&rotationMatrix, &rotationMatrix, &matrix);
D3DXMatrixRotationZ(&matrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&rotationMatrix, &rotationMatrix, &matrix);

// De rotation is voorlopig de world matrix

g_pD3DDevice->SetTransform(D3DTS_WORLD, &rotationMatrix);


// View matrix instellen (7 units naar achteren, maar in het midden)

D3DXMATRIX viewMatrix;
D3DXMatrixLookAtLH(&viewMatrix, &D3DXVECTOR3(0.0f, 0.0f, -7.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_pD3DDevice->SetTransform(D3DTS_VIEW, &viewMatrix);


// Projection matrix:

D3DXMATRIX projMatrix;
D3DXMatrixPerspectiveFovLH(&projMatrix, D3DX_PI / 4, 4.0f / 3.0f, 1.0f, 100.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &projMatrix);

g_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

g_pD3DDevice->SetTexture(0, g_pD3DTexture);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

g_pD3DDevice->SetTexture(1, g_pD3DTexture2);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);


// Geef aan dat we willen beginnen met renderen:

g_pD3DDevice->BeginScene();

// Streamsource aangeven (de buffer is de source)

g_pD3DDevice->SetStreamSource(0, g_pD3DVertexBuffer, sizeof(CUSTOMVERTEX));

// Vertex shader instellen (zit in FVF)

g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);

// Primitive tekenen. Laatste twee argumenten is de

// index van de vertex waarmee je wil beginnen,

// de laatste is het aantal primitives.

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);

// Scale matrix: helft van de grootte

D3DXMATRIX scaleMatrix;
D3DXMatrixScaling(&scaleMatrix, 0.5f, 0.5f, 0.5f);

// Translation matrix: 3 units naar links

D3DXMatrixTranslation(&matrix, 3.0f, 0.0f, 0.0f);

// Matrices multiplyen:

D3DXMatrixMultiply(&matrix, &rotationMatrix, &matrix);
D3DXMatrixMultiply(&matrix, &scaleMatrix, &matrix);

// Rotation veranderen, alleen om Y as rotaten

D3DXMatrixRotationY(&rotationMatrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&matrix, &matrix, &rotationMatrix );

// Zeggen wat de world matrix is

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matrix);


// En de tweede kubus tekenen

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);


// Nieuwe kubus wordt 10 % en op plek van 1e licht:

D3DXMatrixTranslation(&matrix, -1.0f, 1.0f, -2.0f);
D3DXMatrixScaling(&scaleMatrix, 0.1f, 0.1f, 0.1f);
D3DXMatrixMultiply(&matrix, &scaleMatrix, &matrix);

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matrix);

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);


// Laatste kubus, ook 10% en op plek van 2e licht:

D3DXMatrixTranslation(&matrix, 2.0f, -1.0f, -2.0f);
D3DXMatrixScaling(&scaleMatrix, 0.1f, 0.1f, 0.1f);
D3DXMatrixMultiply(&matrix, &scaleMatrix, &matrix);

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matrix);

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);

// Geef aan dat we willen stoppen met renderen:

g_pD3DDevice->EndScene();

// Presenteer tenslotte de backbuffer:

g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}
// Einde Render()



//-----------------------------------------------------------------------------

// MsgProc()

// De Message Handler

//-----------------------------------------------------------------------------

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case 76: //L

DWORD aanuit;
g_pD3DDevice->GetRenderState(D3DRS_LIGHTING, &aanuit);
g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, !aanuit );
break;
case 49: //1

g_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_POINT);
break;
case 50: //2

g_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
break;
case 51: //3

g_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_ANISOTROPIC);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_ANISOTROPIC);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_ANISOTROPIC);
break;
}
break;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}
// Einde MsgProc()



//-----------------------------------------------------------------------------

// WinMain()

// Het beginpunt van de applicatie

//-----------------------------------------------------------------------------

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// De Windows Class registreren...

WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"Standaard Applicatie", NULL };
RegisterClassEx( &wc );

// Venster maken:

HWND hWnd = CreateWindow( "Standaard Applicatie", "Standaard Applicatie",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
GetDesktopWindow(), NULL, wc.hInstance, NULL );

// Direct3D initialiseren:

if( SUCCEEDED( InitD3D( hWnd ) ) ) // Initialisatie is gelukt!

{
if (SUCCEEDED(InitVertexBuffer()))
{
// Astemblaft, hier is het venster:

ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

// Begin de message loop:

MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT ) // Zolang we niet willen stoppen...

{
// ...kijken of er een bericht is. Zo ja...

if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
// ...dan handelen we het af...

TranslateMessage( &msg );
DispatchMessage( &msg );
}
// ...zo nee, dan gaan we renderen.

else
Render();
}
}
}

// We stoppen ermee, dus netjes alles opruimen en unregisteren.

Cleanup();
UnregisterClass( "Standaard Applicatie", wc.hInstance );
return 0;
}
// Einde WinMain()



You''ll have to ignore the dutch comments, but otherwise it should be clear. I''m making a bit of a mess with the matrices too, I think, but that shouldn''t be a problem.... If anyone could tell me what''s going wrong, I''d be most grateful. Thanks in advance!

Share this post


Link to post
Share on other sites
Are you turning lighting OFF and setting texture coords for both textures correctly?

_____________________________________________
Come join us on IRC in #directxdev @ irc.afternet.org

Share this post


Link to post
Share on other sites
Hi !

You are setting Texture Stage 0 to only using Argument 1.
So you miss the lightning calculations which are the D3DTA_CURRENT argument for Stage 0.

Remove the SetTextureStageState for Stage 0, so your code should look like this:

g_pD3DDevice->SetTexture(0, g_pD3DTexture);
g_pD3DDevice->SetTexture(1, g_pD3DTexture2);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);

Then it should work.

MfG Jack

Share this post


Link to post
Share on other sites
Hmm, some improvement, but not the desired result yet. Removing the SetTextureStageState calls indeed got me my lighting back. I figured it must've been something like that. However, When adding the SetTextureStageStates for stage 1, my image turns black once more. However! I figured out why exactly it turns black: Not because lighting is disabled, but because it only uses the color of the second texture's top left pixel for the entire primitive.

I tried it out in Paint Shop Pro, making all the pixels in the corners different colors (the rest of the picture is just greyscales), and after changing my texture code to this:


    

g_pD3DDevice->SetTexture(0, g_pD3DTexture);
g_pD3DDevice->SetTexture(1, g_pD3DTexture2);

g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);



All my rotating cubes came out red. I experimented with this some more, and it's always the top left pixel of the texture in stage 1.

So, that got us somewhere. It's not a lighting issue any more (thanks for that, Sniper!), it has something to do with it not blending the second texture right.

It's modulating perfectly by the way: When I set the top left pixel to black, I get a black cube (none of the original texture shines through), and when I set it to white, all of the original texture is visible. Setting it to a shade of grey indeed makes the texture darker or lighter.

This leaves me with one thing: Why does it only use the top left pixel of the second stage? How can I make it so that it uses the entire image?

Tergiver mentioned something about setting texture coords for both textures: I've only set one pair of texture coordinates, figuring the textures in all stages would use those coordinates. Is this not the case, or is something else going on?

Well, I'll look into this some more.... Thank you all for helping me out so far!

[edited by - Bas Paap on July 30, 2002 6:14:54 AM]

Share this post


Link to post
Share on other sites
HeHe ...

Exactly thats your problem. You don't have Texture coordinates for Stage 1 (second Stage).
You need another pair of Texture coordinates in your FVF Format, and D3DFVF_TEX2 in your FVF definition.

Look up "Vertex Formats" in your DirectX Help, there it is described.

Here how it should be :
// Structure voor custom Vertex Format:
struct CUSTOMVERTEX{
FLOAT x,y,z; // Ongetransformeerde vertex coördinaten
FLOAT nx,ny,nz; // Normaal
FLOAT tu1, tv1; // Coords Stage 0
FLOAT tu2, tv2; // Coords Stage 1 etc.
};// Custom FVF:
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1, D3DFVF_TEX2)

MfG Jack

[edited by - Jack Sniper on July 30, 2002 6:35:42 AM]

Share this post


Link to post
Share on other sites
Hi again..

Yep, I looked into that, and that looks like it's exactly my problem. So I changed it...


...and now I'm completely stuck. The program renders one frame my translated, rotated, lit cubes, and then does nothing. What's even weirder: When I change the filenames of the textures, it still uses the original texture. When I delete the texture file it uses, it -still- uses that image for rendering. I resetted and created a new project in a different directory and copied the source code: still no dice. It looks like that texture is stuck in my video memory permanently....

So, my code -should- work now, but some unexplainable bug prevents me from seeing the result.

The debug output mentions some errors by the way:


--
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\d3d8.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\d3d8thk.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\version.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\winmm.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ctwdm32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\uxtheme.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\MSCTF.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\d3d8d.dll', no matching symbolic information found.
Direct3D8: :====> ENTER: DLLMAIN(00b63650): Process Attach: 00000304, tid=00000320
Direct3D8: (WARN) :Executing processor detection code (benign first-chance exception possible)

Direct3D8: (INFO) :MMX detected

Direct3D8: (INFO) entium Pro CPU features (fcomi, cmov) detected

Direct3D8: (INFO) :dwCPUFamily = 15, dwCPUFeatures = 3

Direct3D8: (INFO) :szCPUString = GenuineIntel

Direct3D8: :====> EXIT: DLLMAIN(00b63650): Process Attach: 00000304
Direct3D8: (INFO) :Direct3D8 Debug Runtime selected.
Direct3D8: (INFO) :======================= Hal SWVP device selected

Direct3D8: :BackBufferCount not specified, considered default 1
Direct3D8: :DoneExclusiveMode
Direct3D8: (INFO) :HalDevice Driver Style 7

Direct3D8: (INFO) :Failed to create driver indexbuffer
Direct3D8: (INFO) :Using WLMT PSGP

Loaded 'C:\WINDOWS\system32\CTAGENT.DLL', no matching symbolic information found.
Loaded 'C:\Program Files\Logitech\MouseWare\system\LGMOUSHK.DLL', no matching symbolic information found.
Direct3D8: (WARN) :Ignoring redundant SetTextureStageState. Stage: 1, State: 2

Direct3D8: (WARN) :Ignoring redundant SetTextureStageState. Stage: 1, State: 3

Direct3D8: (ERROR) :*** Exception in d:\builds\nt32_chk\multimedia\directx\dxg\d3d8\fe\vshader.cpp Line: 664

Direct3D8: (ERROR) :Stream 0 stride should match the stride, implied by the current vertex shader

Direct3D8: (ERROR) :DrawIndexedPrimitive failed.


---

and so on and so on, repeating those last three lines for a while, occasionally throwing this in:

--
Direct3D8: (WARN) :Ignoring redundant SetTextureStageState. Stage: 1, State: 2

Direct3D8: (WARN) :Ignoring redundant SetTextureStageState. Stage: 1, State: 3

Direct3D8: (WARN) :Ignoring redundant SetTextureStageState. Stage: 1, State: 1

First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
Direct3D8: (ERROR) :*** Exception in d:\builds\nt32_chk\multimedia\directx\dxg\d3d8\fe\vshader.cpp Line: 664
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.

Direct3D8: (ERROR) :Stream 0 stride should match the stride, implied by the current vertex shader

Direct3D8: (ERROR) :DrawIndexedPrimitive failed.
--

and spurting out this:

---
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.

First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
Direct3D8: (ERROR) :*** Exception in d:\builds\nt32_chk\multimedia\directx\dxg\d3d8\fe\vshader.cpp Line: 664

First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
Direct3D8: (ERROR) :Stream 0 stride should match the stride, implied by the current vertex shader
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in DX Test zoveel.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
---

So, that looks pretty bad. When I remove everything referring to the new set of coordinates, everything runs fine again. Except for the blending, of course.

Here's all the bits that have changed:

struct CUSTOMVERTEX
{
FLOAT x,y,z; // Ongetransformeerde vertex coördinaten
FLOAT nx,ny,nz; // Normaal
FLOAT tu, tv;
FLOAT tu2, tv2; //<----CHANGED
};

// Custom FVF (ADDED TEX2):
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEX2)

and this, where I added two more coordinates at the end of each array element:

CUSTOMVERTEX vertices[] =
{
{ 1, 1, -1, 0,0,-1, 1,0, 1,0 }, //Voorkant kubus
{ 1, -1, -1, 0,0,-1, 1,1, 1,1},
{ -1, -1, -1, 0,0,-1, 0,1, 0,1},
{ -1, 1, -1, 0,0,-1, 0,0, 0,0},

{ -1, 1, 1, 0,0,1, 1,0, 1,0}, //Achterkant kubus
{ -1, -1, 1, 0,0,1, 1,1, 1,1},
{ 1, -1, 1, 0,0,1, 0,1, 0,1},
{ 1, 1, 1, 0,0,1, 0,0, 0,0},

{ -1, 1, -1, -1,0,0, 1,0, 1,0}, //Linkerkant kubus
{ -1, -1, -1, -1,0,0, 1,1, 1,1},
{ -1, -1, 1, -1,0,0, 0,1, 0,1},
{ -1, 1, 1, -1,0,0, 0,0, 0,0},

{ 1, 1, 1, 1,0,0, 1,0, 1,0}, //Rechterkant kubus
{ 1, -1, 1, 1,0,0, 1,1, 1,1},
{ 1, -1, -1, 1,0,0, 0,1, 0,1},
{ 1, 1, -1, 1,0,0, 0,0, 0,0},

{ 1, 1, 1, 0,1,0, 1,0, 1,0}, //Bovenkant kubus
{ 1, 1, -1, 0,1,0, 1,1, 1,1},
{ -1, 1, -1, 0,1,0, 0,1, 0,1},
{ -1, 1, 1, 0,1,0, 0,0, 0,0},

{ 1, -1, -1, 0,-1,0, 1,0, 1,0}, //Onderkant kubus
{ 1, -1, 1, 0,-1,0, 1,1, 1,1},
{ -1, -1, 1, 0,-1,0, 0,1, 0,1},
{ -1, -1, -1, 0,-1,0, 0,0, 0,0},
};


Here's my complete code, just to be complete:


    
#include <d3d8.h>
#include <d3dx8.h>
#include <mmsystem.h>

//-----------------------------------------------------------------------------

// Globale variabelen

//-----------------------------------------------------------------------------

LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER8 g_pD3DVertexBuffer = NULL;
LPDIRECT3DINDEXBUFFER8 g_pD3DIndexBuffer = NULL;
LPDIRECT3DTEXTURE8 g_pD3DTexture = NULL;
LPDIRECT3DTEXTURE8 g_pD3DTexture2 = NULL;

// Structure voor custom Vertex Format:


struct CUSTOMVERTEX
{
FLOAT x,y,z; // Ongetransformeerde vertex coördinaten

FLOAT nx,ny,nz; // Normaal

FLOAT tu, tv;
FLOAT tu2, tv2;
};

// Custom FVF:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEX2)


//-----------------------------------------------------------------------------

// InitD3D()

// Initialiseert Direct3D

//-----------------------------------------------------------------------------

HRESULT InitD3D( HWND hWnd )
{

// het D3D object maken

if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Huidige display format pakken:

D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
return E_FAIL;


// Present parameters invullen:

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

// Het D3D Device maken:

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pD3DDevice ) ) )
{
return E_FAIL;
}



// Device state instellen:


//(allemaal redundant: Default values.)

//g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );

//g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

//g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);



if (FAILED(D3DXCreateTextureFromFile( g_pD3DDevice, "textuur2.bmp", &g_pD3DTexture)))
{
return E_FAIL;
}

if (FAILED(D3DXCreateTextureFromFile( g_pD3DDevice, "lightmap.bmp", &g_pD3DTexture2)))
{
return E_FAIL;
}

return S_OK;
}
// Einde InitD3D()



//-----------------------------------------------------------------------------

// InitVertexBuffer()

// Initialiseert de vertex buffer

//-----------------------------------------------------------------------------

HRESULT InitVertexBuffer()
{

//Eerst de vertexbuffer maken:


// Array vullen met vertex info

CUSTOMVERTEX vertices[] =
{
{ 1, 1, -1, 0,0,-1, 1,0, 1,0 }, //Voorkant kubus

{ 1, -1, -1, 0,0,-1, 1,1, 1,1},
{ -1, -1, -1, 0,0,-1, 0,1, 0,1},
{ -1, 1, -1, 0,0,-1, 0,0, 0,0},

{ -1, 1, 1, 0,0,1, 1,0, 1,0}, //Achterkant kubus

{ -1, -1, 1, 0,0,1, 1,1, 1,1},
{ 1, -1, 1, 0,0,1, 0,1, 0,1},
{ 1, 1, 1, 0,0,1, 0,0, 0,0},

{ -1, 1, -1, -1,0,0, 1,0, 1,0}, //Linkerkant kubus

{ -1, -1, -1, -1,0,0, 1,1, 1,1},
{ -1, -1, 1, -1,0,0, 0,1, 0,1},
{ -1, 1, 1, -1,0,0, 0,0, 0,0},

{ 1, 1, 1, 1,0,0, 1,0, 1,0}, //Rechterkant kubus

{ 1, -1, 1, 1,0,0, 1,1, 1,1},
{ 1, -1, -1, 1,0,0, 0,1, 0,1},
{ 1, 1, -1, 1,0,0, 0,0, 0,0},

{ 1, 1, 1, 0,1,0, 1,0, 1,0}, //Bovenkant kubus

{ 1, 1, -1, 0,1,0, 1,1, 1,1},
{ -1, 1, -1, 0,1,0, 0,1, 0,1},
{ -1, 1, 1, 0,1,0, 0,0, 0,0},

{ 1, -1, -1, 0,-1,0, 1,0, 1,0}, //Onderkant kubus

{ 1, -1, 1, 0,-1,0, 1,1, 1,1},
{ -1, -1, 1, 0,-1,0, 0,1, 0,1},
{ -1, -1, -1, 0,-1,0, 0,0, 0,0},
};

// Vertexbuffer creeren: Grootte die buffer nodig heeft, gebruik, FVF,

// geheugen en pointer naar de buffer zelf.

if (FAILED(g_pD3DDevice->CreateVertexBuffer(24 * sizeof(CUSTOMVERTEX),
D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&g_pD3DVertexBuffer)))
{

return E_FAIL;
}

// Stuk geheugen waarnaar geschreven zal worden maken.

// Daarna de vertexbuffer locken: Offset en aantal

// (beiden 0 om de hele buffer te kiezen), pointer

// naar stuk geheugen, flags over geheugengebruik.

// Vervolgens vertexdata naar stuk geheugen kopieren,

// en de buffer weer unlocken.


VOID* pVertices;
if (FAILED(g_pD3DVertexBuffer->Lock(0,0, (BYTE**)&pVertices, 0)))
return E_FAIL;
memcpy(pVertices, vertices, sizeof(vertices));
g_pD3DVertexBuffer->Unlock();

// Nu de indexbuffer:

// Array vullen met index info


WORD indices[] = {
0,1,2, 0,2,3,
4,5,6, 4,6,7,
8,9,10, 8,10,11,
12,13,14, 12,14,15,

16,17,18, 16,18,19,
20,21,22, 20,22,23,

};

if (FAILED(g_pD3DDevice->CreateIndexBuffer(sizeof(WORD) * 36, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pD3DIndexBuffer)))
return E_FAIL;

WORD *pIndex;

g_pD3DIndexBuffer->Lock(0, 0, (BYTE **)&pIndex, 0);
memcpy(pIndex, indices, sizeof(indices));
g_pD3DIndexBuffer->Unlock();

g_pD3DDevice->SetIndices(g_pD3DIndexBuffer, 0);

return S_OK;
}
// Einde InitVertexBuffer()





//-----------------------------------------------------------------------------

// Cleanup()

// Alle objecten releasen

//-----------------------------------------------------------------------------

VOID Cleanup()
{

if( g_pD3DTexture2 != NULL )
g_pD3DTexture2->Release();

if( g_pD3DTexture != NULL )
g_pD3DTexture->Release();

if( g_pD3DIndexBuffer != NULL )
g_pD3DIndexBuffer->Release();

if( g_pD3DVertexBuffer != NULL )
g_pD3DVertexBuffer->Release();

if( g_pD3DDevice != NULL )
g_pD3DDevice->Release();

if( g_pD3D != NULL )
g_pD3D->Release();
}
// Einde Cleanup()




//-----------------------------------------------------------------------------

// Render()

// Rendert de scene

//-----------------------------------------------------------------------------

VOID Render()
{
// Clear de backbuffer en vul hem met blauw.

g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,127,0), 1.0f, 0 );

// Materiaal instellen

D3DMATERIAL8 mat;
ZeroMemory(&mat, sizeof(D3DMATERIAL8));
mat.Diffuse.r = 5.0f;
mat.Diffuse.g = 5.0f;
mat.Diffuse.b = 5.0f;
mat.Diffuse.a = 1.0f;
mat.Specular.r = 0.0f;
mat.Specular.g = 0.0f;
mat.Specular.b = 0.0f;
mat.Specular.a = 0.0f;
mat.Power = 1.0f;

g_pD3DDevice->SetMaterial(&mat);


// Rode puntlichtbron instellen

D3DLIGHT8 light;
ZeroMemory(&light, sizeof(D3DLIGHT8));
light.Type = D3DLIGHT_POINT;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 0.0f;
light.Diffuse.b = 0.0f;
light.Diffuse.a = 0.0f;
light.Specular.r = 1.0f;
light.Specular.g = 1.0f;
light.Specular.b = 1.0f;
light.Specular.a = 1.0f;
light.Ambient.r = 1.0f;
light.Ambient.g = 1.0f;
light.Ambient.b = 1.0f;
light.Ambient.a = 1.0f;
light.Position.x = -1.0f;
light.Position.y = 1.0f;
light.Position.z = -2.0f;
light.Range = 1000.0f;
light.Attenuation1 = 1.0f;

g_pD3DDevice->SetLight(0, &light);


// Blauw licht instellen

light.Diffuse.r = 0.0f;
light.Diffuse.b = 1.0f;
light.Position.x = 2.0f;
light.Position.y = -1.0f;
light.Position.z = -2.0f;

g_pD3DDevice->SetLight(1, &light);


// Allebei aanzetten

g_pD3DDevice->LightEnable(1, TRUE);
g_pD3DDevice->LightEnable(0, TRUE);


// Matrices:

D3DXMATRIX matrix;
D3DXMATRIX rotationMatrix;

// De rotationmatrix laat dingen in het rond tollen

D3DXMatrixIdentity(&rotationMatrix);
D3DXMatrixRotationX(&matrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&rotationMatrix, &rotationMatrix, &matrix);
D3DXMatrixRotationY(&matrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&rotationMatrix, &rotationMatrix, &matrix);
D3DXMatrixRotationZ(&matrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&rotationMatrix, &rotationMatrix, &matrix);

// De rotation is voorlopig de world matrix

g_pD3DDevice->SetTransform(D3DTS_WORLD, &rotationMatrix);


// View matrix instellen (7 units naar achteren, maar in het midden)

D3DXMATRIX viewMatrix;
D3DXMatrixLookAtLH(&viewMatrix, &D3DXVECTOR3(0.0f, 0.0f, -7.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_pD3DDevice->SetTransform(D3DTS_VIEW, &viewMatrix);


// Projection matrix:

D3DXMATRIX projMatrix;
D3DXMatrixPerspectiveFovLH(&projMatrix, D3DX_PI / 4, 4.0f / 3.0f, 1.0f, 100.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &projMatrix);



//g_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

//g_pD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );



g_pD3DDevice->SetTexture(0, g_pD3DTexture);
g_pD3DDevice->SetTexture(1, g_pD3DTexture2);

g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
g_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);


// Geef aan dat we willen beginnen met renderen:

g_pD3DDevice->BeginScene();

// Streamsource aangeven (de buffer is de source)

g_pD3DDevice->SetStreamSource(0, g_pD3DVertexBuffer, sizeof(CUSTOMVERTEX));

// Vertex shader instellen (zit in FVF)

g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);

// Primitive tekenen. Laatste twee argumenten is de

// index van de vertex waarmee je wil beginnen,

// de laatste is het aantal primitives.

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);

// Scale matrix: helft van de grootte

D3DXMATRIX scaleMatrix;
D3DXMatrixScaling(&scaleMatrix, 0.5f, 0.5f, 0.5f);

// Translation matrix: 3 units naar links

D3DXMatrixTranslation(&matrix, 3.0f, 0.0f, 0.0f);

// Matrices multiplyen:

D3DXMatrixMultiply(&matrix, &rotationMatrix, &matrix);
D3DXMatrixMultiply(&matrix, &scaleMatrix, &matrix);

// Rotation veranderen, alleen om Y as rotaten

D3DXMatrixRotationY(&rotationMatrix, timeGetTime() / 1000.0f);
D3DXMatrixMultiply(&matrix, &matrix, &rotationMatrix );

// Zeggen wat de world matrix is

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matrix);


// En de tweede kubus tekenen

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);


// Nieuwe kubus wordt 10 % en op plek van 1e licht:

D3DXMatrixTranslation(&matrix, -1.0f, 1.0f, -2.0f);
D3DXMatrixScaling(&scaleMatrix, 0.1f, 0.1f, 0.1f);
D3DXMatrixMultiply(&matrix, &scaleMatrix, &matrix);

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matrix);

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);


// Laatste kubus, ook 10% en op plek van 2e licht:

D3DXMatrixTranslation(&matrix, 2.0f, -1.0f, -2.0f);
D3DXMatrixScaling(&scaleMatrix, 0.1f, 0.1f, 0.1f);
D3DXMatrixMultiply(&matrix, &scaleMatrix, &matrix);

g_pD3DDevice->SetTransform(D3DTS_WORLD, &matrix);

g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 24, 0, 12);

// Geef aan dat we willen stoppen met renderen:

g_pD3DDevice->EndScene();

// Presenteer tenslotte de backbuffer:

g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}
// Einde Render()



//-----------------------------------------------------------------------------

// MsgProc()

// De Message Handler

//-----------------------------------------------------------------------------

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case 76: //L

DWORD aanuit;
g_pD3DDevice->GetRenderState(D3DRS_LIGHTING, &aanuit);
g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, !aanuit );
break;
case 49: //1

g_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_POINT);
break;
case 50: //2

g_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
break;
case 51: //3

g_pD3DDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_ANISOTROPIC);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_ANISOTROPIC);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_ANISOTROPIC);
break;
}
break;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}
// Einde MsgProc()



//-----------------------------------------------------------------------------

// WinMain()

// Het beginpunt van de applicatie

//-----------------------------------------------------------------------------

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// De Windows Class registreren...

WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"Standaard Applicatie", NULL };
RegisterClassEx( &wc );

// Venster maken:

HWND hWnd = CreateWindow( "Standaard Applicatie", "Standaard Applicatie",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
GetDesktopWindow(), NULL, wc.hInstance, NULL );

// Direct3D initialiseren:

if( SUCCEEDED( InitD3D( hWnd ) ) ) // Initialisatie is gelukt!

{
if (SUCCEEDED(InitVertexBuffer()))
{
// Astemblaft, hier is het venster:

ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

// Begin de message loop:

MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT ) // Zolang we niet willen stoppen...

{
// ...kijken of er een bericht is. Zo ja...

if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
// ...dan handelen we het af...

TranslateMessage( &msg );
DispatchMessage( &msg );
}
// ...zo nee, dan gaan we renderen.

else
Render();
}
}
}

// We stoppen ermee, dus netjes alles opruimen en unregisteren.

Cleanup();
UnregisterClass( "Standaard Applicatie", wc.hInstance );
return 0;
}
// Einde WinMain()




I must be violating some C++ rule in a horrible way... Can anyone tell me how and what? Again, thanks in advance!


EDIT: Changed #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEX2)

to

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1, D3DFVF_TEX2)

but same results.





[edited by - Bas Paap on July 30, 2002 6:49:42 AM]

Share this post


Link to post
Share on other sites
Alright! It finally works! Thanks millions for all your help, Jack. I looked up Vertex Formats in the SDK help, but I figured you had to specify each stage one by one, so TEX1 | TEX2 | TEX3 etcetera. I see now that that''s not the case, but the SDK Help isn''t quite clear on that.

Anyway, it works now, and it looks amazing. Thanks again to everybody who replied, I would''ve gone insane by now if it wasn''t for you people. :-)

Share this post


Link to post
Share on other sites
Sure, why not?

When I say it looks great, I mean that it works, of course. The light mapping has no real function, it's just there to see how it works.

The thing I've been working on is a window showing two cubes: One smaller cube rotates around a larger one, which itself is rotating around it's x, y, and z axis like an insane amusement park ride. I've just been adding things to this as I went along: rotation, lights and materials, textures, and now texture blending.

Anyway, here's a bunch of screenshots. The first two are without texture blending: One with lighting turned off, and one with two point lights. The other two are the same, only with texture blending. The two small cubes, by the way, just indicate where the point lights are in the scene. (they illuminate each other, so the blue one is actually the red light, and vice versa).

http://baspaap.hypermart.net/noblendedtextures_nolight.jpg
http://baspaap.hypermart.net/noblendedtextures_light.jpg
http://baspaap.hypermart.net/blendedtextures_nolight.jpg
http://baspaap.hypermart.net/blendedtextures_light.jpg
(no link tags?)

It may look a bit weird in this particular scene, but it works, and that's what counts. :-)


[edited by - Bas Paap on July 30, 2002 9:19:14 AM]

Share this post


Link to post
Share on other sites