Create basic vertex and fragment shader. Error handling.

master
sigonasr2 1 year ago
parent 182fe9c28c
commit 3a2d18f731
  1. 3
      OpenGLTemplate/OpenGLTemplate.vcxproj
  2. 5
      OpenGLTemplate/OpenGLTemplate.vcxproj.filters
  3. 39
      OpenGLTemplate/ShaderError.h
  4. 100
      OpenGLTemplate/main.cpp

@ -141,6 +141,9 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="ShaderError.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

@ -19,4 +19,9 @@
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="ShaderError.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project> </Project>

@ -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 <iostream>
#include "ShaderError.h"
int main(){ #define numVAOs 1
std::cout<<"It works!"<<std::endl;
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…
Cancel
Save