VIZ++ Class: OpenGLShader
|
Source code
/*
* OpenGLShader.h
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
//2016/07/30 Added a new constructor OpenGLShader(GLenum type, const char* filename)
//2016/08/01 Added some methods to get shader object information.
#pragma once
#ifdef GL_VERSION_2_0
#include <viz++/StringT.h>
#include <viz++/ModuleFilename.h>
#include <viz++/opengl/OpenGLObject.h>
#include <viz++/CharFileReader.h>
namespace VIZ {
class OpenGLShader :public OpenGLObject {
private:
GLuint shader;
public:
OpenGLShader(GLenum type)
:OpenGLObject(),
shader(INVALID_VALUE)
{
shader = create(type);
if (shader == 0) {
throw IException("Failed to createShader: %d", (int)type);
}
}
//2016/07/30
OpenGLShader(GLenum type, const char* filename)
:OpenGLObject(),
shader(INVALID_VALUE)
{
shader = create(type);
if (shader == 0) {
throw IException("Failed to createShader: %d", (int)type);
}
char fullpath[MAX_PATH];
ModuleFileName moduleFileName;
const char* drive = moduleFileName.getDrive();
const char* dir = moduleFileName.getDir();
sprintf(fullpath, "%s%s%s", drive, dir, filename);
shaderSource(fullpath);
//Do compile automatically.
compile();
}
public:
~OpenGLShader()
{
destroy();
}
operator GLuint()
{
return shader;
}
GLuint create(GLenum type)
{
static PFNGLCREATESHADERPROC glCreateShader = NULL;
if (glCreateShader == NULL) {
glCreateShader = (PFNGLCREATESHADERPROC)load("glCreateShader");
}
return glCreateShader(type);
}
void compile()
{
static PFNGLCOMPILESHADERPROC glCompileShader = NULL;
if (glCompileShader == NULL) {
glCompileShader = (PFNGLCOMPILESHADERPROC)load("glCompileShader");
}
glCompileShader(shader);
GLint compile_ok = GL_FALSE;
getShader(GL_COMPILE_STATUS, &compile_ok);
if (compile_ok == GL_FALSE) {
destroy();
throw IException("Failed to compile a shader");
}
}
void destroy()
{
static PFNGLDELETESHADERPROC glDeleteShader = NULL;
if (glDeleteShader == NULL) {
glDeleteShader = (PFNGLDELETESHADERPROC)load("glDeleteShader");
}
if (shader > 0) {
glDeleteShader(shader);
}
shader = INVALID_VALUE;
}
void getInfoLog(GLsizei bufSize, GLsizei* length, GLchar* infoLog)
{
static PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog = NULL;
if (glGetShaderInfoLog == NULL) {
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)load("glGetShaderInfoLog");
}
glGetShaderInfoLog(shader, bufSize, length, infoLog);
}
void getShaderSource(GLsizei maxLength, GLsizei* length, GLchar* source)
{
static PFNGLGETSHADERSOURCEPROC glGetShaderSource = NULL;
if (glGetShaderSource == NULL) {
glGetShaderSource = (PFNGLGETSHADERSOURCEPROC)load("glGetShaderSource");
}
glGetShaderSource(shader, maxLength, length, source);
}
/**
pname takes one of the following values:
GL_SHADER_TYPE,
GL_DELETE_STATUS
GL_COMPILE_STATUS
GL_INFO_LOG_LENGTH
GL_SHADER_SOURCE_LENGTH
*/
void getShader(GLenum pname, GLint* param)
{
static PFNGLGETSHADERIVPROC glGetShaderiv = NULL;
if (glGetShaderiv == NULL) {
glGetShaderiv = (PFNGLGETSHADERIVPROC)load("glGetShaderiv");
}
glGetShaderiv(shader, pname, param);
}
//<added date="2016/08/01">
int getShaderType()
{
GLint value = 0;
getShader(GL_SHADER_TYPE, &value);
return value;
}
int getDeleteStatus()
{
GLint value = 0;
getShader(GL_DELETE_STATUS, &value);
return value;
}
int getCompileStatus()
{
GLint value = 0;
getShader(GL_COMPILE_STATUS, &value);
return value;
}
int getInfoLogLength()
{
GLint value = 0;
getShader(GL_INFO_LOG_LENGTH, &value);
return value;
}
int getShaderSourceLength()
{
GLint value = 0;
getShader(GL_SHADER_SOURCE_LENGTH, &value);
return value;
}
//</added>
void shaderSource(GLsizei count, const GLchar *const* string, const GLint* length)
{
static PFNGLSHADERSOURCEPROC glShaderSource = NULL;
if (glShaderSource == NULL) {
glShaderSource = (PFNGLSHADERSOURCEPROC)load("glShaderSource");
}
glShaderSource(shader, count, string, length);
}
void shaderSource(const char* filename)
{
CharFileReader reader(filename);
const char* string = reader.getData();
shaderSource(1, (const GLchar *const*)&string, NULL);
}
GLboolean isShader()
{
static PFNGLISSHADERPROC glIsShader = NULL;
if (glIsShader == NULL) {
glIsShader = (PFNGLISSHADERPROC)load("glIsShader");
}
return glIsShader(shader);
}
void getSource(StringT<char>& shaderSource)
{
GLuint maxLength = getShaderSourceLength();
GLchar* source = new GLchar[maxLength+1];
memset(source, 0, maxLength + 1);
GLsizei length = 0;
getShaderSource(maxLength, &length, source);
shaderSource.shallowCopy(source);
}
void displaySource()
{
GLuint maxLength = getShaderSourceLength();
GLchar* source = new GLchar[maxLength+1];
memset(source, 0, maxLength + 1);
GLsizei length = 0;
getShaderSource(maxLength, &length, source);
#ifdef WIN32
MessageBoxA(NULL, (const char*)source, "ShaderSource", MB_OK);
#else
::printf("ShaderSource: %s\n", (const char*)source);
#endif
delete [] source;
}
void getLogInfo(StringT<char>& logInfo)
{
GLuint maxLength = getInfoLogLength();
GLchar* info = new GLchar[maxLength+1];
memset(info, 0, maxLength + 1);
GLsizei length = 0;
getInfoLog(maxLength, &length, info);
logInfo.shallowCopy(info);
}
void displayLogInfo()
{
GLuint maxLength = getInfoLogLength();
GLchar* info = new GLchar[maxLength+1];
memset(info, 0, maxLength + 1);
GLsizei length = 0;
getInfoLog(maxLength, &length, info);
#ifdef WIN32
MessageBoxA(NULL, (const char*)info, "ShaderLogInfo", MB_OK);
#else
::printf("ShaderLogInfo: %s\n", (const char*)info);
#endif
delete [] info;
}
};
}
#endif
Last modified: 10 Feb 2017
Copyright (c) 2009-2017 Antillia.com ALL RIGHTS RESERVED.