Archived

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

Zeblar Nagrim

Normals

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];

        if(pfTexcoords)
        {
            pVertices[i].tu = pfTexcoords[i*2];
            pVertices[i].tv = pfTexcoords[i*2+1];
        }
        else
        {
            pVertices[i].tu = 0.0f;
            pVertices[i].tv = 0.0f;
        }
    }

    m_lpVertexBuffer->Unlock();

    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.

Share this post


Link to post
Share on other sites
damn I hate when people just dump all their code, add like 2 sentances explanation, and basically say "this is my problem can you fix it"

Anyway, to get the normals of a set of vertices, go through each triangle. Normalize the cross product of the two vectors formed from the vertex in question to the two other points in the triangle. This gives you the surface normal

to get a vertex normal, you need the sum of all the surface normals attached to the vertex.

Share this post


Link to post
Share on other sites