]> git.cworth.org Git - vogl/blob - src/voglcommon/vogl_shader_utils.h
Initial vogl checkin
[vogl] / src / voglcommon / vogl_shader_utils.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 // File: vogl_shader_utils.h
27 #ifndef VOGL_SHADER_UTILS_H
28 #define VOGL_SHADER_UTILS_H
29
30 #include "vogl_core.h"
31 #include "vogl_common.h"
32 #include "vogl_matrix.h"
33
34 GLuint vogl_create_program(const char *pVertex_shader, const char *pFragment_shader);
35
36 class vogl_scoped_program_binder
37 {
38     VOGL_NO_COPY_OR_ASSIGNMENT_OP(vogl_scoped_program_binder);
39
40 public:
41     vogl_scoped_program_binder(GLuint new_prog) :
42         m_prev_program(0)
43     {
44         GL_ENTRYPOINT(glGetIntegerv)(GL_CURRENT_PROGRAM, reinterpret_cast<GLint *>(&m_prev_program));
45         VOGL_CHECK_GL_ERROR;
46
47         GL_ENTRYPOINT(glUseProgram)(new_prog);
48         VOGL_CHECK_GL_ERROR;
49     }
50
51     ~vogl_scoped_program_binder()
52     {
53         GL_ENTRYPOINT(glUseProgram)(m_prev_program);
54         VOGL_CHECK_GL_ERROR;
55     }
56
57 private:
58     GLuint m_prev_program;
59 };
60
61 class vogl_simple_gl_program
62 {
63     VOGL_NO_COPY_OR_ASSIGNMENT_OP(vogl_simple_gl_program);
64
65 public:
66     vogl_simple_gl_program();
67     vogl_simple_gl_program(const char *pVertex_shader, const char *pFragment_shader);
68     ~vogl_simple_gl_program();
69
70     bool init(const char *pVertex_shader, const char *pFragment_shader);
71     void deinit();
72
73     bool is_valid() const { return m_program != 0; }
74
75     GLuint get_handle() const { return m_program; }
76
77     GLint get_uniform_location(const char *pName) const;
78     bool has_uniform(const char *pName) const;
79
80     void set_uniform(const char *pName, int s);
81     void set_uniform(const char *pName, uint s);
82     void set_uniform(const char *pName, float s);
83
84     void set_uniform(const char *pName, const vec2I &v);
85     void set_uniform(const char *pName, const vec2F &v);
86
87     void set_uniform(const char *pName, const vec3I &v);
88     void set_uniform(const char *pName, const vec3F &v);
89
90     void set_uniform(const char *pName, const vec4I &v);
91     void set_uniform(const char *pName, const vec4F &v);
92
93     void set_uniform(const char *pName, const matrix33F &m, bool transpose);
94     void set_uniform(const char *pName, const matrix44F &m, bool transpose);
95
96 private:
97     GLuint m_program;
98 };
99
100 #endif // VOGL_SHADER_UTILS_H