c++ OpenGL FBX model not loaded properly - c++

I'm trying to load FBX models using OpenGL, I got the importer to work and read the mesh data so I can draw it
but some parts are not displayed correctly.
Here's what I use
FbxMesh*mesh = pNode->GetMesh();
//================= Get Vertices ====================================
int numVerts = mesh->GetControlPointsCount();
for(int j = 0; j < numVerts; j++)
{
FbxVector4 vert = mesh->GetControlPointAt(j);
vertices[numVertices].x=(float)vert.mData[0];
vertices[numVertices].y=(float)vert.mData[1];
vertices[numVertices++].z=(float)vert.mData[2];
printf("MeshVert: x: %f y: %f z: %f\n", vertices[numVertices-1].x, vertices[numVertices-1].y, vertices[numVertices-1].z);
}
//================= Get Indices ====================================
numIndices = mesh->GetPolygonVertexCount();
int triangleCount = numIndices / 3;
indices = new int[numIndices];
indices = mesh->GetPolygonVertices();
printf("numIndices: %i\n", numIndices);
printf("TriangleCount: %i\n", triangleCount);
//================= Get Normals ====================================
FbxGeometryElementNormal*normalEl = mesh->GetElementNormal();
if(normalEl)
{
int numNormals = mesh->GetPolygonCount()*3;
normals = new float[numNormals*3];
int vertexCounter = 0;
for(int polyCounter = 0 ; polyCounter<mesh->GetPolygonCount(); polyCounter++)
{
for(int k = 0; k < 3; k++)
{
FbxVector4 normal = normalEl->GetDirectArray().GetAt(vertexCounter);
normals[vertexCounter*3+0] = (float)normal[0];
normals[vertexCounter*3+1] = (float)normal[1];
normals[vertexCounter*3+2] = (float)normal[2];
//cout<<"\n"<<normals[vertexCounter*3+0]<<" "<<normals[vertexCounter*3+1]<<" "<<normals[vertexCounter*3+2];
vertexCounter++;
}
}
}
And primitives to draw
for(int i = 0; i < numIndices - 3; i++)
{
glBegin(GL_TRIANGLES);
glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]);
for(int j = i; j <= i + 2; j++)
{
glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
glColor3f(0.3f, 0.3f, 0.3f);
}
glEnd();
}
Just wondering if anyone can help get this to work.
The rest of my projects code is in https://github.com/buttburger/FBXEssentials/tree/master/OpenGL2

Fixed it up now
for(int i = 0; i < numIndices - 3; i+=3)
{
glBegin(GL_TRIANGLES);
glNormal3f(normals[i*3+0], normals[i*3+1], normals[i*3+2]);
for(int j = i; j <= i + 2; j++)
{
glVertex3f(vertices[indices[j]].x, vertices[indices[j]].y, vertices[indices[j]].z);
glColor3f(1.0f, 1.0f, 1.0f);
}
}
glEnd();
glFlush();
glutSwapBuffers();

Related

OpenGl Terrain mesh into gaussian blob

How do I make this blob. I'm kind of confused on how to make the mesh rise up like that when I click the mouse button
Here is my mesh code. Its not too important to read through it. I just want to know how I get the blob by rising the the mesh:
{
QuadMesh qm; // The new quad mesh to be returned
qm.numVertices = 0;
qm.vertices = NULL;
qm.numQuads = 0;
qm.quads = NULL;
qm.numFacesDrawn = 0;
qm.maxMeshSize = maxMeshSize < minMeshSize ? minMeshSize : maxMeshSize;
CreateMemoryQM(&qm);
// Set up default material used for the mesh
qm.mat_ambient[0] = 0.0;
qm.mat_ambient[1] = 0.0;
qm.mat_ambient[2] = 0.0;
qm.mat_ambient[3] = 1.0;
qm.mat_specular[0] = 0.0;
qm.mat_specular[1] = 0.0;
qm.mat_specular[2] = 0.0;
qm.mat_specular[3] = 1.0;
qm.mat_diffuse[0] = 0.75;
qm.mat_diffuse[1] = 0.5;
qm.mat_diffuse[2] = 0.0;
qm.mat_diffuse[3] = 1.0;
qm.mat_shininess[0] = 0.0;
return qm;
}
void SetMaterialQM(QuadMesh* qm, Vector3D ambient, Vector3D diffuse, Vector3D specular, double shininess)
{
qm->mat_ambient[0] = ambient.x;
qm->mat_ambient[1] = ambient.y;
qm->mat_ambient[2] = ambient.z;
qm->mat_ambient[3] = 1.0;
qm->mat_specular[0] = specular.x;
qm->mat_specular[1] = specular.y;
qm->mat_specular[2] = specular.z;
qm->mat_specular[3] = 1.0;
qm->mat_diffuse[0] = diffuse.x;
qm->mat_diffuse[1] = diffuse.y;
qm->mat_diffuse[2] = diffuse.z;
qm->mat_diffuse[3] = 1.0;
qm->mat_shininess[0] = (float)shininess;
}
// Allocate dynamic arrays.
bool CreateMemoryQM(QuadMesh* qm)
{
const int maxVertices = (qm->maxMeshSize + 1) * (qm->maxMeshSize + 1);
qm->vertices = (MeshVertex *)malloc(sizeof(MeshVertex) * maxVertices);
if (qm->vertices == NULL)
{
return false;
}
const int maxQuads = qm->maxMeshSize * qm->maxMeshSize;
qm->quads = (MeshQuad *)malloc(sizeof(MeshQuad) * maxQuads);
if (qm->quads == NULL)
{
return false;
}
return true;
}
// Fills the array of vertices and the array of quads.
bool InitMeshQM(QuadMesh* qm, int meshSize, Vector3D origin, double meshLength, double meshWidth, Vector3D dir1, Vector3D dir2)
{
Vector3D o;
double sf1, sf2;
Vector3D v1,v2;
v1.x = dir1.x;
v1.y = dir1.y;
v1.z = dir1.z;
sf1 = meshLength/meshSize;
ScalarMul(&v1, (float)sf1, &v1);
v2.x = dir2.x;
v2.y = dir2.y;
v2.z = dir2.z;
sf2 = meshWidth/meshSize;
ScalarMul(&v2, (float)sf2, &v2);
Vector3D meshpt;
// Build Vertices
qm->numVertices=(meshSize+1)*(meshSize+1);
int currentVertex = 0;
// Starts at front left corner of mesh
Set(&o, origin.x,origin.y,origin.z);
for(int i=0; i< meshSize+1; i++)
{
for(int j=0; j< meshSize+1; j++)
{
// compute vertex position along mesh row (along x direction)
meshpt.x = o.x + j * v1.x;
meshpt.y = o.y + j * v1.y;
meshpt.z = o.z + j * v1.z;
Set(&qm->vertices[currentVertex].position, meshpt.x,meshpt.y,meshpt.z);
currentVertex++;
}
// go to next row in mesh (negative z direction)
Add(&o, &v2, &o);
}
// Build Quad Polygons
qm->numQuads=(meshSize)*(meshSize);
int currentQuad=0;
for (int j=0; j < meshSize; j++)
{
for (int k=0; k < meshSize; k++)
{
// Counterclockwise order
qm->quads[currentQuad].vertices[0]=&qm->vertices[j* (meshSize+1)+k];
qm->quads[currentQuad].vertices[1]=&qm->vertices[j* (meshSize+1)+k+1];
qm->quads[currentQuad].vertices[2]=&qm->vertices[(j+1)*(meshSize+1)+k+1];
qm->quads[currentQuad].vertices[3]=&qm->vertices[(j+1)*(meshSize+1)+k];
currentQuad++;
}
}
ComputeNormalsQM(qm);
return true;
}
// Draw the mesh by drawing all quads.
void DrawMeshQM(QuadMesh* qm, int meshSize)
{
int currentQuad=0;
glMaterialfv(GL_FRONT, GL_AMBIENT, qm->mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, qm->mat_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, qm->mat_diffuse);
glMaterialfv(GL_FRONT, GL_SHININESS, qm->mat_shininess);
for(int j=0; j < meshSize; j++)
{
for(int k=0; k < meshSize; k++)
{
glBegin(GL_QUADS);
glNormal3f(qm->quads[currentQuad].vertices[0]->normal.x,
qm->quads[currentQuad].vertices[0]->normal.y,
qm->quads[currentQuad].vertices[0]->normal.z);
glVertex3f(qm->quads[currentQuad].vertices[0]->position.x,
qm->quads[currentQuad].vertices[0]->position.y,
qm->quads[currentQuad].vertices[0]->position.z);
glNormal3f(qm->quads[currentQuad].vertices[1]->normal.x,
qm->quads[currentQuad].vertices[1]->normal.y,
qm->quads[currentQuad].vertices[1]->normal.z);
glVertex3f(qm->quads[currentQuad].vertices[1]->position.x,
qm->quads[currentQuad].vertices[1]->position.y,
qm->quads[currentQuad].vertices[1]->position.z);
glNormal3f(qm->quads[currentQuad].vertices[2]->normal.x,
qm->quads[currentQuad].vertices[2]->normal.y,
qm->quads[currentQuad].vertices[2]->normal.z);
glVertex3f(qm->quads[currentQuad].vertices[2]->position.x,
qm->quads[currentQuad].vertices[2]->position.y,
qm->quads[currentQuad].vertices[2]->position.z);
glNormal3f(qm->quads[currentQuad].vertices[3]->normal.x,
qm->quads[currentQuad].vertices[3]->normal.y,
qm->quads[currentQuad].vertices[3]->normal.z);
glVertex3f(qm->quads[currentQuad].vertices[3]->position.x,
qm->quads[currentQuad].vertices[3]->position.y,
qm->quads[currentQuad].vertices[3]->position.z);
glEnd();
currentQuad++;
}
}
}
// Deallocate dynamic arrays.
void FreeMemoryQM(QuadMesh* qm)
{
if (qm->vertices != NULL)
free(qm->vertices);
qm->vertices=NULL;
qm->numVertices=0;
if (qm->quads != NULL)
free(qm->quads);
qm->quads=NULL;
qm->numQuads=0;
}
// Use cross-products to compute the normal vector at each vertex
void ComputeNormalsQM(QuadMesh* qm)
{
int currentQuad=0;
for(int j=0; j < qm->maxMeshSize; j++)
{
for(int k=0; k < qm->maxMeshSize; k++)
{
Vector3D n0, n1, n2, n3;
Vector3D e0, e1, e2, e3;
for (int i=0; i < 4; i++)
{
LoadZero(&qm->quads[currentQuad].vertices[i]->normal);
}
Subtract(&qm->quads[currentQuad].vertices[1]->position, &qm->quads[currentQuad].vertices[0]->position, &e0);
Subtract(&qm->quads[currentQuad].vertices[2]->position, &qm->quads[currentQuad].vertices[1]->position, &e1);
Subtract(&qm->quads[currentQuad].vertices[3]->position, &qm->quads[currentQuad].vertices[2]->position, &e2);
Subtract(&qm->quads[currentQuad].vertices[0]->position, &qm->quads[currentQuad].vertices[3]->position, &e3);
Normalize(&e0);
Normalize(&e1);
Normalize(&e2);
Normalize(&e3);
Vector3D w; // Working vector;
Negate(&e3, &w);
CrossProduct(&e0, &w, &n0);
Normalize(&n0);
Add(&qm->quads[currentQuad].vertices[0]->normal, &n0, &qm->quads[currentQuad].vertices[0]->normal);
Negate(&e0, &w);
CrossProduct(&e1, &w, &n1);
Normalize(&n1);
Add(&qm->quads[currentQuad].vertices[1]->normal, &n1, &qm->quads[currentQuad].vertices[1]->normal);
Negate(&e1, &w);
CrossProduct(&e2, &w, &n2);
Normalize(&n2);
Add(&qm->quads[currentQuad].vertices[2]->normal, &n2, &qm->quads[currentQuad].vertices[2]->normal);
Negate(&e2, &w);
CrossProduct(&e3, &w, &n3);
Normalize(&n3);
Add(&qm->quads[currentQuad].vertices[3]->normal, &n3, &qm->quads[currentQuad].vertices[3]->normal);
for (int i = 0; i < 4; i++)
{
Normalize(&qm->quads[currentQuad].vertices[i]->normal);
}
currentQuad++;
}
}
}
It will not if you try it because its linked to a main.c file but its basically generating a paper thin mesh with a green shade.

My Neural Network Doesn't Work [XOR problem]

I'm trying to make a neural network for solving XOR problem.But I couldn't make it.Always giving false results.Maybe I'm making a mistake in your math.The network does not learn.Result always similarly.
I am not using BIAS.
Note: execute function = (feed-forward + backpropagation)
ALPHA = 0.5
Here is the code:
//main.cpp
#include <iostream>
#include "neural_network.h"
int main(int argc, char const *argv[])
{
srand(time(NULL));
double array[][3] = {{0.0, 0.0, 0.0},
{0.0, 1.0, 1.0},
{1.0, 0.0, 1.0},
{1.0, 1.0, 0.0}};
neural_network* nn = new neural_network(3, 2, 2, 1, 1.0);
nn->create_network();
for(int i = 0; i < 15000; i++)
{
int index = rand() % 4;
#if DEBUG
std::cout<<"Inputs :"<<array[index][0]<<" , "<<array[index][1]<<std::endl;
std::cout<<"Outputs :"<<array[index][2]<<std::endl;
#endif
nn->execute(array[index], &array[index][2]);
}
nn->print_weight();
nn->execute(array[0], &array[0][2]);
nn->print_output();
nn->execute(array[1], &array[1][2]);
nn->print_output();
nn->execute(array[2], &array[2][2]);
nn->print_output();
nn->execute(array[3], &array[3][2]);
nn->print_output();
return 0;
}
//feed-forward function
void neural_network::feed_forward(double* inputs)
{
int index = 0;
for(int i = 0; i < neural_network::input_layer_size; i++)
neural_network::input_neuron[i] = inputs[i];
for(int i = 0; i < neural_network::hidden_layer_size; i++)
{
for(int j = 0; j < neural_network::input_layer_size; j++)
{
neural_network::hidden_neuron[i] += neural_network::input_neuron[j] * weight_I_H[index++];
}
neural_network::hidden_neuron[i] = neural_network::activation_func(neural_network::hidden_neuron[i]);
}
index = 0;
for(int i = 0; i < neural_network::output_layer_size; i++)
{
for(int j = 0; j < neural_network::hidden_layer_size; j++)
{
neural_network::output_neuron[i] += neural_network::hidden_neuron[j] * weight_H_O[index++];
}
neural_network::output_neuron[i] = neural_network::activation_func(neural_network::output_neuron[i]);
}
}
//backpropagation function
void neural_network::back_propagation(double* outputs)
{
int index;
for(int i = 0; i < neural_network::output_layer_size; i++)
neural_network::err_output[i] = (outputs[i] - neural_network::output_neuron[i]);
for(int i = 0; i < neural_network::hidden_layer_size; i++)
{
index = i;
for(int j = 0; j < neural_network::output_layer_size; j++)
{
neural_network::err_hidden[i] += neural_network::weight_H_O[index] * neural_network::err_output[j] * neural_network::derivative_act_func(neural_network::output_neuron[j]);
neural_network::weight_H_O[index] += ALPHA * neural_network::err_output[j] * neural_network::derivative_act_func(neural_network::output_neuron[j]) * neural_network::hidden_neuron[i];
index += neural_network::hidden_layer_size;
}
}
for(int i = 0; i < neural_network::input_layer_size; i++)
{
index = i;
for(int j = 0; j < neural_network::hidden_layer_size; j++)
{
neural_network::weight_I_H[index] += ALPHA * neural_network::err_hidden[j] * neural_network::derivative_act_func(neural_network::hidden_neuron[j]) * neural_network::input_neuron[i];
index += neural_network::input_layer_size;
}
}
}
//output
Input To Hidden :
H-1 :
Weight :-13.269
Weight :-13.2705
H-2 :
Weight :-12.5172
Weight :-12.5195
Hidden To Output :
O-1 :
Weight :-5.37707
Weight :-2.93218
Outputs for (0,0):
O-1 :0.0294265
Outputs for (0,1):
O-1 :0.507348
Outputs for (1,0):
O-1 :0.62418
Outputs for (1,1):
O-1 :0.651169
It is real impossible no keras no my developed net based on Furye transformation(which is more power than keras) real decide this XOR task.I tested very accuracy both of this ANN.Maximum the recognize is 3 examples of 4 (acc=0.75->75%).No one answered 1 xor 1=0.It seems nowbody realy tested this case seriosly.(ANN were multilayered)

Assimp access violation on existing aiVector3D

I'm using the following code to load in an .obj file using Assimp. Something goes wrong at the TextureCoordinates. The aiVector3D I try to access exists, but as soon as I store the values in a temp variable, it crashes my application.
Here's the code I use:
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(filename,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
if (!scene)
{
printf("[ASSIMP] ");
printf(importer.GetErrorString());
printf("\n");
return nullptr;
}
Mesh* newMesh = new Mesh();
unsigned int vertexCount = scene->mMeshes[0]->mNumVertices;
unsigned int triangleCount = scene->mMeshes[0]->mNumFaces;
bool hasUv = scene->mMeshes[0]->HasTextureCoords(0);
newMesh->vertexCount = vertexCount;
newMesh->triangleCount = triangleCount;
newMesh->m_Vertices = new Vertex[vertexCount];
newMesh->m_Triangles = new Triangle[triangleCount];
for (unsigned int i = 0; i < vertexCount; i++) {
aiVector3D vertexPosition = scene->mMeshes[0]->mVertices[i];
aiVector3D vertexNormal = scene->mMeshes[0]->mNormals[i];
newMesh->m_Vertices[i].pos = glm::vec3(vertexPosition.x, vertexPosition.y, vertexPosition.z);
newMesh->m_Vertices[i].normal = glm::vec3(vertexNormal.x, vertexNormal.y, vertexNormal.z);
if (hasUv) {
aiVector3D uvCoordinates = scene->mMeshes[0]->mTextureCoords[i][0];
printf("uvCoordinates: %f %f %f\n", uvCoordinates.x, uvCoordinates.y, uvCoordinates.z);
newMesh->m_Vertices[i].u = uvCoordinates.x;
newMesh->m_Vertices[i].v = uvCoordinates.y;
}
}
for (unsigned int i = 0; i < triangleCount; i++) {
aiFace face = scene->mMeshes[0]->mFaces[i];
for (int j = 0; j < 3; j++) {
Triangle* tri = &newMesh->m_Triangles[i];
tri->vertex[j] = &newMesh->m_Vertices[face.mIndices[j]];
if (tri->vertex[0]->normal.y == 1.0f || tri->vertex[0]->normal.y == -1.0f) {
tri->color = glm::vec3(0.2f, 0.2f, 0.2f);
}
else
{
tri->color = glm::vec3(1.0f, 0.0f, 0.0f);
}
}
}
It crashes at the line printf("uvCoordinates: %f %f %f\n", uvCoordinates.x, uvCoordinates.y, uvCoordinates.z);, but if I remove this line, it crashes at newMesh->m_Vertices[i].u = uvCoordinates.x;. If I comment out the printf(), set both the u and v of the vertex to 0.0f and not use uvCoordinates at all, it still crashes on the newMesh->m_Vertices[i].u = uvCoordinates.x; line. If I leave the printf() uncommented, it prints the values of the uvCoordinates, but throws an access violation after.
I'm honestly out of ideas here. Here's a screenshot showing what I explained.
scene->mMeshes[0]->mTextureCoords[i][0]; is first texture coordinates for set i. What you wanted is to get first set of texture coordinates - so it should be scene->mMeshes[0]->mTextureCoords[0][i];

glDrawElements does not show anything when I move it to another method

When I draw terrain like this - it works normal
void Terrain::RenderLandscape()
{
int x, y;
int Index = 0;
for (int i = 0; i < MapSize-1; i++)
{
Index = 0;
for (int j = 0;j < MapSize-1; j++)
{
x = j * Zoom;
y = i * Zoom;
TextureMap[Index+0][0]= j * TextureBit;
TextureMap[Index+0][1]= i * TextureBit;
TextureMap[Index+1][0]= j * TextureBit;
TextureMap[Index+1][1]= (i+1) * TextureBit;
VertexMap[Index+0][2] = HeightMap[j][i];
VertexMap[Index+1][2] = HeightMap[j][i+1];
VertexMap[Index+0][0] = x;
VertexMap[Index+0][1] = y;
VertexMap[Index+1][0] = x;
VertexMap[Index+1][1] = y+Zoom;
Index += 2;
}
glDrawElements(GL_TRIANGLE_STRIP, Index, GL_UNSIGNED_INT, Indices);
}
}
But when I move loops to another method and leave in RenderLandscape
void Terrain::RenderLandscape()
{
glDrawElements(GL_TRIANGLE_STRIP, 512, GL_UNSIGNED_INT, Indices);
}
Where 512 - size of Indices array - It shows only black screen without terrain.
What is the problem?
I move loops to method which is called before renderLandscape. There I initialize vertex, normals, texture arrays. Method init.
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, VertexMap);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, TextureMap);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT,0, NormalMap);
for (int Row = 0; Row < MapSize*2; Row++)
{
Indices[Row] = Row;
}
int x, y;
int Index = 0;
for (int i = 0; i < MapSize-1; i++)
{
Index = 0;
for (int j = 0;j < MapSize-1; j++)
{
x = j * Zoom;
y = i * Zoom;
TextureMap[Index+0][0]= j * TextureBit;
TextureMap[Index+0][1]= i * TextureBit;
TextureMap[Index+1][0]= j * TextureBit;
TextureMap[Index+1][1]= (i+1) * TextureBit;
VertexMap[Index+0][2] = HeightMap[j][i];
VertexMap[Index+1][2] = HeightMap[j][i+1];
VertexMap[Index+0][0] = x;
VertexMap[Index+0][1] = y;
VertexMap[Index+1][0] = x;
VertexMap[Index+1][1] = y+Zoom;
Index += 2;
}
}
Index = 0;
for(int i = 0; i < MapSize - 1; i++)
{
Index = 0;
for (int j = 0;j < MapSize-1; j++)
{
CVector3 vTriangle[] = { CVector3(VertexMap[Index + 0]),
CVector3(VertexMap[Index + 1]),
CVector3(VertexMap[Index + 2]) };
CVector3 normal = Normal(vTriangle);
NormalMap[Index + 0][0] = normal.x;
NormalMap[Index + 0][1] = normal.y;
NormalMap[Index + 0][2] = normal.z;
NormalMap[Index + 1][0] = normal.x;
NormalMap[Index + 1][1] = normal.y;
NormalMap[Index + 1][2] = normal.z;
NormalMap[Index + 2][0] = normal.x;
NormalMap[Index + 2][1] = normal.y;
NormalMap[Index + 2][2] = normal.z;
Index += 3;
}
}
So it works like this
1) I initialize opengl and create terrain object.
........
landscape.Init();
glutPassiveMotionFunc(MousePassive);
glutIdleFunc(draw)
........
2) in method draw I call renderLandscape method
glPushMatrix();
glTranslatef(-20, 0, 0);
glScalef(0.01, 0.01, 0.01);
glRotatef(90, 1, 0, 0);
landscape.RenderLandscape();
glPopMatrix();

