Solved

Direct3D Texture Problem, 500 points up for grabs!!

Posted on 2003-12-04
6
509 Views
Last Modified: 2013-12-26
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
Comment
Question by:oldsch00l
  • 2
6 Comments
 

Accepted Solution

by:
DeathCheese earned 170 total points
ID: 9883189
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
 
LVL 1

Assisted Solution

by:th83
th83 earned 165 total points
ID: 9891890
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
 
LVL 1

Expert Comment

by:th83
ID: 9891904
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
 
LVL 1

Assisted Solution

by:Maroy
Maroy earned 165 total points
ID: 10092097
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question