Subversion Repositories gelsvn

Rev

Rev 393 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 393 Rev 395
Line 5... Line 5...
5
 *  Created by J. Andreas Bærentzen on 05/08/08.
5
 *  Created by J. Andreas Bærentzen on 05/08/08.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
7
 *
7
 *
8
 */
8
 */
9
#include <iostream>
9
#include <iostream>
-
 
10
#include <CGLA/Vec3f.h>
10
#include <GLGraphics/glsl_shader.h>
11
#include <GLGraphics/glsl_shader.h>
11
#include "wireframe.h"
12
#include "wireframe.h"
-
 
13
#include <HMesh/FaceCirculator.h>
12
 
14
 
-
 
15
using namespace HMesh;
13
using namespace std;
16
using namespace std;
-
 
17
using namespace CGLA;
14
using namespace GLGraphics;
18
using namespace GLGraphics;
15
 
19
 
16
namespace 
20
namespace 
17
{
21
{
18
	const string vp = 
-
 
19
	"#version 120\n"
-
 
20
	"#extension GL_EXT_gpu_shader4 : enable\n"
22
	GLuint wire_prog_hex, wire_prog_generic;
21
	"\n"
-
 
22
	"void main(void)\n"
23
	GLuint texid=-1;
23
	"{\n"
-
 
24
	"   gl_Position =  ftransform();\n"
-
 
25
	"}\n";
24
}
26
	
25
 
27
	const string gp = 
26
void initialize_generic()
28
	"#version 120\n"
-
 
29
	"#extension GL_EXT_gpu_shader4 : enable\n"
-
 
30
	"\n"
27
{
31
	"uniform vec2 WIN_SCALE;\n"
-
 
32
	"noperspective varying vec3 dist;\n"
28
	// Create shaders directly from file
33
	"void main(void)\n"
-
 
34
	"{\n"
-
 
35
	"  vec2 p0 = WIN_SCALE * gl_PositionIn[0].xy/gl_PositionIn[0].w;\n"
29
	GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, "/Users/jab/GEL/apps/MeshEdit", "wire-generic.vert");
36
	"  vec2 p1 = WIN_SCALE * gl_PositionIn[1].xy/gl_PositionIn[1].w;\n"
30
	GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, "/Users/jab/GEL/apps/MeshEdit", "wire-generic.geom");
37
	"  vec2 p2 = WIN_SCALE * gl_PositionIn[2].xy/gl_PositionIn[2].w;\n"
31
	GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, "/Users/jab/GEL/apps/MeshEdit", "wire-generic.frag");
38
	"  \n"
32
	
39
	"  vec2 v0 = p2-p1;\n"
-
 
40
	"  vec2 v1 = p2-p0;\n"
-
 
41
	"  vec2 v2 = p1-p0;\n"
33
	// Create the program
42
	"  float area = abs(v1.x*v2.y - v1.y * v2.x);\n"
34
	wire_prog_generic = glCreateProgram();
43
	"\n"
35
	
44
	"  dist = vec3(area/length(v0),0,0);\n"
36
	// Attach all shaders
45
	"  gl_Position = gl_PositionIn[0];\n"
37
	if(vs) glAttachShader(wire_prog_generic, vs);
46
	"  EmitVertex();\n"
-
 
47
	"	\n"
-
 
48
	"  dist = vec3(0,area/length(v1),0);\n"
38
	if(gs) glAttachShader(wire_prog_generic, gs);
49
	"  gl_Position = gl_PositionIn[1];\n"
39
	if(fs) glAttachShader(wire_prog_generic, fs);
50
	"  EmitVertex();\n"
-
 
51
	"\n"
40
	
52
	"  dist = vec3(0,0,area/length(v2));\n"
41
	// Specify input and output for the geometry shader. Note that this must be
53
	"  gl_Position = gl_PositionIn[2];\n"
42
	// done before linking the program.
54
	"  EmitVertex();\n"
43
	glProgramParameteriEXT(wire_prog_generic,GL_GEOMETRY_INPUT_TYPE_EXT,GL_POINTS);
55
	"\n"
-
 
56
	"  EndPrimitive();\n"
