Subversion Repositories gelsvn

Rev

Rev 369 | Rev 447 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 369 Rev 373
1
#ifndef __GRAPHICS_GLSL_SHADER_H__
1
#ifndef __GRAPHICS_GLSL_SHADER_H__
2
#define __GRAPHICS_GLSL_SHADER_H__
2
#define __GRAPHICS_GLSL_SHADER_H__
3
 
3
 
4
#include <GL/glew.h>
4
#include <GL/glew.h>
5
#include <string>
5
#include <string>
6
 
6
 
7
/* It is a little tricky to make shader programs in C++ using GLSL but the problems are
7
/* It is a little tricky to make shader programs in C++ using GLSL but the problems are
8
	almost all silly little things. For instance, how do you robustly read from a text 
8
	almost all silly little things. For instance, how do you robustly read from a text 
9
	file? How do you compile a shader and check for errors? In OpenGL What is the difference 
9
	file? How do you compile a shader and check for errors? In OpenGL What is the difference 
10
	between a GLSL program and a shader anyway?
10
	between a GLSL program and a shader anyway?
11
 
11
 
12
	The short answer to the last question is this: A "shader" can be either a vertex shader, 
12
	The short answer to the last question is this: A "shader" can be either a vertex shader, 
13
	a geometry shader (in OpenGL 2.0) or a fragment shader. A "program" is a linked combination
13
	a geometry shader (in OpenGL 2.0) or a fragment shader. A "program" is a linked combination
14
	of these three types of shaders. Note that you don't have to have a geometry shader.
14
	of these three types of shaders. Note that you don't have to have a geometry shader.
15
	
15
	
16
	This API attempts to obviate the need for an answer to the first two questions by providing 
16
	This API attempts to obviate the need for an answer to the first two questions by providing 
17
	an API for loading and compiling shaders and checking for errors. However, I don't
17
	an API for loading and compiling shaders and checking for errors. However, I don't
18
	include functions for creating the program, attaching shaders and linking. Why not??!
18
	include functions for creating the program, attaching shaders and linking. Why not??!
19
	
19
	
20
	Because the need for flexibility means that the API would be just as complex as just using
20
	Because the need for flexibility means that the API would be just as complex as just using
21
	the OpenGL functions directly! However, loading shaders and checking for errors in compiled shaders
21
	the OpenGL functions directly! However, loading shaders and checking for errors in compiled shaders
22
	is different. It makes sense to wrap that. It also makes sense to wrap the error checking for 
22
	is different. It makes sense to wrap that. It also makes sense to wrap the error checking for 
23
	programs that are linked, so there is a function for that too.
23
	programs that are linked, so there is a function for that too.
24
	
24
	
25
	Since shader loading and error check sandwhich the two calls needed for compilation, the most 
25
	Since shader loading and error check sandwhich the two calls needed for compilation, the most 
26
	important function in this API, create_glsl_shader, loads a shader, compiles it, checks for errors 
26
	important function in this API, create_glsl_shader, loads a shader, compiles it, checks for errors 
27
	and returns the shader handle. There is also a version which creates a shader from a string.
27
	and returns the shader handle. There is also a version which creates a shader from a string.
28
	
28
	
29
	There is some code below to illustrate usage.
29
	There is some code below to illustrate usage.
30
	
30
	
31
<code snippet>
31
<code snippet>
32
	// Create shaders directly from file
32
	// Create shaders directly from file
33
	GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, shader_path, "tri.vert");
33
	GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, shader_path, "tri.vert");
34
	GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, shader_path, "tri.geom");
34
	GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, shader_path, "tri.geom");
35
	GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, shader_path, "tri.frag");
35
	GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, shader_path, "tri.frag");
36
 
36
 
37
	// Create the program
37
	// Create the program
38
	prog_P0 = glCreateProgram();
38
	prog_P0 = glCreateProgram();
39
	
39
	
40
	// Attach all shaders
40
	// Attach all shaders
41
	if(vs) glAttachShader(prog_P0, vs);
41
	if(vs) glAttachShader(prog_P0, vs);
42
	if(gs) glAttachShader(prog_P0, gs);
42
	if(gs) glAttachShader(prog_P0, gs);
43
	if(fs) glAttachShader(prog_P0, fs);
43
	if(fs) glAttachShader(prog_P0, fs);
44
	
44
	
45
	// Specify input and output for the geometry shader. Note that this must be
45
	// Specify input and output for the geometry shader. Note that this must be
46
	// done before linking the program.
46
	// done before linking the program.
47
	glProgramParameteriEXT(prog_P0,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
47
	glProgramParameteriEXT(prog_P0,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
48
	glProgramParameteriEXT(prog_P0,GL_GEOMETRY_VERTICES_OUT_EXT,3);
48
	glProgramParameteriEXT(prog_P0,GL_GEOMETRY_VERTICES_OUT_EXT,3);
49
	glProgramParameteriEXT(prog_P0,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
49
	glProgramParameteriEXT(prog_P0,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
50
 
50
 
51
	// Link the program object and print out the info log
51
	// Link the program object and print out the info log
52
	glLinkProgram(prog_P0);
52
	glLinkProgram(prog_P0);
53
	print_glsl_program_log(prog_P0);
53
	print_glsl_program_log(prog_P0);
54
	
54
	
55
	// Install program object as part of current state
55
	// Install program object as part of current state
56
	glUseProgram(prog_P0);
56
	glUseProgram(prog_P0);
57
 
57
 
58
	// Set the value of a uniform
58
	// Set the value of a uniform
59
	glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
59
	glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
60
</code snippet>
60
</code snippet>
61
	
61
	
62
	Happy shader coding.
62
	Happy shader coding.
63
	
63
	
64
	Andreas B¾rentzen, 2007
64
	Andreas B¾rentzen, 2007
65
	
65
	
66
	*/
