Subversion Repositories gelsvn

Rev

Rev 414 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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