parent
182fe9c28c
commit
3a2d18f731
@ -0,0 +1,39 @@ |
||||
#pragma once |
||||
#include <GL/glew.h> |
||||
#include <cstdlib> |
||||
#include <iostream> |
||||
|
||||
namespace ErrorCheck{ |
||||
inline void printShaderLog(GLuint shader) { |
||||
int len = 0; |
||||
int chWrittn = 0; |
||||
char *log; |
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); |
||||
if (len > 0) { |
||||
log = (char *)malloc(len); |
||||
glGetShaderInfoLog(shader, len, &chWrittn, log); |
||||
std::cout << "Shader Info Log: " << log << std::endl; |
||||
free(log); |
||||
} } |
||||
inline void printProgramLog(int prog) { |
||||
int len = 0; |
||||
int chWrittn = 0; |
||||
char *log; |
||||
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len); |
||||
if (len > 0) { |
||||
log = (char *)malloc(len); |
||||
glGetProgramInfoLog(prog, len, &chWrittn, log); |
||||
std::cout << "Program Info Log: " << log << std::endl; |
||||
free(log); |
||||
} } |
||||
inline bool checkOpenGLError() { |
||||
bool foundError = false; |
||||
int glErr = glGetError(); |
||||
while (glErr != GL_NO_ERROR) { |
||||
std::cout << "glError: " << glErr << std::endl; |
||||
foundError = true; |
||||
glErr = glGetError(); |
||||
} |
||||
return foundError; |
||||
} |
||||
} |
@ -1,5 +1,101 @@ |
||||
#include <GL/glew.h> |
||||
#include <GLFW/glfw3.h> |
||||
#include <iostream> |
||||
#include "ShaderError.h" |
||||
|
||||
int main(){ |
||||
std::cout<<"It works!"<<std::endl; |
||||
#define numVAOs 1 |
||||
|
||||
GLuint renderingProgram; |
||||
GLuint vao[numVAOs]; |
||||
|
||||
GLuint createShaderProgram(){ |
||||
GLint vertCompiled; |
||||
GLint fragCompiled; |
||||
GLint linked; |
||||
// catch errors while compiling shaders
|
||||
|
||||
const char*vShaderSource= |
||||
"#version 430 \n" |
||||
"void main(void) \n" |
||||
"{gl_Position=vec4(0.0,0.0,0.0,1.0);}"; |
||||
const char*fShaderSource= |
||||
"#version 430 \n" |
||||
"out vec4 color; \n" |
||||
"void main(void) \n" |
||||
"{ color=vec4((gl_FragCoord.x-300)/5.f,0.0,5-(gl_FragCoord.x-300)/5.f,1.0); }"; |
||||
|
||||
GLuint vShader=glCreateShader(GL_VERTEX_SHADER); |
||||
GLuint fShader=glCreateShader(GL_FRAGMENT_SHADER); |
||||
|
||||
glShaderSource(vShader,1,&vShaderSource,NULL); |
||||
glShaderSource(fShader,1,&fShaderSource,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(); |
||||
glGenVertexArrays(numVAOs,vao); |
||||
glBindVertexArray(vao[0]); |
||||
} |
||||
|
||||
void display(GLFWwindow* window, double currentTime) { |
||||
glUseProgram(renderingProgram); |
||||
glPointSize(30); |
||||
glDrawArrays(GL_POINTS,0,1); |
||||
} |
||||
|
||||
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); |
||||
glfwMakeContextCurrent(window); |
||||
if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); } |
||||
glfwSwapInterval(1); |
||||
|
||||
init(window); |
||||
|
||||
while (!glfwWindowShouldClose(window)) { |
||||
display(window,glfwGetTime()); |
||||
glfwSwapBuffers(window); |
||||
glfwPollEvents(); |
||||
} |
||||
|
||||
glfwDestroyWindow(window); |
||||
glfwTerminate(); |
||||
exit(EXIT_SUCCESS); |
||||
} |
Loading…
Reference in new issue