Update widgets module. Raster example initial.
This commit is contained in:
parent
aa10424fbb
commit
90244934d9
48 changed files with 1169 additions and 530 deletions
117
RasterRender/private/FrameBuffer.cpp
Normal file
117
RasterRender/private/FrameBuffer.cpp
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
|
||||
#include "FrameBuffer.hpp"
|
||||
#include "GraphicsApi.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void glerr(GLenum type) { printf("GL ERROR\n"); }
|
||||
#define AssertGL(x) \
|
||||
{ \
|
||||
x; \
|
||||
GLenum __gle = glGetError(); \
|
||||
if (__gle != GL_NO_ERROR) glerr(__gle); \
|
||||
}
|
||||
|
||||
using namespace tp;
|
||||
|
||||
RenderBuffer::RenderBuffer(const Vec2F& size) :
|
||||
mSize(size) {
|
||||
|
||||
mDrawBuffers[0] = { GL_COLOR_ATTACHMENT0 };
|
||||
|
||||
// --------- texture ---------
|
||||
AssertGL(glGenTextures(1, &mTextureId));
|
||||
AssertGL(glBindTexture(GL_TEXTURE_2D, mTextureId));
|
||||
// Give an empty image to OpenGL ( the last "0" )
|
||||
AssertGL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) size.x, (GLsizei) size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)
|
||||
);
|
||||
// Poor filtering. Needed
|
||||
AssertGL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
|
||||
AssertGL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
|
||||
|
||||
// --------- depth ---------
|
||||
AssertGL(glGenRenderbuffers(1, &mDepthBufferID));
|
||||
AssertGL(glBindRenderbuffer(GL_RENDERBUFFER, mDepthBufferID));
|
||||
AssertGL(glRenderbufferStorage(GL_RENDERBUFFER, GLW_CONTEXT_DEPTH_COMPONENT, (GLsizei) size.x, (GLsizei) size.y));
|
||||
|
||||
// ------------ framebuffer ------------
|
||||
AssertGL(glGenFramebuffers(1, &mFrameBufferID));
|
||||
AssertGL(glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferID));
|
||||
AssertGL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBufferID));
|
||||
// Set "renderedTexture" as our colour attachement #0
|
||||
AssertGL(glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTextureId, 0));
|
||||
// Set the list of draw buffers.
|
||||
AssertGL(glDrawBuffers(1, mDrawBuffers)); // "1" is the size of DrawBuffers
|
||||
ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
RenderBuffer::RenderBuffer(const Vec2F& size, tp::uint1 samples) :
|
||||
mSize(size) {
|
||||
|
||||
mDrawBuffers[0] = { GL_COLOR_ATTACHMENT0 };
|
||||
|
||||
// ------- texture ---------
|
||||
AssertGL(glGenTextures(1, &mTextureId));
|
||||
AssertGL(glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mTextureId));
|
||||
#ifdef ENV_OS_ANDROID
|
||||
AssertGL(
|
||||
glTexStorage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, (GLsizei) size.x, (GLsizei) size.y, GL_TRUE)
|
||||
);
|
||||
#else
|
||||
AssertGL(
|
||||
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, (GLsizei) size.x, (GLsizei) size.y, GL_TRUE)
|
||||
);
|
||||
#endif
|
||||
// !?
|
||||
// AssertGL(glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
|
||||
// AssertGL(glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
|
||||
|
||||
// ------- depth -------
|
||||
AssertGL(glGenRenderbuffers(1, &mDepthBufferID));
|
||||
AssertGL(glBindRenderbuffer(GL_RENDERBUFFER, mDepthBufferID));
|
||||
AssertGL(glRenderbufferStorageMultisample(
|
||||
GL_RENDERBUFFER, samples, GLW_CONTEXT_DEPTH_COMPONENT, (GLsizei) size.x, (GLsizei) size.y
|
||||
));
|
||||
|
||||
// ------- fbuff -------
|
||||
AssertGL(glGenFramebuffers(1, &mFrameBufferID));
|
||||
AssertGL(glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferID));
|
||||
AssertGL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, mTextureId, 0));
|
||||
AssertGL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBufferID));
|
||||
AssertGL(glDrawBuffers(1, mDrawBuffers));
|
||||
ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
uint4 RenderBuffer::buffId() const { return mFrameBufferID; }
|
||||
|
||||
void RenderBuffer::beginDraw() {
|
||||
AssertGL(glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferID));
|
||||
setViewport({ 0.f, 0.f, mSize.x, mSize.y });
|
||||
}
|
||||
|
||||
void RenderBuffer::setViewport(const RectF& viewport) {
|
||||
AssertGL(glViewport((GLsizei) viewport.x, (GLsizei) viewport.y, (GLsizei) viewport.z, (GLsizei) viewport.w));
|
||||
}
|
||||
|
||||
void RenderBuffer::clear() {
|
||||
AssertGL(glClearColor(mClearCol.r, mClearCol.g, mClearCol.b, mClearCol.a));
|
||||
AssertGL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
|
||||
}
|
||||
|
||||
void RenderBuffer::endDraw() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glClearColor(0, 0, 0, 0);
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
RenderBuffer::~RenderBuffer() {
|
||||
glDeleteFramebuffers(1, &mFrameBufferID);
|
||||
glDeleteTextures(1, &mTextureId);
|
||||
glDeleteRenderbuffers(1, &mDepthBufferID);
|
||||
}
|
||||
|
||||
uint4 RenderBuffer::texId() const { return mTextureId; }
|
||||
|
||||
const Vec2F& RenderBuffer::getSize() const { return mSize; }
|
||||
162
RasterRender/private/Shader.cpp
Normal file
162
RasterRender/private/Shader.cpp
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
|
||||
|
||||
#include "Buffer.hpp"
|
||||
#include "LocalConnection.hpp"
|
||||
#include "Shader.hpp"
|
||||
#include "GraphicsApi.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
using namespace tp;
|
||||
|
||||
RenderShader::RenderShader() {
|
||||
programm = 0;
|
||||
VertexShaderID = 0;
|
||||
FragmentShaderID = 0;
|
||||
GeometryShaderID = 0;
|
||||
}
|
||||
|
||||
void RenderShader::vert_bind_source(const char* vert_src) {
|
||||
VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||
compile_shader(vert_src, VertexShaderID);
|
||||
}
|
||||
|
||||
void RenderShader::frag_bind_source(const char* frag_src) {
|
||||
FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
compile_shader(frag_src, FragmentShaderID);
|
||||
}
|
||||
|
||||
void RenderShader::geom_bind_source(const char* geom_src) {
|
||||
GeometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
compile_shader(geom_src, GeometryShaderID);
|
||||
}
|
||||
|
||||
void RenderShader::compile() {
|
||||
GLint Result = GL_FALSE;
|
||||
int InfoLogLength;
|
||||
|
||||
programm = glCreateProgram();
|
||||
glAttachShader(programm, VertexShaderID);
|
||||
if (GeometryShaderID) glAttachShader(programm, GeometryShaderID);
|
||||
glAttachShader(programm, FragmentShaderID);
|
||||
glLinkProgram(programm);
|
||||
|
||||
// Check the program
|
||||
glGetProgramiv(programm, GL_LINK_STATUS, &Result);
|
||||
glGetProgramiv(programm, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
|
||||
if (InfoLogLength > 0) {
|
||||
Buffer<char> ProgramErrorMessage(InfoLogLength + 1);
|
||||
glGetProgramInfoLog(programm, InfoLogLength, NULL, &ProgramErrorMessage[0]);
|
||||
printf("%s\n", &ProgramErrorMessage[0]);
|
||||
}
|
||||
|
||||
glDetachShader(programm, VertexShaderID);
|
||||
glDetachShader(programm, FragmentShaderID);
|
||||
if (GeometryShaderID) glDetachShader(programm, GeometryShaderID);
|
||||
|
||||
glDeleteShader(VertexShaderID);
|
||||
glDeleteShader(FragmentShaderID);
|
||||
if (GeometryShaderID) glDeleteShader(GeometryShaderID);
|
||||
}
|
||||
|
||||
bool RenderShader::compile_shader(const char* ShaderCode, uint4 ShaderID) {
|
||||
GLint Result = GL_FALSE;
|
||||
int InfoLogLength;
|
||||
|
||||
char const* SourcePointer = ShaderCode;
|
||||
glShaderSource(ShaderID, 1, &SourcePointer, NULL);
|
||||
glCompileShader(ShaderID);
|
||||
|
||||
// Check Shader
|
||||
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, &Result);
|
||||
glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
|
||||
if (InfoLogLength > 0) {
|
||||
Buffer<char> VertexShaderErrorMessage(InfoLogLength + 1);
|
||||
glGetShaderInfoLog(ShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
|
||||
printf("%s\n", &VertexShaderErrorMessage[0]);
|
||||
}
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
|
||||
void RenderShader::load(const char* pvert, const char* pgeom, const char* pfrag, bool paths) {
|
||||
|
||||
// Create the shaders
|
||||
VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||
FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
if (pgeom) GeometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
else GeometryShaderID = 0;
|
||||
|
||||
GLint Result = GL_FALSE;
|
||||
int InfoLogLength = 0;
|
||||
|
||||
if (paths) {
|
||||
|
||||
std::string content;
|
||||
|
||||
auto loadFile = [&](const char* path) {
|
||||
LocalConnection file;
|
||||
if (!file.connect(LocalConnection::Location(path), LocalConnection::Type(true))) {
|
||||
content = "";
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto size = file.size();
|
||||
auto tmp = new char[size + 1];
|
||||
tmp[size] = 0;
|
||||
file.readBytes(tmp, size);
|
||||
content = tmp;
|
||||
delete[] tmp;
|
||||
return true;
|
||||
};
|
||||
|
||||
printf("Compiling shader : %s\n", pvert);
|
||||
if (loadFile(pvert)) {
|
||||
compile_shader(content.c_str(), VertexShaderID);
|
||||
}
|
||||
|
||||
if (GeometryShaderID) {
|
||||
printf("Compiling shader : %s\n", pgeom);
|
||||
if (loadFile(pgeom)) {
|
||||
compile_shader(content.c_str(), GeometryShaderID);
|
||||
}
|
||||
}
|
||||
|
||||
printf("Compiling shader : %s\n", pfrag);
|
||||
if (loadFile(pfrag)) {
|
||||
compile_shader(content.c_str(), FragmentShaderID);
|
||||
}
|
||||
}
|
||||
else {
|
||||
compile_shader(pvert, VertexShaderID);
|
||||
if (GeometryShaderID) {
|
||||
compile_shader(pgeom, GeometryShaderID);
|
||||
}
|
||||
compile_shader(pfrag, FragmentShaderID);
|
||||
}
|
||||
|
||||
compile();
|
||||
}
|
||||
|
||||
RenderShader::RenderShader(const char* vert, const char* geom, const char* frag, bool paths) {
|
||||
load(vert, geom, frag, paths);
|
||||
}
|
||||
|
||||
void RenderShader::bind() {
|
||||
glUseProgram(programm);
|
||||
}
|
||||
|
||||
GLuint RenderShader::getu(const char* uid) {
|
||||
return glGetUniformLocation(programm, uid);
|
||||
}
|
||||
|
||||
void RenderShader::unbind() {
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
RenderShader::~RenderShader() {
|
||||
glDeleteProgram(programm);
|
||||
}
|
||||
205
RasterRender/private/Texture.cpp
Normal file
205
RasterRender/private/Texture.cpp
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
|
||||
#include "Texture.hpp"
|
||||
|
||||
#include "Map.hpp"
|
||||
|
||||
#include "Shader.hpp"
|
||||
|
||||
#include "GraphicsApi.hpp"
|
||||
|
||||
const char* texture_vertex =
|
||||
#ifdef ENV_OS_ANDROID
|
||||
"#version 300 es\n"
|
||||
"precision mediump float;\n"
|
||||
#else
|
||||
"#version 330 core\n"
|
||||
#endif
|
||||
"layout(location = 0) in vec3 vPos;\n"
|
||||
"out vec2 UV;\n"
|
||||
"void main() {\n"
|
||||
" gl_Position = vec4(vPos, 1);\n"
|
||||
" UV = (vPos.xy + vec2(1, 1)) / 2.0;\n"
|
||||
"}\n";
|
||||
|
||||
const char* texture_fragment =
|
||||
#ifdef ENV_OS_ANDROID
|
||||
"#version 300 es\n"
|
||||
"precision mediump float;\n"
|
||||
#else
|
||||
"#version 330 core\n"
|
||||
#endif
|
||||
"in vec2 UV;\n"
|
||||
"out vec4 color;\n"
|
||||
"uniform sampler2D renderedTexture;\n"
|
||||
"uniform float time;\n"
|
||||
"void main() {\n"
|
||||
" vec4 texColor = texture(renderedTexture, UV);\n"
|
||||
" if (texColor.a < 0.2)\n"
|
||||
" discard;\n"
|
||||
" color = texColor;\n"
|
||||
"}\n";
|
||||
|
||||
|
||||
using namespace tp;
|
||||
|
||||
GLuint RenderTexture::getid() { return id; }
|
||||
|
||||
RenderTexture::RenderTexture() {
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
RenderTexture::~RenderTexture() { glDeleteTextures(1, &id); }
|
||||
|
||||
|
||||
void RenderTexture::update(const Buffer2D<RGBA>& buff) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)buff.size().x, (GLsizei)buff.size().y, 0, GL_RGBA, GL_FLOAT, buff.getBuff());
|
||||
}
|
||||
|
||||
void RenderTexture::draw(const GLuint& out) {
|
||||
draw_texture(out, id);
|
||||
}
|
||||
|
||||
|
||||
struct texture_drawer_data {
|
||||
|
||||
Map<std::string, GLuint> textures;
|
||||
|
||||
GLuint quad_VertexArrayID;
|
||||
GLuint quad_vertexbuffer;
|
||||
GLuint texID;
|
||||
RenderShader shader;
|
||||
|
||||
const GLfloat g_quad_vertex_buffer_data[18] = {
|
||||
-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f,
|
||||
-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
|
||||
};
|
||||
|
||||
texture_drawer_data() : shader(texture_vertex, nullptr, texture_fragment, false) {
|
||||
// The fullscreen quad's FBO
|
||||
glGenVertexArrays(1, &quad_VertexArrayID);
|
||||
glBindVertexArray(quad_VertexArrayID);
|
||||
|
||||
glGenBuffers(1, &quad_vertexbuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data),
|
||||
g_quad_vertex_buffer_data, GL_STATIC_DRAW);
|
||||
|
||||
texID = shader.getu("renderedTexture");
|
||||
}
|
||||
|
||||
~texture_drawer_data() {
|
||||
glDeleteBuffers(1, &quad_vertexbuffer);
|
||||
glDeleteVertexArrays(1, &quad_VertexArrayID);
|
||||
|
||||
for (auto tex : textures) {
|
||||
glDeleteTextures(1, &tex->val);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
texture_drawer_data* texdd = NULL;
|
||||
|
||||
void RenderTexture::init() {
|
||||
if (!texdd) texdd = new texture_drawer_data();
|
||||
}
|
||||
|
||||
void RenderTexture::deinit() {
|
||||
if (texdd) delete texdd;
|
||||
texdd = NULL;
|
||||
}
|
||||
|
||||
void RenderTexture::draw_texture(uint4 out, uint4 in) {
|
||||
ASSERT(in);
|
||||
|
||||
// Render to the screen
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, out);
|
||||
|
||||
// Use our shader
|
||||
texdd->shader.bind();
|
||||
|
||||
// Bind our texture in Texture Unit 0
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, in);
|
||||
// Set our "renderedTexture" sampler to use Texture Unit 0
|
||||
glUniform1i(texdd->texID, 0);
|
||||
|
||||
// glUniformMatrix4fv(texdd->rect_mat, 1, GL_FALSE, &tmat[0][0]);
|
||||
|
||||
// 1rst attribute buffer : vertices
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, texdd->quad_vertexbuffer);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
// Draw the triangles. 2*3 indices starting at 0 -> 2 triangles
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
glDisableVertexAttribArray(0);
|
||||
|
||||
texdd->shader.unbind();
|
||||
}
|
||||
|
||||
GLuint load_texture(const std::string& name) {
|
||||
GLuint tex_2d = 0;
|
||||
|
||||
ASSERT(0 && "incomplete compilation - no SOIL support added");
|
||||
|
||||
if (0) {
|
||||
//auto document = lunasvg::Document::loadFromFile("tiger.svg");
|
||||
//auto bitmap = document->renderToBitmap();
|
||||
}
|
||||
else {
|
||||
/*
|
||||
tex_2d = SOIL_load_OGL_texture(
|
||||
name.cstr(), SOIL_LOAD_RGBA, SOIL_CREATE_NEW_ID,
|
||||
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB |
|
||||
SOIL_FLAG_COMPRESS_TO_DXT);
|
||||
|
||||
if (0 == tex_2d) {
|
||||
printf("SOIL loading error: '%s'\n", SOIL_last_result());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
return tex_2d;
|
||||
}
|
||||
|
||||
GLuint RenderTexture::get_tex(const char* TexId) {
|
||||
GLuint out = 0;
|
||||
auto idx = texdd->textures.presents(TexId);
|
||||
if (idx) {
|
||||
out = texdd->textures.get(TexId);
|
||||
}
|
||||
else {
|
||||
out = load_texture(TexId);
|
||||
texdd->textures.put(TexId, out);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void RenderTexture::drawCurcle(Vec2F pos, double radius, RGBA col) {
|
||||
#ifndef ENV_OS_ANDROID
|
||||
static alni precision = 40;
|
||||
|
||||
glColor4f(col.r, col.g, col.b, col.a);
|
||||
|
||||
double twicePi = 2.0 * 3.142;
|
||||
|
||||
glBegin(GL_TRIANGLE_FAN); // BEGIN CIRCLE
|
||||
glVertex2f(pos.x, pos.y); // center of circle
|
||||
|
||||
for (alni i = 0; i <= precision; i++) {
|
||||
glVertex2f((GLfloat)(pos.x + (radius * cos(i * twicePi / precision))),
|
||||
(GLfloat)(pos.y + (radius * sin(i * twicePi / precision))));
|
||||
}
|
||||
glEnd(); // END
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue