Subversion Repositories gelsvn

Rev

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

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