Exemplo n.º 1
0
    GLVertexBuffer::GLVertexBuffer(GraphicBuffer::Access access,
                                   GraphicBuffer::Usage usage,
                                   uint32 desired_count,
                                   const void* initData,
                                   const vertex_elements_type& format):
    GraphicBuffer(access, usage),
    mFormat(format),
    mMaped(false) {

        glGenBuffers(1, &mId);
        glBindBuffer(GL_ARRAY_BUFFER, mId);

        if(initData) {
            CHECK_GL_CALL(glBufferData(GL_ARRAY_BUFFER,
                                  static_cast<GLsizeiptr>(desired_count * GetVertexElementsTotalSize(format)),
                                  static_cast<const GLvoid*>(initData),
                                  _usage_to_gl(usage)));

        } else {
            CHECK_GL_CALL(glBufferData(GL_ARRAY_BUFFER,
                         static_cast<GLsizeiptr>(desired_count * GetVertexElementsTotalSize(format)),
                         0,
                         _usage_to_gl(usage)));
        }
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        mCount = desired_count;
    }
Exemplo n.º 2
0
    void GLTexture2DRenderView::onAttached(FrameBuffer& fb, uint32 att) {
        mIndex = att - ATT_Color0;
        mFBO = checked_cast<GLFrameBuffer*>(&fb)->getGLFBO();

        GLGraphicDevice& gd = *checked_cast<GLGraphicDevice*>(&Context::Instance().getGraphicFactory().getGraphicDevice());

        gd.bindGLFrameBuffer(mFBO);
#ifndef UKN_OSX_REQUEST_OPENGL_32_CORE_PROFILE
        CHECK_GL_CALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + att - ATT_Color0, GL_TEXTURE_2D, mTex, mLevel));
#else
        CHECK_GL_CALL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + att - ATT_Color0, GL_TEXTURE_2D, mTex, mLevel));
#endif
        gd.bindGLFrameBuffer(0);
    }
Exemplo n.º 3
0
    void* GLVertexBuffer::map() {
        if(access() == None) {
            log_error("GLVertexBuffer: trying to map buffer with access = None");
            return 0;
        }
        if(mMaped)
            unmap();
        
        GLenum gl_acess = GL_READ_ONLY;

        switch (access()) {
            case GraphicBuffer::ReadOnly:
                gl_acess = GL_READ_ONLY;
                break;

            case GraphicBuffer::ReadWrite:
                gl_acess = GL_READ_WRITE;
                break;

            case GraphicBuffer::WriteOnly:
                gl_acess = GL_WRITE_ONLY;
                break;
                
            default:
                break;
        }

        glBindBuffer(GL_ARRAY_BUFFER, mId);
        void* p = CHECK_GL_CALL(glMapBuffer(GL_ARRAY_BUFFER, gl_acess));
        if(p)
            mMaped = true;
        return p;
    }
Exemplo n.º 4
0
    void GLFrameBuffer::clear(uint32 flags, const class Color& clr, float depth, int32 stencil) {
        GLbitfield glflags = 0;
        
        if(flags & CM_Color) {
            glClearColor(clr.r(), clr.g(), clr.b(), clr.a());
            
            glflags |= GL_COLOR_BUFFER_BIT;
        }
        if(flags & CM_Depth) {
            glClearDepth(depth);
            glDepthMask(GL_TRUE);
            
            glflags |= GL_DEPTH_BUFFER_BIT;
        }
        if(flags & CM_Stencil) {
            glClearStencil(stencil);
            
            glflags |= GL_STENCIL_BUFFER_BIT;
        }
        
        CHECK_GL_CALL(glClear(glflags));

        if(flags & CM_Depth)
            glDepthMask(GL_FALSE);
    }
Exemplo n.º 5
0
    void GLTexture2DRenderView::clearColor(const Color& clr) {
        if(mFBO != 0) {
            this->doClear(GL_COLOR_BUFFER_BIT, clr, 0, 0);
        } else {
            glBindTexture(GL_TEXTURE_2D, mTex);

            std::vector<Color> mem_clr(mWidth * mHeight, clr);
            CHECK_GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, mLevel, 0, 0, mWidth, mHeight, GL_RGBA, GL_FLOAT, mem_clr.data()));
        }
    }
