Guest User

Untitled

a guest
Sep 15th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <SDL2/SDL_events.h>
  2. #include <SDL2/SDL_render.h>
  3. #include <SDL2/SDL_video.h>
  4. #include <cstdio>
  5. #include <SDL2/SDL.h>
  6. #include <GL/glew.h>
  7. #include <SDL2/SDL_opengl.h>
  8. #include <GL/glut.h>
  9.  
  10. const GLchar* vertexShaderSource[] ={
  11.   "#version 460 core\nin vec3 pos; void main() {gl_Position = vec4(pos, 1.0); }"
  12. };
  13.  
  14. const GLchar* fragShaderSource[] = {
  15.   "#version 460 core\n out vec4 FragColor; void main() { FragColor = vec4(1.0);}"
  16. };
  17.  
  18. int main(){
  19.  
  20.   bool running = true;
  21.  
  22.   SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
  23.   SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
  24.   SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
  25.  
  26.  
  27.   SDL_Window* window;
  28.   SDL_Renderer* renderer;
  29.  
  30.   SDL_CreateWindowAndRenderer(600, 400, SDL_WINDOW_RESIZABLE, &window, &renderer);
  31.  
  32.   SDL_GLContext gl_context = SDL_GL_CreateContext(window);
  33.  
  34.   if (gl_context == NULL){
  35.     printf("Failed to create OpenGL context\n");
  36.     return -1;
  37.   }
  38.  
  39.   glewExperimental = true;
  40.  
  41.   GLenum glewError = glewInit();
  42.   if (glewError != GLEW_OK){
  43.     printf("Failed to init glew\n");
  44.     return -1;
  45.   }
  46.  
  47.   GLuint vsShader = glCreateShader(GL_VERTEX_SHADER);
  48.   glShaderSource(vsShader, 1, vertexShaderSource, NULL);
  49.   glCompileShader(vsShader);
  50.  
  51.   GLuint fsShader = glCreateShader(GL_FRAGMENT_SHADER);
  52.   glShaderSource(fsShader, 1, fragShaderSource, NULL);
  53.   glCompileShader(fsShader);
  54.  
  55.   GLuint program = glCreateProgram();
  56.   glAttachShader(program, vsShader);
  57.   glAttachShader(program, fsShader);
  58.   glLinkProgram(program);
  59.  
  60.   float vertices[] ={
  61.     0.0f, 0.5f, 0.0f,
  62.     -0.5f, -0.5f, 0.0f,
  63.     0.5f, -0.5f, 0.0f,
  64.   };
  65.  
  66.   GLuint VAO, VBO;
  67.   glGenVertexArrays(1, &VAO);
  68.   glGenBuffers(1, &VBO);
  69.  
  70.   glBindVertexArray(VAO);
  71.   glBindBuffer(GL_ARRAY_BUFFER, VBO);
  72.  
  73.   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  74.  
  75.   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  76.   glEnableVertexAttribArray(0);
  77.  
  78.   glClear(GL_COLOR_BUFFER_BIT);
  79.  
  80.   SDL_Event event;
  81.  
  82.   while(running){
  83.     while(SDL_PollEvent(&event)){
  84.       if (event.type == SDL_QUIT){
  85.         running = false;
  86.       }
  87.     }
  88.    
  89.     //glClear(GL_COLOR_BUFFER_BIT);
  90.    
  91.  
  92.     /*glDrawArrays(GL_TRIANGLES, 0, 9);*/
  93.  
  94.     SDL_GL_SwapWindow(window);
  95.   }
  96.  
  97.  
  98.   return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment