summary refs log tree commit diff stats
path: root/src/renderer/mesh.cpp
blob: b06b723a6be0f616b2c0968a8c10a1e0a04c3613 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "mesh.h"
#include <fstream>
#include <vector>
#include <map>
#include <glm/glm.hpp>
#include "util.h"

Mesh::Mesh(std::string filename)
{
  std::ifstream meshfile(filename);
  if (!meshfile.is_open())
  {
    throw std::invalid_argument("Could not open mesh file");
  }

  std::vector<glm::vec3> tempVertices;
  std::vector<glm::vec2> tempUvs;
  std::vector<glm::vec3> tempNormals;

  std::vector<glm::vec3> outVertices;
  std::vector<glm::vec2> outUvs;
  std::vector<glm::vec3> outNormals;
  std::map<element, unsigned short> elementIds;
  std::vector<unsigned short> indices;

  while (meshfile)
  {
    std::string linetype;
    meshfile >> linetype;

    if (linetype == "v")
    {
      glm::vec3 vertex;
      meshfile >> vertex.x >> vertex.y >> vertex.z;

      tempVertices.push_back(std::move(vertex));
    } else if (linetype == "vt")
    {
      glm::vec2 uv;
      meshfile >> uv.x >> uv.y;

      tempUvs.push_back(std::move(uv));
    } else if (linetype == "vn")
    {
      glm::vec3 normal;
      meshfile >> normal.x >> normal.y >> normal.z;

      tempNormals.push_back(std::move(normal));
    } else if (linetype == "f")
    {
      element elements[3];

      meshfile
        >> elements[0].vertexId >> chlit('/')
        >> elements[0].uvId >> chlit('/')
        >> elements[0].normalId
        >> elements[1].vertexId >> chlit('/')
        >> elements[1].uvId >> chlit('/')
        >> elements[1].normalId
        >> elements[2].vertexId >> chlit('/')
        >> elements[2].uvId >> chlit('/')
        >> elements[2].normalId;

      for (size_t i = 0; i < 3; i++)
      {
        if (!elementIds.count(elements[i]))
        {
          elementIds[elements[i]] = outVertices.size();

          outVertices.push_back(tempVertices[elements[i].vertexId - 1]);
          outUvs.push_back(tempUvs[elements[i].uvId - 1]);
          outNormals.push_back(tempNormals[elements[i].normalId - 1]);
        }

        indices.push_back(elementIds[elements[i]]);
      }
    }
  }

  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer_.getId());
  glBufferData(
    GL_ARRAY_BUFFER,
    outVertices.size() * sizeof(glm::vec3),
    outVertices.data(),
    GL_STATIC_DRAW);

  glBindBuffer(GL_ARRAY_BUFFER, uvBuffer_.getId());
  glBufferData(
    GL_ARRAY_BUFFER,
    outUvs.size() * sizeof(glm::vec2),
    outUvs.data(),
    GL_STATIC_DRAW);

  glBindBuffer(GL_ARRAY_BUFFER, normalBuffer_.getId());
  glBufferData(
    GL_ARRAY_BUFFER,
    outNormals.size() * sizeof(glm::vec3),
    outNormals.data(),
    GL_STATIC_DRAW);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer_.getId());
  glBufferData(
    GL_ELEMENT_ARRAY_BUFFER,
    indices.size() * sizeof(unsigned short),
    indices.data(),
    GL_STATIC_DRAW);

  indexCount_ = indices.size();
}