triangle render (and correct line endings in code)
This commit is contained in:
parent
d8e4055ae3
commit
a21091cad6
4 changed files with 176 additions and 151 deletions
|
@ -1,6 +1,6 @@
|
||||||
# CMake entry point
|
# CMake entry point
|
||||||
cmake_minimum_required (VERSION 3.0)
|
cmake_minimum_required (VERSION 3.0)
|
||||||
project (OpenGL_Analog_Clock)
|
project(OpenGL_Analog_Clock C)
|
||||||
|
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
find_package(GLEW REQUIRED)
|
find_package(GLEW REQUIRED)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef SHADER_HPP
|
#ifndef SHADER_HPP
|
||||||
#define SHADER_HPP
|
#define SHADER_HPP
|
||||||
|
|
||||||
GLuint LoadShaders(const char *vertex_file_path, const char *fragment_file_path);
|
#define xfree(p) if (p) free((void *)p)
|
||||||
|
|
||||||
|
int LoadShaders(GLuint *, const char *, const char *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
8
main.c
8
main.c
|
@ -58,7 +58,13 @@ int main(void) {
|
||||||
glBindVertexArray(VertexArrayID);
|
glBindVertexArray(VertexArrayID);
|
||||||
|
|
||||||
// Create and compile our GLSL program from the shaders
|
// Create and compile our GLSL program from the shaders
|
||||||
GLuint programID = LoadShaders("../shaders/SimpleVertexShader.vertexshader", "../shaders/SimpleFragmentShader.fragmentshader");
|
GLuint programID;
|
||||||
|
if (LoadShaders(&programID,
|
||||||
|
"../shaders/SimpleVertexShader.vertexshader",
|
||||||
|
"../shaders/SimpleFragmentShader.fragmentshader")) {
|
||||||
|
fprintf(stderr, "Could not load shaders.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
static const GLfloat g_vertex_buffer_data[] = {
|
static const GLfloat g_vertex_buffer_data[] = {
|
||||||
-1.0f, -1.0f, 0.0f,
|
-1.0f, -1.0f, 0.0f,
|
||||||
|
|
57
shader.c
57
shader.c
|
@ -6,7 +6,8 @@
|
||||||
|
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
|
||||||
static int load_code(const char *filepath, char **code) {
|
static int
|
||||||
|
load_code(const char *filepath, char **code) {
|
||||||
FILE *file;
|
FILE *file;
|
||||||
int cursor;
|
int cursor;
|
||||||
char c;
|
char c;
|
||||||
|
@ -32,7 +33,8 @@ static int load_code(const char *filepath, char **code) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int compile_code(const char *filepath, const char *code,
|
static int
|
||||||
|
compile_code(const char *filepath, const char *code,
|
||||||
GLuint ShaderID, GLuint *Result) {
|
GLuint ShaderID, GLuint *Result) {
|
||||||
int InfoLogLength;
|
int InfoLogLength;
|
||||||
// Compile Shader
|
// Compile Shader
|
||||||
|
@ -53,55 +55,70 @@ static int compile_code(const char *filepath, const char *code,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint
|
int
|
||||||
LoadShaders(const char *vertex_file_path,
|
LoadShaders(GLuint *programID, const char *vertex_file_path,
|
||||||
const char *fragment_file_path) {
|
const char *fragment_file_path) {
|
||||||
char *vertex_code;
|
int retval = -1;
|
||||||
char *fragment_code;
|
const char *vertex_code = NULL;
|
||||||
|
const char *fragment_code = NULL;
|
||||||
|
|
||||||
// Create the shaders
|
// Create the shaders
|
||||||
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||||
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
// Read the Vertex Shader code from the file
|
// Read the Vertex Shader code from the file
|
||||||
load_code(vertex_file_path, &vertex_code);
|
if (load_code(vertex_file_path, (char **)&vertex_code)) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
// Read the Fragment Shader code from the file
|
// Read the Fragment Shader code from the file
|
||||||
load_code(fragment_file_path, &fragment_code);
|
if (load_code(fragment_file_path, (char **)&fragment_code)) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
GLint Result = GL_FALSE;
|
GLint Result = GL_FALSE;
|
||||||
|
|
||||||
// Compile Vertex Shader
|
// Compile Vertex Shader
|
||||||
compile_code(vertex_file_path, vertex_code, VertexShaderID, &Result);
|
if (compile_code(vertex_file_path, vertex_code, VertexShaderID, &Result)) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
// Compile Fragment Shader
|
// Compile Fragment Shader
|
||||||
compile_code(fragment_file_path, fragment_code, FragmentShaderID, &Result);
|
if (compile_code(fragment_file_path, fragment_code, FragmentShaderID, &Result)) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
// Link the program
|
// Link the program
|
||||||
printf("Linking program\n");
|
printf("Linking program\n");
|
||||||
GLuint ProgramID = glCreateProgram();
|
*programID = glCreateProgram();
|
||||||
glAttachShader(ProgramID, VertexShaderID);
|
glAttachShader(*programID, VertexShaderID);
|
||||||
glAttachShader(ProgramID, FragmentShaderID);
|
glAttachShader(*programID, FragmentShaderID);
|
||||||
glLinkProgram(ProgramID);
|
glLinkProgram(*programID);
|
||||||
|
|
||||||
GLuint InfoLogLength;
|
GLuint InfoLogLength;
|
||||||
// Check the program
|
// Check the program
|
||||||
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
|
glGetProgramiv(*programID, GL_LINK_STATUS, &Result);
|
||||||
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
glGetProgramiv(*programID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||||
if (InfoLogLength > 0) {
|
if (InfoLogLength > 0) {
|
||||||
char *ProgramErrorMessage = malloc(InfoLogLength+1);
|
char *ProgramErrorMessage = malloc(InfoLogLength+1);
|
||||||
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, ProgramErrorMessage);
|
glGetProgramInfoLog(*programID, InfoLogLength, NULL, ProgramErrorMessage);
|
||||||
printf("%s\n", ProgramErrorMessage);
|
printf("%s\n", ProgramErrorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
glDetachShader(ProgramID, VertexShaderID);
|
glDetachShader(*programID, VertexShaderID);
|
||||||
glDetachShader(ProgramID, FragmentShaderID);
|
glDetachShader(*programID, FragmentShaderID);
|
||||||
|
|
||||||
glDeleteShader(VertexShaderID);
|
glDeleteShader(VertexShaderID);
|
||||||
glDeleteShader(FragmentShaderID);
|
glDeleteShader(FragmentShaderID);
|
||||||
|
|
||||||
return ProgramID;
|
// If code got here, it means it was successful
|
||||||
|
retval = 0;
|
||||||
|
|
||||||
|
end:
|
||||||
|
xfree(vertex_code);
|
||||||
|
xfree(fragment_code);
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue