Compare commits

..

2 Commits

4 changed files with 19 additions and 91 deletions

View File

@ -27,14 +27,6 @@ RK_EXPORT rk_window_t rk_create_context(
rk_uint width,
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_destroy_context();

View File

@ -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() {
if (rk_display) {
glXMakeCurrent(rk_display, 0, nullptr);

View File

@ -96,55 +96,40 @@ static void rk_print_program_infolog(GLuint program) {
//TODO: external loading of shader sources
//TODO: error handling
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;
shader->vertex = glCreateShader(GL_VERTEX_SHADER);
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
shader->program = glCreateProgram();
char vertex_name[256];
snprintf(vertex_name, sizeof(vertex_name), "%s_opengles.vert", name);
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 (vert_nlines == 0 || vert_lines == nullptr) {
rk_printf("Missing vertex shader.");
return 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);
rk_error("glCompileShader() failed.");
rk_print_shader_infolog(shader->vertex);
char fragment_name[256];
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);
}
rk_printf("Compiling fragment shader...");
glShaderSource(shader->fragment, frag_nlines, frag_lines, nullptr);
glCompileShader(shader->fragment);
rk_error("glCompileShader() failed.");
rk_print_shader_infolog(shader->fragment);
rk_printf("Linking program...");
glAttachShader(shader->program, shader->vertex);
glAttachShader(shader->program, shader->fragment);
glLinkProgram(shader->program);
rk_error("glLinkProgram() failed.");
rk_print_program_infolog(shader->program);
rk_printf("Done.");
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;
}

View File

@ -87,7 +87,10 @@ RK_EXPORT rk_window_t rk_initialize(
rk_uint height);
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_shader_t _shader);
@ -137,18 +140,6 @@ RK_EXPORT rk_batch_t rk_create_batch(
rk_batch_translation_format translation_format,
rk_batch_orientation_format orientation_format);
//TODO: export math function and take a projection matrix instead of its parameters
RK_EXPORT void rk_set_projection(
float hfov,
float ratio,
float near,
float far);
//TODO: export math function and take a view matrix instead of its parameters
RK_EXPORT void rk_set_view(
rk_vec3 const & position,
rk_vec3 const & lookat);
RK_EXPORT void rk_begin_frame();
RK_EXPORT void rk_select_texture(