- #include "glen.h"
- Model::Model() { }
- Model::Model(char *filename) {
- FILE *model;
- model = fopen(filename, "r");
- if (model) {
- std::vector<Vector3> vertices;
- std::vector<Vector3> normals;
- std::vector<Vector2> uvs;
- this->vertex_buffer.setNormal(0.0f, 0.0f, 1.0f);
- while (!feof(model)) {
- char type[7];
- fscanf(model, "%s ", type);
- if (!strcmp("#", type)) {
- while (fgetc(model) != '\n');
- } else if (!strcmp("v", type)) {
- Vector3 vertex;
- fscanf(model, "%f %f %f", &vertex.x, &vertex.y, &vertex.z);
- vertices.push_back(vertex);
- continue;
- } else if (!strcmp("vn", type)) {
- Vector3 normal;
- fscanf(model, "%f %f %f", &normal.x, &normal.y, &normal.z);
- normals.push_back(normal);
- continue;
- } else if (!strcmp("vt", type)) {
- Vector2 uv;
- fscanf(model, "%f %f", &uv.x, &uv.y);
- uvs.push_back(uv);
- } else if (!strcmp("s", type)) {
- char shade[4];
- fscanf(model, "%s", &shade);
- } else if (!strcmp("usemtl", type)) {
- char material[50];
- fscanf(model, "%s", &material);
- } else if (!strcmp("mtllib", type)) {
- char material[50];
- fscanf(model, "%s", &material);
- } else if (!strcmp("f", type)) {
- int v[4], vt[4], vn[4];
- if (uvs.size() > 0) {
- fscanf(model, "%d/%d/%d ", &v[0], &vt[0], &vn[0]);
- fscanf(model, "%d/%d/%d ", &v[1], &vt[1], &vn[1]);
- fscanf(model, "%d/%d/%d", &v[2], &vt[2], &vn[2]);
- } else {
- fscanf(model, "%d//%d ", &v[0], &vn[0]);
- fscanf(model, "%d//%d ", &v[1], &vn[1]);
- fscanf(model, "%d//%d", &v[2], &vn[2]);
- }
- if (uvs.size() > 0)
- this->vertex_buffer.setUV(uvs.at(vt[0] - 1).x, uvs.at(vt[0] - 1).y);
- else
- this->vertex_buffer.setUV(0.0f, 0.0f);
- this->vertex_buffer.setNormal(normals.at(vn[0] - 1).y, normals.at(vn[0] - 1).x, normals.at(vn[0] - 1).z);
- this->vertex_buffer.addVertex(vertices.at(v[0] - 1).x, vertices.at(v[0] - 1).y, vertices.at(v[0] - 1).z);
- if (uvs.size() > 0)
- this->vertex_buffer.setUV(uvs.at(vt[1] - 1).x, uvs.at(vt[1] - 1).y);
- this->vertex_buffer.setNormal(normals.at(vn[1] - 1).y, normals.at(vn[1] - 1).x, normals.at(vn[1] - 1).z);
- this->vertex_buffer.addVertex(vertices.at(v[1] - 1).x, vertices.at(v[1] - 1).y, vertices.at(v[1] - 1).z);
- if (uvs.size() > 0)
- this->vertex_buffer.setUV(uvs.at(vt[2] - 1).x, uvs.at(vt[2] - 1).y);
- this->vertex_buffer.setNormal(normals.at(vn[2] - 1).y, normals.at(vn[2] - 1).x, normals.at(vn[2] - 1).z);
- this->vertex_buffer.addVertex(vertices.at(v[2] - 1).x, vertices.at(v[2] - 1).y, vertices.at(v[2] - 1).z);
- continue;
- }
- }
- fclose(model);
- } else {
- #ifdef _WIN32
- MessageBox(NULL, "Couldn't load model!", "Error!", MB_OK);
- #else
- printf("Error: Couldn't load model!\n");
- #endif
- }
- }
- VertexBuffer Model::getBuffer() {
- return this->vertex_buffer;
- }
- ModelInstance::ModelInstance(Model model, Texture2D texture, Matrix matrix) {
- this->model = model;
- this->texture = texture;
- this->matrix = matrix;
- }
- Matrix ModelInstance::getMatrix() {
- return this->matrix;
- }
- void ModelInstance::setMatrix(Matrix matrix) {
- this->matrix = matrix;
- }
- void ModelInstance::setTexture(Texture2D texture) {
- this->texture = texture;
- }
- void ModelInstance::render() {
- glLoadIdentity();
- glTranslatef(this->matrix.translation.x, this->matrix.translation.y, this->matrix.translation.z);
- glScalef(this->matrix.scale.x, this->matrix.scale.y, this->matrix.scale.z);
- glRotatef(this->matrix.rotation.x, 1.0f, 0.0f, 0.0f);
- glRotatef(this->matrix.rotation.y, 0.0f, 1.0f, 0.0f);
- glRotatef(this->matrix.rotation.z, 0.0f, 0.0f, 1.0f);
- this->texture.use();
- this->model.getBuffer().render();
- }
