Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lesson0(setup): https://pastebin.com/89B4QWsQ
- import std.conv: to;
- import std.stdio;
- import std.string: toStringz;
- import bindbc.opengl;
- import bindbc.sdl;
- /// Screen dimensions
- const int WIDTH = 800, HEIGHT = 600;
- /// Window title
- const string TITLE = "OpenGL SDL D";
- /// Vertex shader
- const string vertexShaderSource = q{
- #version 330 core
- layout ( location = 0 ) in vec3 position;
- void main()
- {
- gl_Position = vec4(position.x, position.y, position.z, 1.0);
- }
- };
- /// Fragment shader
- const string fragmentShaderSource = q{
- #version 330 core
- out vec4 color;
- void main()
- {
- color = vec4( 1.0f, 0.5f, 0.2f, 1.0f );
- }
- };
- void main()
- {
- if(loadSDL() != sdlSupport)
- {
- std.stdio.stderr.writeln("Error loading SDL library");
- return;
- }
- if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
- {
- std.stdio.stderr.writeln("Error initializing SDL: ", SDL_GetError());
- return;
- }
- scope(exit)
- {
- SDL_Quit();
- }
- version(OSX)
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
- else
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- SDL_Window* window = SDL_CreateWindow(TITLE.toStringz, SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
- scope(exit)
- {
- SDL_DestroyWindow(window);
- }
- SDL_GLContext context = SDL_GL_CreateContext(window);
- scope(exit)
- {
- SDL_GL_DeleteContext(context);
- }
- const auto openglLoaded = loadOpenGL();
- if(openglLoaded != glSupport)
- {
- std.stdio.stderr.writeln("Error loading OpenGL library: ", openglLoaded.to!string);
- return;
- }
- glViewport(0, 0, WIDTH, HEIGHT);
- GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
- auto vs = vertexShaderSource.toStringz;
- glShaderSource(vertexShader, 1, &vs, null);
- glCompileShader(vertexShader);
- GLint success;
- GLchar[512] infoLog;
- glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &success);
- if(!success)
- {
- glGetShaderInfoLog(vertexShader, 512, null, cast(char*)infoLog);
- std.stdio.stderr.writeln("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n", infoLog);
- }
- GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- auto fs = fragmentShaderSource.toStringz;
- glShaderSource(fragmentShader, 1, &fs, null);
- glCompileShader(fragmentShader);
- glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
- if(!success)
- {
- glGetShaderInfoLog(fragmentShader, 512, null, cast(char*)infoLog);
- std.stdio.stderr.writeln("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n", infoLog);
- }
- GLuint shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
- if(!success)
- {
- glGetProgramInfoLog(shaderProgram, 512, null, cast(char*)infoLog);
- std.stdio.stderr.writeln("ERROR::SHADER::PROGRAM::LINKING_FAILED\n", infoLog);
- }
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- GLfloat[] vertices = [
- -0.5f, -0.5f, 0.0f, // bottom left
- 0.5f, -0.5f, 0.0f,
- 0.0f, 0.5f, 0.0f
- ];
- GLuint VBO, VAO;
- glGenVertexArrays(1, &VAO );
- scope(exit) { glDeleteVertexArrays(1, &VAO); }
- glGenBuffers(1, &VBO );
- scope(exit) { glDeleteBuffers(1, &VBO); }
- glBindVertexArray( VAO );
- glBindBuffer( GL_ARRAY_BUFFER, VBO );
- glBufferData( GL_ARRAY_BUFFER, vertices.length * GLfloat.sizeof, cast(GLfloat*)vertices, GL_STATIC_DRAW);
- writeln(vertices.sizeof);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * GLfloat.sizeof, cast(GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- bool quit = false;
- while(!quit)
- {
- SDL_Event event;
- while(SDL_PollEvent(&event) > 0)
- {
- switch(event.type)
- {
- case SDL_QUIT:
- quit = true;
- break;
- default:
- break;
- }
- }
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- glUseProgram(shaderProgram);
- glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glBindVertexArray( 0 );
- SDL_GL_SwapWindow(window);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement