Don't worry about the heap allocation. In my original code most things are constructed on the stack. It was just a quick and dirty piece of code (and probably my "new" habbit from Java
).
Anyway, I've removed some of the heap allocation and pasted in the code for drawing:
void draw_face(Face3* f) {
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3; ++i) {
Vertex v = f->vertices[i];
if (v.material) {
GLfloat amb[] = {v.material->amb_r, v.material->amb_g,v.material->amb_b,1.0};
GLfloat dif[] = {v.material->dif_r, v.material->dif_g,v.material->dif_b,1.0};
GLfloat spc[] = {v.material->spc_r, v.material->spc_g,v.material->spc_b,1.0};
glMaterialfv(GL_FRONT,GL_AMBIENT, amb);
glMaterialfv(GL_FRONT,GL_DIFFUSE, dif);
glMaterialfv(GL_FRONT,GL_SPECULAR,spc);
glMaterialf(GL_FRONT,GL_SHININESS,v.material->ns);
if (v.material->image)
v.material->image->bind();
}
if (v.normal)
glNormal3f(v.normal->x, v.normal->y, v.normal->z);
if (v.texture)
glTexCoord2f(v.texture->x, v.texture->y);
glVertex3f(v.point->x, v.point->y, v.point->z);
}
glEnd();
}
int main() {
sf::Window window(sf::VideoMode(1024, 576), "First Person Shooter");
sf::Texture* texture = new sf::Texture();
if (!texture->loadFromFile("texCube.png")) {
cout << "file was not found or could not be read" << endl;
delete texture;
texture = NULL;
} else {
texture->setRepeated(true);
}
sf::Texture* texture2 = new sf::Texture();
if (!texture2->loadFromFile("textures\\test.bmp")) {
cout << "file was not found or could not be read" << endl;
delete texture2;
texture2 = NULL;
} else {
texture2->setRepeated(true);
}
Material m1; m1.image = texture;
Material m2; m2.image = texture2;
Face3* face1 = new Face3(Vertex(new sf::Vector3f(-0.2,-0.2,0.0),new sf::Vector3f(0.0,0.0,1.0),&m1,new sf::Vector2f(0.0,0.0)),
Vertex(new sf::Vector3f(0.2,-0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),&m1,new sf::Vector2f(1.0,0.0)),
Vertex(new sf::Vector3f(0.2,0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),&m1,new sf::Vector2f(1.0,1.0)));
Face3* face2 = new Face3(Vertex(new sf::Vector3f(0.2,0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),&m2,new sf::Vector2f(1.0,1.0)),
Vertex(new sf::Vector3f(-0.2,0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),&m2,new sf::Vector2f(0.0,1.0)),
Vertex(new sf::Vector3f(-0.2,-0.2,0.0),new sf::Vector3f(0.0,0.0,1.0),&m2,new sf::Vector2f(0.0,0.0)));
while (true) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_face(face1);
draw_face(face2);
window.display();
}
window.close();
delete texture,texture2;
_CrtDumpMemoryLeaks();
return EXIT_SUCCESS;
}