Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

Zeblar Nagrim

Normal problem

This topic is 6226 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

Hello! My d3d8 normals for my 3ds object are so fu*k*ng wrong! Here is some code:


// Name: CreateVertexBuffer()

// Desc: Create vertexbuffer from vertex and texcoord arrarys


bool CD3DMesh::CreateVertexBuffer(LPDIRECT3DDEVICE8 device, int NumVertices, float *pfVertices, float *pfTexcoords)
    m_iNumFaces = NumVertices/3;

    if(FAILED(device->CreateVertexBuffer( NumVertices*sizeof(CUSTOMVERTEX),
        0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_lpVertexBuffer ))) return false;

    CUSTOMVERTEX *pVertices;
    if(FAILED(m_lpVertexBuffer->Lock( 0, 0, (BYTE**)&pVertices, 0 ))) return false;

    float *pfNormals = new float[NumVertices*3];
    GenerateFaceNormals(NumVertices, pfVertices, pfNormals);

    for(int i=0; i<NumVertices; i++)
        pVertices[i].p.x = pfVertices[i*3];
        pVertices[i].p.y = pfVertices[i*3+1];
        pVertices[i].p.z = pfVertices[i*3+2];

        pVertices[i].n.x = pfNormals[i*3];
        pVertices[i].n.y = pfNormals[i*3+1];
        pVertices[i].n.z = pfNormals[i*3+2];

            pVertices[i].tu = pfTexcoords[i*2];
            pVertices[i].tv = pfTexcoords[i*2+1];
            pVertices[i].tu = 0.0f;
            pVertices[i].tv = 0.0f;


    delete[] pfNormals;

    return true;


// Name: GenerateNormals()

// Desc: 


void CD3DMesh::GenerateFaceNormals(int NumVertex, float *pdVertexData, float *pfNormalData)
    D3DXVECTOR3 vertex1, vertex2, vertex3, normal, vector1, vector2;

    for(int i=0; i<NumVertex/3; i++)
        vertex1 = D3DXVECTOR3(pdVertexData[i*9+0], pdVertexData[i*9+1] , pdVertexData[i*9+2]);
        vertex2 = D3DXVECTOR3(pdVertexData[i*9+3], pdVertexData[i*9+4] , pdVertexData[i*9+5]);
        vertex3 = D3DXVECTOR3(pdVertexData[i*9+6], pdVertexData[i*9+7] , pdVertexData[i*9+8]);

        vector1 = vertex2 - vertex1;
        vector2 = vertex3 - vertex1;

        D3DXVec3Cross(&normal, &vector1, &vector2);
        D3DXVec3Normalize(&normal, &normal);
        normal = -normal;

        pfNormalData[i*9+0] = normal.x;
        pfNormalData[i*9+1] = normal.x;
        pfNormalData[i*9+2] = normal.x;

        pfNormalData[i*9+3] = normal.y;
        pfNormalData[i*9+4] = normal.y;
        pfNormalData[i*9+5] = normal.y;

        pfNormalData[i*9+6] = normal.z;
        pfNormalData[i*9+7] = normal.z;
        pfNormalData[i*9+8] = normal.z;

Please! If somebody can see what I have done wrong this time, please respond. Zeblar Nagrim, Lord of Chaos

Share this post

Link to post
Share on other sites
I think you're making a mistake in your renderloop. In the code you use i as the base index for your vertex/normals (as in i+0 gives x, i+1 gives y and i+2 gives z). Then for the next vertex you have to add 3, but in stead i is incremented by 1! Change it to this:

int index = 0;

for(int i = 0; i < NumVertices; i++)
pVertices.p.x = pfVertices[index*3];
pVertices[i].p.y = pfVertices[index*3+1];
pVertices[i].p.z = pfVertices[index*3+2];
pVertices[i].n.x = pfNormals[index*3];
pVertices[i].n.y = pfNormals[index*3+1];
pVertices[i].n.z = pfNormals[index*3+2];


index += 3;

This should work.

Edited by - sysmark on July 28, 2001 9:00:58 PM

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!