Compare commits

..

No commits in common. "7e015fda86aaf66c81752d83b4383678918e2364" and "1e56cc1c28d4f9977b55f47566c3c8050f25670c" have entirely different histories.

4 changed files with 91 additions and 19 deletions

View File

@ -27,6 +27,14 @@ 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();

View File

@ -215,6 +215,46 @@ 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);

View File

@ -96,40 +96,55 @@ 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(
rk_uint const vert_nlines, char const * name) {
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) {
rk_printf("Missing vertex shader."); char vertex_name[256];
return nullptr; snprintf(vertex_name, sizeof(vertex_name), "%s_opengles.vert", name);
} printf("[RK_ENGINE] Loading vertex shader %s...\n", vertex_name);
if (frag_nlines == 0 || frag_lines == nullptr) { rk_uint vertex_length = 0;
rk_printf("Missing fragment shader."); char ** vertex_source = rk_load_shader_source(vertex_name, &vertex_length);
return nullptr; if (vertex_source) {
}
rk_printf("Compiling vertex shader..."); rk_printf("Compiling vertex shader...");
glShaderSource(shader->vertex, vert_nlines, vert_lines, nullptr); glShaderSource(shader->vertex, vertex_length, vertex_source, 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);
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..."); rk_printf("Compiling fragment shader...");
glShaderSource(shader->fragment, frag_nlines, frag_lines, nullptr); 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;
} }

View File

@ -87,10 +87,7 @@ 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(
rk_uint const vert_nlines, char const * name);
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);
@ -140,6 +137,18 @@ RK_EXPORT rk_batch_t rk_create_batch(
rk_batch_translation_format translation_format, rk_batch_translation_format translation_format,
rk_batch_orientation_format orientation_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_begin_frame();
RK_EXPORT void rk_select_texture( RK_EXPORT void rk_select_texture(