summaryrefslogtreecommitdiffstats
path: root/dev/a6-harold/edge_mesh.cc
blob: 1abe2e00f6ce2acf04273efe5f78bdc187a6c284 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/** CSci-4611 Assignment 5:  Art Render
 */

#include "edge_mesh.h"

EdgeMesh::EdgeMesh() : gpuDirty_(true) {
    
}

EdgeMesh::~EdgeMesh() {
}



void EdgeMesh::CreateFromMesh(const Mesh &mesh) {
    verts_.clear();
    norms_.clear();
    indices_.clear();
    leftNorms_.clear();
    rightNorms_.clear();
    
    // temp data holders for use while constructing the mesh in an indexed primitives mode
    std::vector<Point3> vertices;                   // vertex positions
    std::vector<Vector3> normals;                   // vertex normals
    std::vector<Vector3> leftNormals, rightNormals; // normals of adj. faces
    std::vector< std::vector<unsigned int> > triangles;      // fin triangles

    for (int t = 0; t < mesh.num_triangles(); t++) {
        std::vector<unsigned int> tri = mesh.read_triangle_indices_data(t);
        Point3 a = mesh.read_vertex_data(tri[0]);
        Point3 b = mesh.read_vertex_data(tri[1]);
        Point3 c = mesh.read_vertex_data(tri[2]);
        Vector3 e1 = b-a;
        e1.Normalize();
        Vector3 e2 = c-a;
        e2.Normalize();
        Vector3 n = e1.Cross(e2);
        
        //Vector3 n = ((b-a).cross(c-a)).to_unit();
        //std::cout << n << std::endl;
        addEdge(&vertices, &normals, &leftNormals, &rightNormals, &triangles, mesh, tri[0], tri[1], n);
        addEdge(&vertices, &normals, &leftNormals, &rightNormals, &triangles, mesh, tri[1], tri[2], n);
        addEdge(&vertices, &normals, &leftNormals, &rightNormals, &triangles, mesh, tri[2], tri[0], n);
    }
    
    // now transfer data to the member variable data holders
    for (int i=0; i<vertices.size(); i++) {
        verts_.push_back(vertices[i][0]);
        verts_.push_back(vertices[i][1]);
        verts_.push_back(vertices[i][2]);
        norms_.push_back(normals[i][0]);
        norms_.push_back(normals[i][1]);
        norms_.push_back(normals[i][2]);
        leftNorms_.push_back(leftNormals[i][0]);
        leftNorms_.push_back(leftNormals[i][1]);
        leftNorms_.push_back(leftNormals[i][2]);
        rightNorms_.push_back(rightNormals[i][0]);
        rightNorms_.push_back(rightNormals[i][1]);
        rightNorms_.push_back(rightNormals[i][2]);
    }
    
    for (int i=0; i<triangles.size(); i++) {
        indices_.push_back(triangles[i][0]);
        indices_.push_back(triangles[i][1]);
        indices_.push_back(triangles[i][2]);
    }
    gpuDirty_ = true;
}


void EdgeMesh::addEdge(std::vector<Point3> *vertices,
                       std::vector<Vector3> *normals,
                       std::vector<Vector3> *leftNormals,
                       std::vector<Vector3> *rightNormals,
                       std::vector< std::vector<unsigned int> > *triangles,
                       const Mesh &mesh, int v0, int v1, Vector3 n)
{
    // edgeMap contains the index of the first of four consecutive vertices
    // that form the quad fin

    EdgeMap::iterator it = edgeMap.find(std::make_pair(v1, v0));
    if (it != edgeMap.end()) { // found
        int v = it->second;
        (*rightNormals)[v+0]
            = (*rightNormals)[v+1]
            = (*rightNormals)[v+2]
            = (*rightNormals)[v+3]
            = n;
    }
    else {
        int v = (int)vertices->size();
        edgeMap[std::make_pair(v0, v1)] = v;
        
        vertices->push_back(mesh.read_vertex_data(v0));
        vertices->push_back(mesh.read_vertex_data(v0));
        vertices->push_back(mesh.read_vertex_data(v1));
        vertices->push_back(mesh.read_vertex_data(v1));
        
        normals->push_back(Vector3());
        normals->push_back(mesh.read_normal_data(v0));
        normals->push_back(Vector3());
        normals->push_back(mesh.read_normal_data(v1));
        
        leftNormals->push_back(n);
        leftNormals->push_back(n);
        leftNormals->push_back(n);
        leftNormals->push_back(n);
        
        rightNormals->push_back(-n);
        rightNormals->push_back(-n);
        rightNormals->push_back(-n);
        rightNormals->push_back(-n);

        std::vector<unsigned int> tri1;
        tri1.push_back(v+0);
        tri1.push_back(v+2);
        tri1.push_back(v+3);
        triangles->push_back(tri1);
        
        std::vector<unsigned int> tri2;
        tri2.push_back(v+0);
        tri2.push_back(v+3);
        tri2.push_back(v+1);
        triangles->push_back(tri2);
    }
}



