?
Solved

Direct3D Texture Problem, 500 points up for grabs!!

Posted on 2003-12-04
6
Medium Priority
?
512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 

Accepted Solution

by:
DeathCheese earned 680 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 660 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 660 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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