TRX/lib/ddraw/Renderer.cpp

123 lines
2.9 KiB
C++
Raw Normal View History

2021-11-12 20:03:04 +01:00
#include "Renderer.hpp"
#include <glrage_gl/Shader.hpp>
#include <glrage_gl/Utils.hpp>
2021-11-14 23:42:18 +01:00
// #include "openssl/md5.h"
2021-11-12 20:03:04 +01:00
#include <glrage_util/StringUtils.hpp>
namespace glrage {
namespace ddraw {
Renderer::Renderer()
: m_surfaceBuffer(GL_ARRAY_BUFFER)
{
// configure buffer
m_surfaceBuffer.bind();
2021-11-14 23:59:19 +01:00
GLfloat verts[] = {
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0};
2021-11-12 20:03:04 +01:00
m_surfaceBuffer.data(sizeof(verts), verts, GL_STATIC_DRAW);
m_surfaceFormat.bind();
m_surfaceFormat.attribute(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
// configure sampler
2021-11-15 00:00:40 +01:00
// TODO: make me configurable
GLint filterMethodEnum = GL_LINEAR; // GL_NEAREST
2021-11-12 20:03:04 +01:00
m_sampler.bind(0);
m_sampler.parameteri(GL_TEXTURE_MAG_FILTER, filterMethodEnum);
m_sampler.parameteri(GL_TEXTURE_MIN_FILTER, filterMethodEnum);
// configure shaders
2021-11-14 23:42:18 +01:00
std::string basePath = m_context.getBasePath();
2021-11-12 20:03:04 +01:00
m_program.attach(gl::Shader(GL_VERTEX_SHADER)
2021-11-14 23:42:18 +01:00
.fromFile(basePath + "\\shaders\\ddraw.vsh"));
2021-11-12 20:03:04 +01:00
m_program.attach(gl::Shader(GL_FRAGMENT_SHADER)
2021-11-14 23:42:18 +01:00
.fromFile(basePath + "\\shaders\\ddraw.fsh"));
2021-11-12 20:03:04 +01:00
m_program.link();
m_program.fragmentData("fragColor");
gl::Utils::checkError(__FUNCTION__);
}
void Renderer::upload(DDSURFACEDESC& desc, std::vector<uint8_t>& data)
{
uint32_t width = desc.dwWidth;
uint32_t height = desc.dwHeight;
2021-11-14 23:59:19 +01:00
uint8_t* bits = &data[0];
2021-11-12 20:03:04 +01:00
GLenum tex_format = TEX_FORMAT;
GLenum tex_type = TEX_TYPE;
m_surfaceTexture.bind();
2021-11-15 00:00:40 +01:00
// TODO: implement texture packs
2021-11-12 20:03:04 +01:00
// update buffer if the size is unchanged, otherwise create a new one
if (width != m_width || height != m_height) {
m_width = width;
m_height = height;
2021-11-14 23:59:19 +01:00
glTexImage2D(GL_TEXTURE_2D,
0,
TEX_INTERNAL_FORMAT,
m_width,
m_height,
0,
tex_format,
tex_type,
bits);
2021-11-12 20:03:04 +01:00
} else {
2021-11-14 23:59:19 +01:00
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
m_width,
m_height,
tex_format,
tex_type,
bits);
2021-11-12 20:03:04 +01:00
}
}
void Renderer::render()
{
m_program.bind();
m_surfaceBuffer.bind();
m_surfaceFormat.bind();
m_surfaceTexture.bind();
m_sampler.bind(0);
GLboolean texture2d = glIsEnabled(GL_TEXTURE_2D);
if (!texture2d) {
glEnable(GL_TEXTURE_2D);
}
GLboolean blend = glIsEnabled(GL_BLEND);
if (blend) {
glDisable(GL_BLEND);
}
GLboolean depthTest = glIsEnabled(GL_DEPTH_TEST);
if (depthTest) {
glDisable(GL_DEPTH_TEST);
}
glDrawArrays(GL_TRIANGLES, 0, 6);
if (!texture2d) {
glDisable(GL_TEXTURE_2D);
}
if (blend) {
glEnable(GL_BLEND);
}
if (depthTest) {
glEnable(GL_DEPTH_TEST);
}
gl::Utils::checkError(__FUNCTION__);
}
} // namespace ddraw
2021-11-14 23:42:18 +01:00
} // namespace glrage