mkxp/src/gl-meta.cpp

125 lines
2.6 KiB
C++
Raw Normal View History

/*
** gl-meta.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2014 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gl-meta.h"
#include "gl-fun.h"
namespace GLMeta
{
void subRectImageUpload(GLint srcW, GLint srcX, GLint srcY,
GLint dstX, GLint dstY, GLsizei dstW, GLsizei dstH,
SDL_Surface *src, GLenum format)
{
if (gl.unpack_subimage)
{
PixelStore::setupSubImage(srcW, srcX, srcY);
TEX::uploadSubImage(dstX, dstY, dstW, dstH, src->pixels, format);
}
else
{
SDL_PixelFormat *form = src->format;
SDL_Surface *tmp = SDL_CreateRGBSurface(0, dstW, dstH, form->BitsPerPixel,
form->Rmask, form->Gmask, form->Bmask, form->Amask);
SDL_Rect srcRect = { srcX, srcY, dstW, dstH };
SDL_BlitSurface(src, &srcRect, tmp, 0);
TEX::uploadSubImage(dstX, dstY, dstW, dstH, tmp->pixels, format);
SDL_FreeSurface(tmp);
}
}
void subRectImageFinish()
{
if (gl.unpack_subimage)
PixelStore::reset();
}
#define HAVE_NATIVE_VAO gl.GenVertexArrays
static void vaoBindRes(VAO &vao)
{
VBO::bind(vao.vbo);
IBO::bind(vao.ibo);
for (size_t i = 0; i < vao.attrCount; ++i)
{
const VertexAttribute &va = vao.attr[i];
gl.EnableVertexAttribArray(va.index);
gl.VertexAttribPointer(va.index, va.size, va.type, GL_FALSE, vao.vertSize, va.offset);
}
}
void vaoInit(VAO &vao, bool keepBound)
{
if (HAVE_NATIVE_VAO)
{
vao.vao = ::VAO::gen();
::VAO::bind(vao.vao);
vaoBindRes(vao);
if (!keepBound)
::VAO::unbind();
}
else
{
if (keepBound)
{
VBO::bind(vao.vbo);
IBO::bind(vao.ibo);
}
}
}
void vaoFini(VAO &vao)
{
if (HAVE_NATIVE_VAO)
::VAO::del(vao.vao);
}
void vaoBind(VAO &vao)
{
if (HAVE_NATIVE_VAO)
::VAO::bind(vao.vao);
else
vaoBindRes(vao);
}
void vaoUnbind(VAO &vao)
{
if (HAVE_NATIVE_VAO)
{
::VAO::unbind();
}
else
{
for (size_t i = 0; i < vao.attrCount; ++i)
gl.DisableVertexAttribArray(vao.attr[i].index);
VBO::unbind();
IBO::unbind();
}
}
}