|
|
|
@ -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); |
|
|
|
|