Add ContourLineExtractor to Sandbox
[SARndbox.git] / ShaderHelper.cpp
blobd139de8a05b7670dbd9aebf94ce435eae3e8bec8
1 /***********************************************************************
2 ShaderHelper - Helper functions to create GLSL shaders from text files.
3 Copyright (c) 2014 Oliver Kreylos
5 This file is part of the Augmented Reality Sandbox (SARndbox).
7 The Augmented Reality Sandbox is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Augmented Reality Sandbox is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with the Augmented Reality Sandbox; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ***********************************************************************/
22 #include "ShaderHelper.h"
24 #include <string>
25 #include <GL/gl.h>
26 #include <GL/Extensions/GLARBFragmentShader.h>
27 #include <GL/Extensions/GLARBShaderObjects.h>
28 #include <GL/Extensions/GLARBVertexShader.h>
30 #include "Config.h"
32 GLhandleARB compileVertexShader(const char* vertexShaderFileName) {
33 /* Construct the full shader source file name: */
34 std::string fullShaderFileName = CONFIG_SHADERDIR;
35 fullShaderFileName.push_back('/');
36 fullShaderFileName.append(vertexShaderFileName);
37 fullShaderFileName.append(".vs");
39 /* Compile and return the vertex shader: */
40 return glCompileVertexShaderFromFile(fullShaderFileName.c_str());
43 GLhandleARB compileFragmentShader(const char* fragmentShaderFileName) {
44 /* Construct the full shader source file name: */
45 std::string fullShaderFileName = CONFIG_SHADERDIR;
46 fullShaderFileName.push_back('/');
47 fullShaderFileName.append(fragmentShaderFileName);
48 fullShaderFileName.append(".fs");
50 /* Compile and return the fragment shader: */
51 return glCompileFragmentShaderFromFile(fullShaderFileName.c_str());
54 GLhandleARB linkVertexAndFragmentShader(const char* shaderFileName) {
55 /* Compile the vertex and fragment shaders: */
56 GLhandleARB vertexShader = compileVertexShader(shaderFileName);
57 GLhandleARB fragmentShader = compileFragmentShader(shaderFileName);
59 /* Link the shader program: */
60 GLhandleARB shaderProgram = glLinkShader(vertexShader, fragmentShader);
62 /* Release the compiled shaders (won't get deleted until shader program is released): */
63 glDeleteObjectARB(vertexShader);
64 glDeleteObjectARB(fragmentShader);
66 return shaderProgram;