libsegl/models/model.cpp

111 lines
2.5 KiB
C++
Raw Normal View History

2008-04-11 16:43:46 +02:00
#include "model.h"
ModelObject::ModelObject() {
}
void ModelObject::clear() {
normal.clear();
vertex.clear();
mapcoord.clear();
polygon.clear();
}
void ModelObject::calcNormales() {
normal.clear();
Punkt3D a, b, c;
2008-04-11 16:43:46 +02:00
for(unsigned int i=0; i<polygon.size(); i++) {
a = vertex.at((unsigned short)polygon[i].x) - vertex.at((unsigned short)polygon[i].z);
b = vertex.at((unsigned short)polygon[i].y) - vertex.at((unsigned short)polygon[i].z);
c = a.kreuzprodukt(b);
c.normalize();
normal.push_back(c);
// c.print("Normale");
2008-04-11 16:43:46 +02:00
}
}
void ModelObject::render() {
// std::cout << "render " << name << " mit " << polygon.size()<< std::endl;
// std::cout << "Vertex: " << vertex.size() << std::endl;
2008-04-11 16:43:46 +02:00
glBegin(GL_TRIANGLES);
for(unsigned int i=0; i<polygon.size(); i++) {
// std::cout << "whee nr " <<i << std::endl;
// polygon[i].print();
// std::cout << "Stat: " << polygon.size() << " " << vertex.size() << " " << mapcoord.size() << " " << normal.size() << std::endl;
// glNormal3f(normal.at( (unsigned short)polygon[i].x));
// glColor3f(mat.diffuse.r, mat.diffuse.g, mat.diffuse.b);
glColorGLC(mat.ambient);
// std::cout << mat.ambient.r << ", " << mat.ambient.g << ", " << mat.ambient.b << std::endl;
glNormal3f(normal.at(i));
glTexCoord2f(mapcoord.at((unsigned short)polygon[i].x));
2008-04-11 16:43:46 +02:00
glVertex3f(vertex.at( (unsigned short)polygon[i].x));
glTexCoord2f(mapcoord.at((unsigned short)polygon[i].y));
2008-04-11 16:43:46 +02:00
glVertex3f(vertex.at( (unsigned short)polygon[i].y));
glTexCoord2f(mapcoord.at((unsigned short)polygon[i].z));
2008-04-11 16:43:46 +02:00
glVertex3f(vertex.at( (unsigned short)polygon[i].z));
// vertex.at( (unsigned short)polygon[i].x).print();
2008-04-11 16:43:46 +02:00
}
glEnd();
}
std::string ModelObject::getName() {
return name;
}
Material::Material() {
clear();
}
void Material::clear() {
name = "";
ambient.set(0.0f, 0.0f, 0.0f);
diffuse.set(0.0f, 0.0f, 0.0f);
specular.set(0.0f, 0.0f, 0.0f);
percent = 0.0f;
}
std::string Material::getName() {
return name;
}
2008-04-11 16:43:46 +02:00
Model::Model() {
}
void Model::clear() {
objects.clear();
}
void Model::addObject(ModelObject c) {
objects.push_back(c);
}
void Model::render() {
// std::cout << "Render Model " << objects.size() << std::endl;
2008-04-11 16:43:46 +02:00
for(unsigned int i=0; i<objects.size(); i++)
objects[i].render();
}
void Model::addMaterial(Material m) {
materials.push_back(m);
}
Material Model::findMaterial(std::string name) {
for(unsigned int i=0; i<materials.size(); i++) {
if(name==materials[i].getName()) {
std::cout<<"found!"<<std::endl;
return materials[i];
}
}
return Material();
}