Subversion Repositories gelsvn

Rev

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