Consolidated all shader loading inside its own util.

master
sigonasr2 1 year ago
parent 3f8fd07eb3
commit d493d60fe5
  1. 4
      OpenGLTemplate/fragShader.glsl
  2. 80
      OpenGLTemplate/main.cpp
  3. 56
      OpenGLTemplate/utils.h
  4. 13
      OpenGLTemplate/vertShader.glsl

@ -3,9 +3,9 @@ out vec4 color;
void main(void)
{
color=vec4(
(gl_FragCoord.x - 300) / 5.f,
(gl_FragCoord.x) / 600.f,
0.0,
5 - (gl_FragCoord.x - 300) / 5.f,
1-(gl_FragCoord.x) / 600.f,
1.0
);
}

@ -1,85 +1,45 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "utils.h"
#include "ShaderError.h"
#define numVAOs 1
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint createShaderProgram(){
GLint vertCompiled;
GLint fragCompiled;
GLint linked;
// catch errors while compiling shaders
GLuint vShader=glCreateShader(GL_VERTEX_SHADER);
GLuint fShader=glCreateShader(GL_FRAGMENT_SHADER);
std::string vertShaderStr=utils::readShaderSource("vertShader.glsl");
std::string fragShaderStr=utils::readShaderSource("fragShader.glsl");
const char*vertShaderSrc=vertShaderStr.c_str();
const char*fragShaderSrc=fragShaderStr.c_str();
glShaderSource(vShader,1,&vertShaderSrc,NULL);
glShaderSource(fShader,1,&fragShaderSrc,NULL);
glCompileShader(vShader);
ErrorCheck::checkOpenGLError();
glGetShaderiv(vShader,GL_COMPILE_STATUS,&vertCompiled);
if(vertCompiled!=1){
std::cout<<"Vertex Compilation Failed!"<<std::endl;
ErrorCheck::printShaderLog(vShader);
throw;
}
glCompileShader(fShader);
ErrorCheck::checkOpenGLError();
glGetShaderiv(fShader,GL_COMPILE_STATUS,&fragCompiled);
if(fragCompiled!=1){
std::cout<<"Fragment Compilation Failed!"<<std::endl;
ErrorCheck::printShaderLog(fShader);
throw;
}
//catch errors while linking shaders
GLuint vfProgram=glCreateProgram();
glAttachShader(vfProgram,vShader);
glAttachShader(vfProgram,fShader);
glLinkProgram(vfProgram);
ErrorCheck::checkOpenGLError();
glGetProgramiv(vfProgram,GL_LINK_STATUS,&linked);
if(linked!=1){
std::cout<<"Linking Failed!"<<std::endl;
ErrorCheck::printProgramLog(vfProgram);
}
return vfProgram;
}
void init(GLFWwindow* window) {
renderingProgram=createShaderProgram();
renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl");
glGenVertexArrays(numVAOs,vao);
glBindVertexArray(vao[0]);
}
float x=0;
float inc=0.01;
double lastTime=0;
void display(GLFWwindow* window, double currentTime) {
double elapsedTime=currentTime-lastTime;
lastTime=currentTime;
glClear(GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0.2,1);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderingProgram);
glPointSize(30);
glDrawArrays(GL_POINTS,0,1);
x+=inc;//*elapsedTime;
if(x>1||x<-1){inc*=-1;}
GLuint offsetLoc=glGetUniformLocation(renderingProgram,"offset");
glProgramUniform1f(renderingProgram,offsetLoc,x);
glDrawArrays(GL_TRIANGLES,0,3);
}
int main(void) {
if (!glfwInit()) { exit(EXIT_FAILURE); }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow* window = glfwCreateWindow(600, 600, "My First OpenGL Program!", NULL, NULL);
GLFWwindow* window = glfwCreateWindow(600, 600, "Hello Triangle!", NULL, NULL);
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); }
glfwSwapInterval(1);

@ -2,9 +2,10 @@
#include <string>
#include <fstream>
#include <GL/glew.h>
#include "ShaderError.h"
namespace utils{
inline std::string readShaderSource(const char *filePath){
class utils{
inline static std::string readShaderSource(const char *filePath){
std::string content;
std::ifstream fileStream(filePath, std::ios::in);
std::string line = "";
@ -19,4 +20,55 @@ namespace utils{
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;
}
};

@ -1,5 +1,16 @@
#version 430
uniform float offset;
void main(void)
{
gl_Position=vec4(0.0,0.0,0.0,1.0);
switch(gl_VertexID){
case 0:{
gl_Position=vec4(-0.5+offset,-0.5,0.0,1.0);
}break;
case 1:{
gl_Position=vec4(0.5+offset,-0.5,0.0,1.0);
}break;
case 2:{
gl_Position=vec4(0.0+offset,0.25,0.0,1.0);
}break;
}
}

Loading…
Cancel
Save