• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KGLLib

shader.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Rivo Laks <rivolaks@hot.ee>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either 
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public 
00015  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "shader.h"
00019 
00020 #include <QFile>
00021 #include <QtDebug>
00022 
00023 namespace KGLLib
00024 {
00025 
00026 Shader::Shader(GLenum type)
00027 {
00028     init(type);
00029 }
00030 
00031 Shader::Shader(GLenum type, const QString& filename)
00032 {
00033     init(type);
00034 
00035     QFile f(filename);
00036     if (!f.open(QIODevice::ReadOnly)) {
00037         qCritical() << "Shader::Shader(QString): Can't open file" << filename << "for reading";
00038         return;
00039     }
00040     setSource(f.readAll());
00041     compile();
00042 }
00043 
00044 Shader::~Shader()
00045 {
00046     glDeleteShader(glId());
00047     delete[] mCompileLog;
00048 }
00049 
00050 void Shader::init(GLenum type)
00051 {
00052     mType = type;
00053     mValid = false;
00054     mCompiled = false;
00055     mCompileLog = false;
00056     mGLId = glCreateShader(mType);
00057 }
00058 
00059 void Shader::setSource(const QString& source)
00060 {
00061     setSource(source.toLatin1());
00062 }
00063 
00064 void Shader::setSource(const QByteArray& source)
00065 {
00066     if (isCompiled()) {
00067         qCritical() << "Shader::setSource(): Can't set source for compiled shaders";
00068         return;
00069     }
00070     const char* src = source.data();
00071     glShaderSource(glId(), 1, &src, NULL);
00072 }
00073 
00074 bool Shader::compile()
00075 {
00076     if (isCompiled()) {
00077         qCritical() << "Shader::compile(): Can't compile a shader twice";
00078         return false;
00079     }
00080     // Compile the shader
00081     glCompileShader(glId());
00082     mCompiled = true;
00083     // Make sure it compiled correctly
00084     int compiled;
00085     glGetShaderiv(glId(), GL_COMPILE_STATUS, &compiled);
00086     mValid = compiled;
00087     // Get info log
00088     GLsizei logsize, logarraysize;
00089     glGetShaderiv(glId(), GL_INFO_LOG_LENGTH, &logarraysize);
00090     mCompileLog = new char[logarraysize];
00091     glGetShaderInfoLog(glId(), logarraysize, &logsize, mCompileLog);
00092     // Output the log if compilation failed
00093     if (!mValid) {
00094         qCritical() << "Shader::compile(): Couldn't compile shader. Log follows:" << endl << mCompileLog;
00095     }
00096     if (!logsize) {
00097         delete[] mCompileLog;
00098         mCompileLog = 0;
00099     }
00100     return mValid;
00101  }
00102 
00103 VertexShader::VertexShader() : Shader(GL_VERTEX_SHADER)
00104 {
00105 }
00106 
00107 VertexShader::VertexShader(const QString& filename) : Shader(GL_VERTEX_SHADER, filename)
00108 {
00109 }
00110 
00111 FragmentShader::FragmentShader() : Shader(GL_FRAGMENT_SHADER)
00112 {
00113 }
00114 
00115 FragmentShader::FragmentShader(const QString& filename) : Shader(GL_FRAGMENT_SHADER, filename)
00116 {
00117 }
00118 
00119 
00120 }
00121 

KGLLib

Skip menu "KGLLib"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KGLLib
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal