386 |
jab |
1 |
/*
|
|
|
2 |
* wireframe.cpp
|
|
|
3 |
* GEL
|
|
|
4 |
*
|
|
|
5 |
* Created by J. Andreas Bærentzen on 05/08/08.
|
|
|
6 |
* Copyright 2008 __MyCompanyName__. All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
*/
|
|
|
9 |
#include <iostream>
|
395 |
jab |
10 |
#include <CGLA/Vec3f.h>
|
386 |
jab |
11 |
#include <GLGraphics/glsl_shader.h>
|
|
|
12 |
#include "wireframe.h"
|
395 |
jab |
13 |
#include <HMesh/FaceCirculator.h>
|
386 |
jab |
14 |
|
395 |
jab |
15 |
using namespace HMesh;
|
386 |
jab |
16 |
using namespace std;
|
395 |
jab |
17 |
using namespace CGLA;
|
386 |
jab |
18 |
using namespace GLGraphics;
|
|
|
19 |
|
|
|
20 |
namespace
|
|
|
21 |
{
|
395 |
jab |
22 |
GLuint wire_prog_hex, wire_prog_generic;
|
|
|
23 |
GLuint texid=-1;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
void initialize_generic()
|
|
|
27 |
{
|
|
|
28 |
// Create shaders directly from file
|
|
|
29 |
GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, "/Users/jab/GEL/apps/MeshEdit", "wire-generic.vert");
|
|
|
30 |
GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, "/Users/jab/GEL/apps/MeshEdit", "wire-generic.geom");
|
|
|
31 |
GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, "/Users/jab/GEL/apps/MeshEdit", "wire-generic.frag");
|
386 |
jab |
32 |
|
395 |
jab |
33 |
// Create the program
|
|
|
34 |
wire_prog_generic = glCreateProgram();
|
386 |
jab |
35 |
|
395 |
jab |
36 |
// Attach all shaders
|
|
|
37 |
if(vs) glAttachShader(wire_prog_generic, vs);
|
|
|
38 |
if(gs) glAttachShader(wire_prog_generic, gs);
|
|
|
39 |
if(fs) glAttachShader(wire_prog_generic, fs);
|
386 |
jab |
40 |
|
395 |
jab |
41 |
// Specify input and output for the geometry shader. Note that this must be
|
|
|
42 |
// done before linking the program.
|
|
|
43 |
glProgramParameteriEXT(wire_prog_generic,GL_GEOMETRY_INPUT_TYPE_EXT,GL_POINTS);
|
|
|
44 |
glProgramParameteriEXT(wire_prog_generic,GL_GEOMETRY_VERTICES_OUT_EXT,20);
|
|
|
45 |
glProgramParameteriEXT(wire_prog_generic,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
|
|
|
46 |
|
|
|
47 |
// Link the program object and print out the info log
|
|
|
48 |
glLinkProgram(wire_prog_generic);
|
|
|
49 |
|
|
|
50 |
glGenTextures(1,&texid);
|
|
|
51 |
glEnable(GL_TEXTURE_RECTANGLE_ARB);
|
|
|
52 |
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texid);
|
|
|
53 |
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
54 |
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
55 |
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
56 |
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
|
|
57 |
|
386 |
jab |
58 |
}
|
|
|
59 |
|
395 |
jab |
60 |
void initialize_hex()
|
|
|
61 |
{
|
|
|
62 |
// Create shaders directly from file
|
|
|
63 |
GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, "/Users/jab/GEL/apps/MeshEdit", "wire-hex.vert");
|
|
|
64 |
GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, "/Users/jab/GEL/apps/MeshEdit", "wire-hex.geom");
|
|
|
65 |
GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, "/Users/jab/GEL/apps/MeshEdit", "wire-hex.frag");
|
|
|
66 |
|
|
|
67 |
// Create the program
|
|
|
68 |
wire_prog_hex = glCreateProgram();
|
|
|
69 |
|
|
|
70 |
// Attach all shaders
|
|
|
71 |
if(vs) glAttachShader(wire_prog_hex, vs);
|
|
|
72 |
if(gs) glAttachShader(wire_prog_hex, gs);
|
|
|
73 |
if(fs) glAttachShader(wire_prog_hex, fs);
|
|
|
74 |
|
|
|
75 |
// Specify input and output for the geometry shader. Note that this must be
|
|
|
76 |
// done before linking the program.
|
|
|
77 |
glProgramParameteriEXT(wire_prog_hex,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES_ADJACENCY_EXT);
|
|
|
78 |
glProgramParameteriEXT(wire_prog_hex,GL_GEOMETRY_VERTICES_OUT_EXT,6);
|
|
|
79 |
glProgramParameteriEXT(wire_prog_hex,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
|
|
|
80 |
|
|
|
81 |
// Link the program object and print out the info log
|
|
|
82 |
glLinkProgram(wire_prog_hex);
|
|
|
83 |
}
|
386 |
jab |
84 |
|
|
|
85 |
void initialize_wireframe_shaders()
|
|
|
86 |
{
|
|
|
87 |
static bool washere = false;
|
|
|
88 |
if(!washere)
|
|
|
89 |
{
|
|
|
90 |
washere = true;
|
395 |
jab |
91 |
initialize_generic();
|
|
|
92 |
initialize_hex();
|
386 |
jab |
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
395 |
jab |
96 |
void draw_as_wire(Manifold& m, bool per_vertex_norms)
|
386 |
jab |
97 |
{
|
395 |
jab |
98 |
|
|
|
99 |
GLint vpdim[4];
|
|
|
100 |
glGetIntegerv(GL_VIEWPORT,vpdim);
|
386 |
jab |
101 |
|
395 |
jab |
102 |
glUseProgram(wire_prog_hex);
|
|
|
103 |
|
|
|
104 |
GLuint no_edges_attrib = glGetAttribLocationARB(wire_prog_hex, "no_edges");
|
|
|
105 |
|
|
|
106 |
// Set the value of a uniform
|
|
|
107 |
glUniform2f(glGetUniformLocation(wire_prog_hex,"WIN_SCALE"),
|
|
|
108 |
static_cast<float>(vpdim[2]/2), static_cast<float>(vpdim[3]/2));
|
|
|
109 |
|
|
|
110 |
glBegin(GL_TRIANGLES_ADJACENCY_EXT);
|
|
|
111 |
for(FaceIter f=m.faces_begin(); f != m.faces_end(); ++f)
|
|
|
112 |
{
|
|
|
113 |
glNormal3fv(normal(f).get());
|
|
|
114 |
glVertexAttrib1f(no_edges_attrib, no_edges(f));
|
|
|
115 |
FaceCirculator fc(f);
|
|
|
116 |
for(int i=0;i<6;++i)
|
|
|
117 |
{
|
|
|
118 |
glVertex3fv(fc.get_vertex()->pos.get());
|
|
|
119 |
if(!fc.end()) ++fc;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
glEnd();
|
|
|
123 |
glUseProgram(0);
|
|
|
124 |
|
|
|
125 |
/*/
|
|
|
126 |
int total_vertices = 0;
|
|
|
127 |
for(FaceIter f=m.faces_begin(); f != m.faces_end(); ++f)
|
|
|
128 |
total_vertices += no_edges(f);
|
|
|
129 |
|
|
|
130 |
vector<Vec3f> mesh_tex(ceil(total_vertices*1.01/4096)*4096);
|
|
|
131 |
vector<Vec3i> face_idx(m.no_faces());
|
|
|
132 |
vector<Vec3f> normals(m.no_faces());
|
|
|
133 |
int column=0;
|
|
|
134 |
int row=0;
|
|
|
135 |
for(FaceIter f=m.faces_begin(); f != m.faces_end(); ++f)
|
|
|
136 |
{
|
|
|
137 |
int N = no_edges(f);
|
|
|
138 |
if( (N + column) >= 4096)
|
|
|
139 |
{
|
|
|
140 |
++row;
|
|
|
141 |
column = 0;
|
|
|
142 |
}
|
|
|
143 |
face_idx.push_back(Vec3i(column, row, N));
|
|
|
144 |
normals.push_back(normal(f));
|
|
|
145 |
for(FaceCirculator fc(f); !fc.end(); ++fc)
|
|
|
146 |
{
|
|
|
147 |
mesh_tex[column + row * 4096] = fc.get_vertex()->pos;
|
|
|
148 |
++column;
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
386 |
jab |
152 |
GLint vpdim[4];
|
|
|
153 |
glGetIntegerv(GL_VIEWPORT,vpdim);
|
|
|
154 |
|
395 |
jab |
155 |
glUseProgram(wire_prog_generic);
|
|
|
156 |
|
386 |
jab |
157 |
// Set the value of a uniform
|
395 |
jab |
158 |
glUniform2f(glGetUniformLocation(wire_prog_generic,"WIN_SCALE"),
|
|
|
159 |
static_cast<float>(vpdim[2]/2), static_cast<float>(vpdim[3]/2));
|
|
|
160 |
glUniform1iARB(glGetUniformLocationARB(wire_prog_generic, "mesh_tex"),0);
|
|
|
161 |
glEnable(GL_TEXTURE_RECTANGLE_ARB);
|
|
|
162 |
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texid);
|
|
|
163 |
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGB32F_ARB,4096,row+1,0,GL_RGB,GL_FLOAT,&mesh_tex[0]);
|
|
|
164 |
|
|
|
165 |
glBegin(GL_POINTS);
|
|
|
166 |
for(int i=0;i<face_idx.size(); ++i)
|
|
|
167 |
{
|
|
|
168 |
glNormal3fv(normals[i].get());
|
|
|
169 |
glVertex3iv((const GLint*)face_idx[i].get());
|
|
|
170 |
}
|
|
|
171 |
glEnd();
|
|
|
172 |
glUseProgram(0);
|
|
|
173 |
glDisable(GL_TEXTURE_RECTANGLE_ARB);
|
|
|
174 |
*/
|
386 |
jab |
175 |
}
|
395 |
jab |
176 |
|