00001
00002
00003
00004
00005
00006 #include "glew.h"
00007 #include "GL/glu.h"
00008 #pragma comment(lib,"OpenGL32.lib")
00009 #pragma comment(lib,"glu32.lib")
00010 #pragma comment(lib,"glew32.lib")
00011
00012 #include <QWidget>
00013 #include "GLProgram.h"
00014 #include "CGLSurface.h"
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 CGLProgram::CGLProgram(void){
00026 Reset();
00027 }
00028
00029
00030 CGLProgram::~CGLProgram(void){
00031 Reset();
00032 }
00033
00034
00035 void CGLProgram::SetRenderer(CGLSurface * const renderer)
00036 {
00037 pCurrentRenderer = renderer;
00038
00039 if(programID == 0){
00040
00041 pCurrentRenderer->lockGLContext();
00042
00043 GLenum err = glewInit();
00044 if (GLEW_OK != err){
00045
00046
00047 }
00048 programID = glCreateProgram();
00049
00050 pCurrentRenderer->unlockGLContext();
00051 }
00052 }
00053
00054 unsigned int CGLProgram::CreateShader(int shaderType)
00055 {
00056 pCurrentRenderer->lockGLContext();
00057 unsigned int id = glCreateShader(shaderType);
00058 pCurrentRenderer->unlockGLContext();
00059
00060 return id;
00061 }
00062
00063
00064 void CGLProgram::AttachShader(unsigned int sObjectID)
00065 {
00066 pCurrentRenderer->lockGLContext();
00067 glAttachShader(programID,sObjectID);
00068 pCurrentRenderer->unlockGLContext();
00069 }
00070
00071
00072 char * CGLProgram::Compile(unsigned int sObjectID,const char **source,int nmbOfLines)
00073 {
00074 pCurrentRenderer->lockGLContext();
00075 glShaderSource(sObjectID,nmbOfLines,source,NULL);
00076 glCompileShader(sObjectID);
00077
00078
00079 GLint status;
00080 glGetShaderiv(sObjectID,GL_COMPILE_STATUS,&status);
00081
00082 char * newMessage = NULL;
00083
00084 if(!status){
00085
00086 GLint logLength;
00087 glGetShaderiv(sObjectID,GL_INFO_LOG_LENGTH,&logLength);
00088
00089 newMessage = new char[logLength];
00090 glGetShaderInfoLog(sObjectID,logLength,NULL,newMessage);
00091 compileMessages.push_back(newMessage);
00092 }
00093 pCurrentRenderer->unlockGLContext();
00094
00095 return newMessage;
00096 }
00097
00098
00099 void CGLProgram::Detach(unsigned int sObjectID)
00100 {
00101 pCurrentRenderer->lockGLContext();
00102 glDetachShader(programID,sObjectID);
00103 pCurrentRenderer->unlockGLContext();
00104 }
00105
00106
00107 char * CGLProgram::Link()
00108 {
00109
00110 pCurrentRenderer->lockGLContext();
00111
00112 char * newMessage = NULL;
00113
00114
00115 int nmbOfShaders;
00116 glGetProgramiv(programID,GL_ATTACHED_SHADERS,&nmbOfShaders);
00117
00118 if(nmbOfShaders == 0){
00119
00120 linkSuccess = false;
00121
00122 }else{
00123
00124 glLinkProgram(programID);
00125
00126
00127 GLint status;
00128 glGetProgramiv(programID,GL_LINK_STATUS,&status);
00129
00130 if(!status){
00131
00132 GLint logLength;
00133 glGetProgramiv(programID,GL_INFO_LOG_LENGTH,&logLength);
00134
00135 newMessage = new char[logLength],
00136 glGetProgramInfoLog(programID,logLength,NULL,newMessage);
00137 linkMessages.push_back(newMessage);
00138 }
00139
00140
00141
00142 if(status){
00143 linkSuccess = true;
00144 }else{
00145 linkSuccess = false;
00146 }
00147 }
00148 pCurrentRenderer->unlockGLContext();
00149 pCurrentRenderer->update();
00150 return newMessage;
00151 }
00152
00153 bool CGLProgram::LinkSuccess()
00154 {
00155 return linkSuccess;
00156 }
00157
00158 bool CGLProgram::InUse()
00159 {
00160 int idInUse;
00161 glGetIntegerv(GL_CURRENT_PROGRAM,&idInUse);
00162 return (bool)idInUse;
00163 }
00164
00165 void CGLProgram::Use(bool tf)
00166 {
00167 if(tf){
00168 glUseProgram(programID);
00169 }else{
00170 glUseProgram(0);
00171 }
00172 }
00173
00174
00175
00176
00177
00178 void CGLProgram::Reset()
00179 {
00180 programID = 0;
00181 pCurrentRenderer = NULL;
00182 linkSuccess = false;
00183
00184 for(int i = 0 ; i < compileMessages.size() ; i++){
00185 delete [] compileMessages[i];
00186 }
00187 compileMessages.clear();
00188
00189 for(int i = 0 ; i < linkMessages.size() ; i++){
00190 delete [] linkMessages[i];
00191 }
00192 linkMessages.clear();
00193 }