File size: 14,932 Bytes
5b89cb9 | 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | // This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "MeshGL.h"
#include "bind_vertex_attrib_array.h"
#include "create_shader_program.h"
#include "destroy_shader_program.h"
#include "verasansmono_compressed.h"
#include <iostream>
IGL_INLINE igl::opengl::MeshGL::MeshGL():
tex_filter(GL_LINEAR),
tex_wrap(GL_REPEAT)
{
}
IGL_INLINE void igl::opengl::MeshGL::init_buffers()
{
// Mesh: Vertex Array Object & Buffer objects
glGenVertexArrays(1, &vao_mesh);
glBindVertexArray(vao_mesh);
glGenBuffers(1, &vbo_V);
glGenBuffers(1, &vbo_V_normals);
glGenBuffers(1, &vbo_V_ambient);
glGenBuffers(1, &vbo_V_diffuse);
glGenBuffers(1, &vbo_V_specular);
glGenBuffers(1, &vbo_V_uv);
glGenBuffers(1, &vbo_F);
glGenTextures(1, &vbo_tex);
glGenTextures(1, &font_atlas);
// Line overlay
glGenVertexArrays(1, &vao_overlay_lines);
glBindVertexArray(vao_overlay_lines);
glGenBuffers(1, &vbo_lines_F);
glGenBuffers(1, &vbo_lines_V);
glGenBuffers(1, &vbo_lines_V_colors);
// Point overlay
glGenVertexArrays(1, &vao_overlay_points);
glBindVertexArray(vao_overlay_points);
glGenBuffers(1, &vbo_points_F);
glGenBuffers(1, &vbo_points_V);
glGenBuffers(1, &vbo_points_V_colors);
// Text Labels
vertex_labels.init_buffers();
face_labels.init_buffers();
custom_labels.init_buffers();
dirty = MeshGL::DIRTY_ALL;
}
IGL_INLINE void igl::opengl::MeshGL::free_buffers()
{
if (is_initialized)
{
glDeleteVertexArrays(1, &vao_mesh);
glDeleteVertexArrays(1, &vao_overlay_lines);
glDeleteVertexArrays(1, &vao_overlay_points);
glDeleteBuffers(1, &vbo_V);
glDeleteBuffers(1, &vbo_V_normals);
glDeleteBuffers(1, &vbo_V_ambient);
glDeleteBuffers(1, &vbo_V_diffuse);
glDeleteBuffers(1, &vbo_V_specular);
glDeleteBuffers(1, &vbo_V_uv);
glDeleteBuffers(1, &vbo_F);
glDeleteBuffers(1, &vbo_lines_F);
glDeleteBuffers(1, &vbo_lines_V);
glDeleteBuffers(1, &vbo_lines_V_colors);
glDeleteBuffers(1, &vbo_points_F);
glDeleteBuffers(1, &vbo_points_V);
glDeleteBuffers(1, &vbo_points_V_colors);
// Text Labels
vertex_labels.free_buffers();
face_labels.free_buffers();
custom_labels.free_buffers();
glDeleteTextures(1, &vbo_tex);
glDeleteTextures(1, &font_atlas);
}
}
IGL_INLINE void igl::opengl::MeshGL::TextGL::init_buffers()
{
glGenVertexArrays(1, &vao_labels);
glBindVertexArray(vao_labels);
glGenBuffers(1, &vbo_labels_pos);
glGenBuffers(1, &vbo_labels_characters);
glGenBuffers(1, &vbo_labels_offset);
glGenBuffers(1, &vbo_labels_indices);
}
IGL_INLINE void igl::opengl::MeshGL::TextGL::free_buffers()
{
glDeleteBuffers(1, &vbo_labels_pos);
glDeleteBuffers(1, &vbo_labels_characters);
glDeleteBuffers(1, &vbo_labels_offset);
glDeleteBuffers(1, &vbo_labels_indices);
}
IGL_INLINE void igl::opengl::MeshGL::bind_mesh()
{
glBindVertexArray(vao_mesh);
glUseProgram(shader_mesh);
bind_vertex_attrib_array(shader_mesh,"position", vbo_V, V_vbo, dirty & MeshGL::DIRTY_POSITION);
bind_vertex_attrib_array(shader_mesh,"normal", vbo_V_normals, V_normals_vbo, dirty & MeshGL::DIRTY_NORMAL);
bind_vertex_attrib_array(shader_mesh,"Ka", vbo_V_ambient, V_ambient_vbo, dirty & MeshGL::DIRTY_AMBIENT);
bind_vertex_attrib_array(shader_mesh,"Kd", vbo_V_diffuse, V_diffuse_vbo, dirty & MeshGL::DIRTY_DIFFUSE);
bind_vertex_attrib_array(shader_mesh,"Ks", vbo_V_specular, V_specular_vbo, dirty & MeshGL::DIRTY_SPECULAR);
bind_vertex_attrib_array(shader_mesh,"texcoord", vbo_V_uv, V_uv_vbo, dirty & MeshGL::DIRTY_UV);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_F);
if (dirty & MeshGL::DIRTY_FACE)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned)*F_vbo.size(), F_vbo.data(), GL_DYNAMIC_DRAW);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, vbo_tex);
if (dirty & MeshGL::DIRTY_TEXTURE)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, tex_wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tex_wrap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, tex_filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, tex_filter);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_u, tex_v, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data());
}
glUniform1i(glGetUniformLocation(shader_mesh,"tex"), 0);
dirty &= ~MeshGL::DIRTY_MESH;
}
IGL_INLINE void igl::opengl::MeshGL::bind_overlay_lines()
{
bool is_dirty = dirty & MeshGL::DIRTY_OVERLAY_LINES;
glBindVertexArray(vao_overlay_lines);
glUseProgram(shader_overlay_lines);
bind_vertex_attrib_array(shader_overlay_lines,"position", vbo_lines_V, lines_V_vbo, is_dirty);
bind_vertex_attrib_array(shader_overlay_lines,"color", vbo_lines_V_colors, lines_V_colors_vbo, is_dirty);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_lines_F);
if (is_dirty)
{
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned)*lines_F_vbo.size(), lines_F_vbo.data(), GL_DYNAMIC_DRAW);
}
dirty &= ~MeshGL::DIRTY_OVERLAY_LINES;
}
IGL_INLINE void igl::opengl::MeshGL::bind_overlay_points()
{
bool is_dirty = dirty & MeshGL::DIRTY_OVERLAY_POINTS;
glBindVertexArray(vao_overlay_points);
glUseProgram(shader_overlay_points);
bind_vertex_attrib_array(shader_overlay_points,"position", vbo_points_V, points_V_vbo, is_dirty);
bind_vertex_attrib_array(shader_overlay_points,"color", vbo_points_V_colors, points_V_colors_vbo, is_dirty);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_points_F);
if (is_dirty)
{
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned)*points_F_vbo.size(), points_F_vbo.data(), GL_DYNAMIC_DRAW);
}
dirty &= ~MeshGL::DIRTY_OVERLAY_POINTS;
}
IGL_INLINE void igl::opengl::MeshGL::init_text_rendering()
{
// Decompress the png of the font atlas
unsigned char verasansmono_font_atlas[256*256];
decompress_verasansmono_atlas(verasansmono_font_atlas);
// Bind atlas
glBindTexture(GL_TEXTURE_2D, font_atlas);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, verasansmono_font_atlas);
// TextGL initialization
vertex_labels.dirty_flag = MeshGL::DIRTY_VERTEX_LABELS;
face_labels.dirty_flag = MeshGL::DIRTY_FACE_LABELS;
custom_labels.dirty_flag = MeshGL::DIRTY_CUSTOM_LABELS;
}
IGL_INLINE void igl::opengl::MeshGL::bind_labels(const TextGL& labels)
{
bool is_dirty = dirty & labels.dirty_flag;
glBindTexture(GL_TEXTURE_2D, font_atlas);
glBindVertexArray(labels.vao_labels);
glUseProgram(shader_text);
bind_vertex_attrib_array(shader_text, "position" , labels.vbo_labels_pos , labels.label_pos_vbo , is_dirty);
bind_vertex_attrib_array(shader_text, "character", labels.vbo_labels_characters, labels.label_char_vbo , is_dirty);
bind_vertex_attrib_array(shader_text, "offset" , labels.vbo_labels_offset , labels.label_offset_vbo, is_dirty);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, labels.vbo_labels_indices);
if (is_dirty)
{
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned)*labels.label_indices_vbo.size(), labels.label_indices_vbo.data(), GL_DYNAMIC_DRAW);
}
dirty &= ~labels.dirty_flag;
}
IGL_INLINE void igl::opengl::MeshGL::draw_mesh(bool solid)
{
glPolygonMode(GL_FRONT_AND_BACK, solid ? GL_FILL : GL_LINE);
/* Avoid Z-buffer fighting between filled triangles & wireframe lines */
if (solid)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1.0);
}
glDrawElements(GL_TRIANGLES, 3*F_vbo.rows(), GL_UNSIGNED_INT, 0);
glDisable(GL_POLYGON_OFFSET_FILL);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
IGL_INLINE void igl::opengl::MeshGL::draw_overlay_lines()
{
glDrawElements(GL_LINES, lines_F_vbo.rows(), GL_UNSIGNED_INT, 0);
}
IGL_INLINE void igl::opengl::MeshGL::draw_overlay_points()
{
glDrawElements(GL_POINTS, points_F_vbo.rows(), GL_UNSIGNED_INT, 0);
}
IGL_INLINE void igl::opengl::MeshGL::draw_labels(const TextGL& labels)
{
glDrawElements(GL_POINTS, labels.label_indices_vbo.rows(), GL_UNSIGNED_INT, 0);
}
IGL_INLINE void igl::opengl::MeshGL::init()
{
if(is_initialized)
{
return;
}
is_initialized = true;
std::string mesh_vertex_shader_string =
R"(#version 150
uniform mat4 view;
uniform mat4 proj;
uniform mat4 normal_matrix;
in vec3 position;
in vec3 normal;
out vec3 position_eye;
out vec3 normal_eye;
in vec4 Ka;
in vec4 Kd;
in vec4 Ks;
in vec2 texcoord;
out vec2 texcoordi;
out vec4 Kai;
out vec4 Kdi;
out vec4 Ksi;
void main()
{
position_eye = vec3 (view * vec4 (position, 1.0));
normal_eye = vec3 (normal_matrix * vec4 (normal, 0.0));
normal_eye = normalize(normal_eye);
gl_Position = proj * vec4 (position_eye, 1.0); //proj * view * vec4(position, 1.0);"
Kai = Ka;
Kdi = Kd;
Ksi = Ks;
texcoordi = texcoord;
}
)";
std::string mesh_fragment_shader_string =
R"(#version 150
uniform mat4 view;
uniform mat4 proj;
uniform vec4 fixed_color;
in vec3 position_eye;
in vec3 normal_eye;
uniform vec3 light_position_eye;
vec3 Ls = vec3 (1, 1, 1);
vec3 Ld = vec3 (1, 1, 1);
vec3 La = vec3 (1, 1, 1);
in vec4 Ksi;
in vec4 Kdi;
in vec4 Kai;
in vec2 texcoordi;
uniform sampler2D tex;
uniform float specular_exponent;
uniform float lighting_factor;
uniform float texture_factor;
uniform float matcap_factor;
uniform float double_sided;
out vec4 outColor;
void main()
{
if(matcap_factor == 1.0f)
{
vec2 uv = normalize(normal_eye).xy * 0.5 + 0.5;
outColor = texture(tex, uv);
}else
{
vec3 Ia = La * vec3(Kai); // ambient intensity
vec3 vector_to_light_eye = light_position_eye - position_eye;
vec3 direction_to_light_eye = normalize (vector_to_light_eye);
float dot_prod = dot (direction_to_light_eye, normalize(normal_eye));
float clamped_dot_prod = abs(max (dot_prod, -double_sided));
vec3 Id = Ld * vec3(Kdi) * clamped_dot_prod; // Diffuse intensity
vec3 reflection_eye = reflect (-direction_to_light_eye, normalize(normal_eye));
vec3 surface_to_viewer_eye = normalize (-position_eye);
float dot_prod_specular = dot (reflection_eye, surface_to_viewer_eye);
dot_prod_specular = float(abs(dot_prod)==dot_prod) * abs(max (dot_prod_specular, -double_sided));
float specular_factor = pow (dot_prod_specular, specular_exponent);
vec3 Is = Ls * vec3(Ksi) * specular_factor; // specular intensity
vec4 color = vec4(lighting_factor * (Is + Id) + Ia + (1.0-lighting_factor) * vec3(Kdi),(Kai.a+Ksi.a+Kdi.a)/3);
outColor = mix(vec4(1,1,1,1), texture(tex, texcoordi), texture_factor) * color;
if (fixed_color != vec4(0.0)) outColor = fixed_color;
}
}
)";
std::string overlay_vertex_shader_string =
R"(#version 150
uniform mat4 view;
uniform mat4 proj;
in vec3 position;
in vec3 color;
out vec3 color_frag;
void main()
{
gl_Position = proj * view * vec4 (position, 1.0);
color_frag = color;
}
)";
std::string overlay_fragment_shader_string =
R"(#version 150
in vec3 color_frag;
out vec4 outColor;
void main()
{
outColor = vec4(color_frag, 1.0);
}
)";
std::string overlay_point_fragment_shader_string =
R"(#version 150
in vec3 color_frag;
out vec4 outColor;
void main()
{
if (length(gl_PointCoord - vec2(0.5)) > 0.5)
discard;
outColor = vec4(color_frag, 1.0);
}
)";
std::string text_vert_shader =
R"(#version 330
in vec3 position;
in float character;
in float offset;
uniform mat4 view;
uniform mat4 proj;
out int vCharacter;
out float vOffset;
void main()
{
vCharacter = int(character);
vOffset = offset;
gl_Position = proj * view * vec4(position, 1.0);
}
)";
std::string text_geom_shader =
R"(#version 150 core
layout(points) in;
layout(triangle_strip, max_vertices = 4) out;
out vec2 gTexCoord;
uniform mat4 view;
uniform mat4 proj;
uniform vec2 CellSize;
uniform vec2 CellOffset;
uniform vec2 RenderSize;
uniform vec2 RenderOrigin;
uniform float TextShiftFactor;
in int vCharacter[1];
in float vOffset[1];
void main()
{
// Code taken from https://prideout.net/strings-inside-vertex-buffers
// Determine the final quad's position and size:
vec4 P = gl_in[0].gl_Position + vec4( vOffset[0]*TextShiftFactor, 0.0, 0.0, 0.0 ); // 0.04
vec4 U = vec4(1, 0, 0, 0) * RenderSize.x; // 1.0
vec4 V = vec4(0, 1, 0, 0) * RenderSize.y; // 1.0
// Determine the texture coordinates:
int letter = vCharacter[0]; // used to be the character
letter = clamp(letter - 32, 0, 96);
int row = letter / 16 + 1;
int col = letter % 16;
float S0 = CellOffset.x + CellSize.x * col;
float T0 = CellOffset.y + 1 - CellSize.y * row;
float S1 = S0 + CellSize.x - CellOffset.x;
float T1 = T0 + CellSize.y;
// Output the quad's vertices:
gTexCoord = vec2(S0, T1); gl_Position = P - U - V; EmitVertex();
gTexCoord = vec2(S1, T1); gl_Position = P + U - V; EmitVertex();
gTexCoord = vec2(S0, T0); gl_Position = P - U + V; EmitVertex();
gTexCoord = vec2(S1, T0); gl_Position = P + U + V; EmitVertex();
EndPrimitive();
}
)";
std::string text_frag_shader =
R"(#version 330
out vec4 outColor;
in vec2 gTexCoord;
uniform sampler2D font_atlas;
uniform vec3 TextColor;
void main()
{
float A = texture(font_atlas, gTexCoord).r;
outColor = vec4(TextColor, A);
}
)";
init_buffers();
init_text_rendering();
create_shader_program(
mesh_vertex_shader_string,
mesh_fragment_shader_string,
{},
shader_mesh);
create_shader_program(
overlay_vertex_shader_string,
overlay_fragment_shader_string,
{},
shader_overlay_lines);
create_shader_program(
overlay_vertex_shader_string,
overlay_point_fragment_shader_string,
{},
shader_overlay_points);
create_shader_program(
text_geom_shader,
text_vert_shader,
text_frag_shader,
{},
shader_text);
}
IGL_INLINE void igl::opengl::MeshGL::free()
{
const auto free = [](GLuint & id)
{
if(id)
{
destroy_shader_program(id);
id = 0;
}
};
if (is_initialized)
{
free(shader_mesh);
free(shader_overlay_lines);
free(shader_overlay_points);
free(shader_text);
free_buffers();
}
}
|