-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSGLFW_Mesh.h
More file actions
203 lines (158 loc) · 5.06 KB
/
Copy pathSGLFW_Mesh.h
File metadata and controls
203 lines (158 loc) · 5.06 KB
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
#pragma once
/**
SGLFW_Mesh.h : create OpenGL buffers with SMesh and instance data and render
==============================================================================
Canonical use is from SGLFW_Scene::initMesh
* (in a former incarnation this was called SGLFW_Render)
**/
struct NP ;
struct SMesh ;
struct SGLFW_Program ;
struct SGLFW_Mesh
{
static constexpr const char* _DUMP = "SGLFW_Mesh__DUMP" ;
bool DUMP ;
const SMesh* mesh ;
SGLFW_Buffer* vtx ;
SGLFW_Buffer* nrm ;
SGLFW_Buffer* ins ;
SGLFW_VAO* vao ;
SGLFW_Buffer* idx ;
int inst_num ;
const float* inst_values ;
int render_count ;
SGLFW_Mesh(const SMesh* mesh ) ;
void init();
void set_inst(const NP* _inst );
void set_inst(int _inst_num, const float* _inst_values );
bool has_inst() const ;
std::string descInst() const ;
std::string desc() const ;
void render(const SGLFW_Program* prog);
void render_drawElements() const ;
};
inline SGLFW_Mesh::SGLFW_Mesh(const SMesh* _mesh )
:
DUMP(ssys::getenvbool(_DUMP)),
mesh(_mesh),
vtx(nullptr),
nrm(nullptr),
ins(nullptr),
vao(nullptr),
idx(nullptr),
inst_num(0),
inst_values(nullptr),
render_count(0)
{
init();
}
/**
SGLFW_Mesh::init
----------------
Creates vtx, nrm, idx OpenGL buffers using SGLFW_Buffers
**/
inline void SGLFW_Mesh::init()
{
vtx = new SGLFW_Buffer( mesh->vtx->arr_bytes(), mesh->vtx->cvalues<float>(), GL_ARRAY_BUFFER, GL_STATIC_DRAW );
vtx->bind();
vtx->upload();
nrm = new SGLFW_Buffer( mesh->nrm->arr_bytes(), mesh->nrm->cvalues<float>(), GL_ARRAY_BUFFER, GL_STATIC_DRAW );
nrm->bind();
nrm->upload();
vao = new SGLFW_VAO ; // vao: establishes context for OpenGL attrib state and element array (not GL_ARRAY_BUFFER)
vao->bind();
idx = new SGLFW_Buffer( mesh->tri->arr_bytes(), mesh->tri->cvalues<int>() , GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW );
idx->bind();
idx->upload();
}
inline void SGLFW_Mesh::set_inst(const NP* _inst )
{
if(_inst == nullptr) return ;
assert( _inst->uifc == 'f' );
assert( _inst->ebyte == 4 );
assert( _inst->has_shape(-1,4,4));
set_inst( _inst->num_items(), _inst->cvalues<float>() );
}
inline void SGLFW_Mesh::set_inst(int _inst_num, const float* _inst_values )
{
inst_num = _inst_num ;
inst_values = _inst_values ;
int itemsize = 4*4*sizeof(float) ;
int num_bytes = inst_num*itemsize ;
ins = new SGLFW_Buffer( num_bytes, inst_values, GL_ARRAY_BUFFER, GL_STATIC_DRAW );
ins->bind();
ins->upload();
}
inline bool SGLFW_Mesh::has_inst() const
{
return inst_num > 0 && inst_values != nullptr ;
}
inline std::string SGLFW_Mesh::desc() const
{
std::stringstream ss ;
ss << descInst() ;
std::string str = ss.str() ;
return str ;
}
inline std::string SGLFW_Mesh::descInst() const
{
int edge_items = 10 ;
std::stringstream ss ;
ss << "[SGLFW_Mesh::descInst inst_num " << inst_num << std::endl ;
ss << stra<float>::DescItems( inst_values, 16, inst_num, edge_items );
ss << "]SGLFW_Mesh::descInst inst_num " << inst_num << std::endl ;
std::string str = ss.str() ;
return str ;
}
/**
SGLFW_Mesh::render
---------------------
Use argument prog to render the mesh
NB: careful that the intended buffer is bound (making it the active GL_ARRAY_BUFFER)
when the vertex attrib is enabled. Getting this wrong can for example easily cause
normals to appear in position slots causing perplexing renders.
**/
inline void SGLFW_Mesh::render(const SGLFW_Program* prog)
{
prog->use();
vao->bind();
vtx->bind();
prog->enableVertexAttribArray( prog->vtx_attname, SMesh::VTX_SPEC );
nrm->bind();
prog->enableVertexAttribArray( prog->nrm_attname, SMesh::NRM_SPEC );
if(ins)
{
ins->bind();
prog->enableVertexAttribArray_OfTransforms( prog->ins_attname ) ;
}
idx->bind();
prog->updateMVP();
render_drawElements();
render_count += 1 ;
}
inline void SGLFW_Mesh::render_drawElements() const
{
GLenum mode = GL_TRIANGLES ;
GLsizei count = mesh->indices_num() ; // number of elements to render (eg 3 for 1 triangle)
GLenum type = GL_UNSIGNED_INT ;
const void * indices = (GLvoid*)(sizeof(GLuint) * mesh->indices_offset() ) ;
GLsizei instancecount = ins ? inst_num : 0 ;
if(instancecount > 0)
{
GLint basevertex = 0 ;
GLuint baseinstance = 0 ;
glDrawElementsInstancedBaseVertexBaseInstance(mode, count, type, indices, instancecount, basevertex, baseinstance );
// SEGV on laptop, OK on workstation
// https://github.com/moderngl/moderngl/issues/346
if(DUMP && render_count < 10 ) std::cout
<< "SGLFW_Mesh::render_drawElements.glDrawElementsInstancedBaseVertexBaseInstance"
<< " render_count " << render_count
<< " instancecount " << instancecount
<< std::endl
;
}
else
{
glDrawElements(mode, count, type, indices );
}
}