Anyone using glad?
Jason Jeffory via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 12 11:16:51 PST 2016
So, I finally got it to work by abandoning demios and static
linking. Derelict + dynamic linking worked with only about a min
of problems(copying the proper dll to the correct place). I'd
prefer static linking but I can deal with that later.
My current problem is: 1. The code doesn't work as expected: It
should show a type of triangle on the display, instead the whole
display is colored, probably user error as I cobbled together
some tutorial code. 2*. I get an access violation when exiting
the program. I have no idea how, why, or where this is
happening(except, obviously towards the end of the program...
probably a cleanup issue).
Any ideas? Thanks.
Here is the full code:
import std.stdio;
import std.string;
import std.conv;
import glad.gl.all;
import glad.gl.loader;
import derelict.glfw3.glfw3;
import std.exception;
immutable string minimalVertexShader = `
#version 120
attribute vec2 position;
void main(void)
{
gl_Position = vec4(position, 0, 1);
}
`;
immutable string minimalFragmentShader = `
#version 120
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`;
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
void main()
{
DerelictGLFW3.load();
glfwInit();
// Set all the required options for GLFW
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);
// Create a GLFWwindow object that we can use for GLFW's
functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT,
"LearnOpenGL", null, null);
glfwMakeContextCurrent(window);
if (window == null)
{
writeln("Failed to create GLFW window");
glfwTerminate();
return;
}
// Set the required callback functions
glfwSetKeyCallback(window, cast(GLFWkeyfun)&key_callback);
enforce(gladLoadGL()); // optionally you can pass a loader to
this function
writefln("OpenGL Version %d.%d loaded", GLVersion.major,
GLVersion.minor);
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
float[] vertices = [ -0.1, -0.1, 0.1, -0.1, -1, 1, 1, -0.1];
ushort[] indices = [0, 1, 2, 3];
uint vbo, ibo;
// Create VBO
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.length * float.sizeof,
vertices.ptr, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Create IBO
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.sizeof,
indices.ptr, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// Program
auto program = glCreateProgram();
// Vertex Shader
auto vsh = glCreateShader(GL_VERTEX_SHADER);
auto vshSrc = minimalVertexShader.toStringz;
glShaderSource(vsh, 1, &vshSrc, null);
glCompileShader(vsh);
glAttachShader(program, vsh);
// Fragment Shader
auto fsh = glCreateShader(GL_FRAGMENT_SHADER);
auto fshSrc = minimalFragmentShader.toStringz;
glShaderSource(fsh, 1, &fshSrc, null);
glCompileShader(fsh);
glAttachShader(program, fsh);
glLinkProgram(program);
glUseProgram(program);
auto position = glGetAttribLocation(program, "position");
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse
moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0f, 0f, 0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1, 0.9, 0.8, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(position);
glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 2 *
float.sizeof, null);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, null);
glDisableVertexAttribArray(position);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminates GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int
action, int mode)
{
write("Key Pressed = ");
writeln(key);
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
OpenGL Version 3.3 loaded
57
object.Error@(0): Access Violation
----------------
0x00000001
Program exited with code 1
More information about the Digitalmars-d-learn
mailing list