Subversion Repositories gelsvn

Rev

Rev 601 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 601 Rev 630
1
/* ----------------------------------------------------------------------- *
1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
5
 * ----------------------------------------------------------------------- */
6
 
6
 
7
#include <iostream>
7
#include <iostream>
8
#include "../GLGraphics/glsl_shader.h"
8
#include "../GLGraphics/glsl_shader.h"
9
#include "SinglePassWireframeRenderer.h"
9
#include "SinglePassWireframeRenderer.h"
10
 
10
 
11
using namespace CGLA;
11
using namespace CGLA;
12
using namespace std;
12
using namespace std;
13
using namespace GLGraphics;
13
using namespace GLGraphics;
14
 
14
 
15
namespace 
15
namespace 
16
{
16
{
17
	const string vp = 
17
	const string vp = 
18
	"#version 120\n"
18
	"#version 120\n"
19
	"#extension GL_EXT_gpu_shader4 : enable\n"
19
	"#extension GL_EXT_gpu_shader4 : enable\n"
20
	"varying vec4 diffuseIn;\n"
20
	"varying vec4 diffuseIn;\n"
21
	"void main(void)\n"
21
	"void main(void)\n"
22
	"{\n"
22
	"{\n"
23
	"   float ndot = dot(gl_NormalMatrix*gl_Normal,vec3(0,0,1));\n"
23
	"   float ndot = dot(gl_NormalMatrix*gl_Normal,vec3(0,0,1));\n"
24
	"   diffuseIn = vec4(0.7,0.9,1,1) * abs(ndot);\n"
24
	"   diffuseIn = vec4(0.7,0.9,1,1) * abs(ndot);\n"
25
	"   gl_Position =  ftransform();\n"
25
	"   gl_Position =  ftransform();\n"
26
	"}\n";
26
	"}\n";
27
	
27
	
28
	const string gp = 
28
	const string gp = 
29
	"#version 120\n"
29
	"#version 120\n"
30
	"#extension GL_EXT_gpu_shader4 : enable\n"
30
	"#extension GL_EXT_gpu_shader4 : enable\n"
31
	"#extension GL_EXT_geometry_shader4 : enable\n"
31
	"#extension GL_EXT_geometry_shader4 : enable\n"
32
	"\n"
32
	"\n"
33
	"uniform vec2 WIN_SCALE;\n"
33
	"uniform vec2 WIN_SCALE;\n"
34
	"varying in vec4 diffuseIn[3];\n"
34
	"varying in vec4 diffuseIn[3];\n"
35
	"varying vec4 diffuse;\n"
35
	"varying vec4 diffuse;\n"
36
	"noperspective varying vec3 dist;\n"
36
	"noperspective varying vec3 dist;\n"
37
	"void main(void)\n"
37
	"void main(void)\n"
38
	"{\n"
38
	"{\n"
39
	"  vec2 p0 = WIN_SCALE * gl_PositionIn[0].xy/gl_PositionIn[0].w;\n"
39
	"  vec2 p0 = WIN_SCALE * gl_PositionIn[0].xy/gl_PositionIn[0].w;\n"
40
	"  vec2 p1 = WIN_SCALE * gl_PositionIn[1].xy/gl_PositionIn[1].w;\n"
40
	"  vec2 p1 = WIN_SCALE * gl_PositionIn[1].xy/gl_PositionIn[1].w;\n"
41
	"  vec2 p2 = WIN_SCALE * gl_PositionIn[2].xy/gl_PositionIn[2].w;\n"
41
	"  vec2 p2 = WIN_SCALE * gl_PositionIn[2].xy/gl_PositionIn[2].w;\n"
42
	"  \n"
42
	"  \n"
43
	"  vec2 v0 = p2-p1;\n"
43
	"  vec2 v0 = p2-p1;\n"
44
	"  vec2 v1 = p2-p0;\n"
44
	"  vec2 v1 = p2-p0;\n"
45
	"  vec2 v2 = p1-p0;\n"
45
	"  vec2 v2 = p1-p0;\n"
46
	"  float area = abs(v1.x*v2.y - v1.y * v2.x);\n"
46
	"  float area = abs(v1.x*v2.y - v1.y * v2.x);\n"
47
	"\n"
47
	"\n"
48
	"  dist = vec3(area/length(v0),0,0);\n"
48
	"  dist = vec3(area/length(v0),0,0);\n"
49
	"  gl_Position = gl_PositionIn[0];\n"
49
	"  gl_Position = gl_PositionIn[0];\n"
50
	"  diffuse = diffuseIn[0];\n"
50
	"  diffuse = diffuseIn[0];\n"
51
	"  EmitVertex();\n"
51
	"  EmitVertex();\n"
52
	"	\n"
52
	"	\n"
53
	"  dist = vec3(0,area/length(v1),0);\n"
53
	"  dist = vec3(0,area/length(v1),0);\n"
54
	"  gl_Position = gl_PositionIn[1];\n"
54
	"  gl_Position = gl_PositionIn[1];\n"
55
	"  diffuse = diffuseIn[1];\n"
55
	"  diffuse = diffuseIn[1];\n"
56
	"  EmitVertex();\n"
56
	"  EmitVertex();\n"
57
	"\n"
57
	"\n"
58
	"  dist = vec3(0,0,area/length(v2));\n"
58
	"  dist = vec3(0,0,area/length(v2));\n"
59
	"  gl_Position = gl_PositionIn[2];\n"
59
	"  gl_Position = gl_PositionIn[2];\n"
60
	"  diffuse = diffuseIn[2];\n"
60
	"  diffuse = diffuseIn[2];\n"
61
	"  EmitVertex();\n"
61
	"  EmitVertex();\n"
62
	"\n"
62
	"\n"
63
	"  EndPrimitive();\n"
63
	"  EndPrimitive();\n"
64
	"}\n";
64
	"}\n";
65
	
65
	
66
	const string fp =
66
	const string fp =
67
	"#version 120\n"
67
	"#version 120\n"
68
	"#extension GL_EXT_gpu_shader4 : enable\n"
68
	"#extension GL_EXT_gpu_shader4 : enable\n"
69
	"\n"
69
	"\n"
70
	"noperspective varying vec3 dist;\n"
70
	"noperspective varying vec3 dist;\n"
71
	"varying vec4 diffuse;\n"
71
	"varying vec4 diffuse;\n"
72
	"uniform vec4 WIRE_COL;\n"
72
	"uniform vec4 WIRE_COL;\n"
73
	"\n"
73
	"\n"
74
	"void main(void)\n"
74
	"void main(void)\n"
75
	"{\n"
75
	"{\n"
76
	"	float d = min(dist[0],min(dist[1],dist[2]));\n"
76
	"	float d = min(dist[0],min(dist[1],dist[2]));\n"
77
	"	// Compute intensity\n"
77
	"	// Compute intensity\n"
78
	"\n"
78
	"\n"
79
	" 	float I = exp2(-2*d*d);\n"
79
	" 	float I = exp2(-2*d*d);\n"
80
	" 	gl_FragColor = I*WIRE_COL + (1.0 - I)*diffuse;\n"
80
	" 	gl_FragColor = I*WIRE_COL + (1.0 - I)*diffuse;\n"
81
	"}\n";
81
	"}\n";
82
	
82
	
83
}
83
}
84
 
