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