Cleanup: remove loading of shader sources.
This commit is contained in:
parent
d5abbb0415
commit
7e015fda86
@ -27,14 +27,6 @@ RK_EXPORT rk_window_t rk_create_context(
|
|||||||
rk_uint width,
|
rk_uint width,
|
||||||
rk_uint height);
|
rk_uint height);
|
||||||
|
|
||||||
RK_EXPORT char ** rk_load_shader_source(
|
|
||||||
char const * filename,
|
|
||||||
rk_uint * length);
|
|
||||||
|
|
||||||
RK_EXPORT void rk_free_shader_source(
|
|
||||||
char ** shader,
|
|
||||||
rk_uint length);
|
|
||||||
|
|
||||||
RK_EXPORT void rk_swap_buffers();
|
RK_EXPORT void rk_swap_buffers();
|
||||||
RK_EXPORT void rk_destroy_context();
|
RK_EXPORT void rk_destroy_context();
|
||||||
|
|
||||||
|
@ -215,46 +215,6 @@ void rk_swap_buffers() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char ** rk_load_shader_source(
|
|
||||||
char const * filename,
|
|
||||||
rk_uint * length) {
|
|
||||||
char ** shader = nullptr;
|
|
||||||
char buffer[1024];
|
|
||||||
FILE * const file = fopen(filename, "rt");
|
|
||||||
if (file) {
|
|
||||||
int nlines = 0;
|
|
||||||
while (fgets(buffer, sizeof(buffer), file)) {
|
|
||||||
++nlines;
|
|
||||||
}
|
|
||||||
if (nlines) {
|
|
||||||
rewind(file);
|
|
||||||
shader = new char*[nlines];
|
|
||||||
for (int line = 0; line < nlines; ++line) {
|
|
||||||
shader[line] = new char[sizeof(buffer)];
|
|
||||||
fgets(shader[line], sizeof(buffer), file);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
printf("Shader %s is empty.\n", filename);
|
|
||||||
}
|
|
||||||
fclose(file);
|
|
||||||
*length = nlines;
|
|
||||||
} else {
|
|
||||||
printf("Cannot open shader %s.\n", filename);
|
|
||||||
}
|
|
||||||
return shader;
|
|
||||||
}
|
|
||||||
|
|
||||||
void rk_free_shader_source(
|
|
||||||
char ** shader,
|
|
||||||
rk_uint length) {
|
|
||||||
if (shader) {
|
|
||||||
for (rk_uint line = 0; line < length; ++line) {
|
|
||||||
delete[] shader[line];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delete[] shader;
|
|
||||||
}
|
|
||||||
|
|
||||||
void rk_destroy_context() {
|
void rk_destroy_context() {
|
||||||
if (rk_display) {
|
if (rk_display) {
|
||||||
glXMakeCurrent(rk_display, 0, nullptr);
|
glXMakeCurrent(rk_display, 0, nullptr);
|
||||||
|
@ -96,55 +96,40 @@ static void rk_print_program_infolog(GLuint program) {
|
|||||||
//TODO: external loading of shader sources
|
//TODO: external loading of shader sources
|
||||||
//TODO: error handling
|
//TODO: error handling
|
||||||
rk_shader_t rk_load_shader(
|
rk_shader_t rk_load_shader(
|
||||||
char const * name) {
|
rk_uint const vert_nlines,
|
||||||
|
char const ** const vert_lines,
|
||||||
|
rk_uint const frag_nlines,
|
||||||
|
char const ** const frag_lines) {
|
||||||
rk_shader * const shader = new rk_shader;
|
rk_shader * const shader = new rk_shader;
|
||||||
shader->vertex = glCreateShader(GL_VERTEX_SHADER);
|
shader->vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||||
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
shader->program = glCreateProgram();
|
shader->program = glCreateProgram();
|
||||||
|
if (vert_nlines == 0 || vert_lines == nullptr) {
|
||||||
char vertex_name[256];
|
rk_printf("Missing vertex shader.");
|
||||||
snprintf(vertex_name, sizeof(vertex_name), "%s_opengles.vert", name);
|
return nullptr;
|
||||||
printf("[RK_ENGINE] Loading vertex shader %s...\n", vertex_name);
|
|
||||||
rk_uint vertex_length = 0;
|
|
||||||
char ** vertex_source = rk_load_shader_source(vertex_name, &vertex_length);
|
|
||||||
if (vertex_source) {
|
|
||||||
rk_printf("Compiling vertex shader...");
|
|
||||||
glShaderSource(shader->vertex, vertex_length, vertex_source, nullptr);
|
|
||||||
}
|
}
|
||||||
|
if (frag_nlines == 0 || frag_lines == nullptr) {
|
||||||
|
rk_printf("Missing fragment shader.");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
rk_printf("Compiling vertex shader...");
|
||||||
|
glShaderSource(shader->vertex, vert_nlines, vert_lines, nullptr);
|
||||||
glCompileShader(shader->vertex);
|
glCompileShader(shader->vertex);
|
||||||
rk_error("glCompileShader() failed.");
|
rk_error("glCompileShader() failed.");
|
||||||
rk_print_shader_infolog(shader->vertex);
|
rk_print_shader_infolog(shader->vertex);
|
||||||
|
rk_printf("Compiling fragment shader...");
|
||||||
char fragment_name[256];
|
glShaderSource(shader->fragment, frag_nlines, frag_lines, nullptr);
|
||||||
snprintf(fragment_name, sizeof(fragment_name), "%s_opengles.frag", name);
|
|
||||||
printf("[RK_ENGINE] Loading fragment shader %s...\n", fragment_name);
|
|
||||||
rk_uint fragment_length = 0;
|
|
||||||
char ** fragment_source = rk_load_shader_source(fragment_name, &fragment_length);
|
|
||||||
if (fragment_source) {
|
|
||||||
rk_printf("Compiling fragment shader...");
|
|
||||||
glShaderSource(shader->fragment, fragment_length, fragment_source, nullptr);
|
|
||||||
}
|
|
||||||
glCompileShader(shader->fragment);
|
glCompileShader(shader->fragment);
|
||||||
rk_error("glCompileShader() failed.");
|
rk_error("glCompileShader() failed.");
|
||||||
rk_print_shader_infolog(shader->fragment);
|
rk_print_shader_infolog(shader->fragment);
|
||||||
|
|
||||||
rk_printf("Linking program...");
|
rk_printf("Linking program...");
|
||||||
glAttachShader(shader->program, shader->vertex);
|
glAttachShader(shader->program, shader->vertex);
|
||||||
glAttachShader(shader->program, shader->fragment);
|
glAttachShader(shader->program, shader->fragment);
|
||||||
glLinkProgram(shader->program);
|
glLinkProgram(shader->program);
|
||||||
rk_error("glLinkProgram() failed.");
|
rk_error("glLinkProgram() failed.");
|
||||||
rk_print_program_infolog(shader->program);
|
rk_print_program_infolog(shader->program);
|
||||||
|
|
||||||
rk_printf("Done.");
|
rk_printf("Done.");
|
||||||
|
|
||||||
glReleaseShaderCompiler();
|
glReleaseShaderCompiler();
|
||||||
if (vertex_source != nullptr) {
|
|
||||||
rk_free_shader_source(vertex_source, vertex_length);
|
|
||||||
}
|
|
||||||
if (fragment_source != nullptr) {
|
|
||||||
rk_free_shader_source(fragment_source, fragment_length);
|
|
||||||
}
|
|
||||||
|
|
||||||
return shader;
|
return shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,10 @@ RK_EXPORT rk_window_t rk_initialize(
|
|||||||
rk_uint height);
|
rk_uint height);
|
||||||
|
|
||||||
RK_EXPORT rk_shader_t rk_load_shader(
|
RK_EXPORT rk_shader_t rk_load_shader(
|
||||||
char const * name);
|
rk_uint const vert_nlines,
|
||||||
|
char const ** const vert_lines,
|
||||||
|
rk_uint const frag_nlines,
|
||||||
|
char const ** const frag_lines);
|
||||||
|
|
||||||
RK_EXPORT void rk_select_shader(
|
RK_EXPORT void rk_select_shader(
|
||||||
rk_shader_t _shader);
|
rk_shader_t _shader);
|
||||||
|
Loading…
Reference in New Issue
Block a user