Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
386 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 "wireframe.h"
12
 
13
using namespace std;
14
using namespace GLGraphics;
15
 
16
namespace 
17
{
18
	const string vp = 
19
	"#version 120\n"
20
	"#extension GL_EXT_gpu_shader4 : enable\n"
21
	"\n"
22
	"void main(void)\n"
23
	"{\n"
24
	"   gl_Position =  ftransform();\n"
25
	"}\n";
26
 
27
	const string gp = 
28
	"#version 120\n"
29
	"#extension GL_EXT_gpu_shader4 : enable\n"
30
	"\n"
31
	"uniform vec2 WIN_SCALE;\n"
32
	"noperspective varying vec3 dist;\n"
33
	"void main(void)\n"
34
	"{\n"
35
	"  vec2 p0 = WIN_SCALE * gl_PositionIn[0].xy/gl_PositionIn[0].w;\n"
36
	"  vec2 p1 = WIN_SCALE * gl_PositionIn[1].xy/gl_PositionIn[1].w;\n"
37
	"  vec2 p2 = WIN_SCALE * gl_PositionIn[2].xy/gl_PositionIn[2].w;\n"
38
	"  \n"
39
	"  vec2 v0 = p2-p1;\n"
40
	"  vec2 v1 = p2-p0;\n"
41
	"  vec2 v2 = p1-p0;\n"
42
	"  float area = abs(v1.x*v2.y - v1.y * v2.x);\n"
43
	"\n"
44
	"  dist = vec3(area/length(v0),0,0);\n"
45
	"  gl_Position = gl_PositionIn[0];\n"
46
	"  EmitVertex();\n"
47
	"	\n"
48
	"  dist = vec3(0,area/length(v1),0);\n"
49
	"  gl_Position = gl_PositionIn[1];\n"
50
	"  EmitVertex();\n"
51
	"\n"
52
	"  dist = vec3(0,0,area/length(v2));\n"
53
	"  gl_Position = gl_PositionIn[2];\n"
54
	"  EmitVertex();\n"
55
	"\n"
56
	"  EndPrimitive();\n"
57
	"}\n";
58
 
59
	const string fp =
60
	"#version 120\n"
61
	"#extension GL_EXT_gpu_shader4 : enable\n"
62
	"\n"
63
	"noperspective varying vec3 dist;\n"
64
	"const vec4 WIRE_COL = vec4(1.0,0.0,0.0,1);\n"
65
	"const vec4 FILL_COL = vec4(1,1,1,1);\n"
66
	"\n"
67
	"void main(void)\n"
68
	"{\n"
69
	"	float d = min(dist[0],min(dist[1],dist[2]));\n"
70
	"	// Compute intensity\n"
71
	"\n"
72
	" 	float I = exp2(-2*d*d);\n"
73
	" 	gl_FragColor = I*WIRE_COL + (1.0 - I)*FILL_COL;\n"
74
	"}\n";
75
 
76
	static GLhandleARB prog_P0;
77
}
78
 
79
 
80
void initialize_wireframe_shaders()
81
{
82
	static bool washere = false;
83
	if(!washere)
84
	{
85
		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();
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);
108
	}
109
}
110
 
111
void enable_wireframe()
112
{
113
	glUseProgram(prog_P0);
114
 
115
	GLint vpdim[4];
116
	glGetIntegerv(GL_VIEWPORT,vpdim); 
117
 
118
	// Set the value of a uniform
119
	glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), 
120
              static_cast<float>(vpdim[2]/2), static_cast<float>(vpdim[3]/2));
121
}