Opengl glRotatef rotating a polygon, won't rotate in place

I'm trying to rotate a polygon in place, but it keeps revolving instead.
To rotate, I calculate the center by finding the average location of each vertex. I call the rotation functions and then call a translation using the center to move it to the middle of the screen. It does end up centered, but it still rotates as if it's not. Any ideas as to what I could be doing wrong?
Here's my code:
void Polygon::DrawPolygon()
{
glPushMatrix();
glLoadMatrixf(matrix);
glTranslatef(displace[0], displace[1], displace[2]);
glRotatef(rotation[0], 1, 0, 0);
glRotatef(rotation[1], 0, 1, 0);
glRotatef(rotation[2], 0, 0, 1);
glTranslatef(-displace[0], -displace[1], displace[2]);
displace[0] = 0; displace[1] = 0; displace[2] = 0;
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
DrawMaterial();
DrawFaces();
ConnectFaces();
glPopMatrix();
}
Here's how I calculate the center:
void Polygon::FindCenter()
{
float x = 0;
float y = 0;
float z = 0;
for(int j = 0; j < 2; j++)
{
for(int i = 0; i < vertexCount; i++)
{
x += vertices[i][0];
y += vertices[i][1];
z += vertices[i][2] + extrusionDistance * j;
}
}
x = x / (vertexCount * 2);
y = y / (vertexCount * 2);
z = z / (vertexCount * 2);
displace[0] = x;
displace[1] = y;
displace[2] = z;
}
Because of the way my extrusion works I don't need to add the x and y for the vertices of both faces, but I did anyway to keep it consistent.
Here is how I draw the shape:
void Polygon::DrawFaces()
{
for(int j = 0; j < 2; j++)
{
glBegin(GL_POLYGON);
for(int i = 0; i < vertexCount; i++)
{
glVertex3f(vertices[i][0], vertices[i][1], j*extrusionDistance);
}
glEnd();
}
}
void Polygon::ConnectFaces()
{
for(int i = 0; i < vertexCount; i++)
{
glBegin(GL_POLYGON);
glVertex3f(vertices[i][0], vertices[i][1], 0);
glVertex3f(vertices[i][0], vertices[i][1], extrusionDistance);
glVertex3f(vertices[(i+1)%vertexCount][0], vertices[(i+1)%vertexCount][1], extrusionDistance);
glVertex3f(vertices[(i+1)%vertexCount][0], vertices[(i+1)%vertexCount][1], 0);
glEnd();
}
}
I see a few things that stand out to me as being odd:
1) You're calling glLoadMatrixf(matrix) before the call to glTranslate() and glRotate(). Depending on what's in the matrix you're loading, that changes things.
2) You're FindCenter() method calculates the center by including the vertex[i][2] in the calculation of z, but when you actually draw the faces in DrawFaces(), you don't include the vertex[i][2] part, just the extrusion * j part. So you're not drawing the same thing that you're calculating the center of.