Exemplo n.º 6
0
    void GLIndexBuffer::resize(uint32 desired_count) {
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mId);

        CHECK_GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                     static_cast<GLsizeiptr>(desired_count * sizeof(uint32)),
                     0,
                     usage() == Dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
        mCount = desired_count;
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    }
Exemplo n.º 7
0
    void GLVertexBuffer::resize(uint32 desired_count) {
        glBindBuffer(GL_ARRAY_BUFFER, mId);

        CHECK_GL_CALL(glBufferData(GL_ARRAY_BUFFER,
                     static_cast<GLsizeiptr>(desired_count * GetVertexElementsTotalSize(mFormat)),
                     0,
                     usage() == Dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
        mCount = desired_count;
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
Exemplo n.º 8
0
    void GLDepthStencilView::onDetached(FrameBuffer& fb, uint32 att) {
        mFBO = checked_cast<GLFrameBuffer*>(&fb)->getGLFBO();
        
        GLGraphicDevice& gd = *checked_cast<GLGraphicDevice*>(&Context::Instance().getGraphicFactory().getGraphicDevice());
        
        gd.bindGLFrameBuffer(mFBO);

        if(mLevel >= 0) {
#ifndef UKN_OSX_REQUEST_OPENGL_32_CORE_PROFILE
            CHECK_GL_CALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));

            if(element_format_contains_stencil(mElementFormat)) 
                CHECK_GL_CALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));

#else
            CHECK_GL_CALL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));

            if(element_format_contains_stencil(mElementFormat)) 
                CHECK_GL_CALL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));
#endif
        }
        else if(mRBO) {
            
            CHECK_GL_CALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0));

            if(element_format_contains_stencil(mElementFormat)) 
                CHECK_GL_CALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0));

        }
       
        gd.bindGLFrameBuffer(0);
        mFBO = 0;
    }
Exemplo n.º 9
0
    GLDepthStencilView::GLDepthStencilView(const TexturePtr& texture, int32 level):
    mTexture(texture),
    mLevel(level),
    mRBO(0),
    mSampleCount(1),
    mSampleQuaility(1) {
        GLTexture2D* glTexture = (GLTexture2D*)mTexture.get();
        mTex = (GLuint)glTexture->getTextureId();
        mWidth = glTexture->width(0);
        mHeight = glTexture->height(0);
        mElementFormat = glTexture->format();

        CHECK_GL_CALL(glGenRenderbuffers(1, &mRBO));
        if(mRBO) {
            CHECK_GL_CALL(glBindRenderbuffer(GL_RENDERBUFFER, mRBO));

            if(mSampleCount <= 1 || !glRenderbufferStorageMultisampleEXT) {
                CHECK_GL_CALL(glRenderbufferStorage(GL_RENDERBUFFER, element_format_to_gl_format(mElementFormat), mWidth, mHeight));
            }
            else {
                CHECK_GL_CALL(glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER, mSampleCount, element_format_to_gl_format(mElementFormat), mWidth, mHeight));
            }
        }
    }
Exemplo n.º 10
0
    GLIndexBuffer::GLIndexBuffer(GraphicBuffer::Access access,
                                 GraphicBuffer::Usage usage,
                                 uint32 desired_count,
                                 const void* initData):
    GraphicBuffer(access, usage),
    mMaped(false) {
        glGenBuffers(1, &mId);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mId);
        if(initData) {
            CHECK_GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                         static_cast<GLsizeiptr>(desired_count * sizeof(uint32)),
                         static_cast<const GLvoid*>(initData),
                         usage == Dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
        } else {
            CHECK_GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                         static_cast<GLsizeiptr>(desired_count * sizeof(uint32)),
                         0,
                         usage == Dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
        }
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

        mCount = desired_count;
    }
