2012-03-06 59 views
10

我疯了,因为我无法在屏幕上出现一组简单的三角形。在D语言中用OpenGL 3渲染简单的矩形

我正在使用OpenGL3(没有弃用的固定管道)使用D编程语言的废弃绑定。

你能在下面的程序中发现错误吗?它编译得很好,不会抛出任何OpenGL/GLSL错误。它只是显示我设置的清晰颜色的空白屏幕。

import std.string; 
import std.conv; 
import derelict.opengl3.gl3; 
import derelict.sdl2.sdl2; 

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); 
} 
`; 

void main() { 
    DerelictSDL2.load(); 
    DerelictGL3.load(); 

    if (SDL_Init(SDL_INIT_VIDEO) < 0) { 
     throw new Exception("Failed to initialize SDL: " ~ to!string(SDL_GetError())); 
    } 

    // Set OpenGL version 
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); 

    // Set OpenGL attributes 
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); 

    auto sdlwindow = SDL_CreateWindow("D App", 
     SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
     640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); 

    if (!sdlwindow) 
     throw new Exception("Failed to create a SDL window: " ~ to!string(SDL_GetError())); 

    SDL_GL_CreateContext(sdlwindow); 
    DerelictGL3.reload(); 

    float[] vertices = [ -1, -1, 1, -1, -1, 1, 1, 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.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"); 
    auto run = true; 

    while (run) { 
     SDL_Event event; 
     while (SDL_PollEvent(&event)) { 
      switch (event.type) { 
       case SDL_QUIT: 
        run = false; 
       default: 
        break; 
      } 
     } 

     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, vertices.sizeof, null); 
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); 
     glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, null); 
     glDisableVertexAttribArray(position); 

     SDL_GL_SwapWindow(sdlwindow); 
    } 
} 
+1

我不能告诉你问题是什么,但如果使用glGetError()检查OpenGL错误,它几乎肯定会有帮助。查看std.exception中的强制函数以及它如何使用延迟参数 - 您可以将其调整为“enforceGL()”函数,以便更容易捕获OpenGL错误。 – 2012-03-06 16:49:24

+0

我做了这段代码的一个版本,我用'assert(glGetError()== 0)检查了每个gl调用;'行...没有引发错误。 – 2012-03-06 22:04:48

+0

你为什么要重新装载废弃物? :o – 2014-04-02 19:46:09

回答

13

在此行中:

glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, vertices.sizeof, null); 

你确定你想要vertices.sizeof,其中有16个值?在D中,动态数组是一个具有两个成员的结构(ptrlength)。您可能需要float.sizeoffloat.sizeof * 2

而你的BufferData调用也是如此。

+0

感谢您的回答!我改变了声明以使它们成为静态数组(我认为它们是用一个文字定义的,它们将是静态的),现在.sizeof是32(length * float.sizeof),对于'vertices'是32,对于'indices',8。但我仍然没有得到任何东西。 :( – 2012-03-07 04:37:03

+0

哦,等等,你太棒了,我在glBufferData调用中用'vertices.length * float.sizeof'替换了'vertices.sizeof',和'indices'一样,但在'glVertexAttribPointer'中我需要'2 * float.sizeof'作为stride参数,它的工作原理!!谢谢! – 2012-03-07 04:42:38

+0

@SantiagoV。嘿,我能够使用你的例子来帮助我开始(不使用sdl),我知道这是旧的,但当我做到了,我只是一个有颜色的窗户......好像三角形比窗户大得多,对此有什么想法? – AbstractDissonance 2016-01-12 09:10:58