You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

124 lines
5.8 KiB

#pragma once
#include <string>
#include <fstream>
#include <GL/glew.h>
#include "ShaderError.h"
#include <SOIL2/SOIL2.h>
#define PI 3.14159
class utils{
inline static std::string readShaderSource(const char *filePath){
std::string content;
std::ifstream fileStream(filePath, std::ios::in);
std::string line = "";
if(fileStream.fail()){
std::cout<<"Could not open shader "<<filePath<<"!"<<std::endl;
throw;
}
while (fileStream.good()) {
getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
return content;
}
inline static void CompileShader(GLuint shader,std::string type){
GLint compiled;
glCompileShader(shader);
ErrorCheck::checkOpenGLError();
glGetShaderiv(shader,GL_COMPILE_STATUS,&compiled);
if(compiled!=1){
std::cout<<type<<" Compilation Failed!"<<std::endl;
ErrorCheck::printShaderLog(shader);
throw;
}
}
inline static void CreateShader(GLuint type,std::string shaderName,const char*source,GLuint vfProgram){
if(source!=nullptr){
GLuint vShader=glCreateShader(type);
std::string shaderFile=readShaderSource(source);
const char*vertShaderSrc=shaderFile.c_str();
glShaderSource(vShader,1,&vertShaderSrc,NULL);
CompileShader(vShader,shaderName);
glAttachShader(vfProgram,vShader);
}
};
public:
inline static GLuint createShaderProgram(const char*vertex,const char*fragment){
return createShaderProgram(vertex,nullptr,nullptr,nullptr,fragment);
}
inline static GLuint createShaderProgram(const char*vertex,const char*geometry,const char*fragment){
return createShaderProgram(vertex,nullptr,nullptr,geometry,fragment);
}
inline static GLuint createShaderProgram(const char*vertex,const char*tesselationCS,const char*tesselationES,const char*fragment){
return createShaderProgram(vertex,tesselationCS,tesselationES,nullptr,fragment);
}
inline static GLuint createShaderProgram(const char*vertex,const char*tesselationCS,const char*tesselationES,const char*geometry,const char*fragment){
GLuint vfProgram=glCreateProgram();
CreateShader(GL_VERTEX_SHADER,"Vertex",vertex,vfProgram);
CreateShader(GL_FRAGMENT_SHADER,"Fragment",fragment,vfProgram);
CreateShader(GL_TESS_CONTROL_SHADER,"Tesselation Control",tesselationCS,vfProgram);
CreateShader(GL_TESS_EVALUATION_SHADER,"Tesselation Evaluation",tesselationES,vfProgram);
CreateShader(GL_GEOMETRY_SHADER,"Geometry",geometry,vfProgram);
GLint linked;
glLinkProgram(vfProgram);
ErrorCheck::checkOpenGLError();
glGetProgramiv(vfProgram,GL_LINK_STATUS,&linked);
if(linked!=1){
std::cout<<"Linking Failed!"<<std::endl;
ErrorCheck::printProgramLog(vfProgram);
}
return vfProgram;
}
inline static float degToRad(float deg){
return deg*(PI/180);
}
inline static float radToDeg(float rad){
return rad*57.2957795130823208767;
}
inline static GLuint loadTexture(const char*file){
GLuint textureID;
textureID=SOIL_load_OGL_texture(file,SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
if(!textureID){
std::cout<<"Could not load texture file "<<file<<std::endl;
throw;
}
return textureID;
}
// GOLD material - ambient, diffuse, specular, and shininess
inline static float* goldAmbient() { static float a[4] = { 0.2473f, 0.1995f, 0.0745f, 1 }; return (float*)a; }
inline static float* goldDiffuse() { static float a[4] = { 0.7516f, 0.6065f, 0.2265f, 1 }; return (float*)a; }
inline static float* goldSpecular() { static float a[4] = { 0.6283f, 0.5558f, 0.3661f, 1 }; return (float*)a; }
inline static float goldShininess() { return 51.2f; }
// SILVER material - ambient, diffuse, specular, and shininess
inline static float* silverAmbient() { static float a[4] = { 0.1923f, 0.1923f, 0.1923f, 1 }; return (float*)a; }
inline static float* silverDiffuse() { static float a[4] = { 0.5075f, 0.5075f, 0.5075f, 1 }; return (float*)a; }
inline static float* silverSpecular() { static float a[4] = { 0.5083f, 0.5083f, 0.5083f, 1 }; return (float*)a; }
inline static float silverShininess() { return 51.2f; }
// BRONZE material - ambient, diffuse, specular, and shininess
inline static float* bronzeAmbient() { static float a[4] = { 0.2125f, 0.1275f, 0.0540f, 1 }; return (float*)a; }
inline static float* bronzeDiffuse() { static float a[4] = { 0.7140f, 0.4284f, 0.1814f, 1 }; return (float*)a; }
inline static float* bronzeSpecular() { static float a[4] = { 0.3935f, 0.2719f, 0.1667f, 1 }; return (float*)a; }
inline static float bronzeShininess() { return 25.6f; }
// JADE material - ambient, diffuse, specular, and shininess
inline static float* jadeAmbient() { static float a[4] = { 0.135f, 0.2225f, 0.1575f, 0.95f }; return (float*)a; }
inline static float* jadeDiffuse() { static float a[4] = { 0.540f, 0.89f, 0.63f, 0.95f }; return (float*)a; }
inline static float* jadeSpecular() { static float a[4] = { 0.3162f, 0.3162f, 0.3162f, 0.95f }; return (float*)a; }
inline static float jadeShininess() { return 12.8f; }
// PEARL material - ambient, diffuse, specular, and shininess
inline static float* pearlAmbient() { static float a[4] = { 0.25f, 0.2073f, 0.2073f, 0.922f }; return (float*)a; }
inline static float* pearlDiffuse() { static float a[4] = { 1.f, 0.829f, 0.829f, 0.922f }; return (float*)a; }
inline static float* pearlSpecular() { static float a[4] = { 0.2966f, 0.2966f, 0.2966f, 0.922f }; return (float*)a; }
inline static float pearlShininess() { return 11.264f; }
};