Jump to content
  • Advertisement
Sign in to follow this  
Rey87

Flip Normals of a D3DXSphere

This topic is 2896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I I made a D3DX SPhere and want to map my texture inside the sphere not outside cause this sphere shall be my environment (Background)
Actually i've no clue how to do that i appriciate evry help!
this is the Code

.
.
.
void initScene()
{
pMesh = CreateMappedSphere(d3ddev, 10, 20, 20);
pMesh2 = CreateMappedSphere(d3ddev, 80, 20, 20);
pMesh3 = CreateMappedSphere(d3ddev, 5, 20, 20);
pMesh4 = CreateMappedSphere(d3ddev, 5, 20, 20);
}

void drawSphere()
{

static float t = 0.0f;
static DWORD dwTimeStart = 0;
DWORD dwTimeCur = GetTickCount();
if( dwTimeStart == 0 )
dwTimeStart = dwTimeCur;
t = ( dwTimeCur - dwTimeStart ) / 1000.0f;

D3DXMATRIX kugel;
D3DXMatrixIdentity(&kugel);
d3ddev->SetMaterial(&MATERIAL_WHITE);

D3DXCreateTextureFromFile(d3ddev,"erde.png",&gTexture);
d3ddev->SetTexture(0,gTexture);
//d3ddev->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
pMesh->DrawSubset(0);

D3DXVECTOR3 vP(0.0f,1.0f,0.0f);
D3DXMatrixRotationAxis(&kugel,&vP,t);
d3ddev->SetTransform(D3DTS_WORLD,&kugel);

gTexture = NULL;

D3DXMATRIX kugel2;
D3DXMatrixIdentity(&kugel2);
d3ddev->SetMaterial(&MATERIAL_WHITE);

D3DXCreateTextureFromFile(d3ddev,"Weltall.jpg",&gTexture); //How to flip the Normals of that spehre So the Texture is Inside ?
d3ddev->SetTexture(0,gTexture);
pMesh2->DrawSubset(0);

D3DXMATRIX scale;
D3DXMATRIX rad;
D3DXMATRIX achse;

D3DXMatrixScaling(&scale,0.5,0.5,0.5);
D3DXMatrixTranslation(&rad,20,0,0);
D3DXVECTOR3 vP2(0.0f,1.0f,0.0f);
D3DXMatrixRotationAxis(&achse,&vP2,t);

D3DXMatrixInverse(&achse,NULL,&kugel2);
D3DXMatrixMultiply(&kugel2,&scale,&achse);
D3DXMatrixMultiply(&kugel2,&kugel2,&rad);
d3ddev->SetTransform(D3DTS_WORLD,&kugel2);

gTexture = NULL;

D3DXMATRIX matrix;
D3DXMatrixIdentity(&matrix);

D3DXMatrixMultiply(&matrix,&kugel2,&kugel);
d3ddev->SetTransform(D3DTS_WORLD,&matrix);


}
.
.
.


LPD3DXMESH CreateMappedSphere(LPDIRECT3DDEVICE9 pDev,float fRad,UINT slices,UINT stacks)
{
// create the sphere
LPD3DXMESH mesh;
VERTEX *pVerts;
if (FAILED(D3DXCreateSphere(pDev,fRad,slices,stacks,&mesh,NULL)))
return NULL;

// create a copy of the mesh with texture coordinates,
// since the D3DX function doesn't include them
LPD3DXMESH texMesh;
if (FAILED(mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM,FVF_VERTEX,pDev,&texMesh)))
// failed, return un-textured mesh
return mesh;

// finished with the original mesh, release it
mesh->Release();

// lock the vertex buffer
if (SUCCEEDED(texMesh->LockVertexBuffer(0,(LPVOID *) &pVerts))) {

// get vertex count
int numVerts=texMesh->GetNumVertices();

// loop through the vertices
for (int i=0;i<numVerts;i++) {

// calculate texture coordinates
pVerts->tu=asinf(pVerts->norm.x)/D3DX_PI+0.5f;
pVerts->tv=asinf(pVerts->norm.y)/D3DX_PI+0.5f;

// go to next vertex
pVerts++;
}

// unlock the vertex buffer
texMesh->UnlockVertexBuffer();
}

// return pointer to caller
return texMesh;
}


[Edited by - Rey87 on July 14, 2010 6:28:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
To flip a normal you can simply negate it. This is basic vector math.

eg.

D3DXVECTOR3 normal(1.0f, 0.0f, 0.0f); // normal along the xaxis
normal = -normal; // = (-1.0f, 0.0f, 0.0f)

// another example:
D3DXVECTOR3 normal(4.0f, -2.0f, 3.24f);
normal = -normal; // = (-4.0f, 2.0f, -3.24f);

D3DXVec3Normalize(&normal, &normal); // dont forget to normalize the vector if need be (which is almost always)

Share this post


Link to post
Share on other sites
What exactly are you trying to do? The thread title talks about flipping normals, but the code you posted doesn't do anything with normals, and the opening sentence of your post implies you're trying to change the texture coordinates rather than the normals?

Assuming the texture coordinates are wrong, how are they wrong? If the texture is mirrored, then just set the U coordinate to 1.0f - U to mirror it back.

EDIT: Also, what is the layout of your VERTEX struct, and how is FVF_VERTEX declared? And are you sure you want to create the mesh in system memory? Why not managed (Which is what I expect you really want)?

Share this post


Link to post
Share on other sites
OK all that not working for cause i'm a noob.
Anyway i tried to add a polygon and set that behind my Objects but i got the problem I can not see the Polygon!

void initScene()
{
D3DXCreatePolygon(d3ddev,300,300,&pMesh4,0);
D3DXCreateTextureFromFile(d3ddev,"Weltall.jpg",&gTextureSterne);
}
void DrawPoly(){
D3DXMATRIX matrix9;
D3DXMATRIX scale9;


D3DXMatrixTranslation(&matrix9, 0.0f, 0.0f, 10.0f);
D3DXMatrixScaling(&scale9,100.0f,100.0f,1.0f);
d3ddev->SetTexture(0,gTextureSterne);
D3DXMatrixMultiply(&matrix9,&scale9,&matrix9);
d3ddev->SetTransform(D3DTS_WORLD, &matrix9);
pMesh4->DrawSubset(0);
}


Any Idea's?

Share this post


Link to post
Share on other sites
You've got several problems. First: read the docs for D3DXCreatePolygon.

Second: start simple, not complex.

You've created a polygon that has 300 sides, each side 300 units long. The perimeter of your polygon is 90000 units long. The diameter is on the order of 29000 units. That's a big polygon! You then scale it by 100!, making it 2900000 units in diameter. With an average perspective fov, your eyepoint would have to be somewhere around 1500000 units away to see the whole thing. I doubt seriously if your projection far plane is that far away. [wink] Also, make sure your eyepoint and direction are appropriate for viewing the mesh. One thing you'll find (if I remember correctly) is that CreatePolygon meshes face the +Z axis. Unless your eyepoint has a z value > 0, and a direction looking along the -Z axis, you won't see much.

You're created a polygon with vertices which have a position and a normal (see the docs). It has no texture coordinates so setting a texture is useless.

Start simple: try a polygon with, say, 3 sides (a triangle), each side maybe 5 units on a side.

Get that working. Then look at the function CloneMesh ( or CloneMeshFVF ) to add texture coordinates. There are several files in the SDK that demonstrate cloning meshes. [EDIT - sorry. it appears you know about that]

EDIT: All DX functions give some indication of success or failure. In the code you posted, you don't check for success of any function. You may have other problems you don't know about. Take a look at the SUCCEEDED and FAILED macros to test your DX functions calls.

In addition, Debug Runtime is your friend. It will provide a lot of information in the IDE Output window while your app runs.

EDIT2: with regard to your original question about viewing the inside of a sphere, remember that, from the inside, the faces will have a CCW winding order unless you change the indices. You can also change culling mode to CW to view the sphere from the inside.

[Edited by - Buckeye on July 14, 2010 8:04:49 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!