0
\$\begingroup\$

I have been working on a game engine for a couple of days now and I've had it rendering a colourful cube and playing sounds in OGG format, but, after refactoring my code-base to make things easier; it no longer displays the cube! (This happened when I put the shader program and methods into a seperate class)

What could be the problem?

OpenGL/SDL2 context class:

class RenderContext{
    public:
        RenderContext();
        ~RenderContext();

        void draw();

        int16_t good;

        ShaderProgram *program;
};

BufferObject class

class BufferObject{
    public:
        BufferObject(){};

        template<class Type>
        BufferObject(GLenum bufferTarget, vector<Type> &bufferData){
            target = bufferTarget;

            glGenBuffers(1, &buffer);
            glBindBuffer(bufferTarget, buffer);
            glBufferData(bufferTarget, bufferData.size(), bufferData.data(), GL_STATIC_DRAW);
            glBindBuffer(bufferTarget, 0);

            glGetBufferParameteriv(bufferTarget, GL_BUFFER_SIZE, &size);

            good = true;
        }

        void bind(){
            glBindBuffer(target, buffer);
        }

        void unbind(){
            glBindBuffer(target, 0);
        }

        void draw(GLenum drawMode){
            switch(target){
                case GL_ELEMENT_ARRAY_BUFFER:
                    glDrawElements(drawMode, size/sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
                    return;
                    break;

                case GL_ARRAY_BUFFER:
                    glDrawArrays(drawMode, 0, size/sizeof(float));
                    return;
                    break;

                default:
                    return;
            }
        }

        ~BufferObject(){
            glDeleteBuffers(1, &buffer);
        }

        bool good;

    private:
        uint32_t target;
        uint32_t buffer;
        int size;
};

ShaderProgram class:

class ShaderProgram{
    public:
        ShaderProgram(){
        }

        ShaderProgram(const char *vsPath, const char *fsPath){
            int32_t compileStat, linkStat;
            uint32_t vertexS = glCreateShader(GL_VERTEX_SHADER);
            uint32_t fragS = glCreateShader(GL_FRAGMENT_SHADER);

            string vsSource = LoadFile(vsPath);
            string fsSource = LoadFile(fsPath);
            const char *source = vsSource.c_str();
            glShaderSource(vertexS, 1, &source, NULL);
            source = fsSource.c_str();
            glShaderSource(fragS, 1, &source, NULL);

            glCompileShader(vertexS);
            glGetShaderiv(vertexS, GL_COMPILE_STATUS, &compileStat);
            if(compileStat == 0){
                char BUFFER[256];
                glGetShaderInfoLog(vertexS, sizeof(BUFFER), NULL, BUFFER);
                cerr << "-!- Error compiling vertex shader: " << BUFFER << endl;
                good = false;
                return;
            }

            glCompileShader(fragS);
            glGetShaderiv(fragS, GL_COMPILE_STATUS, &compileStat);
            if(compileStat == 0){
                char BUFFER[256];
                glGetShaderInfoLog(fragS, sizeof(BUFFER), NULL, BUFFER);
                cerr << "-!- Error compiling fragment shader: " << BUFFER << endl;
                good = false;
                return;
            }

            program = glCreateProgram();
            glAttachShader(program, vertexS);
            glAttachShader(program, fragS);

            glLinkProgram(program);
            glGetProgramiv(program, GL_LINK_STATUS, &linkStat);
            if(!linkStat){
                cerr << "-!- Error linking program." << endl;
                good = false;
                return;
            }

            good = true;
        }

        void getUniform(uint32_t &location, const char *uniformName){
            location = glGetUniformLocation(program, uniformName);
            assert(location != 0xFFFFFFFF);
        }

        void bind(){
            glUseProgram(program);
        }

        void unbind(){
            glUseProgram(0);
        }

        ~ShaderProgram(){
            glDeleteProgram(program);
            glUseProgram(0);
        }

        bool good;
        uint32_t program;
};

Then the ShaderProgram is used like so in the drawing loop:

**first init:**
VBO = new BufferObject(GL_ARRAY_BUFFER, coord);
IBO = new BufferObject(GL_ELEMENT_ARRAY_BUFFER, indicies);

**then:**
program->bind();

glUniformMatrix4fv(projMatrixLoc, 1, GL_FALSE, &projMatrix[0][0]);
glUniformMatrix4fv(viewMatrixLoc, 1, GL_FALSE, &viewMatrix[0][0]);
glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, &modelMatrix[0][0]);

glEnableVertexAttribArray(0);

VBO->bind();
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
VBO->unbind();

IBO->bind();
IBO->draw(GL_TRIANGLES);
IBO->unbind();

glDisableVertexAttribArray(0);

program->unbind();
\$\endgroup\$
  • 4
    \$\begingroup\$ This is why you should use a version control tool ;) \$\endgroup\$ – Alex Apr 18 '13 at 10:28
0
\$\begingroup\$

It's hard to tell from just that code. How do you use ShaderProgram during the actual drawing process? The only thing that I can think of from that code (assuming you aren't getting shader compile/link errors) is that maybe an instance of the ShaderProgram object is going out of scope and deleting the program prematurely.

EDIT:

 glBufferData(bufferTarget, bufferData.size(), bufferData.data(), GL_STATIC_DRAW);

bufferData.size() is the number of elements in the vector, not the size in bytes. You are probably not allocating enough space for all your data. Change it to:

glBufferData(bufferTarget, bufferData.size()*sizeof(Type), ...

Not sure how valid the sizeof(Type) thing is but you can always add an extra parameter or something if that doesn't work.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Edited, it will be something silly like that. \$\endgroup\$ – CoffeeandCode Apr 18 '13 at 10:40
  • \$\begingroup\$ I can't see anything obviously wrong. Try hacking a glGetError call in before and after your drawing code and see if that gives us any clues. \$\endgroup\$ – Lewis Wakeford Apr 18 '13 at 11:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.