Advertisement
Pillager86

Lesson1

Apr 25th, 2020
3,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.83 KB | None | 0 0
  1. // Lesson0(setup): https://pastebin.com/89B4QWsQ
  2.  
  3. import std.conv: to;
  4. import std.stdio;
  5. import std.string: toStringz;
  6.  
  7. import bindbc.opengl;
  8. import bindbc.sdl;
  9.  
  10. /// Screen dimensions
  11. const int WIDTH = 800, HEIGHT = 600;
  12. /// Window title
  13. const string TITLE = "OpenGL SDL D";
  14. /// Vertex shader
  15. const string vertexShaderSource = q{
  16.     #version 330 core
  17.     layout ( location = 0 ) in vec3 position;
  18.     void main()
  19.     {
  20.         gl_Position = vec4(position.x, position.y, position.z, 1.0);
  21.     }
  22. };
  23. /// Fragment shader
  24. const string fragmentShaderSource = q{
  25.     #version 330 core
  26.     out vec4 color;
  27.     void main()
  28.     {
  29.         color = vec4( 1.0f, 0.5f, 0.2f, 1.0f );
  30.     }
  31. };
  32.  
  33. void main()
  34. {
  35.     if(loadSDL() != sdlSupport)
  36.     {
  37.         std.stdio.stderr.writeln("Error loading SDL library");
  38.         return;
  39.     }
  40.    
  41.     if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
  42.     {
  43.         std.stdio.stderr.writeln("Error initializing SDL: ", SDL_GetError());
  44.         return;
  45.     }
  46.     scope(exit)
  47.     {
  48.         SDL_Quit();
  49.     }
  50.  
  51. version(OSX)
  52.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  53. else
  54.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
  55.  
  56.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  57.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  58.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  59.     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  60.  
  61.     SDL_Window* window = SDL_CreateWindow(TITLE.toStringz, SDL_WINDOWPOS_CENTERED,
  62.             SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
  63.     scope(exit)
  64.     {
  65.         SDL_DestroyWindow(window);
  66.     }
  67.    
  68.     SDL_GLContext context = SDL_GL_CreateContext(window);
  69.     scope(exit)
  70.     {
  71.         SDL_GL_DeleteContext(context);
  72.     }
  73.  
  74.     const auto openglLoaded = loadOpenGL();
  75.     if(openglLoaded != glSupport)
  76.     {
  77.         std.stdio.stderr.writeln("Error loading OpenGL library: ", openglLoaded.to!string);
  78.         return;
  79.     }
  80.  
  81.     glViewport(0, 0, WIDTH, HEIGHT);
  82.  
  83.     GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
  84.     auto vs = vertexShaderSource.toStringz;
  85.     glShaderSource(vertexShader, 1, &vs, null);
  86.     glCompileShader(vertexShader);
  87.     GLint success;
  88.     GLchar[512] infoLog;
  89.     glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &success);
  90.     if(!success)
  91.     {
  92.         glGetShaderInfoLog(vertexShader, 512, null, cast(char*)infoLog);
  93.         std.stdio.stderr.writeln("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n", infoLog);
  94.     }
  95.  
  96.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  97.     auto fs = fragmentShaderSource.toStringz;
  98.     glShaderSource(fragmentShader, 1, &fs, null);
  99.     glCompileShader(fragmentShader);
  100.     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  101.     if(!success)
  102.     {
  103.         glGetShaderInfoLog(fragmentShader, 512, null, cast(char*)infoLog);
  104.         std.stdio.stderr.writeln("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n", infoLog);
  105.     }
  106.  
  107.     GLuint shaderProgram = glCreateProgram();
  108.     glAttachShader(shaderProgram, vertexShader);
  109.     glAttachShader(shaderProgram, fragmentShader);
  110.     glLinkProgram(shaderProgram);
  111.     glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  112.     if(!success)
  113.     {
  114.         glGetProgramInfoLog(shaderProgram, 512, null, cast(char*)infoLog);
  115.         std.stdio.stderr.writeln("ERROR::SHADER::PROGRAM::LINKING_FAILED\n", infoLog);
  116.     }
  117.  
  118.     glDeleteShader(vertexShader);
  119.     glDeleteShader(fragmentShader);
  120.  
  121.     GLfloat[] vertices = [
  122.         -0.5f, -0.5f, 0.0f, // bottom left
  123.          0.5f, -0.5f, 0.0f,
  124.          0.0f,  0.5f, 0.0f
  125.     ];
  126.  
  127.     GLuint VBO, VAO;
  128.     glGenVertexArrays(1, &VAO );
  129.     scope(exit) { glDeleteVertexArrays(1, &VAO); }
  130.     glGenBuffers(1, &VBO );
  131.     scope(exit) { glDeleteBuffers(1, &VBO); }
  132.     glBindVertexArray( VAO );
  133.     glBindBuffer( GL_ARRAY_BUFFER, VBO );
  134.     glBufferData( GL_ARRAY_BUFFER, vertices.length * GLfloat.sizeof, cast(GLfloat*)vertices, GL_STATIC_DRAW);
  135.     writeln(vertices.sizeof);
  136.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * GLfloat.sizeof, cast(GLvoid*)0);
  137.     glEnableVertexAttribArray(0);
  138.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  139.     glBindVertexArray(0);
  140.  
  141.     bool quit = false;
  142.     while(!quit)
  143.     {
  144.         SDL_Event event;
  145.         while(SDL_PollEvent(&event) > 0)
  146.         {
  147.             switch(event.type)
  148.             {
  149.                 case SDL_QUIT:
  150.                     quit = true;
  151.                     break;
  152.                 default:
  153.                     break;
  154.             }
  155.         }
  156.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  157.         glClear(GL_COLOR_BUFFER_BIT);
  158.  
  159.         glUseProgram(shaderProgram);
  160.         glBindVertexArray(VAO);
  161.         glDrawArrays(GL_TRIANGLES, 0, 3);
  162.         glBindVertexArray( 0 );
  163.  
  164.         SDL_GL_SwapWindow(window);
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement