Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();
share|improve this question

closed as too localized by Byte56, Josh Petrie, Anko, bummzack, msell Apr 25 '13 at 8:55

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
This is why you should use a version control tool ;) –  Alex Apr 18 '13 at 10:28

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Edited, it will be something silly like that. –  CoffeeandCode Apr 18 '13 at 10:40
    
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. –  Lewis Wakeford Apr 18 '13 at 11:22

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