Blame view

stim/gl/rts_glShaderObject.h 2.6 KB
7006df5f   David Mayerich   reformat of direc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  #ifndef RTS_GLSHADERS

  #define RTS_GLSHADERS

  

  #include <GL/glew.h>

  //#include "windows.h"

  #include <GL/gl.h>

  #include "rtsSourceCode.h"

  

  class rts_glShaderObject

  {

  private:

  	void init()

  	{

  		id = 0;

  		compiled = false;

  		type = GL_FRAGMENT_SHADER;

  	}

  public:

  	bool compiled;

  	GLenum type;

  	rtsSourceCode source;

  	GLuint id;

  	string log;

  

  	rts_glShaderObject(GLenum type, const char* filename)

  	{

  		init();					//initialize the shader

  		SetType(type);	//set the shader type

  		LoadSource(filename);	//load the source code

  	}

  	rts_glShaderObject(GLenum type, rtsSourceCode sourceCode)

  	{

  		init();					//initialize the shader

  		SetType(type);	//set the shader type

  		source = sourceCode;

  	}

  	rts_glShaderObject()

  	{

  		init();

  	}

  	rts_glShaderObject(GLenum type)

  	{

  		init();

  		SetType(type);

  	}

  	void LoadSource(const char* filename)

  	{

  		source = rtsSourceCode(filename);	//get the shader source code

  

  	}

  	void SetType(GLenum type)

  	{

  		if(id != 0)					//if a shader currently exists, delete it

  		{

  			glDeleteShader(id);

  			id = 0;

  		}

  		type = type;

  		id = glCreateShader(type);		//create a shader object

  		if(id == 0)						//if a shader was not created, log an error

  		{

  			log = "Error getting shader ID from OpenGL";

  			return;

  		}

  	}

  	void UploadSource()

  	{

  		//create the structure for the shader source code

  		GLsizei count = source.source.size();

  		GLchar** code_string = new GLchar*[count];

  		GLint* length = new GLint[count];

  		for(int l = 0; l<count; l++)	//for each line of code

  		{

  			length[l] = source.source[l].size();

  			code_string[l] = new GLchar[length[l]];	//copy the string into a new structure

  			source.source[l].copy(code_string[l], (unsigned int)length[l]);

  

  		}

  		glShaderSource(id, count, (const GLchar**)code_string, length);		//attach the shader source

  	}

  	void Compile()

  	{

  		/*

  		This function compiles the shader source code, records any errors to a log, and sets the compiled flag.

  		*/

  		//send the source code to the GPU

  		UploadSource();

  

  		//compile the shader

  		glCompileShader(id);												//compile the shader

  		GLint compile_status;

  		glGetShaderiv(id, GL_COMPILE_STATUS, &compile_status);				//get the compile status

  		if(compile_status != GL_TRUE)	//if there was an error

  		{

  			GLchar buffer[1000];		//create a log buffer

  			GLsizei length;

  			glGetShaderInfoLog(id, 1000, &length, buffer);	//get the log

  			log = buffer;

  			compiled = false;

  		}

  		else

  			compiled = true;

  

  	}

  	void PrintLog()

  	{

  		cout<<log;
  		if(log.size() != 0) cout<<endl;

  	}

  	void Clean(){if(id != 0) glDeleteShader(id);}

  };

  

  

  

  #endif