• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

Direct3D Texture Problem, 500 points up for grabs!!

Hi,
I am v. new to Direct 3D and have come up against a bit of a problem. I am simply trying to create a spinning cube with a different texture on each face. I managed to do this but have run into a couple of probmlems when trying to add lighting.

Initially, I had my cube set up as

struct CubeVertex
{
    float x, y, z;      //3d position in vertex  
    DWORD colour;      //colour of vertex      
    float tu, tv;      //texture coordinates

};

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

CubeVertex g_cubeVertices[] =
{
      {-1.0f, 1.0f,-1.0f,  0.0f,0.0f, 0xFFFFFFFF},
      { 1.0f, 1.0f,-1.0f,  1.0f,0.0f, 0xFFFFFFFF},
      {-1.0f,-1.0f,-1.0f,  0.0f,1.0f, 0xFFFFFFFF },
      { 1.0f,-1.0f,-1.0f,  1.0f,1.0f, 0xFFFFFFFF },  etc.....etc....etc....

and rendered a different texture to each face using:

                g_pd3dDevice->SetTexture( 0, g_pTexture5 );
      g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );
      g_pd3dDevice->SetTexture( 0, g_pTexture2 );
      g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );
      g_pd3dDevice->SetTexture( 0, g_pTexture3 );
      g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );
      g_pd3dDevice->SetTexture( 0, g_pTexture4 );
      g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
      g_pd3dDevice->SetTexture( 0, g_pTexture1 );
      g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
      g_pd3dDevice->SetTexture( 0, g_pTexture6 );
      g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );

However, when I tried to add lighting all I got was total darkness. I was informed that I needed to add normals to my customVertices and did so :

struct CubeVertex
{
    float x, y, z;      //3d position in vertex  
    float tu, tv;      //texture coordinates
   
    DWORD colour;      //colour of vertex      
      float nx,ny,nz; //normals 3d position in vertex
};

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

CubeVertex g_cubeVertices[] =
{
      {-1.0f, 1.0f,-1.0f,  0.0f,0.0f, 0xFFFFFFFF, 1.0f,0.0f,0.0f, },
      { 1.0f, 1.0f,-1.0f,  1.0f,0.0f, 0xFFFFFFFF, 1.0f,0.0f,0.0f, },
      {-1.0f,-1.0f,-1.0f,  0.0f,1.0f, 0xFFFFFFFF, 1.0f,0.0f,0.0f, },
      { 1.0f,-1.0f,-1.0f,  1.0f,1.0f, 0xFFFFFFFF, 1.0f,0.0f,0.0f, },
      
      {-1.0f, 1.0f, 1.0f,  1.0f,0.0f, 0xFFFFFFFF, 1.0f,0.0f,0.0f, },
      {-1.0f,-1.0f, 1.0f,  1.0f,1.0f, 0xFFFFFFFF, 1.0f,0.0f,0.0f, },
      { 1.0f, 1.0f, 1.0f,  0.0f,0.0f, 0xFFFFFFFF, 1.0f,0.0f,0.0f, },
      { 1.0f,-1.0f, 1.0f,  0.0f,1.0f, 0xFFFFFFFF, 1.  etc etc etc............

However, now not only does the lighting still not work, but my previously ok textures just come out as a flat colour with no detail even with the lighting switched off. It's as if, rather than displaying the texture it is giving an average colour of all the pixels there in.

I am also using this code for the texture stage state....

g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_CURRENT);
g_pd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE);

Someone suggested also emplolying Mag/Min filters but I'm not sure how..... any help much appreciated, I've been struggling with this for days!

Thanks in anticipation,


Rod
0
oldsch00l
Asked:
oldsch00l
  • 2
3 Solutions
 
DeathCheeseCommented:
Looks like the data in your vertex structure is out of order.  Search for "Vertex Formats" in the SDK docs for a complete explanation, but basically the order of your structures must be:  position, normal, diffuse, tex coords.  

struct CubeVertex
{
    float x, y, z;       //3d position in vertex  
    float nx,ny,nz;   //normals 3d position in vertex
    DWORD colour; //colour of vertex    
    float tu, tv;       //texture coordinates
};

Hope that helps!
0
 
th83Commented:
If it is total darkness in a room you forget to switch the lights on.

it is same in DX check this if you have done this while Intializing.

               // enable lighting
               g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE);

      // Set up the level of ambient light in the scene
      g_pd3dDevice->SetRenderState(D3DRS_AMBIENT,color);



TH
0
 
th83Commented:
If it is total darkness in a room you forget to switch the lights on.

it is same in DX check this,if you have lighting enable while Intializing.

D3DCOLOR Ambient_color = D3DCOLOR_XRGB(255,255,255);
               
// enable lighting
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE);

// Set up the level of ambient light in the scene
g_pd3dDevice->SetRenderState(D3DRS_AMBIENT,Ambient_color);



TH
0
 
MaroyCommented:
A Normal is used to calculate how much light is to be reflected from the surface. Assuming your example is a true representation of the entire data structure then I wound suggest that only ONE of the sides of the cube has a correct normal.  5 out of the 6 cube faces will reflect no light (or very little) . Why? because the normal shoud form a  90 degree angle with surface/polygon in question plus the direction must be pointing outward.  Your code would produce 4 surfaces where the normal is parallel to the surface and one where the vector point to the inside of the cube.

There are math formulas to calculate a normal vectors from a plane but the fastest thing to do in your case would be to figure out which surface is the top surface and then set the normal vector  for each of the four point to

(0,1,0)

the bottom surface would be (0,-1,0)
the left face would be (-1, 0,0)
the right face would be (1,0,0)
etc...


As you code stands now it seems to me that if you did not do any rotation, translation, etc, and if the light were set to the right of the cube (down the positive x axis pointing toward the origin) and your camera position is also looking at the right face of the cube that face should be properly light.

PS, DeathCheese is also correct about the ording of items in the data structure.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now