Subversion Repositories gelsvn

Rev

Rev 414 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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