Advertisement
Guest User

Untitled

a guest
Mar 14th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.42 KB | Source Code | 0 0
  1. Light set up:
  2.  
  3. ```c
  4. void smLight3D_MakePointLight(smLight3D* light)
  5. {
  6.     if (light->shadowTransforms == NULL)
  7.     {
  8.         light->shadowTransforms = smVector_Create(sizeof(mat4), 6);
  9.     }
  10.  
  11.     vec3 lightPos;
  12.     glm_vec3_copy(light->position, lightPos);
  13.  
  14.     mat4 shadowProj;
  15.     glm_perspective(glm_rad(90.0f),
  16.                     (float)SHADOW_WIDTH / (float)SHADOW_HEIGHT, 0.1f,
  17.                     150.0f, shadowProj);
  18.  
  19.     smVector_Clear(light->shadowTransforms);
  20.  
  21.     // For each face of the cube
  22.     // +X direction
  23.     {
  24.         mat4 view, result;
  25.         vec3 center, up = {0.0f, -1.0f, 0.0f};
  26.  
  27.         glm_vec3_add(lightPos, (vec3) {1.0f, 0.0f, 0.0f}, center);
  28.         glm_lookat(lightPos, center, up, view);
  29.         glm_mat4_mul(shadowProj, view, result);
  30.  
  31.         smVector_PushBack(light->shadowTransforms, &result);
  32.     }
  33.  
  34.     // -X direction
  35.     {
  36.         mat4 view, result;
  37.         vec3 center, up = {0.0f, -1.0f, 0.0f};
  38.  
  39.         glm_vec3_add(lightPos, (vec3) {-1.0f, 0.0f, 0.0f}, center);
  40.         glm_lookat(lightPos, center, up, view);
  41.         glm_mat4_mul(shadowProj, view, result);
  42.  
  43.         smVector_PushBack(light->shadowTransforms, &result);
  44.     }
  45.  
  46.     // +Y direction
  47.     {
  48.         mat4 view, result;
  49.         vec3 center, up = {0.0f, 0.0f, 1.0f};
  50.  
  51.         glm_vec3_add(lightPos, (vec3) {0.0f, 1.0f, 0.0f}, center);
  52.         glm_lookat(lightPos, center, up, view);
  53.         glm_mat4_mul(shadowProj, view, result);
  54.  
  55.         smVector_PushBack(light->shadowTransforms, &result);
  56.     }
  57.  
  58.     // -Y direction
  59.     {
  60.         mat4 view, result;
  61.         vec3 center, up = {0.0f, 0.0f, -1.0f};
  62.  
  63.         glm_vec3_add(lightPos, (vec3) {0.0f, -1.0f, 0.0f}, center);
  64.         glm_lookat(lightPos, center, up, view);
  65.         glm_mat4_mul(shadowProj, view, result);
  66.  
  67.         smVector_PushBack(light->shadowTransforms, &result);
  68.     }
  69.  
  70.     // +Z direction
  71.     {
  72.         mat4 view, result;
  73.         vec3 center, up = {0.0f, -1.0f, 0.0f};
  74.  
  75.         glm_vec3_add(lightPos, (vec3) {0.0f, 0.0f, 1.0f}, center);
  76.         glm_lookat(lightPos, center, up, view);
  77.         glm_mat4_mul(shadowProj, view, result);
  78.  
  79.         smVector_PushBack(light->shadowTransforms, &result);
  80.     }
  81.  
  82.     // -Z direction
  83.     {
  84.         mat4 view, result;
  85.         vec3 center, up = {0.0f, -1.0f, 0.0f};
  86.  
  87.         glm_vec3_add(lightPos, (vec3) {0.0f, 0.0f, -1.0f}, center);
  88.         glm_lookat(lightPos, center, up, view);
  89.         glm_mat4_mul(shadowProj, view, result);
  90.  
  91.         smVector_PushBack(light->shadowTransforms, &result);
  92.     }
  93.  
  94.     // The OpenGL code remains the same
  95.     glGenFramebuffers(1, &light->depthMapFBO);
  96.  
  97.     // Create depth cubemap texture
  98.     glGenTextures(1, &light->depthCubemap);
  99.     glBindTexture(GL_TEXTURE_CUBE_MAP, light->depthCubemap);
  100.  
  101.     for (unsigned int i = 0; i < 6; ++i)
  102.     {
  103.         glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
  104.                      GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT,
  105.                      0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  106.     }
  107.  
  108.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,
  109.                     GL_NEAREST);
  110.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,
  111.                     GL_NEAREST);
  112.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
  113.                     GL_CLAMP_TO_EDGE);
  114.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,
  115.                     GL_CLAMP_TO_EDGE);
  116.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R,
  117.                     GL_CLAMP_TO_EDGE);
  118.  
  119.     // Attach depth cubemap as the FBO's depth buffer
  120.     glBindFramebuffer(GL_FRAMEBUFFER, light->depthMapFBO);
  121.     glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  122.                          light->depthCubemap, 0);
  123.     glDrawBuffer(GL_NONE); // No color buffer is drawn
  124.     glReadBuffer(GL_NONE); // No need to read from a buffer
  125.  
  126.     // Ensure framebuffer is complete
  127.     if (glCheckFramebufferStatus(GL_FRAMEBUFFER) !=
  128.         GL_FRAMEBUFFER_COMPLETE)
  129.         printf("Framebuffer not complete!\n");
  130.  
  131.     // Unbind framebuffer
  132.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  133. }
  134. ```
  135.  
  136. Model rendering:
  137.  
  138. ```c
  139. // This function is only a helper function that gets called later.
  140. void smModel_Draw(smModel* mesh, smTransform* trans, smShader shader)
  141. {
  142.     if (mesh->meshes == NULL)
  143.     {
  144.         smModel_Create(mesh);
  145.     }
  146.  
  147.     smShader_Use(shader);
  148.  
  149.     for (int i = 0; i < mesh->meshes->size; ++i)
  150.     {
  151.         smMesh* actualMesh = (smMesh*)smVector_Get(mesh->meshes, i);
  152.  
  153.         if (mesh->extractTexture)
  154.         {
  155.             // bind appropriate textures
  156.             unsigned int diffuseNr = 1;
  157.             unsigned int specularNr = 1;
  158.             unsigned int normalNr = 1;
  159.             unsigned int heightNr = 1;
  160.  
  161.             for (unsigned int j = 0; j < actualMesh->textures->size;
  162.                  j++)
  163.             {
  164.                 glActiveTexture(GL_TEXTURE0 + j);
  165.  
  166.                 char       number[128];
  167.                 smTexture* texture =
  168.                     (smTexture*)smVector_Get(actualMesh->textures, j);
  169.                 if (strcmp(texture->type, "texture_diffuse") == 0)
  170.                 {
  171.                     strcpy(number, "");
  172.                     sprintf(number, "%d", diffuseNr++);
  173.                 }
  174.                 else if (strcmp(texture->type, "texture_specular") ==
  175.                          0)
  176.                 {
  177.                     strcpy(number, "");
  178.                     sprintf(number, "%d", specularNr++);
  179.                 }
  180.                 else if (strcmp(texture->type, "texture_normal") == 0)
  181.                 {
  182.                     strcpy(number, "");
  183.                     sprintf(number, "%d", normalNr++);
  184.                 }
  185.                 else if (strcmp(texture->type, "texture_height") == 0)
  186.                 {
  187.                     strcpy(number, "");
  188.                     sprintf(number, "%d", heightNr++);
  189.                 }
  190.  
  191.                 char name[128];
  192.                 sprintf(name, "%s", texture->type);
  193.  
  194.                 // now set the sampler to the correct texture unit
  195.                 glUniform1i(glGetUniformLocation(shader.ID, name), j);
  196.  
  197.                 unsigned int id = ((smTexture*)smVector_Get(
  198.                                        actualMesh->textures, j))
  199.                                       ->ID;
  200.  
  201.                 // and finally bind the texture
  202.                 glBindTexture(GL_TEXTURE_2D, id);
  203.             }
  204.         }
  205.         else
  206.         {
  207.             smShader_SetTexture2D(shader, "texture_diffuse",
  208.                                   mesh->texture, 0);
  209.             smShader_SetTexture2D(shader, "texture_normal",
  210.                                   mesh->normalTexture, 1);
  211.             smShader_SetTexture2D(shader, "texture_specular",
  212.                                   mesh->specularTexture, 2);
  213.         }
  214.  
  215.         smShader_SetMat4(shader, "projection", smState.persProj);
  216.  
  217.         mat4 view;
  218.         smCamera_GetViewMatrix(&smState.camera, view);
  219.  
  220.         smShader_SetMat4(shader, "view", view);
  221.  
  222.         mat4 transform;
  223.  
  224.         glm_mat4_identity(transform);
  225.  
  226.         glm_translate(transform,
  227.                       (vec3) {trans->position[0], trans->position[1],
  228.                               trans->position[2]});
  229.         glm_rotate(transform, trans->rotation[0],
  230.                    (vec3) {1.0f, 0.0f, 0.0f});
  231.         glm_rotate(transform, trans->rotation[1],
  232.                    (vec3) {0.0f, 1.0f, 0.0f});
  233.         glm_rotate(transform, trans->rotation[2],
  234.                    (vec3) {0.0f, 0.0f, 1.0f});
  235.         glm_scale(transform, (vec3) {trans->scale[0], trans->scale[1],
  236.                                      trans->scale[2]});
  237.  
  238.         smShader_SetMat4(shader, "model", transform);
  239.  
  240.         glActiveTexture(GL_TEXTURE0);
  241.  
  242.         // draw mesh
  243.         glBindVertexArray(actualMesh->VAO);
  244.         glDrawElements(GL_TRIANGLES,
  245.                        (unsigned int)actualMesh->indices->size,
  246.                        GL_UNSIGNED_INT, 0);
  247.  
  248.         glBindVertexArray(0);
  249.     }
  250. }
  251.  
  252. // Anytime you see the SM_ECS_ITER nonsense, just think of it as a for loop that loops over lights and models
  253. void smMeshRenderer_Sys()
  254. {
  255.     SM_ECS_ITER_START(smState.scene, SM_ECS_COMPONENT_TYPE(smLight3D))
  256.     {
  257.         smLight3D* light =
  258.             SM_ECS_GET(smState.scene, _entity, smLight3D);
  259.  
  260.         glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
  261.         glBindFramebuffer(GL_FRAMEBUFFER, light->depthMapFBO);
  262.         glClear(GL_DEPTH_BUFFER_BIT);
  263.  
  264.         smShader_Use(sm_shadowShader3d);
  265.  
  266. #ifdef SM_DEBUG_LEVEL_1
  267.         if (light->shadowTransforms == NULL)
  268.         {
  269.             smLight3D_MakePointLight(light);
  270.         }
  271. #endif
  272.  
  273.         for (unsigned int i = 0; i < 6; ++i)
  274.         {
  275.             char uniform[64];
  276.             sprintf(uniform, "shadowMatrices[%d]", i);
  277.             mat4* shadowTransform =
  278.                 (mat4*)smVector_Get(light->shadowTransforms, i);
  279.             smShader_SetMat4(sm_shadowShader3d, uniform,
  280.                              (*shadowTransform));
  281.         }
  282.  
  283.         smShader_SetFloat(sm_shadowShader3d, "farPlane", 1000.0f);
  284.         smShader_SetVec3(sm_shadowShader3d, "lightPos",
  285.                          light->position);
  286.  
  287.         SM_ECS_ITER_START(smState.scene,
  288.                           SM_ECS_COMPONENT_TYPE(smMeshRenderer))
  289.         {
  290.             smMeshRenderer* mesh =
  291.                 SM_ECS_GET(smState.scene, _entity, smMeshRenderer);
  292.             smTransform* trans =
  293.                 SM_ECS_GET(smState.scene, _entity, smTransform);
  294.  
  295.             mat4 transform;
  296.  
  297.             glm_mat4_identity(transform);
  298.  
  299.             glm_translate(transform, (vec3) {trans->position[0],
  300.                                              trans->position[1],
  301.                                              trans->position[2]});
  302.             glm_rotate(transform, trans->rotation[0],
  303.                        (vec3) {1.0f, 0.0f, 0.0f});
  304.             glm_rotate(transform, trans->rotation[1],
  305.                        (vec3) {0.0f, 1.0f, 0.0f});
  306.             glm_rotate(transform, trans->rotation[2],
  307.                        (vec3) {0.0f, 0.0f, 1.0f});
  308.             glm_scale(transform,
  309.                       (vec3) {trans->scale[0], trans->scale[1],
  310.                               trans->scale[2]});
  311.  
  312.             smShader_SetMat4(sm_shadowShader3d, "model", transform);
  313.  
  314.             smModel_Draw(mesh, trans, sm_shadowShader3d);
  315.         }
  316.         SM_ECS_ITER_END();
  317.  
  318.         glBindFramebuffer(GL_FRAMEBUFFER, 0);
  319.     }
  320.     SM_ECS_ITER_END();
  321.  
  322.     glViewport(0, 0, smState.window->width, smState.window->height);
  323.     // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  324.  
  325.     smShader_Use(sm_shader3d);
  326.  
  327.     SM_ECS_ITER_START(smState.scene,
  328.                       SM_ECS_COMPONENT_TYPE(smMeshRenderer))
  329.     {
  330.         smMeshRenderer* mesh =
  331.             SM_ECS_GET(smState.scene, _entity, smMeshRenderer);
  332.         smTransform* trans =
  333.             SM_ECS_GET(smState.scene, _entity, smTransform);
  334.  
  335.         int lightCount = 0;
  336.  
  337.         SM_ECS_ITER_START(smState.scene,
  338.                           SM_ECS_COMPONENT_TYPE(smLight3D))
  339.         {
  340.             smLight3D* light =
  341.                 SM_ECS_GET(smState.scene, _entity, smLight3D);
  342.  
  343.             glActiveTexture(GL_TEXTURE0 + lightCount);
  344.             glBindTexture(GL_TEXTURE_CUBE_MAP, light->depthCubemap);
  345.  
  346.             char uniformName[64];
  347.  
  348.             sprintf(uniformName, "depthMaps[%d]", lightCount);
  349.             smShader_SetInt(sm_shader3d, uniformName, lightCount);
  350.  
  351.             sprintf(uniformName, "lights[%d].pos", lightCount);
  352.             smShader_SetVec3(sm_shader3d, uniformName,
  353.                              light->position);
  354.  
  355.             sprintf(uniformName, "lights[%d].radius", lightCount);
  356.             smShader_SetFloat(sm_shader3d, uniformName,
  357.                               light->radius);
  358.  
  359.             sprintf(uniformName, "lights[%d].color", lightCount);
  360.             smShader_SetVec4(sm_shader3d, uniformName, light->color);
  361.  
  362.             sprintf(uniformName, "lights[%d].intensity", lightCount);
  363.             smShader_SetFloat(sm_shader3d, uniformName,
  364.                               light->intensity);
  365.  
  366.             sprintf(uniformName, "lights[%d].castShadows",
  367.                     lightCount);
  368.             smShader_SetBool(sm_shader3d, uniformName,
  369.                              light->castsShadows);
  370.  
  371.             lightCount++;
  372.         }
  373.         SM_ECS_ITER_END();
  374.  
  375.         smModel_Draw(mesh, trans, sm_shader3d);
  376.     }
  377.     SM_ECS_ITER_END();
  378. }
  379. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement