Solved

Direct3D Texture Problem, 500 points up for grabs!!

Posted on 2003-12-04
6
505 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now