Sign in to follow this  
ste3e

OpenGL Derelict2 bindings for D

Recommended Posts

Maybe someone in the big wide world has Derelict2 running. [font=arial,helvetica,sans-serif]W[color=#000000][background=rgb(246, 246, 246)]hat I have [/background][/color][/font][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]done is created the most parred back, hello world type program [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]that could run under the GL3 enforcement policies dictated by [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]Derelict2, running on DMD2 on XP. Fixed function pipeline is out. The vertex positions [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]fit within clip space so there is no need to be sending [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]projection, view or model matricies to the vertex shader, nor any [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]need therein to be altering the vertex positions; they simply get [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]set to gl_Position. The fragment shader assigns red to [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]gl_FragColor. I have read somewhere that vaos are required for [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]GL3 and that vbos hang pointlessly without the chaperone of a [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]vao, regardless, the code can draw either vao or vbo depending on [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]which draw function is not commented out in the loop. The problem [/background][/font][/color][color=#000000][font=arial, helvetica, sans-serif][background=rgb(246, 246, 246)]is, however, that neither function draws anything. Yet all the initializations are returning true; the SDL window pops up and gets cleared to black but no red triangle. I can't see where the problem lies. [/background][/font][/color]

[CODE]
import std.stdio;
import std.string;
import std.conv;
import derelict.sdl2.sdl;
import derelict.opengl3.gl3;

pragma(lib, "DerelictUtil.lib");
pragma(lib, "DerelictSDL2.lib");
pragma(lib, "DerelictGL3.lib");

SDL_Window *win;
SDL_GLContext context;
int w=800, h=600;
bool running=true;
int shader = 0;
uint vao=0, vbo=0;

bool loadLibs(){
try{
DerelictSDL2.load();
}catch(Exception e){
writeln("Error loading SDL2 lib");
return false;
}
try{
DerelictGL3.load();
}catch(Exception e){
writeln("Error loading GL3 lib");
return false;
}
return true;
}
bool initSDL(){
if(SDL_Init(SDL_INIT_VIDEO) < 0){
writefln("Error initializing SDL");
return false;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

win=SDL_CreateWindow("3Doodle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_OPENGL |
SDL_WINDOW_SHOWN);
if(!win){
writefln("Error creating SDL window");
SDL_Quit();
return false;
}
context=SDL_GL_CreateContext(win);
SDL_GL_SetSwapInterval(1);

DerelictGL3.reload();

return true;
}
bool initGL(){
glClearColor(0.0, 0.0, 0.0, 1.0);
glViewport(0, 0, w, h);
return true;
}
bool initShaders(){
const string vshader="
#version 330
layout(location = 0) in vec4 pos;
void main(void)
{
gl_Position = pos;
}
";
const string fshader="
#version 330
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
";

shader=glCreateProgram();
if(shader == 0){
writeln("Error: GL did not assigh main shader program id");
return false;
}

int vshad=glCreateShader(GL_VERTEX_SHADER);
const char *vptr=toStringz(vshader);
glShaderSource(vshad, 1, &vptr, null);
glCompileShader(vshad);
int status, len;
glGetShaderiv(vshad, GL_COMPILE_STATUS, &status);
if(status==GL_FALSE){
glGetShaderiv(vshad, GL_INFO_LOG_LENGTH, &len);
char[] error=new char[len];
glGetShaderInfoLog(vshad, len, null, cast(char*)error);
writeln(error);
return false;
}

int fshad=glCreateShader(GL_FRAGMENT_SHADER);
const char *fptr=toStringz(fshader);
glShaderSource(fshad, 1, &fptr, null);
glCompileShader(fshad);
glGetShaderiv(vshad, GL_COMPILE_STATUS, &status);
if(status==GL_FALSE){
glGetShaderiv(fshad, GL_INFO_LOG_LENGTH, &len);
char[] error=new char[len];
glGetShaderInfoLog(fshad, len, null, cast(char*)error);
writeln(error);
return false;
}

glAttachShader(shader, vshad);
glAttachShader(shader, fshad);
glLinkProgram(shader);
glGetShaderiv(shader, GL_LINK_STATUS, &status);
if(status==GL_FALSE){
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
char[] error=new char[len];
glGetShaderInfoLog(shader, len, null, cast(char*)error);
writeln(error);
return false;
}

return true;
}
bool initVAO(){
const float[] v = [ 0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f];

glGenVertexArrays(1, &vao);
if(vao<1){
writeln("Error: GL failed to assign vao id");
return false;
}

glBindVertexArray(vao);
glGenBuffers(1, &vbo);
if(vbo<1){
writeln("Error: GL failed to assign vbo id");
return false;
}

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, v.length * GL_FLOAT.sizeof, &v, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, null);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return true;
}

void drawVao(){
glUseProgram(shader);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glUseProgram(0);
}
void drawVbo(){
glUseProgram(shader);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, null);
glDrawArrays(GL_TRIANGLES, 0, 6);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);
}

int main()
{
writeln("Load libs: ", loadLibs());
writeln("Init sdl: ", initSDL());
writeln("Init gl: ", initGL());
writeln("Init shaders: ", initShaders());
writeln("Init vao: ", initVAO());

while(running){
SDL_Event e;
while(SDL_PollEvent(&e)){
switch(e.type){
case SDL_KEYDOWN:
running=false;
break;
default:
break;
}
}

glClear(GL_COLOR_BUFFER_BIT);

//drawVao();
drawVbo();

SDL_GL_SwapWindow(win);
}

SDL_GL_DeleteContext(context);
SDL_DestroyWindow(win);
SDL_Quit();

return 0;
}
[/CODE]

Share this post


Link to post
Share on other sites
Problem sorted thanks to DMD folk. The &v in glBufferData(GL_ARRAY_BUFFER, v.length * GL_FLOAT.sizeof, &v, GL_STATIC_DRAW); should be &v[0]

Share this post


Link to post
Share on other sites
I think in my code I use v.ptr rather than &v[0]. I'm positive dynamic arrays have the ptr property, but I'm not sure about static arrays (though I think they do)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627639
    • Total Posts
      2978344
  • Similar Content

    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
    • By cebugdev
      hi guys, 
      are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
      Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic 
      let me know if you guys have recommendations.
      Thank you in advance!
    • By dud3
      How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below? 
      Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
      Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.
       
      References:
      Code: https://pastebin.com/Hcshj3FQ
      The video shows the difference between blender and my rotation:
       
    • By Defend
      I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
      My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
      * make lots of VAO/VBO pairs and flip through them to render different objects, or
      * make one big VBO and jump around its memory to render different objects. 
      I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
      If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?
       
    • By test opty
      Hello all,
       
      On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
      #include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }  
      The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
      What is the problem and how to solve it please?
  • Popular Now