diff options
Diffstat (limited to '07-july/src/graphics')
| -rw-r--r-- | 07-july/src/graphics/shaders.c | 134 | ||||
| -rw-r--r-- | 07-july/src/graphics/shaders.h | 46 | ||||
| -rw-r--r-- | 07-july/src/graphics/video.c | 14 | ||||
| -rw-r--r-- | 07-july/src/graphics/video.h | 7 | ||||
| -rw-r--r-- | 07-july/src/graphics/window.c | 44 | ||||
| -rw-r--r-- | 07-july/src/graphics/window.h | 20 |
6 files changed, 265 insertions, 0 deletions
diff --git a/07-july/src/graphics/shaders.c b/07-july/src/graphics/shaders.c new file mode 100644 index 0000000..ef4ec0a --- /dev/null +++ b/07-july/src/graphics/shaders.c @@ -0,0 +1,134 @@ +#include "shaders.h" +#include "../util/util.h" + +#include <stdlib.h> + +static void CompileShader(const char* source, GLuint shaderID) +{ + glShaderSource(shaderID, 1, &source, 0); + glCompileShader(shaderID); + GLint error; + glGetShaderiv(shaderID, GL_COMPILE_STATUS, &error); + if(error != GL_TRUE) + { + GLint logLenth; + glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLenth); + GLchar buffer[logLenth]; + glGetShaderInfoLog(shaderID, logLenth, &logLenth, buffer); + glDeleteShader(shaderID); + Util_FatalError(buffer); + } +} + + +GLuint Shader_CompileShaders(const char* vertexShader, const char* fragmentShader) +{ + char* vertexShaderSource = Util_LoadFile(vertexShader); + char* fragmentShaderSource = Util_LoadFile(fragmentShader); + + GLuint vs = 0, fs = 0, program; + vs = glCreateShader(GL_VERTEX_SHADER); + fs = glCreateShader(GL_FRAGMENT_SHADER); + + if(vs == 0 || fs == 0) + Util_FatalError("Shaders could not be created\n"); + + program = glCreateProgram(); + + CompileShader(vertexShaderSource, vs); + CompileShader(fragmentShaderSource, fs); + + glAttachShader(program, vs); + glAttachShader(program, fs); + + glLinkProgram(program); + + GLint error; + glGetProgramiv(program, GL_LINK_STATUS, &error); + + if(error != GL_TRUE) + { + GLint logLength; + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength); + + GLchar buffer[logLength]; + glGetProgramInfoLog(program, logLength, &logLength, buffer); + + glDeleteProgram(program); + glDeleteShader(vs); + glDeleteShader(fs); + + Util_FatalError(buffer); + } + + glDetachShader(program, vs); + glDetachShader(program, fs); + glDeleteShader(vs); + glDeleteShader(fs); + + free(vertexShaderSource); + free(fragmentShaderSource); + + return program; +} + +GLint Shader_GetUniformLocation( GLuint programID, const char* uniformName ) +{ + GLint u = glGetUniformLocation(programID, uniformName); + if(u == GL_INVALID_INDEX) + Util_FatalError("Uniform variable could not be found!"); + else + return u; + + return 0; +} + +GLint Shader_GetAttribLocation ( GLuint programID, const char* attributeName ) +{ + GLint attrLocation = glGetAttribLocation(programID, attributeName); + if(attrLocation < 0) + Util_FatalError("Attribute could not be found\n"); + return attrLocation; +} + +void Shader_Destroy(GLuint programID) +{ + glUseProgram(0); + glDeleteProgram(programID); +} + +void Shader_SetUniformMat4( GLuint programID, const char* name, const float *matrix ) +{ + GLint location = Shader_GetUniformLocation(programID, name); + glUniformMatrix4fv(location, 1, GL_FALSE, matrix); +} + +void Shader_SetUniformFloat( GLuint programID, const char* name, const float val ) +{ + GLint location = Shader_GetUniformLocation(programID, name); + glUniform1f(location, val); +} + +void Shader_SetUniformVec2( GLuint programID, const char* name, const float vec[2] ) +{ + GLint location = Shader_GetUniformLocation(programID, name); + glUniform2fv(location, 1, vec); +} + +void Shader_SetUniformVec3( GLuint programID, const char* name, const float vec[3] ) +{ + GLint location = Shader_GetUniformLocation(programID, name); + glUniform3fv(location, 1, vec); +} + +void Shader_SetUniformVec4( GLuint programID, const char* name, const float vec[4] ) +{ + GLint location = Shader_GetUniformLocation(programID, name); + glUniform4fv(location, 1, vec); +} + +void Shader_SetUniformInt( GLuint programID, const char* name, const int val ) +{ + GLint location = Shader_GetUniformLocation(programID, name); + glUniform1i(location, val); +} diff --git a/07-july/src/graphics/shaders.h b/07-july/src/graphics/shaders.h new file mode 100644 index 0000000..9e0bbab --- /dev/null +++ b/07-july/src/graphics/shaders.h @@ -0,0 +1,46 @@ +#ifndef SHADERS_H +#define SHADERS_H + +#include <GL/glew.h> + +#include "../math/matrix4x4.h" + +typedef struct +{ + GLuint ID; + + GLint position; + GLint color; + GLint uv; + GLint normal; + + GLint totalTransform; + GLint modelToWorld; + GLint lightPosition; + GLint ambientLight; + GLint lightColor; + GLint lightAttenuation; + GLint World_eyePosition; + GLint Texture; + + GLint Texture_Background; + GLint Texture_R; + GLint Texture_G; + GLint Texture_B; + GLint Texture_BlendMap; + +} Shader_Layout; + +extern GLuint Shader_CompileShaders(const char* vertexShader, const char* fragmentShader); +extern GLint Shader_GetUniformLocation( GLuint programID, const char* uniformName ); +extern GLint Shader_GetAttribLocation ( GLuint programID, const char* attributeName ); +extern void Shader_Destroy(GLuint programID); + +extern void Shader_SetUniformMat4( GLuint programID, const char* name, const float *matrix ); +extern void Shader_SetUniformFloat( GLuint programID, const char* name, const float val ); +extern void Shader_SetUniformVec2( GLuint programID, const char* name, const float vec[2] ); +extern void Shader_SetUniformVec3( GLuint programID, const char* name, const float vec[3] ); +extern void Shader_SetUniformVec4( GLuint programID, const char* name, const float vec[4] ); +extern void Shader_SetUniformInt( GLuint programID, const char* name, const int val ); + +#endif // SHADERS_H diff --git a/07-july/src/graphics/video.c b/07-july/src/graphics/video.c new file mode 100644 index 0000000..4b9b084 --- /dev/null +++ b/07-july/src/graphics/video.c @@ -0,0 +1,14 @@ +#include "video.h" + +#include <SDL2/SDL.h> +#include <GL/glew.h> + +void Video_Init() +{ + +} + +void Video_Shutdown() +{ + +} diff --git a/07-july/src/graphics/video.h b/07-july/src/graphics/video.h new file mode 100644 index 0000000..db23a45 --- /dev/null +++ b/07-july/src/graphics/video.h @@ -0,0 +1,7 @@ +#ifndef VIDEO_H +#define VIDEO_H + +extern void Video_Init(void); +extern void Video_Shutdown(void); + +#endif // VIDEO_H_INCLUDED diff --git a/07-july/src/graphics/window.c b/07-july/src/graphics/window.c new file mode 100644 index 0000000..9afb27a --- /dev/null +++ b/07-july/src/graphics/window.c @@ -0,0 +1,44 @@ +#include "window.h" +#include "../util/util.h" +#include <stdlib.h> + +window_t* Window_Create(const char* title, Uint32 width, Uint32 height) +{ + window_t* window = (window_t*) malloc(sizeof(window_t)); + window->title = title; + window->Width = width; + window->Height = height; + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + window->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + width, height, SDL_WINDOW_OPENGL); + if(window->window == NULL) + Util_FatalError( SDL_GetError() ); + + window->context = SDL_GL_CreateContext(window->window); + + if(window->context == NULL) + Util_FatalError( SDL_GetError() ); + + glViewport(0, 0, width, height); + return window; +} + +void Window_Resize(window_t* window, Uint32 width, Uint32 height) +{ + window->Width = width; + window->Height = height; + SDL_SetWindowSize(window->window, width, height); + glViewport(0, 0, width, height); +} + +void Window_Update(window_t* window) +{ + SDL_GL_SwapWindow(window->window); +} + +void Window_Destroy(window_t* window) +{ + SDL_GL_DeleteContext(window->context); + SDL_DestroyWindow(window->window); + free(window); +} diff --git a/07-july/src/graphics/window.h b/07-july/src/graphics/window.h new file mode 100644 index 0000000..1e71a83 --- /dev/null +++ b/07-july/src/graphics/window.h @@ -0,0 +1,20 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include <SDL2/SDL.h> +#include <GL/glew.h> + +typedef struct +{ + SDL_Window* window; + SDL_GLContext context; + Uint32 Width, Height; + const char* title; +} window_t; + +extern window_t* Window_Create(const char* title, Uint32 width, Uint32 height); +extern void Window_Resize(window_t* window, Uint32 width, Uint32 height); +extern void Window_Update(window_t* window); +extern void Window_Destroy(window_t* window); + +#endif // WINDOW_H |
