| 369 | } |
| 370 | |
| 371 | void renderer_init() { |
| 372 | // OpenGL Setup |
| 373 | glEnable(GL_BLEND); |
| 374 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 375 | |
| 376 | state.render.vert_count = 0; |
| 377 | state.render.verts = (Vertex*)malloc(sizeof(Vertex) * MAX_RENDER_BATCH * 4); |
| 378 | |
| 379 | /* Creating vertex array & vertex buffer for the batch renderer */ |
| 380 | glCreateVertexArrays(1, &state.render.vao); |
| 381 | glBindVertexArray(state.render.vao); |
| 382 | |
| 383 | glCreateBuffers(1, &state.render.vbo); |
| 384 | glBindBuffer(GL_ARRAY_BUFFER, state.render.vbo); |
| 385 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * MAX_RENDER_BATCH * 4, NULL, |
| 386 | GL_DYNAMIC_DRAW); |
| 387 | |
| 388 | uint32_t* indices = (uint32_t*)malloc(sizeof(uint32_t) * MAX_RENDER_BATCH * 6); |
| 389 | |
| 390 | uint32_t offset = 0; |
| 391 | for (uint32_t i = 0; i < MAX_RENDER_BATCH * 6; i += 6) { |
| 392 | indices[i + 0] = offset + 0; |
| 393 | indices[i + 1] = offset + 1; |
| 394 | indices[i + 2] = offset + 2; |
| 395 | |
| 396 | indices[i + 3] = offset + 2; |
| 397 | indices[i + 4] = offset + 3; |
| 398 | indices[i + 5] = offset + 0; |
| 399 | offset += 4; |
| 400 | } |
| 401 | glCreateBuffers(1, &state.render.ibo); |
| 402 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.render.ibo); |
| 403 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_RENDER_BATCH * 6 * sizeof(uint32_t), indices, GL_STATIC_DRAW); |
| 404 | |
| 405 | free(indices); |
| 406 | // Setting the vertex layout |
| 407 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL); |
| 408 | glEnableVertexAttribArray(0); |
| 409 | |
| 410 | glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t)offsetof(Vertex, border_color)); |
| 411 | glEnableVertexAttribArray(1); |
| 412 | |
| 413 | glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t)offsetof(Vertex, border_width)); |
| 414 | glEnableVertexAttribArray(2); |
| 415 | |
| 416 | glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t)offsetof(Vertex, color)); |
| 417 | glEnableVertexAttribArray(3); |
| 418 | |
| 419 | glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, texcoord)); |
| 420 | glEnableVertexAttribArray(4); |
| 421 | |
| 422 | glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, tex_index)); |
| 423 | glEnableVertexAttribArray(5); |
| 424 | |
| 425 | glVertexAttribPointer(6, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, scale)); |
| 426 | glEnableVertexAttribArray(6); |
| 427 | |
| 428 | glVertexAttribPointer(7, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(intptr_t*)offsetof(Vertex, pos_px)); |
no test coverage detected