2015-02-10 52 views
0

要作序,我寫我的OpenGL在d使用廢棄。但是,它應該與使用C++的OpenGL幾乎相同,因爲函數調用是相同的。無論如何,我不知道爲什麼我的代碼不會在屏幕上打印任何內容,因爲我的着色器程序沒有提供任何錯誤。我很抱歉大代碼轉儲,但我無法找出問題。我的着色器類如下:爲什麼這OpenGL的代碼給沒有錯誤,但沒有打印到屏幕

module shader; 
import std.stdio; 
import std.string; 
import derelict.sdl2.sdl; 
import derelict.opengl3.gl3; 

class Shader 
{ 
    this(string file_name) 
    { 
     // Load OpenGL versions 1.0 and 1.1. 
     DerelictGL3.load(); 
     // Load versions 1.2+ and all supported ARB and EXT extensions. 
     DerelictGL3.reload(); 

     program = glCreateProgram(); 

     shaders[0] = create_shader(load_shader(file_name ~ ".vs"), GL_VERTEX_SHADER); 
     shaders[1] = create_shader(load_shader(file_name ~ ".fs"), GL_FRAGMENT_SHADER); 

     for (int i = 0; i < NUM_SHADERS; i++) 
      glAttachShader(program, shaders[i]); 

     glBindAttribLocation(program, 0, "position"); 

     glLinkProgram(program); 

     check_shader_error(program, GL_LINK_STATUS, true, "Error linking shader!"); 

     glValidateProgram(program); 

     check_shader_error(program, GL_VALIDATE_STATUS, true, "Error invalid shader!"); 
    } 
    void bind() 
    { 
     glUseProgram(program); 
    } 
    string load_shader(string shader_name) { 
     string output; 
     auto f = File(shader_name);   // open file for reading, 
     scope(exit) f.close();    // and close the file when we're done. 
              // (optional) 
     foreach (str; f.byLine)    // read every line in the file, 
     { 
      output ~= str; 
      output ~= "\n"; 
     } 

     ///writeln(output); 
     return output;      // and return it 
    } 
    GLuint create_shader(string text, GLenum shader_type) 
    { 
     GLuint shader = glCreateShader(shader_type); 

     GLchar* shader_source_strings[1]; 
     GLint shader_source_strings_lengths[1]; 

     shader_source_strings[0] = cast(char*)(text); 
     shader_source_strings_lengths[0] = (cast(int)text.length); 

     glShaderSource(shader, 1, shader_source_strings.ptr, shader_source_strings_lengths.ptr); 
     glCompileShader(shader); 

     check_shader_error(shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); 

     return shader; 
    } 
    void check_shader_error(GLuint shader, GLuint flag, bool isProgram, string error_message) 
    { 
     GLint success = 0; 
     GLchar[1024] error; 

     if (isProgram) 
      glGetProgramiv(shader, flag, &success); 
     else 
      glGetShaderiv(shader, flag, &success); 

     if (success == GL_FALSE) 
     { 
      if(isProgram) 
       glGetProgramInfoLog(shader, error.sizeof, null, error.ptr); 
      else 
       glGetProgramInfoLog(shader, error.sizeof, null, error.ptr); 
      writeln(error_message ~ error ~ "\n"); 
     } 
    } 
private: 
    static const int NUM_SHADERS = 2; 
    GLuint program; 
    GLuint shaders[NUM_SHADERS]; 
} 

我的網類:

module mesh;  
import std.stdio; 
import derelict.opengl3.gl3; 
import vertex; 
class Mesh 
{ 
    this(Vertex* vertices, int num_vertices) 
    { 

     // Load OpenGL versions 1.0 and 1.1. 
     DerelictGL3.load(); 
     // Load versions 1.2+ and all supported ARB and EXT extensions. 
     DerelictGL3.reload(); 

     draw_count = num_vertices; 

     glGenVertexArrays(1, &vertex_array_object); 
     glBindVertexArray(vertex_array_object); 

     glGenBuffers(NUM_BUFFERS, vertex_array_buffers.ptr); 
     glBindBuffer(GL_ARRAY_BUFFER, vertex_array_buffers[POSITION_VB]); 
     glBufferData(GL_ARRAY_BUFFER, num_vertices * vertices[0].sizeof, vertices, GL_STATIC_DRAW); 

     glEnableVertexAttribArray(0); 
     glVertexAttribPointer(cast(GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0); 

     glBindVertexArray(0); 
    } 
    void draw() 
    { 
     glBindVertexArray(vertex_array_object); 

     glDrawArrays(GL_TRIANGLES, 0, draw_count); 

     glBindVertexArray(0); 
    } 
private: 
    enum 
    { 
     POSITION_VB, 
     NUM_BUFFERS 
    }; 

    GLuint vertex_array_object; 
    GLuint vertex_array_buffers[NUM_BUFFERS]; 
    int draw_count; 
} 
+0

您正在檢查着色器編譯錯誤,但不會在任何地方調用'glError()'。調用它會返回一個錯誤嗎? – user1118321 2015-02-10 03:51:35

+0

你的意思是'glGetError()',因爲我調用了它,它返回0. – 2015-02-10 04:13:36

+0

對不起,是的,我的意思是'glGetError()'。 – user1118321 2015-02-10 14:32:43

回答

0

我已經找到了答案,我用函數加載我加載OpenGL庫時,有一些多餘的電話使用(廢棄),我錯誤地代表我的頂點數組。我在Digital Mars論壇here上找到了幫助。

相關問題