forked from TrenchBroom/TrenchBroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAliasModelRenderer.cpp
114 lines (92 loc) · 4.3 KB
/
AliasModelRenderer.cpp
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
/*
Copyright (C) 2010-2012 Kristian Duske
This file is part of TrenchBroom.
TrenchBroom is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TrenchBroom is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AliasModelRenderer.h"
#include <GL/glew.h>
#include "Model/Alias.h"
#include "Model/Entity.h"
#include "Renderer/MapRenderer.h"
#include "Renderer/Palette.h"
#include "Renderer/RenderContext.h"
#include "Renderer/TextureRenderer.h"
#include "Renderer/Shader/Shader.h"
#include "Renderer/Vbo.h"
#include <cassert>
namespace TrenchBroom {
namespace Renderer {
AliasModelRenderer::AliasModelRenderer(const Model::Alias& alias, unsigned int frameIndex, unsigned int skinIndex, Vbo& vbo, const Palette& palette) :
m_alias(alias),
m_frameIndex(frameIndex),
m_skinIndex(skinIndex),
m_palette(palette),
m_texture(NULL),
m_vbo(vbo),
m_vertexArray(NULL) {}
AliasModelRenderer::~AliasModelRenderer() {
m_frameIndex = 0;
m_skinIndex = 0;
delete m_vertexArray;
m_vertexArray = NULL;
}
void AliasModelRenderer::render(ShaderProgram& shaderProgram) {
if (m_vertexArray == NULL) {
assert(m_skinIndex < m_alias.skins().size());
assert(m_frameIndex < m_alias.frames().size());
Model::AliasSkin& skin = *m_alias.skins()[m_skinIndex];
m_texture = TextureRendererPtr(new TextureRenderer(skin, 0, m_palette));
Model::AliasSingleFrame& frame = m_alias.frame(m_frameIndex);
const Model::AliasFrameTriangleList& triangles = frame.triangles();
unsigned int vertexCount = static_cast<unsigned int>(3 * triangles.size());
m_vertexArray = new VertexArray(m_vbo, GL_TRIANGLES, vertexCount,
Attribute::position3f(),
Attribute::texCoord02f());
SetVboState mapVbo(m_vbo, Vbo::VboMapped);
for (unsigned int i = 0; i < triangles.size(); i++) {
Model::AliasFrameTriangle& triangle = *triangles[i];
for (unsigned int j = 0; j < 3; j++) {
Model::AliasFrameVertex& vertex = triangle[j];
m_vertexArray->addAttribute(vertex.position());
m_vertexArray->addAttribute(vertex.texCoords());
}
}
}
assert(m_vertexArray != NULL);
glActiveTexture(GL_TEXTURE0);
m_texture->activate();
shaderProgram.setUniformVariable("Texture", 0);
m_vertexArray->render();
m_texture->deactivate();
}
const Vec3f& AliasModelRenderer::center() const {
return m_alias.frame(m_frameIndex).center();
}
const BBoxf& AliasModelRenderer::bounds() const {
return m_alias.frame(m_frameIndex).bounds();
}
BBoxf AliasModelRenderer::boundsAfterTransformation(const Mat4f& transformation) const {
Model::AliasSingleFrame& frame = m_alias.frame(m_frameIndex);
const Model::AliasFrameTriangleList& triangles = frame.triangles();
BBoxf bounds;
bounds.min = bounds.max = transformation * (*triangles[0])[0].position();
for (unsigned int i = 1; i < triangles.size(); i++) {
Model::AliasFrameTriangle& triangle = *triangles[i];
for (unsigned int j = 0; j < 3; j++) {
Model::AliasFrameVertex& vertex = triangle[j];
bounds.mergeWith(transformation * vertex.position());
}
}
return bounds;
}
}
}