简体   繁体   中英

Why does this OpenGL code give no errors but print nothing to screen

To preface, I'm writing my OpenGL in D using Derelict. However, it should be almost the same as OpenGL with C++ as the function calls are identical. Regardless, I'm at a loss as to why my code will not print anything to the screen as my shader program gives no errors. I'm sorry for the big code dump, but I'm unable to isolate the problem. My shader class is as follows:

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];
}

And my Mesh class is:

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

I've found the answer, I had some redundant calls when loading the OpenGL libraries with the function loader I was using (Derelict) and I was representing my vertex array wrongly. I found help on the Digital Mars forum here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM