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,37 +1,37 @@
|
||||||
# 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)
|
||||||
find_package(glfw3 REQUIRED)
|
find_package(glfw3 REQUIRED)
|
||||||
|
|
||||||
include_directories(
|
include_directories(
|
||||||
Include/
|
Include/
|
||||||
.
|
.
|
||||||
)
|
)
|
||||||
|
|
||||||
set(ALL_LIBS
|
set(ALL_LIBS
|
||||||
${OPENGL_LIBRARY}
|
${OPENGL_LIBRARY}
|
||||||
glfw
|
glfw
|
||||||
GLEW
|
GLEW
|
||||||
)
|
)
|
||||||
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb")
|
||||||
|
|
||||||
add_definitions(
|
add_definitions(
|
||||||
-DTW_STATIC
|
-DTW_STATIC
|
||||||
-DTW_NO_LIB_PRAGMA
|
-DTW_NO_LIB_PRAGMA
|
||||||
-DTW_NO_DIRECT3D
|
-DTW_NO_DIRECT3D
|
||||||
-DGLEW_STATIC
|
-DGLEW_STATIC
|
||||||
-D_CRT_SECURE_NO_WARNINGS
|
-D_CRT_SECURE_NO_WARNINGS
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(clock
|
add_executable(clock
|
||||||
main.c
|
main.c
|
||||||
shader.c
|
shader.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(clock
|
target_link_libraries(clock
|
||||||
${ALL_LIBS}
|
${ALL_LIBS}
|
||||||
)
|
)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
#endif
|
int LoadShaders(GLuint *, const char *, const char *);
|
||||||
|
|
||||||
|
#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,
|
||||||
|
|
231
shader.c
231
shader.c
|
@ -1,107 +1,124 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
|
||||||
static int load_code(const char *filepath, char **code) {
|
static int
|
||||||
FILE *file;
|
load_code(const char *filepath, char **code) {
|
||||||
int cursor;
|
FILE *file;
|
||||||
char c;
|
int cursor;
|
||||||
|
char c;
|
||||||
file = fopen(filepath, "r");
|
|
||||||
if (file == NULL) {
|
file = fopen(filepath, "r");
|
||||||
fprintf(stderr, "Could not open %s.\n", filepath);
|
if (file == NULL) {
|
||||||
return 1;
|
fprintf(stderr, "Could not open %s.\n", filepath);
|
||||||
}
|
return 1;
|
||||||
|
}
|
||||||
*code = malloc(256 * sizeof(char));
|
|
||||||
if (code == NULL) {
|
*code = malloc(256 * sizeof(char));
|
||||||
fprintf(stderr, "Out of memory", filepath);
|
if (code == NULL) {
|
||||||
return 1;
|
fprintf(stderr, "Out of memory", filepath);
|
||||||
}
|
return 1;
|
||||||
cursor = 0;
|
}
|
||||||
|
cursor = 0;
|
||||||
while ((c = fgetc(file)) != EOF) {
|
|
||||||
(*code)[cursor] = c;
|
while ((c = fgetc(file)) != EOF) {
|
||||||
cursor++;
|
(*code)[cursor] = c;
|
||||||
}
|
cursor++;
|
||||||
|
}
|
||||||
return 0;
|
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
static int compile_code(const char *filepath, const char *code,
|
|
||||||
GLuint ShaderID, GLuint *Result) {
|
static int
|
||||||
int InfoLogLength;
|
compile_code(const char *filepath, const char *code,
|
||||||
// Compile Shader
|
GLuint ShaderID, GLuint *Result) {
|
||||||
printf("Compiling shader : %s\n", filepath);
|
int InfoLogLength;
|
||||||
glShaderSource(ShaderID, 1, (const char **)&code, NULL);
|
// Compile Shader
|
||||||
glCompileShader(ShaderID);
|
printf("Compiling shader : %s\n", filepath);
|
||||||
|
glShaderSource(ShaderID, 1, (const char **)&code, NULL);
|
||||||
// Check Shader
|
glCompileShader(ShaderID);
|
||||||
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, Result);
|
|
||||||
glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
// Check Shader
|
||||||
if (InfoLogLength > 0) {
|
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, Result);
|
||||||
char *ShaderErrorMessage = malloc(InfoLogLength+1);
|
glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||||
glGetShaderInfoLog(ShaderID, InfoLogLength, NULL, ShaderErrorMessage);
|
if (InfoLogLength > 0) {
|
||||||
printf("%s\n", ShaderErrorMessage);
|
char *ShaderErrorMessage = malloc(InfoLogLength+1);
|
||||||
free(ShaderErrorMessage);
|
glGetShaderInfoLog(ShaderID, InfoLogLength, NULL, ShaderErrorMessage);
|
||||||
return -1;
|
printf("%s\n", ShaderErrorMessage);
|
||||||
}
|
free(ShaderErrorMessage);
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
GLuint
|
}
|
||||||
LoadShaders(const char *vertex_file_path,
|
|
||||||
const char *fragment_file_path) {
|
int
|
||||||
char *vertex_code;
|
LoadShaders(GLuint *programID, const char *vertex_file_path,
|
||||||
char *fragment_code;
|
const char *fragment_file_path) {
|
||||||
|
int retval = -1;
|
||||||
// Create the shaders
|
const char *vertex_code = NULL;
|
||||||
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
const char *fragment_code = NULL;
|
||||||
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
|
||||||
|
// Create the shaders
|
||||||
// Read the Vertex Shader code from the file
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||||
load_code(vertex_file_path, &vertex_code);
|
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
// Read the Fragment Shader code from the file
|
// Read the Vertex Shader code from the file
|
||||||
load_code(fragment_file_path, &fragment_code);
|
if (load_code(vertex_file_path, (char **)&vertex_code)) {
|
||||||
|
goto end;
|
||||||
GLint Result = GL_FALSE;
|
}
|
||||||
|
|
||||||
// Compile Vertex Shader
|
// Read the Fragment Shader code from the file
|
||||||
compile_code(vertex_file_path, vertex_code, VertexShaderID, &Result);
|
if (load_code(fragment_file_path, (char **)&fragment_code)) {
|
||||||
|
goto end;
|
||||||
// Compile Fragment Shader
|
}
|
||||||
compile_code(fragment_file_path, fragment_code, FragmentShaderID, &Result);
|
|
||||||
|
GLint Result = GL_FALSE;
|
||||||
// Link the program
|
|
||||||
printf("Linking program\n");
|
// Compile Vertex Shader
|
||||||
GLuint ProgramID = glCreateProgram();
|
if (compile_code(vertex_file_path, vertex_code, VertexShaderID, &Result)) {
|
||||||
glAttachShader(ProgramID, VertexShaderID);
|
goto end;
|
||||||
glAttachShader(ProgramID, FragmentShaderID);
|
}
|
||||||
glLinkProgram(ProgramID);
|
|
||||||
|
// Compile Fragment Shader
|
||||||
GLuint InfoLogLength;
|
if (compile_code(fragment_file_path, fragment_code, FragmentShaderID, &Result)) {
|
||||||
// Check the program
|
goto end;
|
||||||
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
|
}
|
||||||
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
|
||||||
if (InfoLogLength > 0) {
|
// Link the program
|
||||||
char *ProgramErrorMessage = malloc(InfoLogLength+1);
|
printf("Linking program\n");
|
||||||
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, ProgramErrorMessage);
|
*programID = glCreateProgram();
|
||||||
printf("%s\n", ProgramErrorMessage);
|
glAttachShader(*programID, VertexShaderID);
|
||||||
}
|
glAttachShader(*programID, FragmentShaderID);
|
||||||
|
glLinkProgram(*programID);
|
||||||
|
|
||||||
glDetachShader(ProgramID, VertexShaderID);
|
GLuint InfoLogLength;
|
||||||
glDetachShader(ProgramID, FragmentShaderID);
|
// Check the program
|
||||||
|
glGetProgramiv(*programID, GL_LINK_STATUS, &Result);
|
||||||
glDeleteShader(VertexShaderID);
|
glGetProgramiv(*programID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||||
glDeleteShader(FragmentShaderID);
|
if (InfoLogLength > 0) {
|
||||||
|
char *ProgramErrorMessage = malloc(InfoLogLength+1);
|
||||||
return ProgramID;
|
glGetProgramInfoLog(*programID, InfoLogLength, NULL, ProgramErrorMessage);
|
||||||
}
|
printf("%s\n", ProgramErrorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
glDetachShader(*programID, VertexShaderID);
|
||||||
|
glDetachShader(*programID, FragmentShaderID);
|
||||||
|
|
||||||
|
glDeleteShader(VertexShaderID);
|
||||||
|
glDeleteShader(FragmentShaderID);
|
||||||
|
|
||||||
|
// 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