Fixing scissoring

This commit is contained in:
Krzosa Karol
2024-07-26 21:47:23 +02:00
parent 7803bc87a6
commit 9d02d5ab78
8 changed files with 572 additions and 616 deletions

View File

@@ -14,7 +14,8 @@ struct Vertex2D {
struct VertexNode2D {
VertexNode2D *next;
int count;
Vertex2D vertices[1024 * 64];
Vertex2D vertices[1024 * 16];
Rect2 scissor;
};
struct VertexList2D {
@@ -29,17 +30,27 @@ unsigned VBO, VAO;
Shader Shader2D;
Arena RenderArena;
Vec2 WindowSize;
Rect2 CurrentScissor;
Font MainFont;
Int FontLineSpacing;
Int FontCharSpacing;
void BeginFrameRender(Vec2 window_size) {
Rect2 GetScreenRectF() {
Rect2 result = {0, 0, WindowSize.x, WindowSize.y};
return result;
}
Rect2I GetScreenRectI() {
Rect2I result = {0, 0, (Int)WindowSize.x, (Int)WindowSize.y};
return result;
}
void BeginFrameRender() {
Clear(&RenderArena);
TotalVertexCount = 0;
Vertices.first = NULL;
Vertices.last = NULL;
WindowSize = window_size;
CurrentScissor = GetScreenRectF();
}
void EndFrameRender(Color color) {
@@ -50,8 +61,9 @@ void EndFrameRender(Color color) {
glDisable(GL_CULL_FACE);
glViewport(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y);
glScissor(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y);
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);
// Default draw using the font texture
glBindProgramPipeline(Shader2D.pipeline);
@@ -59,6 +71,12 @@ void EndFrameRender(Color color) {
float yinverse = 1.f / (WindowSize.y / 2.f);
glProgramUniform2f(Shader2D.vshader, 0, xinverse, yinverse);
for (VertexNode2D *it = Vertices.first; it; it = it->next) {
Rect2 rect = it->scissor;
GLint x = (GLint)rect.min.x;
GLint y = (GLint)rect.min.y;
GLsizei w = (GLsizei)(rect.max.x - rect.min.x);
GLsizei h = (GLsizei)(rect.max.y - rect.min.y);
glScissor(x, (GLint)WindowSize.y - h, w, h);
glNamedBufferSubData(VBO, 0, it->count * sizeof(Vertex2D), it->vertices);
glBindVertexArray(VAO);
GLint s_texture = 0; // texture unit that sampler2D will use in GLSL code
@@ -183,11 +201,39 @@ Shader CreateShader(char *glsl_vshader, char *glsl_fshader) {
} while (0)
#define SLL_QUEUE_ADD(f, l, n) SLL_QUEUE_ADD_MOD(f, l, n, next)
VertexNode2D *AllocVertexNode2D(Allocator allocator, VertexList2D *list) {
VertexNode2D *node = AllocType(allocator, VertexNode2D);
SLL_QUEUE_ADD(list->first, list->last, node);
return node;
}
void SetScissor(Rect2 rect) {
CurrentScissor = rect;
VertexNode2D *node = Vertices.last;
if (!node) {
node = AllocVertexNode2D(RenderArena, &Vertices);
node->scissor = rect;
return;
}
if (node->scissor != rect && node->count == 0) {
node->scissor = rect;
return;
}
if (node->scissor != rect) {
node = AllocVertexNode2D(RenderArena, &Vertices);
node->scissor = rect;
return;
}
}
Vertex2D *AllocVertex2D(Allocator allocator, VertexList2D *list, int count) {
VertexNode2D *node = list->last;
if (node == 0 || node->count + count > Lengthof(node->vertices)) {
node = AllocType(allocator, VertexNode2D);
SLL_QUEUE_ADD(list->first, list->last, node);
node = AllocVertexNode2D(allocator, list);
node->scissor = CurrentScissor;
}
TotalVertexCount += count;
@@ -249,7 +295,7 @@ void DrawRect(Rect2 rect, Color color) {
}
Vec2 DrawString(Font *font, String16 string, Vec2 pos, Color color, bool draw = true) {
pos.y += font->ascent;
pos.y += font->ascent - font->descent;
Vec2 original_pos = pos;
For(string) {
Glyph *g = GetGlyph(font, it);
@@ -277,25 +323,3 @@ Int GetLineSpacing(Font *font) {
Int result = (Int)(font->ascent - font->descent + font->line_gap);
return result;
}
void BeginScissor(Rect2 rect) {
glScissor((GLint)rect.min.x, (GLint)rect.min.y, (GLsizei)(rect.max.x - rect.min.x), (GLsizei)(rect.max.y - rect.min.y));
}
void BeginScissor(Rect2I rect) {
glScissor((GLint)rect.min.x, (GLint)rect.min.y, (GLsizei)(rect.max.x - rect.min.x), (GLsizei)(rect.max.y - rect.min.y));
}
void EndScissor() {
glScissor(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y);
}
Rect2 GetScreenRectF() {
Rect2 result = {0, 0, WindowSize.x, WindowSize.y};
return result;
}
Rect2I GetScreenRectI() {
Rect2I result = {0, 0, (Int)WindowSize.x, (Int)WindowSize.y};
return result;
}