Subversion Repositories Games.Carmageddon

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "gl_renderer.h"
  2. #include "brender/brender.h"
  3. #include "harness.h"
  4. #include "harness/trace.h"
  5. #include "resources/3d_frag.glsl.h"
  6. #include "resources/3d_vert.glsl.h"
  7. #include "resources/framebuffer_frag.glsl.h"
  8. #include "resources/framebuffer_vert.glsl.h"
  9. #include "stored_context.h"
  10.  
  11. #include <glad/glad.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. static GLuint screen_buffer_vao, screen_buffer_ebo;
  16. static GLuint fullscreen_quad_texture, palette_texture, depth_texture;
  17.  
  18. static GLuint shader_program_2d;
  19. static GLuint shader_program_3d;
  20. static GLuint framebuffer_id, framebuffer_texture = 0;
  21.  
  22. // holds the latest uploaded version of the colour_buffer. Available in shader for blending
  23. static GLuint current_colourbuffer_texture;
  24.  
  25. static uint8_t gl_palette[4 * 256]; // RGBA
  26. static uint8_t* screen_buffer_flip_pixels;
  27. static uint16_t* depth_buffer_flip_pixels;
  28.  
  29. static int render_width, render_height;
  30. static int vp_x, vp_y, vp_width, vp_height;
  31.  
  32. static br_pixelmap *last_colour_buffer, *last_depth_buffer;
  33. static int dirty_buffers = 0;
  34.  
  35. static tStored_material* current_material;
  36. static br_pixelmap* current_shade_table;
  37.  
  38. static br_matrix4 view_matrix;
  39.  
  40. // Increment flush_counter during flush, and upload_counter when we upload the colour_buffer texture.
  41. // If the counters are equal, we can avoid re-uploading the same thing.
  42. static unsigned int flush_counter = 0, colourbuffer_upload_counter = 0;
  43.  
  44. typedef struct gl_vertex {
  45.     br_vector3 p;
  46.     br_vector2 map;
  47.     br_vector3 n;
  48.     float colour_index; // float to allow interpolation
  49. } gl_vertex;
  50.  
  51. struct {
  52.     GLuint model, view, projection;
  53.     // gl_NormalMatrix replacement
  54.     GLuint normal_matrix;
  55.     GLuint clip_plane_count;
  56.     GLuint clip_planes[6];
  57.     GLuint colour_buffer_texture;
  58.     GLuint viewport_height;
  59.  
  60.     GLuint material_flags;
  61.     GLuint material_texture_enabled;
  62.     GLuint material_texture_pixelmap;
  63.     GLuint material_uv_transform;
  64.     GLuint material_shade_table;
  65.     GLuint material_blend_enabled;
  66.     GLuint material_blend_table;
  67.     GLuint material_index_base;
  68.     // GLuint material_index_range;  TODO: re-add when we add untextured lighting
  69.     GLuint material_shade_table_height;
  70.  
  71. } uniforms_3d;
  72.  
  73. struct {
  74.     GLuint pixels, palette;
  75. } uniforms_2d;
  76.  
  77. static GLuint CreateShaderProgram(char* name, const char* vertex_shader, const int vertex_shader_len, const char* fragment_shader, const int fragment_shader_len) {
  78.     int success;
  79.     char log_buffer[1024];
  80.     GLuint program;
  81.     GLuint v_shader, f_shader;
  82.  
  83.     program = glCreateProgram();
  84.     v_shader = glCreateShader(GL_VERTEX_SHADER);
  85.     const GLchar* vertex_sources[] = { vertex_shader };
  86.     glShaderSource(v_shader, 1, vertex_sources, &vertex_shader_len);
  87.     glCompileShader(v_shader);
  88.     glGetShaderiv(v_shader, GL_COMPILE_STATUS, &success);
  89.     if (!success) {
  90.         glGetShaderInfoLog(v_shader, 1024, NULL, log_buffer);
  91.         LOG_PANIC("shader %s failed to compile: %s", name, log_buffer);
  92.     }
  93.  
  94.     f_shader = glCreateShader(GL_FRAGMENT_SHADER);
  95.     const GLchar* fragment_sources[] = { fragment_shader };
  96.     glShaderSource(f_shader, 1, fragment_sources, &fragment_shader_len);
  97.     glCompileShader(f_shader);
  98.     glGetShaderiv(f_shader, GL_COMPILE_STATUS, &success);
  99.     if (!success) {
  100.         char log_buffer[1024];
  101.         glGetShaderInfoLog(f_shader, 1024, NULL, log_buffer);
  102.         LOG_PANIC("shader %s failed to compile: %s", name, log_buffer);
  103.     }
  104.  
  105.     glAttachShader(program, v_shader);
  106.     glAttachShader(program, f_shader);
  107.     glLinkProgram(program);
  108.     glDeleteShader(v_shader);
  109.     glDeleteShader(f_shader);
  110.  
  111.     GLint link_ok = GL_FALSE;
  112.     glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
  113.     if (!link_ok) {
  114.         glGetShaderInfoLog(program, 1024, NULL, log_buffer);
  115.         LOG_PANIC("shader program %s failed to link: %s", name, log_buffer);
  116.     }
  117.     return program;
  118. }
  119.  
  120. static GLint GetValidatedUniformLocation(GLuint program, char* uniform_name) {
  121.     GLint location;
  122.     location = glGetUniformLocation(program, uniform_name);
  123.     if (location == -1) {
  124.         LOG_PANIC("glGetUniformLocation(%d, %s) failed. Check the shader uniform names.", program, uniform_name);
  125.     }
  126.     return location;
  127. }
  128.  
  129. static void LoadShaders(void) {
  130.     shader_program_2d = CreateShaderProgram("framebuffer", RESOURCES_FRAMEBUFFER_VERT_GLSL, sizeof(RESOURCES_FRAMEBUFFER_VERT_GLSL), RESOURCES_FRAMEBUFFER_FRAG_GLSL, sizeof(RESOURCES_FRAMEBUFFER_FRAG_GLSL));
  131.     glUseProgram(shader_program_2d);
  132.     uniforms_2d.pixels = GetValidatedUniformLocation(shader_program_2d, "u_pixels");
  133.     uniforms_2d.palette = GetValidatedUniformLocation(shader_program_2d, "u_palette");
  134.  
  135.     // bind the uniform samplers to texture units:
  136.     glUniform1i(uniforms_2d.pixels, 0);
  137.     glUniform1i(uniforms_2d.palette, 1);
  138.  
  139.     shader_program_3d = CreateShaderProgram("3d", RESOURCES_3D_VERT_GLSL, sizeof(RESOURCES_3D_VERT_GLSL), RESOURCES_3D_FRAG_GLSL, sizeof(RESOURCES_3D_FRAG_GLSL));
  140.     glUseProgram(shader_program_3d);
  141.     uniforms_3d.clip_plane_count = GetValidatedUniformLocation(shader_program_3d, "u_clip_plane_count");
  142.     for (int i = 0; i < 6; i++) {
  143.         char name[32];
  144.         sprintf(name, "u_clip_planes[%d]", i);
  145.         uniforms_3d.clip_planes[i] = GetValidatedUniformLocation(shader_program_3d, name);
  146.     }
  147.  
  148.     uniforms_3d.model = GetValidatedUniformLocation(shader_program_3d, "u_model");
  149.     uniforms_3d.colour_buffer_texture = GetValidatedUniformLocation(shader_program_3d, "u_colour_buffer");
  150.     uniforms_3d.projection = GetValidatedUniformLocation(shader_program_3d, "u_projection");
  151.     uniforms_3d.view = GetValidatedUniformLocation(shader_program_3d, "u_view");
  152.     uniforms_3d.viewport_height = GetValidatedUniformLocation(shader_program_3d, "u_viewport_height");
  153.     uniforms_3d.normal_matrix = GetValidatedUniformLocation(shader_program_3d, "u_normal_matrix");
  154.  
  155.     uniforms_3d.material_flags = GetValidatedUniformLocation(shader_program_3d, "u_material_flags");
  156.     uniforms_3d.material_texture_enabled = GetValidatedUniformLocation(shader_program_3d, "u_material_texture_enabled");
  157.     uniforms_3d.material_texture_pixelmap = GetValidatedUniformLocation(shader_program_3d, "u_material_texture_pixelmap");
  158.     uniforms_3d.material_uv_transform = GetValidatedUniformLocation(shader_program_3d, "u_material_uv_transform");
  159.     uniforms_3d.material_shade_table = GetValidatedUniformLocation(shader_program_3d, "u_material_shade_table");
  160.     uniforms_3d.material_shade_table_height = GetValidatedUniformLocation(shader_program_3d, "u_material_shade_table_height");
  161.     uniforms_3d.material_blend_enabled = GetValidatedUniformLocation(shader_program_3d, "u_material_blend_enabled");
  162.     uniforms_3d.material_blend_table = GetValidatedUniformLocation(shader_program_3d, "u_material_blend_table");
  163.     uniforms_3d.material_index_base = GetValidatedUniformLocation(shader_program_3d, "u_material_index_base");
  164.     // TODO: re-add when we support untextured lighting
  165.     // uniforms_3d.material_index_range = GetValidatedUniformLocation(shader_program_3d, "u_material_index_range");
  166.  
  167.     // bind the uniform samplers to texture units
  168.     glUniform1i(uniforms_3d.material_texture_pixelmap, 0);
  169.     // palette occupies texture unit 1 but not required during 3d rendering
  170.     glUniform1i(uniforms_3d.material_shade_table, 2);
  171.     glUniform1i(uniforms_3d.material_blend_table, 3);
  172.     glUniform1i(uniforms_3d.colour_buffer_texture, 4);
  173. }
  174.  
  175. static void SetupFullScreenRectGeometry(void) {
  176.     float vertices[] = {
  177.         // positions          // colors           // texture coords
  178.         1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
  179.         1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,  // bottom right
  180.         -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
  181.         -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
  182.     };
  183.     unsigned int indices[] = {
  184.         0, 1, 3, // first triangle
  185.         1, 2, 3  // second triangle
  186.     };
  187.  
  188.     GLuint vbo;
  189.     glGenVertexArrays(1, &screen_buffer_vao);
  190.     glGenBuffers(1, &vbo);
  191.     glGenBuffers(1, &screen_buffer_ebo);
  192.  
  193.     glBindVertexArray(screen_buffer_vao);
  194.  
  195.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  196.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  197.  
  198.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screen_buffer_ebo);
  199.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  200.  
  201.     // position attribute
  202.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  203.     glEnableVertexAttribArray(0);
  204.     // color attribute
  205.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
  206.     glEnableVertexAttribArray(1);
  207.     // texture coord attribute
  208.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
  209.     glEnableVertexAttribArray(2);
  210.  
  211.     glBindVertexArray(0);
  212. }
  213.  
  214. void GLRenderer_Init(int pRender_width, int pRender_height) {
  215.     render_width = pRender_width;
  216.     render_height = pRender_height;
  217.  
  218.     LOG_INFO("OpenGL vendor string: %s", glGetString(GL_VENDOR));
  219.     LOG_INFO("OpenGL renderer string: %s", glGetString(GL_RENDERER));
  220.     LOG_INFO("OpenGL version string: %s", glGetString(GL_VERSION));
  221.     LOG_INFO("OpenGL shading language version string: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
  222.  
  223.     LoadShaders();
  224.     SetupFullScreenRectGeometry();
  225.  
  226.     // config
  227.     glDisable(GL_BLEND);
  228.     glDepthFunc(GL_LESS);
  229.     glClearColor(0, 0, 0, 1.0f);
  230.     glClear(GL_COLOR_BUFFER_BIT);
  231.     glEnable(GL_CULL_FACE);
  232.     glCullFace(GL_BACK);
  233.  
  234.     // textures
  235.     glGenTextures(1, &fullscreen_quad_texture);
  236.     glGenTextures(1, &palette_texture);
  237.     glGenTextures(1, &framebuffer_texture);
  238.     glGenTextures(1, &depth_texture);
  239.     glGenTextures(1, &current_colourbuffer_texture);
  240.  
  241.     // setup framebuffer
  242.     glGenFramebuffers(1, &framebuffer_id);
  243.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
  244.  
  245.     // set pixel storage alignment to 1 byte
  246.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  247.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  248.  
  249.     glBindTexture(GL_TEXTURE_2D, fullscreen_quad_texture);
  250.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  251.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  252.  
  253.     glBindTexture(GL_TEXTURE_2D, palette_texture);
  254.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  255.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  256.  
  257.     glBindTexture(GL_TEXTURE_2D, framebuffer_texture);
  258.     glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, render_width, render_height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, NULL);
  259.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  260.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  261.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer_texture, 0);
  262.  
  263.     glBindTexture(GL_TEXTURE_2D, current_colourbuffer_texture);
  264.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  265.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  266.  
  267.     glBindTexture(GL_TEXTURE_2D, depth_texture);
  268.     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, render_width, render_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 0);
  269.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture, 0);
  270.  
  271.     if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  272.         LOG_PANIC("Framebuffer is not complete!");
  273.     }
  274.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  275.  
  276.     screen_buffer_flip_pixels = malloc(sizeof(uint8_t) * render_width * render_height);
  277.     depth_buffer_flip_pixels = malloc(sizeof(uint16_t) * render_width * render_height);
  278.  
  279.     CHECK_GL_ERROR("initializeOpenGLContext");
  280. }
  281.  
  282. void GLRenderer_SetPalette(uint8_t* rgba_colors) {
  283.     for (int i = 0; i < 256; i++) {
  284.         gl_palette[i * 4] = rgba_colors[i * 4 + 2];
  285.         gl_palette[i * 4 + 1] = rgba_colors[i * 4 + 1];
  286.         gl_palette[i * 4 + 2] = rgba_colors[i * 4];
  287.         // palette index 0 is transparent, so set alpha to 0
  288.         if (i == 0) {
  289.             gl_palette[i * 4 + 3] = 0;
  290.         } else {
  291.             gl_palette[i * 4 + 3] = 0xff;
  292.         }
  293.     }
  294.  
  295.     glActiveTexture(GL_TEXTURE1);
  296.     glBindTexture(GL_TEXTURE_2D, palette_texture);
  297.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, gl_palette);
  298.  
  299.     // reset active texture back to default
  300.     glActiveTexture(GL_TEXTURE0);
  301.  
  302.     CHECK_GL_ERROR("GLRenderer_SetPalette");
  303. }
  304.  
  305. void GLRenderer_SetShadeTable(br_pixelmap* table) {
  306.     if (current_shade_table == table) {
  307.         return;
  308.     }
  309.  
  310.     // shade table uses texture unit 2
  311.     glActiveTexture(GL_TEXTURE2);
  312.     tStored_pixelmap* stored = table->stored;
  313.     glBindTexture(GL_TEXTURE_2D, stored->id);
  314.  
  315.     // reset active texture back to default
  316.     glActiveTexture(GL_TEXTURE0);
  317.     current_shade_table = table;
  318. }
  319.  
  320. void GLRenderer_SetBlendTable(br_pixelmap* table) {
  321.  
  322.     if (flush_counter != colourbuffer_upload_counter) {
  323.         GLRenderer_FlushBuffer(eFlush_color_buffer);
  324.         glActiveTexture(GL_TEXTURE4);
  325.         glBindTexture(GL_TEXTURE_2D, current_colourbuffer_texture);
  326.         glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, render_width, render_height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, last_colour_buffer->pixels);
  327.         colourbuffer_upload_counter = flush_counter;
  328.     }
  329.  
  330.     // blend table uses texture unit 3
  331.     glActiveTexture(GL_TEXTURE3);
  332.     tStored_pixelmap* stored = table->stored;
  333.     glBindTexture(GL_TEXTURE_2D, stored->id);
  334.  
  335.     // reset active texture back to default
  336.     glActiveTexture(GL_TEXTURE0);
  337. }
  338.  
  339. extern br_v1db_state v1db;
  340.  
  341. void GLRenderer_BeginScene(br_actor* camera, br_pixelmap* colour_buffer, br_pixelmap* depth_buffer) {
  342.     last_colour_buffer = colour_buffer;
  343.     last_depth_buffer = depth_buffer;
  344.     glViewport(colour_buffer->base_x, render_height - colour_buffer->height - colour_buffer->base_y, colour_buffer->width, colour_buffer->height);
  345.     glUseProgram(shader_program_3d);
  346.     glUniform1ui(uniforms_3d.viewport_height, render_height);
  347.  
  348.     br_camera* cam = camera->type_data;
  349.     current_material = NULL;
  350.     current_shade_table = NULL;
  351.  
  352.     // clip planes
  353.     int enabled_clip_planes = 0;
  354.     for (int i = 0; i < v1db.enabled_clip_planes.max; i++) {
  355.         if (v1db.enabled_clip_planes.enabled == NULL || !v1db.enabled_clip_planes.enabled[i]) {
  356.             continue;
  357.         }
  358.         br_vector4* v4 = v1db.enabled_clip_planes.enabled[i]->type_data;
  359.         glUniform4f(uniforms_3d.clip_planes[enabled_clip_planes], v4->v[0], v4->v[1], v4->v[2], v4->v[3]);
  360.         enabled_clip_planes++;
  361.     }
  362.     glUniform1i(uniforms_3d.clip_plane_count, enabled_clip_planes);
  363.  
  364.     // view matrix
  365.     BrMatrix4Copy34(&view_matrix, &v1db.camera_path[0].m);
  366.     BrMatrix4Inverse(&view_matrix, &view_matrix);
  367.     glUniformMatrix4fv(uniforms_3d.view, 1, GL_FALSE, &view_matrix.m[0][0]);
  368.  
  369.     // projection matrix
  370.     br_matrix4 projection;
  371.     BrMatrix4Perspective(&projection, cam->field_of_view, cam->aspect, cam->hither_z, cam->yon_z);
  372.     // hack: not sure why we have to do this, but this makes the result the same as `glm_perspective`
  373.     projection.m[2][2] *= -1;
  374.     glUniformMatrix4fv(uniforms_3d.projection, 1, GL_FALSE, &projection.m[0][0]);
  375.  
  376.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
  377. }
  378.  
  379. void GLRenderer_EndScene(void) {
  380.     //  switch back to default fb and reset state
  381.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  382.     glDepthMask(GL_TRUE);
  383.     glDepthMask(GL_TRUE);
  384.     CHECK_GL_ERROR("GLRenderer_EndScene");
  385. }
  386.  
  387. void GLRenderer_FullScreenQuad(uint8_t* screen_buffer) {
  388.  
  389.     glViewport(vp_x, vp_y, vp_width, vp_height);
  390.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  391.     glDisable(GL_DEPTH_TEST);
  392.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  393.  
  394.     glBindTexture(GL_TEXTURE_2D, fullscreen_quad_texture);
  395.     glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, render_width, render_height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, screen_buffer);
  396.  
  397.     glBindVertexArray(screen_buffer_vao);
  398.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screen_buffer_ebo);
  399.     glUseProgram(shader_program_2d);
  400.     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  401.     glBindVertexArray(0);
  402.  
  403.     glEnable(GL_DEPTH_TEST);
  404.     CHECK_GL_ERROR("GLRenderer_RenderFullScreenQuad");
  405. }
  406.  
  407. void GLRenderer_ClearBuffers(void) {
  408.     // clear our virtual framebuffer
  409.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
  410.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  411.  
  412.     // clear real framebuffer
  413.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  414.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  415.  
  416.     CHECK_GL_ERROR("GLRenderer_ClearBuffers");
  417. }
  418.  
  419. void GLRenderer_BufferModel(br_model* model) {
  420.     tStored_model_context* ctx;
  421.     v11model* v11;
  422.  
  423.     if (model->stored != NULL) {
  424.         LOG_PANIC("trying to build a stored model");
  425.     }
  426.  
  427.     v11 = model->prepared;
  428.     ctx = NewStoredModelContext();
  429.  
  430.     int total_verts = 0, total_faces = 0;
  431.     for (int i = 0; i < v11->ngroups; i++) {
  432.         total_verts += v11->groups[i].nvertices;
  433.         total_faces += v11->groups[i].nfaces;
  434.     }
  435.  
  436.     // override normals
  437.     br_vector3 v3 = { { 0, 0, 0 } };
  438.     for (int g = 0; g < v11->ngroups; g++) {
  439.         for (int i = 0; i < v11->groups[g].nvertices; i++) {
  440.             v11->groups[g].vertices[i].n = v3;
  441.         }
  442.     }
  443.     br_vector3 v0v1, v2v1, normal;
  444.     for (int g = 0; g < v11->ngroups; g++) {
  445.         v11group* group = &v11->groups[g];
  446.         for (int i = 0; i < group->nfaces; i++) {
  447.             v11face* f = &group->faces[i];
  448.             fmt_vertex* v0 = &group->vertices[f->vertices[0]];
  449.             fmt_vertex* v1 = &group->vertices[f->vertices[1]];
  450.             fmt_vertex* v2 = &group->vertices[f->vertices[2]];
  451.             BrVector3Sub(&v0v1, &v0->p, &v1->p);
  452.             BrVector3Sub(&v2v1, &v2->p, &v1->p);
  453.             BrVector3Cross(&normal, &v0v1, &v2v1);
  454.             BrVector3Accumulate(&v0->n, &normal);
  455.             BrVector3Accumulate(&v1->n, &normal);
  456.             BrVector3Accumulate(&v2->n, &normal);
  457.         }
  458.     }
  459.     for (int g = 0; g < v11->ngroups; g++) {
  460.         for (int i = 0; i < v11->groups[g].nvertices; i++) {
  461.             BrVector3Normalise(&v11->groups[g].vertices[i].n, &v11->groups[g].vertices[i].n);
  462.         }
  463.     }
  464.  
  465.     gl_vertex* verts = malloc(sizeof(gl_vertex) * total_verts);
  466.     unsigned int* indices = malloc(sizeof(int) * 3 * total_faces);
  467.  
  468.     int v_index = 0;
  469.     int i_index = 0;
  470.     int face_offset = 0;
  471.     for (int g = 0; g < v11->ngroups; g++) {
  472.         for (int i = 0; i < v11->groups[g].nvertices; i++) {
  473.             fmt_vertex* v = &v11->groups[g].vertices[i];
  474.             verts[v_index].p = v->p;
  475.             verts[v_index].n = v->n;
  476.             verts[v_index].map = v->map;
  477.             verts[v_index].colour_index = BR_ALPHA(v11->groups[g].vertex_colours[i]);
  478.             v_index++;
  479.         }
  480.         for (int i = 0; i < v11->groups[g].nfaces; i++) {
  481.             v11face* f = &v11->groups[g].faces[i];
  482.             indices[i_index++] = f->vertices[0] + face_offset;
  483.             indices[i_index++] = f->vertices[1] + face_offset;
  484.             indices[i_index++] = f->vertices[2] + face_offset;
  485.         }
  486.         face_offset += v11->groups[g].nvertices;
  487.     }
  488.  
  489.     glGenVertexArrays(1, &ctx->vao_id);
  490.     glGenBuffers(1, &ctx->vbo_id);
  491.     glGenBuffers(1, &ctx->ebo_id);
  492.  
  493.     // Vertices
  494.     glBindVertexArray(ctx->vao_id);
  495.     glBindBuffer(GL_ARRAY_BUFFER, ctx->vbo_id);
  496.     glBufferData(GL_ARRAY_BUFFER, sizeof(gl_vertex) * total_verts, verts, GL_STATIC_DRAW);
  497.     // pos
  498.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*)offsetof(gl_vertex, p));
  499.     glEnableVertexAttribArray(0);
  500.     // normal
  501.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*)offsetof(gl_vertex, n));
  502.     glEnableVertexAttribArray(1);
  503.     // uv coordinates
  504.     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*)offsetof(gl_vertex, map));
  505.     glEnableVertexAttribArray(2);
  506.     // color
  507.     glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(gl_vertex), (void*)offsetof(gl_vertex, colour_index));
  508.     glEnableVertexAttribArray(3);
  509.  
  510.     // Indices
  511.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ctx->ebo_id);
  512.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 3 * total_faces, indices, GL_STATIC_DRAW);
  513.     glBindVertexArray(0);
  514.  
  515.     free(verts);
  516.     free(indices);
  517.  
  518.     model->stored = ctx;
  519.  
  520.     CHECK_GL_ERROR("after build model");
  521. }
  522.  
  523. void setActiveMaterial(tStored_material* material) {
  524.     if (material == NULL || material == current_material) {
  525.         return;
  526.     }
  527.  
  528.     glUniformMatrix2x3fv(uniforms_3d.material_uv_transform, 1, GL_TRUE, &material->map_transform.m[0][0]);
  529.  
  530.     if (material->pixelmap) {
  531.         tStored_pixelmap* stored_px = material->pixelmap->stored;
  532.         if (stored_px == NULL) {
  533.             LOG_PANIC("stored_px is null for pixelmap %s", material->pixelmap->identifier);
  534.         }
  535.         glBindTexture(GL_TEXTURE_2D, stored_px->id);
  536.         glUniform1ui(uniforms_3d.material_texture_enabled, 1);
  537.     } else {
  538.         glUniform1ui(uniforms_3d.material_texture_enabled, 0);
  539.  
  540.         // index_base and index_range are only used for untextured materials
  541.         glUniform1ui(uniforms_3d.material_index_base, material->index_base);
  542.         // glUniform1ui(uniforms_3d.material_index_range, material->index_range);  TODO: re-add when we support untextured lighting
  543.     }
  544.  
  545.     if (material->shade_table) {
  546.         glUniform1ui(uniforms_3d.material_shade_table_height, material->shade_table->height);
  547.         GLRenderer_SetShadeTable(material->shade_table);
  548.     }
  549.  
  550.     if (material->index_blend) {
  551.         glUniform1ui(uniforms_3d.material_blend_enabled, 1);
  552.         GLRenderer_SetBlendTable(material->index_blend);
  553.         // materials with index_blend do not write to depth buffer (https://www.cwaboard.co.uk/viewtopic.php?p=105846&sid=58ad8910238000ca14b01dad85117175#p105846)
  554.         glDepthMask(GL_FALSE);
  555.     } else {
  556.         glUniform1ui(uniforms_3d.material_blend_enabled, 0);
  557.         glDepthMask(GL_TRUE);
  558.     }
  559.  
  560.     glUniform1ui(uniforms_3d.material_flags, material->flags);
  561.  
  562.     if (material->flags & (BR_MATF_TWO_SIDED | BR_MATF_ALWAYS_VISIBLE)) {
  563.         glDisable(GL_CULL_FACE);
  564.     } else {
  565.         glEnable(GL_CULL_FACE);
  566.     }
  567.  
  568.     CHECK_GL_ERROR("GLRenderer_RenderModelxx");
  569. }
  570.  
  571. void GLRenderer_Model(br_actor* actor, br_model* model, br_material* material, br_token render_type, br_matrix34 model_matrix) {
  572.     tStored_model_context* ctx;
  573.     v11group* group;
  574.     int element_index = 0;
  575.  
  576.     if (model->flags & BR_MODF_DETHRACE_FORCE_BUFFER_UPDATE) {
  577.         if (model->stored) {
  578.             ((br_object*)model->stored)->dispatch->_free((br_object*)model->stored);
  579.             model->stored = NULL;
  580.         }
  581.         GLRenderer_BufferModel(model);
  582.         model->flags &= ~BR_MODF_DETHRACE_FORCE_BUFFER_UPDATE;
  583.     }
  584.  
  585.     ctx = model->stored;
  586.     v11model* v11 = model->prepared;
  587.  
  588.     if (v11 == NULL) {
  589.         // LOG_WARN("No model prepared for %s", model->identifier);
  590.         return;
  591.     }
  592.  
  593.     // model matrix
  594.     br_matrix4 view_model_matrix, model_matrix4, inverse, inverse_transpose;
  595.     BrMatrix4Copy34(&model_matrix4, &model_matrix);
  596.     glUniformMatrix4fv(uniforms_3d.model, 1, GL_FALSE, model_matrix4.m[0]);
  597.  
  598.     // normal matrix (http://www.lighthouse3d.com/tutorials/glsl-12-tutorial/the-normal-matrix/)
  599.     BrMatrix4Mul(&view_model_matrix, &view_matrix, &model_matrix4);
  600.     BrMatrix4Inverse(&inverse, &view_model_matrix);
  601.     // transpose
  602.     for (int i = 0; i < 4; i++) {
  603.         for (int j = 0; j < 4; j++) {
  604.             inverse_transpose.m[i][j] = inverse.m[j][i];
  605.         }
  606.     }
  607.     glUniformMatrix4fv(uniforms_3d.normal_matrix, 1, GL_FALSE, &inverse_transpose.m[0][0]);
  608.  
  609.     glBindVertexArray(ctx->vao_id);
  610.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ctx->ebo_id);
  611.  
  612.     // set default material for this actor/model
  613.     setActiveMaterial(material->stored);
  614.  
  615.     switch (render_type) {
  616.     case BRT_TRIANGLE:
  617.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  618.         break;
  619.     case BRT_LINE:
  620.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  621.         glUniform1ui(uniforms_3d.material_index_base, 255);
  622.         glUniform1ui(uniforms_3d.material_flags, 0);
  623.         break;
  624.     default:
  625.         LOG_PANIC("render_type %d is not supported?!", render_type);
  626.     }
  627.  
  628.     for (int g = 0; g < v11->ngroups; g++) {
  629.         group = &v11->groups[g];
  630.         setActiveMaterial(group->stored);
  631.         glDrawElements(GL_TRIANGLES, group->nfaces * 3, GL_UNSIGNED_INT, (void*)(element_index * sizeof(int)));
  632.         element_index += group->nfaces * 3;
  633.     }
  634.  
  635.     glBindVertexArray(0);
  636.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  637.     dirty_buffers = 1;
  638.  
  639.     CHECK_GL_ERROR("GLRenderer_RenderModel");
  640. }
  641.  
  642. void GLRenderer_BufferMaterial(br_material* mat) {
  643.     tStored_material* stored = mat->stored;
  644.     if (!stored) {
  645.         stored = NewStoredMaterial();
  646.         mat->stored = stored;
  647.         if (mat->identifier) {
  648.             strcpy(stored->identifier, mat->identifier);
  649.         }
  650.     }
  651.     BrMatrix23Copy(&stored->map_transform, &mat->map_transform);
  652.     stored->pixelmap = mat->colour_map;
  653.     stored->flags = mat->flags;
  654.     stored->shade_table = mat->index_shade;
  655.     stored->index_base = mat->index_base;
  656.     stored->index_range = mat->index_range;
  657.     stored->index_blend = mat->index_blend;
  658. }
  659.  
  660. void GLRenderer_BufferTexture(br_pixelmap* pm) {
  661.     tStored_pixelmap* stored = pm->stored;
  662.     if (pm->stored) {
  663.     } else {
  664.         stored = NewStoredPixelmap();
  665.         glGenTextures(1, &stored->id);
  666.         pm->stored = stored;
  667.     }
  668.  
  669.     // sometimes the pixelmap has row_bytes > width. OpenGL expects linear pixels, so flatten it out
  670.     uint8_t* linear_pixels = malloc(sizeof(uint8_t) * pm->width * pm->height);
  671.     uint8_t* original_pixels = pm->pixels;
  672.     for (int y = 0; y < pm->height; y++) {
  673.         for (int x = 0; x < pm->width; x++) {
  674.             linear_pixels[y * pm->width + x] = original_pixels[y * pm->row_bytes + x];
  675.         }
  676.     }
  677.  
  678.     glBindTexture(GL_TEXTURE_2D, stored->id);
  679.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  680.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  681.     glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, pm->width, pm->height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, linear_pixels);
  682.     free(linear_pixels);
  683.  
  684.     CHECK_GL_ERROR("GLRenderer_BufferTexture");
  685. }
  686.  
  687. void GLRenderer_FlushBuffer(tRenderer_flush_type flush_type) {
  688.  
  689.     if (!dirty_buffers) {
  690.         return;
  691.     }
  692.  
  693.     // pull framebuffer into cpu memory to emulate BRender behavior
  694.     glBindTexture(GL_TEXTURE_2D, framebuffer_texture);
  695.     glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, screen_buffer_flip_pixels);
  696.  
  697.     // flip texture to match the expected orientation
  698.     int dest_y = render_height;
  699.     uint8_t* pm_pixels = last_colour_buffer->pixels;
  700.     uint8_t new_pixel;
  701.     for (int y = 0; y < render_height; y++) {
  702.         dest_y--;
  703.         for (int x = 0; x < render_width; x++) {
  704.             new_pixel = screen_buffer_flip_pixels[y * render_width + x];
  705.             if (new_pixel != 0) {
  706.                 pm_pixels[dest_y * render_width + x] = new_pixel;
  707.             }
  708.         }
  709.     }
  710.  
  711.     if (flush_type == eFlush_all) {
  712.  
  713.         // pull depthbuffer into cpu memory to emulate BRender behavior
  714.         glBindTexture(GL_TEXTURE_2D, depth_texture);
  715.         glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, depth_buffer_flip_pixels);
  716.  
  717.         dest_y = last_colour_buffer->height;
  718.         int src_y = render_height - last_colour_buffer->base_y - last_colour_buffer->height;
  719.         uint16_t* depth_pixels = last_depth_buffer->pixels;
  720.         for (int y = 0; y < last_colour_buffer->height; y++) {
  721.             dest_y--;
  722.             for (int x = 0; x < last_colour_buffer->width; x++) {
  723.                 uint16_t new_depth = depth_buffer_flip_pixels[src_y * render_width + last_colour_buffer->base_x + x];
  724.                 depth_pixels[dest_y * render_width + x] = new_depth;
  725.             }
  726.             src_y++;
  727.         }
  728.     }
  729.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
  730.     glClear(GL_COLOR_BUFFER_BIT);
  731.     flush_counter++;
  732.     dirty_buffers = 0;
  733. }
  734.  
  735. void GLRenderer_FlushBuffers(void) {
  736.     GLRenderer_FlushBuffer(eFlush_all);
  737. }
  738.  
  739. void GLRenderer_SetViewport(int x, int y, int width, int height) {
  740.     vp_x = x;
  741.     vp_y = y;
  742.     vp_width = width;
  743.     vp_height = height;
  744. }
  745.