66
	*/
67
 
67
 
-
 
68
namespace GLGraphics
-
 
69
{
68
	/// Print the info log for a program if the status is not OK.
70
	/// Print the info log for a program if the status is not OK.
69
	void print_glsl_program_log(GLuint obj);
71
	void print_glsl_program_log(GLuint obj);
70
	
72
	
71
	/** The two arguments are concatenated to form the name with full path of a text file.
73
	/** The two arguments are concatenated to form the name with full path of a text file.
72
		This file is read and returned as a string. */
74
		This file is read and returned as a string. */
73
	const std::string read_glsl_source(const std::string& path, const std::string& file);
75
	const std::string read_glsl_source(const std::string& path, const std::string& file);
74
	
76
	
75
	/** Create a shader of type specified by the first argument from a source string given
77
	/** Create a shader of type specified by the first argument from a source string given
76
		as second argument.	Return shader handle. If there is a problem, the infolog is 
78
		as second argument.	Return shader handle. If there is a problem, the infolog is 
77
		printed and 0 is returned. */
79
		printed and 0 is returned. */
78
	GLuint create_glsl_shader(GLuint stype, const std::string& src);
80
	GLuint create_glsl_shader(GLuint stype, const std::string& src);
79
		
81
		
80
	/** Create a shader of type specified as first argument from the file indicated by the 
82
	/** Create a shader of type specified as first argument from the file indicated by the 
81
		supplied path and file name (second and third arguments) and return a shader handle. 
83
		supplied path and file name (second and third arguments) and return a shader handle. 
82
		This function is only a convenience function wrapping read_glsl_source and 
84
		This function is only a convenience function wrapping read_glsl_source and 
83
		create_glsl_shader */
85
		create_glsl_shader */
84
	GLuint create_glsl_shader(GLuint stype, const std::string& path, const std::string& file);
86
	GLuint create_glsl_shader(GLuint stype, const std::string& path, const std::string& file);
-
 
87
}
85
 
88
 
86
#endif
89
#endif
87
 
90