84
 
85
 
85
 
86
namespace GLGraphics
86
namespace GLGraphics
87
{
87
{
88
	
88
	
89
	SinglePassWireframeRenderer::SinglePassWireframeRenderer()
89
	SinglePassWireframeRenderer::SinglePassWireframeRenderer()
90
	{		
90
	{		
91
		static bool was_here = false;
91
		static bool was_here = false;
92
		if(!was_here)
92
		if(!was_here)
93
		{
93
		{
94
			was_here = true;
94
			was_here = true;
95
			// Create s	haders directly from file
95
			// Create s	haders directly from file
96
			static GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vp);
96
			static GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, vp);
97
			static GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, gp);
97
			static GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, gp);
98
			static GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fp);
98
			static GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, fp);
99
			
99
			
100
			// Create the program
100
			// Create the program
101
			static GLuint _prog = glCreateProgram();
101
			static GLuint _prog = glCreateProgram();
102
			prog = _prog;
102
			prog = _prog;
103
			
103
			
104
			// Attach all shaders
104
			// Attach all shaders
105
			if(vs) glAttachShader(prog, vs);
105
			if(vs) glAttachShader(prog, vs);
106
			if(gs) glAttachShader(prog, gs);
106
			if(gs) glAttachShader(prog, gs);
107
			if(fs) glAttachShader(prog, fs);
107
			if(fs) glAttachShader(prog, fs);
108
			
108
			
109
			// Specify input and output for the geometry shader. Note that this must be
109
			// Specify input and output for the geometry shader. Note that this must be
110
			// done before linking the program.
110
			// done before linking the program.
111
			glProgramParameteriEXT(prog,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
111
			glProgramParameteriEXT(prog,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
112
			glProgramParameteriEXT(prog,GL_GEOMETRY_VERTICES_OUT_EXT,3);
112
			glProgramParameteriEXT(prog,GL_GEOMETRY_VERTICES_OUT_EXT,3);
113
			glProgramParameteriEXT(prog,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
113
			glProgramParameteriEXT(prog,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
114
			
114
			
115
			// Link the program object and print out the info log
115
			// Link the program object and print out the info log
116
			glLinkProgram(prog);
116
			glLinkProgram(prog);
117
		}
117
		}
118
	}
118
	}
119
	
119
	
120
	bool SinglePassWireframeRenderer::enable(const CGLA::Vec3f& line_col)
120
	bool SinglePassWireframeRenderer::enable(const CGLA::Vec3f& line_col)
121
	{
121
	{
122
		GLint o;
122
		GLint o;
123
		glGetIntegerv(GL_CURRENT_PROGRAM, &o);
123
		glGetIntegerv(GL_CURRENT_PROGRAM, &o);
124
		old_prog = o;
124
		old_prog = o;
125
		glUseProgram(prog);
125
		glUseProgram(prog);
126
		
126
		
127
		GLint vpdim[4];
127
		GLint vpdim[4];
128
		glGetIntegerv(GL_VIEWPORT,vpdim); 
128
		glGetIntegerv(GL_VIEWPORT,vpdim); 
129
		
129
		
130
		// Set the value of a uniform
130
		// Set the value of a uniform
131
		glUniform2f(glGetUniformLocation(prog,"WIN_SCALE"), 
131
		glUniform2f(glGetUniformLocation(prog,"WIN_SCALE"), 
132
					static_cast<float>(vpdim[2]/2), static_cast<float>(vpdim[3]/2));
132
					static_cast<float>(vpdim[2]/2), static_cast<float>(vpdim[3]/2));
133
		glUniform4f(glGetUniformLocation(prog,"WIRE_COL"), line_col[0], line_col[1], line_col[2], 0);
133
		glUniform4f(glGetUniformLocation(prog,"WIRE_COL"), line_col[0], line_col[1], line_col[2], 0);
134
		
134
		
135
		return true;
135
		return true;
136
	}
136
	}
137
	
137
	
138
	void SinglePassWireframeRenderer::disable()
138
	void SinglePassWireframeRenderer::disable()
139
	{
139
	{
140
		glUseProgram(old_prog);
140
		glUseProgram(old_prog);
141
	}
141
	}
142
	
142
	
143
}
143
}
144
 
144