44
	glProgramParameteriEXT(wire_prog_generic,GL_GEOMETRY_VERTICES_OUT_EXT,20);
57
	"}\n";
-
 
-
 
45
	glProgramParameteriEXT(wire_prog_generic,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
58
	
46
	
59
	const string fp =
47
	// Link the program object and print out the info log
60
	"#version 120\n"
-
 
61
	"#extension GL_EXT_gpu_shader4 : enable\n"
48
	glLinkProgram(wire_prog_generic);
62
	"\n"
49
	
63
	"noperspective varying vec3 dist;\n"
50
	glGenTextures(1,&texid);
64
	"const vec4 WIRE_COL = vec4(1.0,0.0,0.0,1);\n"
51
	glEnable(GL_TEXTURE_RECTANGLE_ARB);
65
	"const vec4 FILL_COL = vec4(1,1,1,1);\n"
52
	glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texid);
66
	"\n"
-
 
67
	"void main(void)\n"
-
 
68
	"{\n"
-
 
69
	"	float d = min(dist[0],min(dist[1],dist[2]));\n"
53
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
70
	"	// Compute intensity\n"
54
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
71
	"\n"
-
 
72
	" 	float I = exp2(-2*d*d);\n"
55
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
73
	" 	gl_FragColor = I*WIRE_COL + (1.0 - I)*FILL_COL;\n"
56
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
74
	"}\n";
-
 
75
	
57
	
76
	static GLhandleARB prog_P0;
-
 
77
}
58
}
78
 
59
 
-
 
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
}
79
 
84
 
80
void initialize_wireframe_shaders()
85
void initialize_wireframe_shaders()
81
{
86
{
82
	static bool washere = false;
87
	static bool washere = false;
83
	if(!washere)
88
	if(!washere)
84
	{
89
	{
85
		washere = true;
90
		washere = true;
86
		
-
 
87
		// Create s	haders directly from file
-
 
88
		GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vp);
-
 
89
		GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, gp);
-
 
90
		GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fp);
-
 
91
		
-
 
92
		// Create the program
-
 
93
		prog_P0 = glCreateProgram();
91
		initialize_generic();
94
		
-
 
95
		// Attach all shaders
-
 
96
		if(vs) glAttachShader(prog_P0, vs);
-
 
97
		if(gs) glAttachShader(prog_P0, gs);
-
 
98
		if(fs) glAttachShader(prog_P0, fs);
-
 
99
		
-
 
100
		// Specify input and output for the geometry shader. Note that this must be
-
 
101
		// done before linking the program.
-
 
102
		glProgramParameteriEXT(prog_P0,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
-
 
103
		glProgramParameteriEXT(prog_P0,GL_GEOMETRY_VERTICES_OUT_EXT,3);
-
 
104
		glProgramParameteriEXT(prog_P0,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
-
 
105
		
-
 
106
		// Link the program object and print out the info log
-
 
107
		glLinkProgram(prog_P0);
92
		initialize_hex();
108
	}
93
	}
109
}
94
}
110
 
95
 
111
void enable_wireframe()
96
void draw_as_wire(Manifold& m, bool per_vertex_norms)
112
{
97
{
-
 
98
 
-
 
99
	GLint vpdim[4];
-
 
100
	glGetIntegerv(GL_VIEWPORT,vpdim); 
-
 
101
	
-
 
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();
113
	glUseProgram(prog_P0);
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
	}
114
	
151
	
115
	GLint vpdim[4];
152
	GLint vpdim[4];
116
	glGetIntegerv(GL_VIEWPORT,vpdim); 
153
	glGetIntegerv(GL_VIEWPORT,vpdim); 
117
	
154
	
-
 
155
	glUseProgram(wire_prog_generic);
-
 
156
	
118
	// Set the value of a uniform
157
	// Set the value of a uniform
119
	glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), 
158
	glUniform2f(glGetUniformLocation(wire_prog_generic,"WIN_SCALE"), 
120
              static_cast<float>(vpdim[2]/2), static_cast<float>(vpdim[3]/2));
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
*/
121
}
175
}
-
 
176