void EdgeMesh::UpdateGPUMemory() {
    if (gpuDirty_) {
        // sanity check -- for each attribute that is added make sure the number
        // of triangles is equal to the number of tris in the verts array.
        if (norms_.size() / 3 != num_vertices()) {
            std::cerr << "EdgeMesh::UpdateGPUMemory() -- warning: the number of per vertex normals in the mesh is not equal to the number vertices in the mesh. (N = " << norms_.size() / 3 << ", V = " << num_vertices() << ")" << std::endl;
        }
        if (leftNorms_.size() / 3 != num_vertices()) {
            std::cerr << "EdgeMesh::UpdateGPUMemory() -- warning: the number of per vertex left-normals in the mesh is not equal to the number vertices in the mesh. (LN = " << leftNorms_.size() / 3 << ", V = " << num_vertices() << ")" << std::endl;
        }
        if (rightNorms_.size() / 3 != num_vertices()) {
            std::cerr << "EdgeMesh::UpdateGPUMemory() -- warning: the number of per vertex right-normals in the mesh is not equal to the number vertices in the mesh. (RN = " << rightNorms_.size() / 3 << ", V = " << num_vertices() << ")" << std::endl;
        }
        
        GLsizeiptr totalMemSize = 0;
        
        GLsizeiptr vertsMemSize = verts_.size() * sizeof(float);
        GLsizeiptr vertsMemOffset = 0;
        totalMemSize += vertsMemSize;
        
        GLsizeiptr normsMemSize = norms_.size() * sizeof(float);
        GLsizeiptr normsMemOffset = totalMemSize;
        totalMemSize += normsMemSize;
        
        GLsizeiptr leftNormsMemSize = leftNorms_.size() * sizeof(float);
        GLsizeiptr leftNormsMemOffset = totalMemSize;
        totalMemSize += leftNormsMemSize;

        GLsizeiptr rightNormsMemSize = rightNorms_.size() * sizeof(float);
        GLsizeiptr rightNormsMemOffset = totalMemSize;
        totalMemSize += rightNormsMemSize;

        glGenBuffers(1, &vertexBuffer_);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer_);
        glBufferData(GL_ARRAY_BUFFER, totalMemSize, NULL, GL_STATIC_DRAW);
        
        glBufferSubData(GL_ARRAY_BUFFER, vertsMemOffset, vertsMemSize, &verts_[0]);
        glBufferSubData(GL_ARRAY_BUFFER, normsMemOffset, normsMemSize, &norms_[0]);
        glBufferSubData(GL_ARRAY_BUFFER, leftNormsMemOffset, leftNormsMemSize, &leftNorms_[0]);
        glBufferSubData(GL_ARRAY_BUFFER, rightNormsMemOffset, rightNormsMemSize, &rightNorms_[0]);
        
        glGenVertexArrays(1, &vertexArray_);
        glBindVertexArray(vertexArray_);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer_);
        
        // attribute 0 = vertices (required)
        int attribID = 0;
        int nComponents = 3;
        glEnableVertexAttribArray(attribID);
        glVertexAttribPointer(attribID, nComponents, GL_FLOAT, GL_FALSE, nComponents*sizeof(GLfloat), (char*)0 + vertsMemOffset);
        
        // attribute 1 = normals (required)
        attribID = 1;
        nComponents = 3;
        glEnableVertexAttribArray(attribID);
        glVertexAttribPointer(attribID, nComponents, GL_FLOAT, GL_TRUE, nComponents*sizeof(GLfloat), (char*)0 + normsMemOffset);

        // attribute 2 = left normals (required)
        attribID = 2;
        nComponents = 3;
        glEnableVertexAttribArray(attribID);
        glVertexAttribPointer(attribID, nComponents, GL_FLOAT, GL_TRUE, nComponents*sizeof(GLfloat), (char*)0 + leftNormsMemOffset);

        // attribute 3 = right normals (required)
        attribID = 3;
        nComponents = 3;
        glEnableVertexAttribArray(attribID);
        glVertexAttribPointer(attribID, nComponents, GL_FLOAT, GL_TRUE, nComponents*sizeof(GLfloat), (char*)0 + rightNormsMemOffset);
        
        glBindVertexArray(0);
        
        
        glGenBuffers(1, &elementBuffer_);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer_);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_.size() * sizeof(unsigned int), &indices_[0], GL_STATIC_DRAW);
        
        
        gpuDirty_ = false;
    }
}


void EdgeMesh::Draw() {
    if (gpuDirty_) {
        UpdateGPUMemory();
    }
    
    // set defaults to pass to shaders any for optional attribs
    glVertexAttrib3f(1, 0.0, 0.0, 1.0);      // normal = +Z
    glVertexAttrib3f(2, 0.0, 0.0, 1.0);      // left-normal = +Z
    glVertexAttrib3f(3, 0.0, 0.0, 1.0);      // right-normal = +Z
    
    glBindVertexArray(vertexArray_);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer_);
    glDrawElements(GL_TRIANGLES, (GLsizei)indices_.size(), GL_UNSIGNED_INT, (void*)0);
    glBindVertexArray(0);
}


int EdgeMesh::num_vertices() const {
    return (int)verts_.size()/3;
}

int EdgeMesh::num_triangles() const {
    return (int)indices_.size()/3;
}