Ok, here is the main loop:
sf::Window window(sf::VideoMode(1024, 576), "First Person Shooter");
Game game(&window);
game.init();
while (game.isRunning())
{
game.tick();
window.display();
}
window.close();
the game.init:
/* ... */
/* loading materials from exernal file */
for (unsigned int i = 0; i < materialSize; ++i) {
Material m;
/* reading data like ambient, diffuse, specular,... */
/* ... */
/* reading texture name */
unsigned char length;
file->read( reinterpret_cast<char*>(&length), sizeof(unsigned char) );
if (length != 0) {
char texname[30];
file->read(texname, length);
texname[length] = 0;
m.image = load_text_image(texname);
} else {
m.image = NULL;
}
addMaterial(m);
}
return true;
/* ... */
the load_text_image() is this:
sf::Texture* Gr_Object::load_text_image(string file) {
if (file == "") {
return NULL;
}
sf::Texture* texture = new sf::Texture();
if (!texture->loadFromFile(file)) {
cout << "file was not found or could not be read" << endl;
delete texture;
texture = NULL;
} else {
texture->setRepeated(true);
}
return texture;
}
The drawing (in game.tick()):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
for (unsigned int i = 0; i < objectList.size(); ++i) {
Gr_Object faces= objectList[i].getFaces();
for (int j = 0; j < faces.size(); ++j) {
faces[i].draw();
}
}
return 1;
And then the drawing of a face:
void Face3::draw() {
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3; ++i) {
Vertex v = vertices[i];
if (v.material)
v.material->set_material();
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();
}
And then the last piece is the set_material method:
void Material::set_material() {
GLfloat amb[] = {amb_r, amb_g,amb_b,1.0};
GLfloat dif[] = {dif_r, dif_g,dif_b,1.0};
GLfloat spc[] = {spc_r, spc_g,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,ns);
if (image)
image->bind(sf::Texture::Normalized);
}
I hope this helps?