From ae2455a43d5ab2f16f29c7eb16573856d7f7e81d Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 24 Sep 2024 06:30:23 +0200 Subject: [PATCH] made opengl global --- gpuapi/opengl/OpenglUtils.h | 346 +++++++++++++++++---------------- gpuapi/opengl/OpenglWin32.h | 371 +++++++++++++++++++++--------------- gpuapi/opengl/ShaderUtils.h | 67 ++++--- image/Image.cpp | 27 +++ math/matrix/MatrixFloat32.h | 50 ++++- object/Vertex.h | 9 + 6 files changed, 516 insertions(+), 354 deletions(-) diff --git a/gpuapi/opengl/OpenglUtils.h b/gpuapi/opengl/OpenglUtils.h index ca2d0d7..a7ad4f5 100644 --- a/gpuapi/opengl/OpenglUtils.h +++ b/gpuapi/opengl/OpenglUtils.h @@ -109,17 +109,17 @@ uint32 get_texture_data_type(uint32 texture_data_type) // 4. load_texture_to_gpu inline -void prepare_texture(OpenGL* gl, Texture* texture, uint32 texture_unit) +void prepare_texture(Texture* texture, uint32 texture_unit) { uint32 texture_data_type = get_texture_data_type(texture->texture_data_type); glGenTextures(1, (GLuint *) &texture->id); - gl->glActiveTexture(GL_TEXTURE0 + texture_unit); + glActiveTexture(GL_TEXTURE0 + texture_unit); glBindTexture(texture_data_type, (GLuint) texture->id); } inline -void load_texture_to_gpu(OpenGL* gl, const Texture* texture, int mipmap_level = 0) +void load_texture_to_gpu(const Texture* texture, int mipmap_level = 0) { uint32 texture_data_type = get_texture_data_type(texture->texture_data_type); glTexImage2D( @@ -130,33 +130,33 @@ void load_texture_to_gpu(OpenGL* gl, const Texture* texture, int mipmap_level = ); if (mipmap_level > -1) { - gl->glGenerateMipmap(GL_TEXTURE_2D); + glGenerateMipmap(GL_TEXTURE_2D); } } inline -void texture_use(OpenGL* gl, const Texture* texture, uint32 texture_unit) +void texture_use(const Texture* texture, uint32 texture_unit) { - gl->glActiveTexture(GL_TEXTURE0 + texture_unit); + glActiveTexture(GL_TEXTURE0 + texture_unit); glBindTexture(GL_TEXTURE_2D, (GLuint) texture->id); } -GLuint shader_make(OpenGL* gl, GLenum type, const char *source, RingMemory* ring) +GLuint shader_make(GLenum type, const char *source, RingMemory* ring) { - GLuint shader = gl->glCreateShader(type); - gl->glShaderSource(shader, 1, (GLchar **) &source, NULL); - gl->glCompileShader(shader); + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, (GLchar **) &source, NULL); + glCompileShader(shader); GLint status; - gl->glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status == GL_FALSE) { GLint length; - gl->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); GLchar *info = (GLchar *) ring_get_memory(ring, length * sizeof(GLchar)); - gl->glGetShaderInfoLog(shader, length, NULL, info); + glGetShaderInfoLog(shader, length, NULL, info); ASSERT_SIMPLE(false); @@ -166,14 +166,14 @@ GLuint shader_make(OpenGL* gl, GLenum type, const char *source, RingMemory* ring return shader; } -GLuint shader_load(OpenGL* gl, GLenum type, const char* path, RingMemory* ring) { +GLuint shader_load(GLenum type, const char* path, RingMemory* ring) { uint64 temp = ring->pos; FileBody file; // @todo consider to accept file as parameter and load file before file_read(path, &file, ring); - GLuint result = shader_make(gl, type, (const char *) file.content, ring); + GLuint result = shader_make(type, (const char *) file.content, ring); // We can immediately dispose of it we can also reset our ring memory position ring->pos = temp; @@ -182,34 +182,33 @@ GLuint shader_load(OpenGL* gl, GLenum type, const char* path, RingMemory* ring) } GLuint program_make( - OpenGL* gl, GLuint vertex_shader, GLuint fragment_shader, GLint geometry_shader, RingMemory* ring ) { - GLuint program = gl->glCreateProgram(); + GLuint program = glCreateProgram(); if (geometry_shader > -1) { - gl->glAttachShader(program, geometry_shader); + glAttachShader(program, geometry_shader); } - gl->glAttachShader(program, vertex_shader); - gl->glAttachShader(program, fragment_shader); - gl->glLinkProgram(program); + glAttachShader(program, vertex_shader); + glAttachShader(program, fragment_shader); + glLinkProgram(program); GLint status; - gl->glGetProgramiv(program, GL_LINK_STATUS, &status); + glGetProgramiv(program, GL_LINK_STATUS, &status); if (status == GL_FALSE) { ASSERT_SIMPLE(false); GLint length; - gl->glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length); + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length); GLchar *info = (GLchar *) ring_get_memory(ring, length * sizeof(GLchar)); - gl->glGetProgramInfoLog(program, length, NULL, info); + glGetProgramInfoLog(program, length, NULL, info); // @todo use global logger fprintf(stderr, "glLinkProgram failed: %s\n", info); @@ -217,234 +216,207 @@ GLuint program_make( // @question really? if (geometry_shader > -1) { - gl->glDetachShader(program, geometry_shader); + glDetachShader(program, geometry_shader); } - gl->glDetachShader(program, vertex_shader); - gl->glDetachShader(program, fragment_shader); + glDetachShader(program, vertex_shader); + glDetachShader(program, fragment_shader); // @question really? if (geometry_shader > -1) { - gl->glDeleteShader(geometry_shader); + glDeleteShader(geometry_shader); } - gl->glDeleteShader(vertex_shader); - gl->glDeleteShader(fragment_shader); + glDeleteShader(vertex_shader); + glDeleteShader(fragment_shader); return program; } GLuint program_load( - OpenGL* gl, const char* path1, const char* path2, const char* path3, RingMemory* ring ) { - GLuint vertex_shader = shader_load(gl, GL_VERTEX_SHADER, path1, ring); - GLuint fragment_shader = shader_load(gl, GL_FRAGMENT_SHADER, path2, ring); + GLuint vertex_shader = shader_load(GL_VERTEX_SHADER, path1, ring); + GLuint fragment_shader = shader_load(GL_FRAGMENT_SHADER, path2, ring); GLint geometry_shader = -1; if (path3) { - geometry_shader = shader_load(gl, GL_GEOMETRY_SHADER, path3, ring); + geometry_shader = shader_load(GL_GEOMETRY_SHADER, path3, ring); } - GLuint program = program_make(gl, vertex_shader, fragment_shader, geometry_shader, ring); + GLuint program = program_make(vertex_shader, fragment_shader, geometry_shader, ring); return program; } inline -void shader_use(OpenGL* gl, uint32 id) +void shader_use(uint32 id) { - gl->glUseProgram(id); + glUseProgram(id); } inline -void draw_triangles_3d(OpenGL* gl, VertexRef* vertices, GLuint buffer, int count) { - gl->glBindBuffer(GL_ARRAY_BUFFER, buffer); +void draw_triangles_3d(VertexRef* vertices, GLuint buffer, int count) { + glBindBuffer(GL_ARRAY_BUFFER, buffer); // position attribute - gl->glVertexAttribPointer(vertices->position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) 0); - gl->glEnableVertexAttribArray(vertices->position); + glVertexAttribPointer(vertices->position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) 0); + glEnableVertexAttribArray(vertices->position); // normal attribute - gl->glVertexAttribPointer(vertices->normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 3)); - gl->glEnableVertexAttribArray(vertices->normal); + glVertexAttribPointer(vertices->normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 3)); + glEnableVertexAttribArray(vertices->normal); // texture coord attribute - gl->glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex3D), (void *) (sizeof(float) * 6)); - gl->glEnableVertexAttribArray(vertices->tex_coord); + glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex3D), (void *) (sizeof(float) * 6)); + glEnableVertexAttribArray(vertices->tex_coord); // color attribute - gl->glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 8)); - gl->glEnableVertexAttribArray(vertices->color); + glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 8)); + glEnableVertexAttribArray(vertices->color); glDrawArrays(GL_TRIANGLES, 0, count); - gl->glDisableVertexAttribArray(vertices->position); - gl->glDisableVertexAttribArray(vertices->normal); - gl->glDisableVertexAttribArray(vertices->tex_coord); - gl->glDisableVertexAttribArray(vertices->color); + glDisableVertexAttribArray(vertices->position); + glDisableVertexAttribArray(vertices->normal); + glDisableVertexAttribArray(vertices->tex_coord); + glDisableVertexAttribArray(vertices->color); - gl->glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); } inline -void draw_triangles_3d_textureless(OpenGL* gl, VertexRef* vertices, GLuint buffer, int count) { - gl->glBindBuffer(GL_ARRAY_BUFFER, buffer); +void draw_triangles_3d_textureless(VertexRef* vertices, GLuint buffer, int count) { + glBindBuffer(GL_ARRAY_BUFFER, buffer); // position attribute - gl->glVertexAttribPointer(vertices->position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) 0); - gl->glEnableVertexAttribArray(vertices->position); + glVertexAttribPointer(vertices->position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) 0); + glEnableVertexAttribArray(vertices->position); // normal attribute - gl->glVertexAttribPointer(vertices->normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 3)); - gl->glEnableVertexAttribArray(vertices->normal); + glVertexAttribPointer(vertices->normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 3)); + glEnableVertexAttribArray(vertices->normal); // color attribute - gl->glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 8)); - gl->glEnableVertexAttribArray(vertices->color); + glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 8)); + glEnableVertexAttribArray(vertices->color); glDrawArrays(GL_TRIANGLES, 0, count); - gl->glDisableVertexAttribArray(vertices->position); - gl->glDisableVertexAttribArray(vertices->normal); - gl->glDisableVertexAttribArray(vertices->color); + glDisableVertexAttribArray(vertices->position); + glDisableVertexAttribArray(vertices->normal); + glDisableVertexAttribArray(vertices->color); - gl->glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); } inline -void draw_triangles_3d_colorless(OpenGL* gl, VertexRef* vertices, GLuint buffer, int count) { - gl->glBindBuffer(GL_ARRAY_BUFFER, buffer); +void draw_triangles_3d_colorless(VertexRef* vertices, GLuint buffer, int count) { + glBindBuffer(GL_ARRAY_BUFFER, buffer); // position attribute - gl->glVertexAttribPointer(vertices->position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) 0); - gl->glEnableVertexAttribArray(vertices->position); + glVertexAttribPointer(vertices->position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) 0); + glEnableVertexAttribArray(vertices->position); // normal attribute - gl->glVertexAttribPointer(vertices->normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 3)); - gl->glEnableVertexAttribArray(vertices->normal); + glVertexAttribPointer(vertices->normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void *) (sizeof(float) * 3)); + glEnableVertexAttribArray(vertices->normal); // texture coord attribute - gl->glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex3D), (void *) (sizeof(float) * 6)); - gl->glEnableVertexAttribArray(vertices->tex_coord); + glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex3D), (void *) (sizeof(float) * 6)); + glEnableVertexAttribArray(vertices->tex_coord); glDrawArrays(GL_TRIANGLES, 0, count); - gl->glDisableVertexAttribArray(vertices->position); - gl->glDisableVertexAttribArray(vertices->normal); - gl->glDisableVertexAttribArray(vertices->tex_coord); + glDisableVertexAttribArray(vertices->position); + glDisableVertexAttribArray(vertices->normal); + glDisableVertexAttribArray(vertices->tex_coord); - gl->glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); } inline -void draw_triangles_2d(OpenGL* gl, VertexRef* vertices, GLuint buffer, int count) { - gl->glBindBuffer(GL_ARRAY_BUFFER, buffer); +void draw_triangles_2d(VertexRef* vertices, GLuint buffer, int count) { + glBindBuffer(GL_ARRAY_BUFFER, buffer); // position attribute - gl->glVertexAttribPointer(vertices->position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); - gl->glEnableVertexAttribArray(vertices->position); + glVertexAttribPointer(vertices->position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); + glEnableVertexAttribArray(vertices->position); // texture coord attribute - gl->glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex2D), (void *) (sizeof(float) * 2)); - gl->glEnableVertexAttribArray(vertices->tex_coord); + glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex2D), (void *) (sizeof(float) * 2)); + glEnableVertexAttribArray(vertices->tex_coord); // color attribute - gl->glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) (sizeof(float) * 4)); - gl->glEnableVertexAttribArray(vertices->color); + glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) (sizeof(float) * 4)); + glEnableVertexAttribArray(vertices->color); glDrawArrays(GL_TRIANGLES, 0, count); - gl->glDisableVertexAttribArray(vertices->position); - gl->glDisableVertexAttribArray(vertices->tex_coord); - gl->glDisableVertexAttribArray(vertices->color); + glDisableVertexAttribArray(vertices->position); + glDisableVertexAttribArray(vertices->tex_coord); + glDisableVertexAttribArray(vertices->color); - gl->glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); } inline -void draw_triangles_2d_textureless(OpenGL* gl, VertexRef* vertices, GLuint buffer, int count) { - gl->glBindBuffer(GL_ARRAY_BUFFER, buffer); +void draw_triangles_2d_textureless(VertexRef* vertices, GLuint buffer, int count) { + glBindBuffer(GL_ARRAY_BUFFER, buffer); // position attribute - gl->glVertexAttribPointer(vertices->position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); - gl->glEnableVertexAttribArray(vertices->position); + glVertexAttribPointer(vertices->position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); + glEnableVertexAttribArray(vertices->position); // color attribute - gl->glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) (sizeof(float) * 4)); - gl->glEnableVertexAttribArray(vertices->color); + glVertexAttribPointer(vertices->color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) (sizeof(float) * 4)); + glEnableVertexAttribArray(vertices->color); glDrawArrays(GL_TRIANGLES, 0, count); - gl->glDisableVertexAttribArray(vertices->position); - gl->glDisableVertexAttribArray(vertices->color); + glDisableVertexAttribArray(vertices->position); + glDisableVertexAttribArray(vertices->color); - gl->glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); } inline -void draw_triangles_2d_colorless(OpenGL* gl, VertexRef* vertices, GLuint buffer, int count) { - gl->glBindBuffer(GL_ARRAY_BUFFER, buffer); +void draw_triangles_2d_colorless(VertexRef* vertices, GLuint buffer, int count) { + glBindBuffer(GL_ARRAY_BUFFER, buffer); // position attribute - gl->glVertexAttribPointer(vertices->position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); - gl->glEnableVertexAttribArray(vertices->position); + glVertexAttribPointer(vertices->position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); + glEnableVertexAttribArray(vertices->position); // texture coord attribute - gl->glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex2D), (void *) (sizeof(float) * 2)); - gl->glEnableVertexAttribArray(vertices->tex_coord); + glVertexAttribIPointer(vertices->tex_coord, 2, GL_UNSIGNED_INT, sizeof(Vertex2D), (void *) (sizeof(float) * 2)); + glEnableVertexAttribArray(vertices->tex_coord); glDrawArrays(GL_TRIANGLES, 0, count); - gl->glDisableVertexAttribArray(vertices->position); - gl->glDisableVertexAttribArray(vertices->tex_coord); + glDisableVertexAttribArray(vertices->position); + glDisableVertexAttribArray(vertices->tex_coord); - gl->glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); } -inline -void draw_text(OpenGL* gl, VertexRef* vertices, GLuint buffer, int length) -{ - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - draw_triangles_2d(gl, vertices, buffer, length * 6); - glDisable(GL_BLEND); -} +struct TextRender { + uint32 align; + uint32 x; + uint32 y; + float scale; // @is it really scale or is it position in texture map? + VertexRef vertices; + char* text; -GLuint gen_text_buffer(float x, float y, float n, const char *text) { - size_t length = strlen(text); - GLfloat *data = NULL; //malloc_faces(4, length); // sizeof(GLfloat) * 6 * 4 * length + TextShader shader_data; +}; - for (int i = 0; i < length; i++) { - make_character(data + i * 24, x, y, n / 2, n, text[i]); - x += n; - } - - return 0; //gen_faces(4, length, data); -} - -inline -void render_text(OpenGL* gl, Attrib* attrib, int justify, float x, float y, float n, const char *text) -{ - float matrix[16]; - //set_matrix_2d(matrix, g->width, g->height); - - gl->glUseProgram(attrib->program); - gl->glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix); - gl->glUniform1i(attrib->sampler, 1); - gl->glUniform1i(attrib->extra1, 0); - - size_t length = strlen(text); - x -= n * justify * (length - 1) / 2; - - GLuint buffer = gen_text_buffer(x, y, n, text); - draw_text(gl, &attrib->vertices, buffer, (int) length); - - gl->glDeleteBuffers(1, &buffer); -} +#define TEXT_ALIGN_LEFT 0 +#define TEXT_ALIGN_CENTER 1 +#define TEXT_ALIGN_RIGHT 2 inline int calculate_face_size(int components, int faces) @@ -455,74 +427,74 @@ int calculate_face_size(int components, int faces) // generates faces // data is no longer needed after this inline -uint32 gpuapi_buffer_generate(OpenGL* gl, int size, void* data) +uint32 gpuapi_buffer_generate(int size, void* data) { uint32 vbo; - gl->glGenBuffers(1, &vbo); - gl->glBindBuffer(GL_ARRAY_BUFFER, vbo); - gl->glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); return vbo; } inline -uint32 gpuapi_shaderbuffer_generate(OpenGL* gl, int size, void* data) +uint32 gpuapi_shaderbuffer_generate(int size, void* data) { uint32 sbo; - gl->glGenBuffers(1, &sbo); - gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, sbo); - gl->glBufferData(GL_SHADER_STORAGE_BUFFER, size, data, GL_DYNAMIC_DRAW); + glGenBuffers(1, &sbo); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, sbo); + glBufferData(GL_SHADER_STORAGE_BUFFER, size, data, GL_DYNAMIC_DRAW); return sbo; } inline -uint32 gpuapi_uniformbuffer_generate(OpenGL* gl, int size, void* data) +uint32 gpuapi_uniformbuffer_generate(int size, void* data) { uint32 ubo; - gl->glGenBuffers(1, &ubo); - gl->glBindBuffer(GL_UNIFORM_BUFFER, ubo); - gl->glBufferData(GL_UNIFORM_BUFFER, size, data, GL_STATIC_DRAW); + glGenBuffers(1, &ubo); + glBindBuffer(GL_UNIFORM_BUFFER, ubo); + glBufferData(GL_UNIFORM_BUFFER, size, data, GL_STATIC_DRAW); return ubo; } inline -uint32 gpuapi_buffer_element_generate(OpenGL* gl, int size, uint32 *data) +uint32 gpuapi_buffer_element_generate(int size, uint32 *data) { uint32 ebo; - gl->glGenBuffers(1, &ebo); - gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - gl->glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); + glGenBuffers(1, &ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); return ebo; } inline -uint32 gpuapi_vertex_array_generate(OpenGL* gl) +uint32 gpuapi_vertex_array_generate() { uint32 vao; - gl->glGenVertexArrays(1, &vao); - gl->glBindVertexArray(vao); + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); return vao; } inline -void gpuapi_unbind_all(OpenGL* gl) +void gpuapi_unbind_all() { - gl->glBindBuffer(GL_ARRAY_BUFFER, 0); - gl->glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); } inline -void gpuapi_buffer_delete(OpenGL* gl, GLuint buffer) +void gpuapi_buffer_delete(GLuint buffer) { - gl->glDeleteBuffers(1, &buffer); + glDeleteBuffers(1, &buffer); } int get_gpu_free_memory() @@ -539,6 +511,44 @@ int get_gpu_free_memory() return available; } +inline +void render_text( + int width, int height, + TextRender* text_data +) { + glUseProgram(text_data->shader_data.program_id); + glUniform1i(text_data->shader_data.sampler_id, 1); + + // @performance Instead of re-creating the matrix every render call just use the id to activate it + // 2d projection + if (text_data->shader_data.matrix_id == 0) { + float matrix[16] = {}; + mat4_ortho_sparse_rh(matrix, 0.0f, (float) width, 0.0f, (float) height, -1.0f, 1.0f); + glUniformMatrix4fv(text_data->shader_data.matrix_id, 1, GL_FALSE, matrix); + } else { + + } + + int length = (int) strlen(text_data->text); + float x = text_data->x - text_data->scale * text_data->align * (length - 1) / 2; + + // @todo fix + GLfloat *data = NULL; //malloc_faces(4, length); // sizeof(GLfloat) * 6 * 4 * length + + for (int i = 0; i < length; i++) { + make_character(data + i * 24, x, (float) text_data->y, text_data->scale / 2, text_data->scale, text_data->text[i]); + x += text_data->scale; + } + + GLuint buffer = gpuapi_buffer_generate(sizeof(GLfloat) * 6 * 4 * length, data); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + draw_triangles_2d(&text_data->vertices, buffer, length * 6); + glDisable(GL_BLEND); + glDeleteBuffers(1, &buffer); +} + /* void render_9_patch(GLuint texture, int imgWidth, int imgHeight, diff --git a/gpuapi/opengl/OpenglWin32.h b/gpuapi/opengl/OpenglWin32.h index e71730f..0b01d55 100644 --- a/gpuapi/opengl/OpenglWin32.h +++ b/gpuapi/opengl/OpenglWin32.h @@ -1040,65 +1040,183 @@ extern "C" { (GLenum target, GLenum pname, GLfloat *params); } + typedef void WINAPI type_glTexImage2DMultisample(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height, GLboolean fixedsamplelocations); +static type_glTexImage2DMultisample* glTexImage2DMultisample; + typedef void WINAPI type_glBindFramebuffer(GLenum target, GLuint framebuffer); +static type_glBindFramebuffer* glBindFramebuffer; + typedef void WINAPI type_glGenFramebuffers(GLsizei n, GLuint *framebuffers); +static type_glGenFramebuffers* glGenFramebuffers; + typedef void WINAPI type_glFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); +static type_glFramebufferTexture2D* glFramebufferTexture2D; + typedef GLenum WINAPI type_glCheckFramebufferStatus(GLenum target); +static type_glCheckFramebufferStatus* glCheckFramebufferStatus; + typedef void WINAPI type_glBlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter); +static type_glBlitFramebuffer* glBlitFramebuffer; + typedef void WINAPI type_glAttachShader(GLuint program, GLuint shader); +static type_glAttachShader* glAttachShader; + typedef void WINAPI type_glCompileShader(GLuint shader); +static type_glCompileShader* glCompileShader; + typedef GLuint WINAPI type_glCreateProgram(void); +static type_glCreateProgram* glCreateProgram; + typedef GLuint WINAPI type_glCreateShader(GLenum type); +static type_glCreateShader* glCreateShader; + typedef void WINAPI type_glLinkProgram(GLuint program); +static type_glLinkProgram* glLinkProgram; + typedef void WINAPI type_glShaderSource(GLuint shader, GLsizei count, GLchar **string, GLint *length); +static type_glShaderSource* glShaderSource; + typedef void WINAPI type_glUseProgram(GLuint program); +static type_glUseProgram* glUseProgram; + typedef void WINAPI type_glGetProgramInfoLog(GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog); +static type_glGetProgramInfoLog* glGetProgramInfoLog; + typedef void WINAPI type_glGetShaderInfoLog(GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); +static type_glGetShaderInfoLog* glGetShaderInfoLog; + typedef void WINAPI type_glValidateProgram(GLuint program); +static type_glValidateProgram* glValidateProgram; + typedef void WINAPI type_glGetProgramiv(GLuint program, GLenum pname, GLint *params); +static type_glGetProgramiv* glGetProgramiv; + typedef GLint WINAPI type_glGetUniformLocation(GLuint program, const GLchar *name); +static type_glGetUniformLocation* glGetUniformLocation; + typedef void WINAPI type_glUniform4fv(GLint location, GLsizei count, const GLfloat* value); +static type_glUniform4fv* glUniform4fv; + typedef void WINAPI type_glUniform4i(GLint location, GLint v0, GLint v1, GLint v2, GLint v3); +static type_glUniform4i* glUniform4i; + typedef void WINAPI type_glUniform4ui(GLint location, GLint v0, GLint v1, GLint v2, GLint v3); +static type_glUniform4ui* glUniform4ui; + typedef void WINAPI type_glUniform1i(GLint location, GLint v0); +static type_glUniform1i* glUniform1i; + typedef void WINAPI type_glUniform1iv(GLint location, GLsizei count, const GLint* value); +static type_glUniform1iv* glUniform1iv; + typedef void WINAPI type_glUniform1f(GLint location, GLfloat v0); +static type_glUniform1f* glUniform1f; + typedef void WINAPI type_glUniform1fv(GLint location, GLsizei count, const GLfloat* value); +static type_glUniform1fv* glUniform1fv; + typedef void WINAPI type_glUniform2fv(GLint location, GLsizei count, const GLfloat* value); +static type_glUniform2fv* glUniform2fv; + typedef void WINAPI type_glUniform3fv(GLint location, GLsizei count, const GLfloat* value); +static type_glUniform3fv* glUniform3fv; + typedef void WINAPI type_glUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2); +static type_glUniform3f* glUniform3f; + typedef void WINAPI type_glUniform3iv(GLint location, GLsizei count, const GLint* value); +static type_glUniform3iv* glUniform3iv; + typedef void WINAPI type_glUniform3i(GLint location, GLint v0, GLint v1, GLint v2); +static type_glUniform3i* glUniform3i; + typedef void WINAPI type_glEnableVertexAttribArray(GLuint index); +static type_glEnableVertexAttribArray* glEnableVertexAttribArray; + typedef void WINAPI type_glDisableVertexAttribArray(GLuint index); +static type_glDisableVertexAttribArray* glDisableVertexAttribArray; + typedef GLint WINAPI type_glGetAttribLocation(GLuint program, const GLchar *name); +static type_glGetAttribLocation* glGetAttribLocation; + typedef void WINAPI type_glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); +static type_glVertexAttribPointer* glVertexAttribPointer; + typedef void WINAPI type_glVertexAttribIPointer (GLuint index, GLint size, GLenum type, GLsizei stride, const void *pointer); +static type_glVertexAttribIPointer* glVertexAttribIPointer; + typedef void WINAPI type_glBindVertexArray(GLuint array); +static type_glBindVertexArray* glBindVertexArray; + typedef void WINAPI type_glGenVertexArrays(GLsizei n, GLuint *arrays); +static type_glGenVertexArrays* glGenVertexArrays; + typedef void WINAPI type_glBindBuffer(GLenum target, GLuint buffer); +static type_glBindBuffer* glBindBuffer; + typedef void WINAPI type_glBindBufferBase(GLenum target, GLuint index, GLuint buffer); +static type_glBindBufferBase* glBindBufferBase; + typedef void WINAPI type_glBufferSubData(GLenum target, GLuint offset, GLsizeiptr size, const void* data); +static type_glBufferSubData* glBufferSubData; + typedef void WINAPI type_glGenBuffers(GLsizei n, GLuint *buffers); +static type_glGenBuffers* glGenBuffers; + typedef void WINAPI type_glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage); +static type_glBufferData* glBufferData; + typedef void WINAPI type_glActiveTexture(GLenum texture); +static type_glActiveTexture* glActiveTexture; + typedef void WINAPI type_glDeleteProgram(GLuint program); +static type_glDeleteProgram* glDeleteProgram; + typedef void WINAPI type_glDeleteShader(GLuint shader); +static type_glDeleteShader* glDeleteShader; + typedef void WINAPI type_glDeleteFramebuffers(GLsizei n, const GLuint *framebuffers); +static type_glDeleteFramebuffers* glDeleteFramebuffers; + typedef void WINAPI type_glDrawBuffers(GLsizei n, const GLenum *bufs); +static type_glDrawBuffers* glDrawBuffers; + typedef void WINAPI type_glTexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels); +static type_glTexImage3D* glTexImage3D; + typedef void WINAPI type_glTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels); +static type_glTexSubImage3D* glTexSubImage3D; + typedef void WINAPI type_glDrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex); +static type_glDrawElementsBaseVertex* glDrawElementsBaseVertex; + typedef void WINAPI type_glGenerateMipmap(GLenum target); +static type_glGenerateMipmap* glGenerateMipmap; + typedef void WINAPI type_glDetachShader(GLuint program, GLuint shader); +static type_glDetachShader* glDetachShader; + typedef void WINAPI type_glDeleteBuffers(GLsizei n, const GLuint* buffers); +static type_glDeleteBuffers* glDeleteBuffers; + typedef void WINAPI type_glUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +static type_glUniformMatrix2fv* glUniformMatrix2fv; + typedef void WINAPI type_glUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +static type_glUniformMatrix3fv* glUniformMatrix3fv; + typedef void WINAPI type_glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +static type_glUniformMatrix4fv* glUniformMatrix4fv; + typedef void WINAPI type_glGetShaderiv(GLuint shader, GLenum pname, GLint* param); +static type_glGetShaderiv* glGetShaderiv; + typedef void WINAPI type_glDrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instancecount); +static type_glDrawArraysInstanced* glDrawArraysInstanced; + typedef void WINAPI type_glDrawElementsInstanced(GLenum mode, GLint count, GLenum type, const void* indices, GLsizei instancecount); +static type_glDrawElementsInstanced* glDrawElementsInstanced; #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092 @@ -1180,89 +1298,31 @@ typedef void WINAPI type_glDrawElementsInstanced(GLenum mode, GLint count, GLenu #define WGL_ALPHA_BITS_ARB 0x201B #define WGL_DEPTH_BITS_ARB 0x2022 + typedef HGLRC WINAPI wgl_create_context_attribs_arb(HDC hDC, HGLRC hShareContext, const int *attribList); +static wgl_create_context_attribs_arb* wglCreateContextAttribsARB; + typedef BOOL WINAPI wgl_get_pixel_format_attrib_iv_arb(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues); +static wgl_get_pixel_format_attrib_iv_arb* wglGetPixelFormatAttribivARB; + typedef BOOL WINAPI wgl_get_pixel_format_attrib_fv_arb(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues); +static wgl_get_pixel_format_attrib_fv_arb* wglGetPixelFormatAttribfvARB; + typedef BOOL WINAPI wgl_choose_pixel_format_arb(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats); +static wgl_choose_pixel_format_arb* wglChoosePixelFormatARB; + typedef BOOL WINAPI wgl_swap_interval_ext(int interval); -typedef const char * WINAPI wgl_get_extensions_string_ext(void); - -// @question consider to make all these functions global -struct OpenGL { - type_glTexImage2DMultisample* glTexImage2DMultisample; - type_glBindFramebuffer* glBindFramebuffer; - type_glGenFramebuffers* glGenFramebuffers; - type_glFramebufferTexture2D* glFramebufferTexture2D; - type_glCheckFramebufferStatus* glCheckFramebufferStatus; - type_glBlitFramebuffer* glBlitFramebuffer; - type_glAttachShader* glAttachShader; - type_glCompileShader* glCompileShader; - type_glCreateProgram* glCreateProgram; - type_glCreateShader* glCreateShader; - type_glLinkProgram* glLinkProgram; - type_glShaderSource* glShaderSource; - type_glUseProgram* glUseProgram; - type_glGetProgramInfoLog* glGetProgramInfoLog; - type_glGetShaderInfoLog* glGetShaderInfoLog; - type_glValidateProgram* glValidateProgram; - type_glGetProgramiv* glGetProgramiv; - type_glGetUniformLocation* glGetUniformLocation; - type_glUniform4fv* glUniform4fv; - type_glUniform4i* glUniform4i; - type_glUniform4ui* glUniform4ui; - type_glUniform1i* glUniform1i; - type_glUniform1iv* glUniform1iv; - type_glUniform1f* glUniform1f; - type_glUniform1fv* glUniform1fv; - type_glUniform2fv* glUniform2fv; - type_glUniform3fv* glUniform3fv; - type_glUniform3iv* glUniform3iv; - type_glUniform3i* glUniform3i; - type_glUniform3f* glUniform3f; - type_glEnableVertexAttribArray* glEnableVertexAttribArray; - type_glDisableVertexAttribArray* glDisableVertexAttribArray; - type_glGetAttribLocation* glGetAttribLocation; - type_glVertexAttribPointer* glVertexAttribPointer; - type_glVertexAttribIPointer* glVertexAttribIPointer; - type_glBindVertexArray* glBindVertexArray; - type_glGenVertexArrays* glGenVertexArrays; - type_glBindBuffer* glBindBuffer; - type_glBindBufferBase* glBindBufferBase; - type_glBufferSubData* glBufferSubData; - type_glGenBuffers* glGenBuffers; - type_glBufferData* glBufferData; - type_glActiveTexture* glActiveTexture; - type_glDeleteProgram* glDeleteProgram; - type_glDeleteShader* glDeleteShader; - type_glDeleteFramebuffers* glDeleteFramebuffers; - type_glDrawBuffers* glDrawBuffers; - type_glTexImage3D* glTexImage3D; - type_glTexSubImage3D* glTexSubImage3D; - type_glDrawElementsBaseVertex* glDrawElementsBaseVertex; - type_glGenerateMipmap* glGenerateMipmap; - type_glDetachShader* glDetachShader; - type_glDeleteBuffers* glDeleteBuffers; - type_glUniformMatrix2fv* glUniformMatrix2fv; - type_glUniformMatrix3fv* glUniformMatrix3fv; - type_glUniformMatrix4fv* glUniformMatrix4fv; - type_glGetShaderiv* glGetShaderiv; - type_glDrawArraysInstanced* glDrawArraysInstanced; - type_glDrawElementsInstanced* glDrawElementsInstanced; - - wgl_choose_pixel_format_arb* wglChoosePixelFormatARB; - wgl_create_context_attribs_arb* wglCreateContextAttribsARB; - wgl_swap_interval_ext* wglSwapIntervalEXT; - wgl_get_extensions_string_ext* wglGetExtensionsStringEXT; -}; - static wgl_swap_interval_ext* wglSwapIntervalEXT; -void set_pixel_format(HDC hdc, OpenGL* gl) +typedef const char* WINAPI wgl_get_extensions_string_ext(void); +static wgl_get_extensions_string_ext* wglGetExtensionsStringEXT; + +void set_pixel_format(HDC hdc) { int suggested_pixel_format_idx = 0; unsigned int extended_pick = 0; - if (gl->wglChoosePixelFormatARB) { + if (wglChoosePixelFormatARB) { int attr_list[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, @@ -1273,7 +1333,7 @@ void set_pixel_format(HDC hdc, OpenGL* gl) 0, }; - gl->wglChoosePixelFormatARB(hdc, attr_list, 0, 1, &suggested_pixel_format_idx, &extended_pick); + wglChoosePixelFormatARB(hdc, attr_list, 0, 1, &suggested_pixel_format_idx, &extended_pick); } if(!extended_pick) { @@ -1295,7 +1355,7 @@ void set_pixel_format(HDC hdc, OpenGL* gl) SetPixelFormat(hdc, suggested_pixel_format_idx, &suggested_pixel_format); } -bool gl_extensions_load(OpenGL* gl) +bool gl_extensions_load() { WNDCLASSA wc = {}; @@ -1323,15 +1383,15 @@ bool gl_extensions_load(OpenGL* gl) ); HDC hdc = GetDC(window); - set_pixel_format(hdc, gl); + set_pixel_format(hdc); HGLRC openGLRC = wglCreateContext(hdc); - if (!wglMakeCurrent(hdc, openGLRC) || !gl->wglGetExtensionsStringEXT) { + if (!wglMakeCurrent(hdc, openGLRC) || !wglGetExtensionsStringEXT) { return false; } - char *extension = (char *) gl->wglGetExtensionsStringEXT(); + char *extension = (char *) wglGetExtensionsStringEXT(); char *pos = extension; while(*pos) { @@ -1367,21 +1427,92 @@ const int win32_opengl_attribs[] = { 0, }; -void opengl_init(Window* window, OpenGL* gl) +void opengl_init_wgl() { - gl_extensions_load(gl); + wglChoosePixelFormatARB = (wgl_choose_pixel_format_arb *) wglGetProcAddress("wglChoosePixelFormatARB"); + wglCreateContextAttribsARB = (wgl_create_context_attribs_arb *) wglGetProcAddress("wglCreateContextAttribsARB"); + wglSwapIntervalEXT = (wgl_swap_interval_ext *) wglGetProcAddress("wglSwapIntervalEXT"); + wglGetExtensionsStringEXT = (wgl_get_extensions_string_ext *) wglGetProcAddress("wglGetExtensionsStringEXT"); +} - gl->wglChoosePixelFormatARB = (wgl_choose_pixel_format_arb *) wglGetProcAddress("wglChoosePixelFormatARB"); - gl->wglCreateContextAttribsARB = (wgl_create_context_attribs_arb *) wglGetProcAddress("wglCreateContextAttribsARB"); - gl->wglSwapIntervalEXT = (wgl_swap_interval_ext *) wglGetProcAddress("wglSwapIntervalEXT"); - wglSwapIntervalEXT = gl->wglSwapIntervalEXT; - gl->wglGetExtensionsStringEXT = (wgl_get_extensions_string_ext *) wglGetProcAddress("wglGetExtensionsStringEXT"); +void opengl_init_gl() +{ + glTexImage2DMultisample = (type_glTexImage2DMultisample *) wglGetProcAddress("glTexImage2DMultisample"); + glBindFramebuffer = (type_glBindFramebuffer *) wglGetProcAddress("glBindFramebuffer"); + glGenFramebuffers = (type_glGenFramebuffers *) wglGetProcAddress("glGenFramebuffers"); + glFramebufferTexture2D = (type_glFramebufferTexture2D *) wglGetProcAddress("glFramebufferTexture2D"); + glCheckFramebufferStatus = (type_glCheckFramebufferStatus *) wglGetProcAddress("glCheckFramebufferStatus"); + glBlitFramebuffer = (type_glBlitFramebuffer *) wglGetProcAddress("glBlitFramebuffer"); + glAttachShader = (type_glAttachShader *) wglGetProcAddress("glAttachShader"); + glCompileShader = (type_glCompileShader *) wglGetProcAddress("glCompileShader"); + glCreateProgram = (type_glCreateProgram *) wglGetProcAddress("glCreateProgram"); + glCreateShader = (type_glCreateShader *) wglGetProcAddress("glCreateShader"); + glLinkProgram = (type_glLinkProgram *) wglGetProcAddress("glLinkProgram"); + glShaderSource = (type_glShaderSource *) wglGetProcAddress("glShaderSource"); + glUseProgram = (type_glUseProgram *) wglGetProcAddress("glUseProgram"); + glGetProgramInfoLog = (type_glGetProgramInfoLog *) wglGetProcAddress("glGetProgramInfoLog"); + glGetShaderInfoLog = (type_glGetShaderInfoLog *) wglGetProcAddress("glGetShaderInfoLog"); + glValidateProgram = (type_glValidateProgram *) wglGetProcAddress("glValidateProgram"); + glGetProgramiv = (type_glGetProgramiv *) wglGetProcAddress("glGetProgramiv"); + glGetUniformLocation = (type_glGetUniformLocation *) wglGetProcAddress("glGetUniformLocation"); + glUniform4fv = (type_glUniform4fv *) wglGetProcAddress("glUniform4fv"); + glUniform4i = (type_glUniform4i *) wglGetProcAddress("glUniform4i"); + glUniform4ui = (type_glUniform4ui *) wglGetProcAddress("glUniform4ui"); + glUniform1i = (type_glUniform1i *) wglGetProcAddress("glUniform1i"); + glUniform1iv = (type_glUniform1iv *) wglGetProcAddress("glUniform1iv"); + glUniform1f = (type_glUniform1f *) wglGetProcAddress("glUniform1f"); + glUniform1fv = (type_glUniform1fv *) wglGetProcAddress("glUniform1fv"); + glUniform2fv = (type_glUniform2fv *) wglGetProcAddress("glUniform2fv"); + glUniform3fv = (type_glUniform3fv *) wglGetProcAddress("glUniform3fv"); + glUniform3iv = (type_glUniform3iv *) wglGetProcAddress("glUniform3iv"); + glUniform3i = (type_glUniform3i *) wglGetProcAddress("glUniform3i"); + glUniform3f = (type_glUniform3f *) wglGetProcAddress("glUniform3f"); + glEnableVertexAttribArray = (type_glEnableVertexAttribArray *) wglGetProcAddress("glEnableVertexAttribArray"); + glDisableVertexAttribArray = (type_glDisableVertexAttribArray *) wglGetProcAddress("glDisableVertexAttribArray"); + glGetAttribLocation = (type_glGetAttribLocation *) wglGetProcAddress("glGetAttribLocation"); + glVertexAttribPointer = (type_glVertexAttribPointer *) wglGetProcAddress("glVertexAttribPointer"); + glVertexAttribIPointer = (type_glVertexAttribIPointer *) wglGetProcAddress("glVertexAttribIPointer"); + glBindVertexArray = (type_glBindVertexArray *) wglGetProcAddress("glBindVertexArray"); + glGenVertexArrays = (type_glGenVertexArrays *) wglGetProcAddress("glGenVertexArrays"); + glBindBuffer = (type_glBindBuffer *) wglGetProcAddress("glBindBuffer"); + glBindBufferBase = (type_glBindBufferBase *) wglGetProcAddress("glBindBufferBase"); + glBufferSubData = (type_glBufferSubData *) wglGetProcAddress("glBufferSubData"); + glGenBuffers = (type_glGenBuffers *) wglGetProcAddress("glGenBuffers"); + glBufferData = (type_glBufferData *) wglGetProcAddress("glBufferData"); + glActiveTexture = (type_glActiveTexture *) wglGetProcAddress("glActiveTexture"); + glDeleteProgram = (type_glDeleteProgram *) wglGetProcAddress("glDeleteProgram"); + glDeleteShader = (type_glDeleteShader *) wglGetProcAddress("glDeleteShader"); + glDeleteFramebuffers = (type_glDeleteFramebuffers *) wglGetProcAddress("glDeleteFramebuffers"); + glDrawBuffers = (type_glDrawBuffers *) wglGetProcAddress("glDrawBuffers"); + glTexImage3D = (type_glTexImage3D *) wglGetProcAddress("glTexImage3D"); + glTexSubImage3D = (type_glTexSubImage3D *) wglGetProcAddress("glTexSubImage3D"); + glDrawElementsBaseVertex = (type_glDrawElementsBaseVertex *) wglGetProcAddress("glDrawElementsBaseVertex"); + glGenerateMipmap = (type_glGenerateMipmap *) wglGetProcAddress("glGenerateMipmap"); + glDetachShader = (type_glDetachShader *) wglGetProcAddress("glDetachShader"); + glDeleteBuffers = (type_glDeleteBuffers *) wglGetProcAddress("glDeleteBuffers"); + glUniformMatrix2fv = (type_glUniformMatrix2fv *) wglGetProcAddress("glUniformMatrix2fv"); + glUniformMatrix3fv = (type_glUniformMatrix3fv *) wglGetProcAddress("glUniformMatrix3fv"); + glUniformMatrix4fv = (type_glUniformMatrix4fv *) wglGetProcAddress("glUniformMatrix4fv"); + glGetShaderiv = (type_glGetShaderiv *) wglGetProcAddress("glGetShaderiv"); + glDrawArraysInstanced = (type_glDrawArraysInstanced *) wglGetProcAddress("glDrawArraysInstanced"); + glDrawElementsInstanced = (type_glDrawElementsInstanced *) wglGetProcAddress("glDrawElementsInstanced"); - set_pixel_format(window->hdc, gl); + if (wglSwapIntervalEXT) { + wglSwapIntervalEXT(0); + } +} + +void opengl_init(Window* window) +{ + gl_extensions_load(); + + opengl_init_wgl(); + + set_pixel_format(window->hdc); HGLRC openGLRC = 0; - if (gl->wglCreateContextAttribsARB) { - openGLRC = gl->wglCreateContextAttribsARB(window->hdc, 0, win32_opengl_attribs); + if (wglCreateContextAttribsARB) { + openGLRC = wglCreateContextAttribsARB(window->hdc, 0, win32_opengl_attribs); } if (!openGLRC) { @@ -1392,71 +1523,7 @@ void opengl_init(Window* window, OpenGL* gl) return; } - gl->glTexImage2DMultisample = (type_glTexImage2DMultisample *) wglGetProcAddress("glTexImage2DMultisample"); - gl->glBindFramebuffer = (type_glBindFramebuffer *) wglGetProcAddress("glBindFramebuffer"); - gl->glGenFramebuffers = (type_glGenFramebuffers *) wglGetProcAddress("glGenFramebuffers"); - gl->glFramebufferTexture2D = (type_glFramebufferTexture2D *) wglGetProcAddress("glFramebufferTexture2D"); - gl->glCheckFramebufferStatus = (type_glCheckFramebufferStatus *) wglGetProcAddress("glCheckFramebufferStatus"); - gl->glBlitFramebuffer = (type_glBlitFramebuffer *) wglGetProcAddress("glBlitFramebuffer"); - gl->glAttachShader = (type_glAttachShader *) wglGetProcAddress("glAttachShader"); - gl->glCompileShader = (type_glCompileShader *) wglGetProcAddress("glCompileShader"); - gl->glCreateProgram = (type_glCreateProgram *) wglGetProcAddress("glCreateProgram"); - gl->glCreateShader = (type_glCreateShader *) wglGetProcAddress("glCreateShader"); - gl->glLinkProgram = (type_glLinkProgram *) wglGetProcAddress("glLinkProgram"); - gl->glShaderSource = (type_glShaderSource *) wglGetProcAddress("glShaderSource"); - gl->glUseProgram = (type_glUseProgram *) wglGetProcAddress("glUseProgram"); - gl->glGetProgramInfoLog = (type_glGetProgramInfoLog *) wglGetProcAddress("glGetProgramInfoLog"); - gl->glGetShaderInfoLog = (type_glGetShaderInfoLog *) wglGetProcAddress("glGetShaderInfoLog"); - gl->glValidateProgram = (type_glValidateProgram *) wglGetProcAddress("glValidateProgram"); - gl->glGetProgramiv = (type_glGetProgramiv *) wglGetProcAddress("glGetProgramiv"); - gl->glGetUniformLocation = (type_glGetUniformLocation *) wglGetProcAddress("glGetUniformLocation"); - gl->glUniform4fv = (type_glUniform4fv *) wglGetProcAddress("glUniform4fv"); - gl->glUniform4i = (type_glUniform4i *) wglGetProcAddress("glUniform4i"); - gl->glUniform4ui = (type_glUniform4ui *) wglGetProcAddress("glUniform4ui"); - gl->glUniform1i = (type_glUniform1i *) wglGetProcAddress("glUniform1i"); - gl->glUniform1iv = (type_glUniform1iv *) wglGetProcAddress("glUniform1iv"); - gl->glUniform1f = (type_glUniform1f *) wglGetProcAddress("glUniform1f"); - gl->glUniform1fv = (type_glUniform1fv *) wglGetProcAddress("glUniform1fv"); - gl->glUniform2fv = (type_glUniform2fv *) wglGetProcAddress("glUniform2fv"); - gl->glUniform3fv = (type_glUniform3fv *) wglGetProcAddress("glUniform3fv"); - gl->glUniform3iv = (type_glUniform3iv *) wglGetProcAddress("glUniform3iv"); - gl->glUniform3i = (type_glUniform3i *) wglGetProcAddress("glUniform3i"); - gl->glUniform3f = (type_glUniform3f *) wglGetProcAddress("glUniform3f"); - gl->glEnableVertexAttribArray = (type_glEnableVertexAttribArray *) wglGetProcAddress("glEnableVertexAttribArray"); - gl->glDisableVertexAttribArray = (type_glDisableVertexAttribArray *) wglGetProcAddress("glDisableVertexAttribArray"); - gl->glGetAttribLocation = (type_glGetAttribLocation *) wglGetProcAddress("glGetAttribLocation"); - gl->glVertexAttribPointer = (type_glVertexAttribPointer *) wglGetProcAddress("glVertexAttribPointer"); - gl->glVertexAttribIPointer = (type_glVertexAttribIPointer *) wglGetProcAddress("glVertexAttribIPointer"); - gl->glBindVertexArray = (type_glBindVertexArray *) wglGetProcAddress("glBindVertexArray"); - gl->glGenVertexArrays = (type_glGenVertexArrays *) wglGetProcAddress("glGenVertexArrays"); - gl->glBindBuffer = (type_glBindBuffer *) wglGetProcAddress("glBindBuffer"); - gl->glBindBufferBase = (type_glBindBufferBase *) wglGetProcAddress("glBindBufferBase"); - gl->glBufferSubData = (type_glBufferSubData *) wglGetProcAddress("glBufferSubData"); - gl->glGenBuffers = (type_glGenBuffers *) wglGetProcAddress("glGenBuffers"); - gl->glBufferData = (type_glBufferData *) wglGetProcAddress("glBufferData"); - gl->glActiveTexture = (type_glActiveTexture *) wglGetProcAddress("glActiveTexture"); - gl->glDeleteProgram = (type_glDeleteProgram *) wglGetProcAddress("glDeleteProgram"); - gl->glDeleteShader = (type_glDeleteShader *) wglGetProcAddress("glDeleteShader"); - gl->glDeleteFramebuffers = (type_glDeleteFramebuffers *) wglGetProcAddress("glDeleteFramebuffers"); - gl->glDrawBuffers = (type_glDrawBuffers *) wglGetProcAddress("glDrawBuffers"); - gl->glTexImage3D = (type_glTexImage3D *) wglGetProcAddress("glTexImage3D"); - gl->glTexSubImage3D = (type_glTexSubImage3D *) wglGetProcAddress("glTexSubImage3D"); - gl->glDrawElementsBaseVertex = (type_glDrawElementsBaseVertex *) wglGetProcAddress("glDrawElementsBaseVertex"); - gl->glGenerateMipmap = (type_glGenerateMipmap *) wglGetProcAddress("glGenerateMipmap"); - gl->glDetachShader = (type_glDetachShader *) wglGetProcAddress("glDetachShader"); - gl->glDeleteBuffers = (type_glDeleteBuffers *) wglGetProcAddress("glDeleteBuffers"); - gl->glUniformMatrix2fv = (type_glUniformMatrix2fv *) wglGetProcAddress("glUniformMatrix2fv"); - gl->glUniformMatrix3fv = (type_glUniformMatrix3fv *) wglGetProcAddress("glUniformMatrix3fv"); - gl->glUniformMatrix4fv = (type_glUniformMatrix4fv *) wglGetProcAddress("glUniformMatrix4fv"); - gl->glGetShaderiv = (type_glGetShaderiv *) wglGetProcAddress("glGetShaderiv"); - gl->glDrawArraysInstanced = (type_glDrawArraysInstanced *) wglGetProcAddress("glDrawArraysInstanced"); - gl->glDrawElementsInstanced = (type_glDrawElementsInstanced *) wglGetProcAddress("glDrawElementsInstanced"); - - if (gl->wglSwapIntervalEXT) { - gl->wglSwapIntervalEXT(0); - } - - // @todo now do: OpenGLInit + opengl_init_gl(); } #endif \ No newline at end of file diff --git a/gpuapi/opengl/ShaderUtils.h b/gpuapi/opengl/ShaderUtils.h index df72bd1..0117814 100644 --- a/gpuapi/opengl/ShaderUtils.h +++ b/gpuapi/opengl/ShaderUtils.h @@ -11,78 +11,97 @@ #include "../../stdlib/Types.h" #include "../../math/matrix/MatrixFloat32.h" +#include "Opengl.h" inline -void shader_set_value(const OpenGL* gl, uint32 id, const char* name, bool value) +void shader_set_value(uint32 id, const char* name, bool value) { - gl->glUniform1i(gl->glGetUniformLocation(id, name), (int) value); + glUniform1i(glGetUniformLocation(id, name), (int) value); } inline -void shader_set_value(const OpenGL* gl, uint32 id, const char* name, int value) +void shader_set_value(uint32 id, const char* name, int value) { - gl->glUniform1i(gl->glGetUniformLocation(id, name), value); + glUniform1i(glGetUniformLocation(id, name), value); } inline -void shader_set_value(const OpenGL* gl, uint32 id, const char* name, float value) +void shader_set_value(uint32 id, const char* name, float value) { - gl->glUniform1f(gl->glGetUniformLocation(id, name), value); + glUniform1f(glGetUniformLocation(id, name), value); } inline -void shader_set_v2(const OpenGL* gl, uint32 id, const char* name, const float* value) +void shader_set_v2(uint32 id, const char* name, const float* value) { - gl->glUniform2fv(gl->glGetUniformLocation(id, name), 1, value); + glUniform2fv(glGetUniformLocation(id, name), 1, value); } inline -void shader_set_v3(const OpenGL* gl, uint32 id, const char* name, const float* value) +void shader_set_v3(uint32 id, const char* name, const float* value) { - gl->glUniform3fv(gl->glGetUniformLocation(id, name), 1, value); + glUniform3fv(glGetUniformLocation(id, name), 1, value); } inline -void shader_set_v4(const OpenGL* gl, uint32 id, const char* name, const float* value) +void shader_set_v4(uint32 id, const char* name, const float* value) { - gl->glUniform4fv(gl->glGetUniformLocation(id, name), 1, value); + glUniform4fv(glGetUniformLocation(id, name), 1, value); } inline -void shader_set_m2(const OpenGL* gl, uint32 id, const char* name, const float* value) +void shader_set_m2(uint32 id, const char* name, const float* value) { - gl->glUniformMatrix2fv(gl->glGetUniformLocation(id, name), 1, GL_FALSE, value); + glUniformMatrix2fv(glGetUniformLocation(id, name), 1, GL_FALSE, value); } inline -void shader_set_m3(const OpenGL* gl, uint32 id, const char* name, const float* value) +void shader_set_m3(uint32 id, const char* name, const float* value) { - gl->glUniformMatrix3fv(gl->glGetUniformLocation(id, name), 1, GL_FALSE, value); + glUniformMatrix3fv(glGetUniformLocation(id, name), 1, GL_FALSE, value); } inline -void shader_set_m4(const OpenGL* gl, uint32 id, const char* name, const float* value) +void shader_set_m4(uint32 id, const char* name, const float* value) { - gl->glUniformMatrix4fv(gl->glGetUniformLocation(id, name), 1, GL_FALSE, value); + glUniformMatrix4fv(glGetUniformLocation(id, name), 1, GL_FALSE, value); } inline -void shader_check_link_errors(const OpenGL* gl, uint32 id, char* log) +uint32 shader_get_attrib_location(uint32 id, const char* name) +{ + // By using this you can retreive the shader variable name at a point where and when you know it + // BUT set values later on in generalized functions without knowing the shader variable name + // Basically like pointers + return glGetAttribLocation(id, name); +} + +inline +uint32 shader_get_uniform_location(uint32 id, const char* name) +{ + // By using this you can retreive the shader variable name at a point where and when you know it + // BUT set values later on in generalized functions without knowing the shader variable name + // Basically like pointers + return glGetUniformLocation(id, name); +} + +inline +void shader_check_link_errors(uint32 id, char* log) { GLint success; - gl->glGetProgramiv(id, GL_LINK_STATUS, &success); + glGetProgramiv(id, GL_LINK_STATUS, &success); if (!success) { - gl->glGetProgramInfoLog(id, 1024, NULL, log); + glGetProgramInfoLog(id, 1024, NULL, log); } } inline -void shader_check_compile_errors(const OpenGL* gl, uint32 id, char* log) +void shader_check_compile_errors(uint32 id, char* log) { GLint success; - gl->glGetShaderiv(id, GL_COMPILE_STATUS, &success); + glGetShaderiv(id, GL_COMPILE_STATUS, &success); if (!success) { - gl->glGetShaderInfoLog(id, 1024, NULL, log); + glGetShaderInfoLog(id, 1024, NULL, log); } } diff --git a/image/Image.cpp b/image/Image.cpp index 6810718..7d0fcde 100644 --- a/image/Image.cpp +++ b/image/Image.cpp @@ -37,4 +37,31 @@ void image_from_file(RingMemory* ring, const char* path, Image* image) } } +void image_flip_vertical(RingMemory* ring, Image* image) +{ + uint32 stride = image->width * sizeof(uint32); + byte* temp = ring_get_memory(ring, image->pixel_count * sizeof(uint32)); + memcpy(temp, image->pixels, image->pixel_count * sizeof(uint32)); + + // Last row + byte* end = temp + image->pixel_count * sizeof(uint32) - image->width * sizeof(uint32); + + for (int y = 0; y < image->height; ++y) { + memcpy(image->pixels + y * stride, end - y * stride, stride); + } + + /* Flipping with small temp row + byte* temp_row = ring_get_memory(ring, stride); + + for (int y = 0; y < image->height / 2; ++y) { + memcpy(temp_row, image->pixels + y * stride, stride); + + memcpy(image->pixels + y * stride, image->pixels - y * stride, stride); + memcpy(image->pixels - y * stride, temp_row, stride); + } + */ + + image->order_rows = (byte) (!((bool) image->order_rows)); +} + #endif \ No newline at end of file diff --git a/math/matrix/MatrixFloat32.h b/math/matrix/MatrixFloat32.h index 7dd5641..e141d94 100644 --- a/math/matrix/MatrixFloat32.h +++ b/math/matrix/MatrixFloat32.h @@ -769,7 +769,7 @@ void mat4_perspective_sparse_rh( mat4_frustum_sparse_rh(matrix, -xmax, xmax, -ymax, ymax, znear, zfar); } -void mat4_ortho( +void mat4_ortho_sparse_rh( float *matrix, float left, float right, float bottom, float top, float near_dist, float far_dist @@ -779,19 +779,49 @@ void mat4_ortho( float fn_delta = far_dist - near_dist; matrix[0] = 2.0f / rl_delta; - matrix[1] = 0.0f; - matrix[2] = 0.0f; - matrix[3] = 0.0f; + //matrix[1] = 0.0f; + //matrix[2] = 0.0f; + //matrix[3] = 0.0f; - matrix[4] = 0.0f; + //matrix[4] = 0.0f; matrix[5] = 2.0f / tb_delta; - matrix[6] = 0.0f; - matrix[7] = 0.0f; + //matrix[6] = 0.0f; + //matrix[7] = 0.0f; - matrix[8] = 0.0f; - matrix[9] = 0.0f; + //matrix[8] = 0.0f; + //matrix[9] = 0.0f; matrix[10] = -2.0f / fn_delta; - matrix[11] = 0.0f; + //matrix[11] = 0.0f; + + matrix[12] = -(right + left) / rl_delta; + matrix[13] = -(top + bottom) / tb_delta; + matrix[14] = -(far_dist + near_dist) / fn_delta; + matrix[15] = 1.0f; +} + +void mat4_ortho_sparse_lh( + float *matrix, + float left, float right, float bottom, float top, + float near_dist, float far_dist +) { + float rl_delta = right - left; + float tb_delta = top - bottom; + float fn_delta = far_dist - near_dist; + + matrix[0] = 2.0f / rl_delta; + //matrix[1] = 0.0f; + //matrix[2] = 0.0f; + //matrix[3] = 0.0f; + + //matrix[4] = 0.0f; + matrix[5] = 2.0f / tb_delta; + //matrix[6] = 0.0f; + //matrix[7] = 0.0f; + + //matrix[8] = 0.0f; + //matrix[9] = 0.0f; + matrix[10] = 2.0f / fn_delta; + //matrix[11] = 0.0f; matrix[12] = -(right + left) / rl_delta; matrix[13] = -(top + bottom) / tb_delta; diff --git a/object/Vertex.h b/object/Vertex.h index 645bf1a..70b6a71 100644 --- a/object/Vertex.h +++ b/object/Vertex.h @@ -32,6 +32,15 @@ struct VertexRef { uint32 index; }; +// Data for the text shader +struct TextShader { + uint32 program_id; + uint32 matrix_id; + uint32 uv_id; + uint32 color_id; + uint32 sampler_id; +}; + enum VertexType { VERTEX_TYPE_POSITION = 1, VERTEX_TYPE_NORMAL = 2,