Using OpenGL
Darren via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 3 03:30:13 PDT 2016
> It's not quite in a practically-usable state yet, but the SDL2
> & OpenGL wrapper I'm working on may interest you as an example
> implementation if nothing else.
> https://github.com/pineapplemachine/mach.d/tree/master/mach/sdl
I'm going to take a look at this, once I get my bearings, on the
merits of the name alone!
I'm sure there are a few tutorials that make use of SDL2 that I
can use. My hope is that once I know how to set up, learning
OpenGL will be more a matter of D-ifying the C-code.
I'll post the modified code I'm trying to get work in full below.
Some code is commented out because I couldn't make it work. Also
any tips for making the code nicer would be welcome (e.g. would I
change the const GLunit into enums?).
#####
import derelict.glfw3.glfw3;
import derelict.opengl3.gl3;
import std.stdio;
void key_callback(GLFWwindow* window, int key, int scancode, int
action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
const GLuint WIDTH = 800, HEIGHT = 600;
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z,
1.0);\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
void main()
{
DerelictGLFW3.load();
DerelictGL3.load();
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT,
"LearnOpenGL", null, null);
glfwMakeContextCurrent(window);
DerelictGL3.reload();
//glfwSetKeyCallback(window, key_callback);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, null);
glCompileShader(vertexShader);
GLint success;
GLchar[512] infoLog;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
//glGetShaderInfoLog(vertexShader, 512, null, infoLog);
writeln("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n",
infoLog);
}
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource,
null);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
//glGetShaderInfoLog(fragmentShader, 512, null, infoLog);
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, infoLog);
writeln("ERROR::SHADER::PROGRAM::LINKING_FAILED\n",
infoLog);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLfloat[] vertices = [
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
];
GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.sizeof,
cast(void*)vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 *
GLfloat.sizeof, cast(GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
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);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
}
More information about the Digitalmars-d-learn
mailing list