0

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;
}
user1118321
  • 25,567
  • 4
  • 55
  • 86
Bennet Leff
  • 376
  • 1
  • 4
  • 18
  • You're checking the shader compile error, but not calling `glError()` anywhere. Does calling it return an error? – user1118321 Feb 10 '15 at 03:51
  • Do you mean `glGetError()` because I have called that and it returns 0. – Bennet Leff Feb 10 '15 at 04:13
  • Sorry, yes, I meant `glGetError()`. – user1118321 Feb 10 '15 at 14:32
  • Yes it returns 0, so I don't see what error is present. – Bennet Leff Feb 10 '15 at 16:10
  • What else do you have in your main routine? Are you `glFlush`ing? – E_net4 Feb 10 '15 at 18:15
  • I'm not calling glFlush. My main just initializes the window, mesh, shader classes and calls shader.bind() and mesh.draw() in a while loop while the context is running. – Bennet Leff Feb 11 '15 at 02:04
  • What I usually do in this context is start simple. Do just a `glClearColor(1.0, 1.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT);` and see if anything is getting drawn at all. If that is, then try drawing a single triangle with just a pass-through vertex shader and fragment shader that just outputs a solid color. If that works, then substitute your real shaders, etc. – user1118321 Feb 11 '15 at 02:11
  • That's actually what I am doing. It does not seem to be working. If it would be helpful, [here's a link to the project on Github](https://github.com/BennetLeff/PhySim). – Bennet Leff Feb 11 '15 at 04:02
  • So clearing the screen color does not work either? I don't know how SDL handles it, but maybe you have to call glViewport on window resize. – weltensturm Feb 12 '15 at 12:31
  • Oh no I mean clearing the screen does work, but adding in my own shaders does not. – Bennet Leff Feb 12 '15 at 14:45

1 Answers1

0

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.

Bennet Leff
  • 376
  • 1
  • 4
  • 18