Exemplo n.º 11
0
    SharedPtr<uint8> GLFrameBuffer::readFrameBufferData(int32 x, int32 y, uint32 width, uint32 height, ElementFormat format) {
        GLint prevBuffer;
        glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevBuffer);
        
#if defined(UKN_OSX_REQUEST_OPENGL_32_CORE_PROFILE)
        glBindBuffer(GL_FRAMEBUFFER, mFBO);
#else
        CHECK_GL_CALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFBO));
#endif
        
        if(width == 0)
            width = this->getViewport().width;
        if(height == 0)
            height = this->getViewport().height;
        
        uint8* texData = new uint8[width * height * GetElementSize(format)];
        CHECK_GL_CALL(glReadPixels(x,
                     y,
                     width,
                     height,
                     element_format_to_gl_format(format),
                     element_format_to_gl_element_type(format),
                     texData));
        
        if(glGetError() != GL_NO_ERROR) {
            log_error("GLGraphicDevice: error when read pixels");
        }
        
#if defined(UKN_OSX_REQUEST_OPENGL_32_CORE_PROFILE)
        CHECK_GL_CALL(glBindBuffer(GL_FRAMEBUFFER, prevBuffer));
#else
        CHECK_GL_CALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, prevBuffer));
#endif
        
        return SharedPtr<uint8>(texData);
    }
Exemplo n.º 12
0
    void GLFrameBuffer::onBind() {
        GLGraphicDevice& gd = *checked_cast<GLGraphicDevice*>(&Context::Instance().getGraphicFactory().getGraphicDevice());
        
        gd.bindGLFrameBuffer(mFBO);
        if(mFBO != 0) {
            check_framebuffer_status();

            std::vector<GLenum> buffers(mClearViews.size());
            for(size_t i=0; i<mClearViews.size(); ++i) {
                buffers[i] = static_cast<GLenum>(i + GL_COLOR_ATTACHMENT0_EXT);
            }
            CHECK_GL_CALL(glDrawBuffers((GLsizei)buffers.size(), &buffers[0]));

        } else {
            GLenum targets[] = { GL_BACK_LEFT };
            glDrawBuffers(1, &targets[0]);
        }
    }
Exemplo n.º 13
0
    GLDepthStencilView::GLDepthStencilView(uint32 width, uint32 height, ElementFormat ef, uint32 sampleCount, uint32 sampleQuality):
    mLevel(-1),
    mRBO(0),
    mSampleCount(sampleCount),
    mSampleQuaility(sampleQuality) {
        mWidth = width;
        mHeight = height;
        mElementFormat = ef;

        CHECK_GL_CALL(glGenRenderbuffers(1, &mRBO));
        if(mRBO) {
            glBindRenderbuffer(GL_RENDERBUFFER, mRBO);

            if(mSampleCount <= 1 || !glRenderbufferStorageMultisampleEXT)
                glRenderbufferStorage(GL_RENDERBUFFER, element_format_to_gl_format(ef), width, height);
            else
                glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER, mSampleCount, element_format_to_gl_format(ef), width, height);
        }
    }
Exemplo n.º 14
0
    void GLVertexBuffer::copyBuffer(const GraphicBufferPtr& to) {
        if(to) {
            GLVertexBuffer* dest = checked_cast<GLVertexBuffer*>(to.get());
            if(glCopyBufferSubData) {
                glBindBuffer(GL_COPY_READ_BUFFER, this->getGLBuffer());
                glBindBuffer(GL_COPY_WRITE_BUFFER, dest->getGLBuffer());

                CHECK_GL_CALL(glCopyBufferSubData(GL_COPY_READ_BUFFER,
                                    GL_COPY_WRITE_BUFFER,
                                    0,
                                    0,
                                    this->count() * GetVertexElementsTotalSize(this->format())));

                glBindBuffer(GL_COPY_READ_BUFFER, 0);
                glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
            } else {
                log_error(L"GLVertexBuffer::copyBuffer: GL Copy Buffer Sub Data not supported");
            }
        }
    }