Guest User

nope

a guest
Jul 8th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | Source Code | 0 0
  1. #include <glad/glad.h>
  2. #include <SDL3/SDL.h>
  3. #include <SDL3/SDL_main.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <chrono>
  8.  
  9. #include "shader_m.h"
  10. //#include "get_shaders.h"
  11.  
  12. #include <string.h>
  13.  
  14. #include <glm/glm.hpp>
  15. #include <glm/gtc/matrix_transform.hpp>
  16. //#include "cglm/cglm.h"
  17.  
  18. // firstly declaration and only then importing
  19. #define STB_IMAGE_IMPLEMENTATION
  20. #include <stb_image.h>
  21.  
  22. #define MAX_CHARACTERS 128 // ASCII range
  23. #define MAX_SIZE 100
  24.  
  25. SDL_Event event;
  26.  
  27. struct Global
  28. {
  29.   int fuck;
  30. };
  31.  
  32. struct integers
  33. {
  34.   unsigned int VBO, VAO, EBO;
  35.   unsigned int texture1, texture2;
  36. } INTs;
  37.  
  38. struct loggs1
  39. {
  40.   char name[15];
  41.   int success;
  42.   char infoLog[512];
  43. } loggs;
  44.  
  45. const unsigned int height = 900;
  46. const unsigned int width = 1500;
  47.  
  48. /* OBJECT PARAMETRS */
  49. /* ----------- */
  50. float vertices[] =
  51. {
  52.   //position        //colors         //texture coordinates
  53.   /* PLANE */
  54.  
  55.   0.5f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
  56.   -0.5f, 0.0f, 0.5f, 0.0f, 1.0f, 0.0f,
  57.   0.0f, 0.0f, -0.5f, 0.0f, 0.0f, 1.0f,
  58.   0.0f, 0.8f, 0.0f, 0.0f, 1.0f, 0.0f,
  59. };
  60.  
  61. unsigned int indices[] =
  62. {
  63.   0, 1, 2, //plane
  64.   0, 1, 3, // behind
  65.   0, 2, 3, // the right one
  66.   1, 2, 3// the left one
  67.  
  68. };
  69. /* ------------------- */
  70.  
  71. /* SHADERS */
  72. /* ------------------ */
  73. const char *VshaderSource =
  74.   "#version 440\n"
  75.   "layout (location = 0) in vec4 aPos;\n"
  76.   "layout (location = 1) in vec4 inColor;\n"
  77.  
  78.   "out vec4 exColor;\n"
  79.  
  80.   "uniform mat4 model;\n"
  81.   "uniform mat4 projection;\n"
  82.   "uniform mat4 view;\n"
  83.  
  84.   "void main()\n"
  85.   "{\n"
  86.   "   gl_Position = (projection * view * model) * aPos;\n"
  87.   "   exColor = inColor;\n"
  88.   "}\0";
  89.  
  90.  
  91. const char *FshaderSource =
  92.   "#version 440\n"
  93.   "in vec4 exColor;\n"
  94.  
  95.   "out vec4 outColor;\n"
  96.  
  97.   "void main()\n"
  98.   "{\n"
  99.     "outColor = exColor;\n"
  100.   "}\0";
  101. /* --------------------- */
  102.  
  103. //bool SDL_GL_MakeCurrent(SDL_Window *window, SDL_GLContext context);
  104. //void shader_get();
  105. //void linking_shaders();
  106. //char* read_shader_source(const char* filePath);
  107. //GLuint compile_shader(GLenum type, const char* source);
  108. //void init_shaders();
  109.  
  110. int main(int argc, char* argv[])
  111. {
  112.  
  113.   // struct Global declaring
  114.   struct Global gl_var;
  115.  
  116.   SDL_Window *window;                  
  117.   bool done = false;
  118.  
  119.   // initializing SDL3
  120.   SDL_Init(SDL_INIT_VIDEO);            
  121.   // Create an application window with the following settings:
  122.   window = SDL_CreateWindow(
  123.       "judas",                  
  124.       width,                      
  125.       height,              
  126.       SDL_WINDOW_OPENGL                
  127.   );
  128.  
  129.   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  130.   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  131.   SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  132.   // Check that the window was successfully created
  133.   if (window == NULL)
  134.   {
  135.       // In the case that the window could not be made...
  136.       SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not create window: %s\n", SDL_GetError());
  137.       return 1;
  138.   }
  139.  
  140.   // an OpenGL context
  141.   SDL_GLContext glcontext = SDL_GL_CreateContext(window);
  142.   // making window current
  143.   bool SDL_GL_MakeCurrent(SDL_Window *window, SDL_GLContext context);
  144.  
  145.   // GLAD initializing
  146.   if(!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
  147.   {
  148.     printf("failed to initialize glad\n");
  149.     return -1;
  150.   }
  151.  
  152.   // configure global opengl state
  153.   // -----------------------------
  154.   glEnable(GL_DEPTH_TEST);
  155.   unsigned int shader_program = load_glsl_shaders("src/shaders/frag.glsl", "src/shaders/vert.glsl");
  156.  
  157.   // generating vertex and fragment shaders
  158.   //shader_get();
  159.   //linking ones before
  160.   //linking_shaders();
  161.  
  162.   //binding all three vao, ebo and vbo
  163.   glGenVertexArrays(1, &INTs.VAO);
  164.   glGenBuffers(1, &INTs.EBO);
  165.   glGenBuffers(1, &INTs.VBO);
  166.  
  167.   //drawing two triangles
  168.   glBindVertexArray(INTs.VAO);
  169.   glBindBuffer(GL_ARRAY_BUFFER, INTs.VBO);
  170.   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  171.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, INTs.EBO);
  172.   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  173.  
  174.   //postions
  175.   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  176.   glEnableVertexAttribArray(0);
  177.  
  178.   //color of vertices
  179.   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(3* sizeof(float)));
  180.   glEnableVertexAttribArray(1);
  181.  
  182.   //init_shaders();
  183.   use_shader(shader_program);
  184.  
  185.   while (!done)
  186.   {
  187.  
  188.     uint32_t startTime = SDL_GetTicks();
  189.  
  190.     //after currTime
  191.     //double elapsedTime = (currTime - startTime) / 1000.0; // Convert to seconds.
  192.  
  193.     while (SDL_PollEvent(&event))
  194.     {
  195.         if (event.type == SDL_EVENT_QUIT)
  196.         {
  197.           done = true;
  198.         }
  199.     }
  200.  
  201.     glClearColor(0,0,0,1);
  202.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  203.  
  204.     use_shader(shader_program);
  205.  
  206.     // rotating stuff
  207.     mat4 model =  {
  208.       {1.0f, 0.0f, 0.0f, 0.0f},
  209.       {0.0f, 1.0f, 0.0f, 0.0f},
  210.       {0.0f, 0.0f, 1.0f, 0.0f},
  211.       {0.0f, 0.0f, 0.0f, 1.0f}
  212.     };
  213.     mat4 view =  {
  214.       {1.0f, 0.0f, 0.0f, 0.0f},
  215.       {0.0f, 1.0f, 0.0f, 0.0f},
  216.       {0.0f, 0.0f, 1.0f, 0.0f},
  217.       {0.0f, 0.0f, 0.0f, 1.0f}
  218.     };
  219.     mat4 projection =  {
  220.       {1.0f, 0.0f, 0.0f, 0.0f},
  221.       {0.0f, 1.0f, 0.0f, 0.0f},
  222.       {0.0f, 0.0f, 1.0f, 0.0f},
  223.       {0.0f, 0.0f, 0.0f, 1.0f}
  224.     };
  225.  
  226.     uint32_t currTime = SDL_GetTicks();
  227.     float elapsedTime = (currTime - startTime) / 1000.0;
  228.  
  229.     vec3 ve2 = { 0.5f, 1.0f, 0.0f };
  230.     glm_vec3_normalize(ve2);
  231.     glm_rotate_make(model, elapsedTime, ve2);
  232.  
  233.     vec3 vv = { 0.0f, 0.0f, -3.0f };
  234.     glm_vec3_normalize(vv);
  235.     glm_translate_make(view, vv);
  236.  
  237.     glm_perspective_resize((float)(width/height), projection);
  238.  
  239.     // retrieve the matrix uniform locations
  240.     unsigned int modelLoc = glGetUniformLocation(shader_program, "model");
  241.     unsigned int viewLoc  = glGetUniformLocation(shader_program, "view");
  242.  
  243.     // pass them to the shaders (3 different ways)
  244.     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, (float*)&model);
  245.     glUniformMatrix4fv(viewLoc, 1, GL_FALSE, (float*)&view);
  246.     set_mat_4(shader_program, "projection", projection);
  247.  
  248.     glBindVertexArray(INTs.VAO);
  249.     glDrawArrays(GL_TRIANGLES, 0, 12);
  250.  
  251.     SDL_GL_SwapWindow(window);
  252.     // Do game logic, present a frame, etc.
  253.   }
  254.  
  255.   // Close and destroy the window
  256.   SDL_GL_DestroyContext(glcontext);  
  257.   SDL_DestroyWindow(window);
  258.   glDeleteVertexArrays(1, &INTs.VAO);
  259.   glDeleteBuffers(1, &INTs.VBO);
  260.  
  261.   // Clean up
  262.   SDL_Quit();
  263.   return 0;
  264. }
Add Comment
Please, Sign In to add comment