ported the University CGI demo from WINDOWS + GLUT + GLEW + GLU + OpenGL 4 to LINUX WAYLAND + EGL + GLES 2 with minimal cuts

This commit is contained in:
beno
2025-07-03 01:26:25 +02:00
commit 6c125fb35e
85 changed files with 91688 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
#version 430 core
//Uniform block containing positions and masses of the attractors
layout (std140, binding = 0) uniform attractor_block
{
vec4 attractor[8]; //xyz = position, w = strength
};
//Process particles in blocks of 128
layout (local_size_x = 1024) in;
//buffers containing the positions and velocities of the particles
layout (rgba32f, binding = 0) uniform imageBuffer velocity_buffer;
layout (rgba32f, binding = 1) uniform imageBuffer start_position_buffer;
layout (rgba32f, binding = 2) uniform imageBuffer end_position_buffer;
//Delta time
uniform float dt;
uniform vec3 nearTopLeft;
uniform vec3 nearBottomRight;
uniform vec3 farTopLeft;
uniform vec3 farBottomRight;
bool insideFrustrum(vec4 point) {
//float zRelative = ((point.z - nearBottomRight.z) / (nearBottomRight.z - farBottomRight.z));
//bool moreLeft = (point.x >= (nearTopLeft.x + (nearTopLeft.x - farTopLeft.x) * zRelative)); // left plane
//bool lessRight = (point.x <= (nearBottomRight.x + (nearBottomRight.x - farBottomRight.x) * zRelative)); // left plane
//bool moreBottom = (point.y >= (nearBottomRight.y + (nearBottomRight.y - farBottomRight.y) * zRelative)); //top plane
//bool lessTop = (point.y <= (nearTopLeft.y + (nearTopLeft.y - farTopLeft.y) * zRelative)); //bottom plane
//return ( (zRelative >= -1.0f) && moreLeft && lessRight && lessTop && moreBottom);
return ((point.x < farBottomRight.x) && (point.x > farTopLeft.x) && (point.y < farTopLeft.y) && (point.y > farBottomRight.y) && (point.z > -1.0));
}
vec4 scaleToZZero(vec4 v){
vec4 newV = v / (1 + v.z);
return newV;
}
void main(void)
{
//read the current position and velocity from the buffers
vec4 vel = imageLoad(velocity_buffer, int(gl_GlobalInvocationID.x));
vec4 pos = imageLoad(start_position_buffer, int(gl_GlobalInvocationID.x));
int i;
bool inFrustrum = true;
float dt2 = dt * dt;
while( inFrustrum && pos.z < 0.0f){
//update position using current velocity * time
pos.xyz += vel.xyz * dt;
//for each attractor... BOTTLENECK
for(i = 0; i < 8; i++)
{
if(attractor[i].w > 0){
//calculate force and update velocity accordingly
vec3 dir = (attractor[i].xyz - pos.xyz);
float dist = dot(dir, dir);
vel.xyz += (dt2 * attractor[i].w * cross(dir, vel.xyz) / (pow(dist, 2)));
}
}
inFrustrum = insideFrustrum(pos);
}
pos = scaleToZZero(pos);
//store the new position back into the buffers
imageStore(end_position_buffer, int(gl_GlobalInvocationID.x), pos);
}

View File

@@ -0,0 +1,24 @@
#version 430 core
layout (location = 0) out vec4 color;
layout(origin_upper_left, pixel_center_integer) in vec4 gl_FragCoord;
in vec4 particleColor;
uniform uint pixelWidth;
void main(void)
{
color = vec4(particleColor.xyz, 1.0f);
/*
color.r *= ((mod(gl_FragCoord.x, 8) != 0) && (mod(gl_FragCoord.y, 8) != 0) && (mod(gl_FragCoord.x, 8) != 1) && (mod(gl_FragCoord.y, 8) != 1))? 0 : 1;
color.g *= ((mod(gl_FragCoord.x, 8) != 2) && (mod(gl_FragCoord.y, 8) != 2) && (mod(gl_FragCoord.x, 8) != 3) && (mod(gl_FragCoord.y, 8) != 3))? 0 : 1;
color.b *= ((mod(gl_FragCoord.x, 8) != 4) && (mod(gl_FragCoord.y, 8) != 4) && (mod(gl_FragCoord.x, 8) != 5) && (mod(gl_FragCoord.y, 8) != 5))? 0 : 1;
*/
float mx = mod(roundEven(gl_FragCoord.x), 4 * pixelWidth);
float my = mod(roundEven(gl_FragCoord.y), 4 * pixelWidth);
color.r *= ((mx < pixelWidth) && (my < pixelWidth * 3)) ? 0.8 : 0.3;
color.g *= ((mx > pixelWidth && mx < pixelWidth * 2) && (my < pixelWidth * 3)) ? 1 : 0.4;
color.b *= ((mx > pixelWidth * 2 && mx < pixelWidth * 3) && (my < pixelWidth * 3)) ? 1 : 0.4;
}

View File

@@ -0,0 +1,17 @@
#version 430 core
layout (location = 0) in vec4 particleData;
//////
layout (location = 2) in vec4 iparticleColor;
out float intensity;
//
out vec4 particleColor;
uniform mat4 mvp;
void main(void)
{
gl_Position = mvp * vec4( particleData.xy, 0, 1);
particleColor = iparticleColor;
}

View File

@@ -0,0 +1,275 @@
#include "../../HEADERS/EFFECTS/CRT_shader.h"
#include "../../HEADERS/UTILS/ShaderMaker.h"
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
int CRT_Shader::CRT_HORIZONTAL_PIXELS;
int CRT_Shader::CRT_VERTICAL_PIXELS;
int CRT_Shader::CRT_WIDTH;
int CRT_Shader::CRT_HEIGHT;
int CRT_Shader::PARTICLE_COUNT;
int CRT_Shader::PARTICLE_GROUP_COUNT;
bool CRT_Shader::CRT_FLIP_Y;
SIM_PRECISION CRT_Shader::resolution;
vec3 CRT_Shader::scaleToZOne(vec3 vector) {
return (1 / vector.z) * vector;
}
vec3 CRT_Shader::crt_emission_angle(int index) {
int xCoord = (index % CRT_HORIZONTAL_PIXELS) - (CRT_HORIZONTAL_PIXELS / 2);
int yCoord = (index / CRT_HORIZONTAL_PIXELS) - (CRT_VERTICAL_PIXELS / 2);
float longCat = CRT_DEPTH;
float shortCatX = CRT_PIXEL_LENGTH * xCoord;
float shortCatY = CRT_PIXEL_LENGTH * yCoord;
vec3 res = vec3(shortCatX, -shortCatY, 1.0);
return res;
}
void CRT_Shader::createRenderShader() {
string vertexSh = resourceRoot + "vertexShader_particles.glsl";
string fragmentSh = resourceRoot + "fragmentShader_particles.glsl";
render_prog = ShaderMaker::createProgram(&vertexSh[0], &fragmentSh[0]);
}
void CRT_Shader::createComputeShader() {
GLenum ErrorCheckValue = glGetError();
string compSh = resourceRoot + "computeShader_particles.glsl";
GLchar* VertexShader = ShaderMaker::readShaderSource(&compSh[0]);
//Visualizzo sulla console il CODICE VERTEX SHADER
std::cout << VertexShader;
//Generiamo un identificativo per il vertex shader
GLuint computeShaderId = glCreateShader(GL_COMPUTE_SHADER);
//Associamo all'identificativo il codice del vertex shader
glShaderSource(computeShaderId, 1, (const char**)&VertexShader, NULL);
//Compiliamo il Vertex SHader
glCompileShader(computeShaderId);
int success;
char infoLog[512];
glGetShaderiv(computeShaderId, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(computeShaderId, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
ErrorCheckValue = glGetError();
std::cout << ErrorCheckValue;
if (ErrorCheckValue != GL_NO_ERROR)
{
fprintf(
stderr,
"ERROR: Could not create the shaders: %s \n",
gluErrorString(ErrorCheckValue)
);
exit(-1);
}
//Creiamo un identificativo di un eseguibile e gli colleghiamo i due shader compilati
compute_prog = glCreateProgram();
glAttachShader(compute_prog, computeShaderId);
glLinkProgram(compute_prog);
dt_location = glGetUniformLocation(compute_prog, "dt");
}
void CRT_Shader::buffersSetup() {
GLuint buffers[5];
GLuint textures[3];
glGenBuffers(5, buffers);
start_position_buffer = buffers[0];
end_position_buffer = buffers[1];
velocity_buffer = buffers[2];
color_buffer = buffers[3];
magnets_buffer = buffers[4];
glBindBuffer(GL_ARRAY_BUFFER, magnets_buffer);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(vec4), NULL, GL_DYNAMIC_COPY); //dynamic copy ???
for (int i = 0; i < 8; i++) {
attractors[i] = vec4();
}
glBindBuffer(GL_ARRAY_BUFFER, start_position_buffer);
glBufferData(GL_ARRAY_BUFFER, PARTICLE_COUNT * sizeof(vec4), NULL, GL_DYNAMIC_COPY);
//Map the position buffer
vec4* positions = (vec4*)glMapNamedBufferRangeEXT(start_position_buffer, 0, PARTICLE_COUNT * sizeof(vec4), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
for (int i = 0; i < PARTICLE_COUNT; i++)
{
positions[i] = vec4(0, 0, -CRT_DEPTH, 0);
}
glUnmapNamedBufferEXT(start_position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, end_position_buffer);
glBufferData(GL_ARRAY_BUFFER, PARTICLE_COUNT * sizeof(vec4), NULL, GL_DYNAMIC_COPY);
glUnmapNamedBufferEXT(end_position_buffer);
//initialization of the velocity buffer
glBindBuffer(GL_ARRAY_BUFFER, velocity_buffer);
glBufferData(GL_ARRAY_BUFFER, PARTICLE_COUNT * sizeof(vec4), NULL, GL_DYNAMIC_COPY);
vec4* velocities = (vec4*)glMapBufferRange(GL_ARRAY_BUFFER, 0, PARTICLE_COUNT * sizeof(vec4), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
for (int i = 0; i < CRT_VERTICAL_PIXELS * CRT_HORIZONTAL_PIXELS; i++)
{
velocities[i] = glm::vec4(crt_emission_angle(i), 0.0);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
//////////////////////////////////////////////////////////
//initialization of the color buffer
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glBufferData(GL_ARRAY_BUFFER, PARTICLE_COUNT * COLOR_CHANNELS * sizeof(unsigned char), NULL, GL_DYNAMIC_COPY);
glGenTextures(3, textures);
velocity_tbo = textures[0];
start_position_tbo = textures[1];
end_position_tbo = textures[2];
glBindTexture(GL_TEXTURE_BUFFER, velocity_tbo);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, velocity_buffer);
glBindTexture(GL_TEXTURE_BUFFER, start_position_tbo);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, start_position_buffer);
glBindTexture(GL_TEXTURE_BUFFER, end_position_tbo);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, end_position_buffer);
}
void CRT_Shader::VAOsSetup() {
glGenVertexArrays(1, &render_vao);
glBindVertexArray(render_vao);
glBindBuffer(GL_ARRAY_BUFFER, end_position_buffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindBuffer(GL_ARRAY_BUFFER, velocity_buffer);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
/////////////////////
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, 4 * sizeof(unsigned char), 0);
glBindBuffer(GL_ARRAY_BUFFER, magnets_buffer);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
}
CRT_Shader::CRT_Shader() {
createRenderShader();
createComputeShader();
float top = crt_emission_angle(0).y;
float bottom = crt_emission_angle((PARTICLE_COUNT)-1).y;
if (CRT_FLIP_Y) {
float swap = top;
top = bottom;
bottom = swap;
}
orthoMat = ortho(crt_emission_angle(0).x,
crt_emission_angle((PARTICLE_COUNT) - 1).x,
bottom,
top,
-1.0f,
1.0f);
buffersSetup();
VAOsSetup();
CS_LTF = vec3(scaleToZOne(crt_emission_angle(0)).x, scaleToZOne(crt_emission_angle(0)).y, 0);
CS_RBF = vec3(scaleToZOne(crt_emission_angle((PARTICLE_COUNT) - 1)).x, scaleToZOne(crt_emission_angle((PARTICLE_COUNT) - 1)).y, 0);
CS_LTN = vec3(-0.1, 0.1, -1);
CS_RBN = vec3(0.1, -0.1, -1);
}
void CRT_Shader::draw(unsigned int ONE_TICK_MS, unsigned int timePassed) {
glBindBuffer(GL_ARRAY_BUFFER, magnets_buffer);
vec4* attractors = (vec4*)glMapNamedBufferRangeEXT(magnets_buffer, 0, 8 * sizeof(vec4), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
for (int i = 0; i < 8; i++)
attractors[i] = this->attractors[i];
glUnmapNamedBufferEXT(magnets_buffer);
//activate the compute program and bind the position
//and velocity buffers
glUseProgram(compute_prog);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, magnets_buffer);
glBindImageTexture(0, velocity_tbo, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
glBindImageTexture(1, start_position_tbo, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
glBindImageTexture(2, end_position_tbo, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glUniform3f(glGetUniformLocation(compute_prog, "nearTopLeft"), CS_LTN.x, CS_LTN.y, CS_LTN.z);
glUniform3f(glGetUniformLocation(compute_prog, "nearBottomRight"), CS_RBN.x, CS_RBN.y, CS_RBN.z);
glUniform3f(glGetUniformLocation(compute_prog, "farTopLeft"), CS_LTF.x, CS_LTF.y, CS_LTF.z);
glUniform3f(glGetUniformLocation(compute_prog, "farBottomRight"), CS_RBF.x, CS_RBF.y, CS_RBF.z);
//set delta time
glUniform1f(dt_location, 1e-2 * pow(4, (int) resolution));
//dispatch the compute shader
glDispatchCompute(PARTICLE_GROUP_COUNT, 1, 1);
//ensure that writes by the compute shader have completed
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
glUseProgram(render_prog);
glUniformMatrix4fv(glGetUniformLocation(render_prog, "mvp"), 1, GL_FALSE, value_ptr(orthoMat));
glUniform1ui(glGetUniformLocation(render_prog, "pixelWidth"), CRT_PIXEL_SCALE * 1000 * CRT_PIXEL_LENGTH);
glBindVertexArray(render_vao);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glPointSize(CRT_PIXEL_SCALE);
glDrawArrays(GL_POINTS, 0, PARTICLE_COUNT);
glDisable(GL_BLEND);
}
void CRT_Shader::setMagnet(int index, vec4 positionAndMass) {
float vieww = scaleToZOne(crt_emission_angle((PARTICLE_COUNT) - 1)).x - scaleToZOne(crt_emission_angle(0)).x;
float viewh = scaleToZOne(crt_emission_angle((PARTICLE_COUNT) - 1)).y - scaleToZOne(crt_emission_angle(0)).y;
magx = vieww * (positionAndMass.x - CRT_WIDTH / 2) / ((float)CRT_WIDTH);
magy = viewh * (positionAndMass.y - CRT_HEIGHT / 2) / ((float)CRT_HEIGHT);
attractors[index] = vec4(magx, magy, positionAndMass.z, positionAndMass.w * pow(4, (int)-resolution));
}
/*
DOES ANYTHING CHANGE WITH CRT_FLIP_Y ???
*/
float CRT_Shader::getLeftBound() {
return -CRT_WIDTH / 2;
}
float CRT_Shader::getRightBound() {
return CRT_WIDTH / 2;
}
float CRT_Shader::getBottomBound() {
return -(CRT_HEIGHT / 2);
}
float CRT_Shader::getTopBound() {
return (CRT_HEIGHT / 2);
}
void CRT_Shader::loadColorFromFramebuffer(GLuint FBO) {
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glBindBuffer(GL_PIXEL_PACK_BUFFER, color_buffer);
glReadPixels(0, 0, CRT_HORIZONTAL_PIXELS, CRT_VERTICAL_PIXELS, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}

View File

@@ -0,0 +1,22 @@
#include "../../HEADERS/EFFECTS/PseudoContext.h"
#include <vector>
PseudoContext::PseudoContext(int left, int top, int width, int height) {
this->width = width;
this->height = height;
this->top = top;
this->left = left;
}
std::vector<int> PseudoContext::getBounds() {
int top = this->top;
int bottom = this->top + this->height;
int left = this->left;
int right = this->left + this->width;
std::vector<int> result = std::vector<int>();
result.push_back(top);
result.push_back(bottom);
result.push_back(left);
result.push_back(right);
return result;
}

View File

@@ -0,0 +1,54 @@
/*
#include <GL/glew.h>
*/
#include <GLES2/gl2.h>
#include "../../HEADERS/EFFECTS/ReelManager.h"
#include <vector>
/* equals stdlib.h from C */
#include <cstdlib>
ReelManager::ReelManager(unsigned int textureNumber, unsigned int width, unsigned int height) {
TEXTURE_NUM = textureNumber;
textures = (GLuint*) malloc(TEXTURE_NUM * sizeof(GLuint));
glGenFramebuffers(1, &FBO);
glGenTextures(TEXTURE_NUM, textures);
for (int i = 0; i < TEXTURE_NUM; i++) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
void ReelManager::nextTexture() {
currentIndex = ++currentIndex % TEXTURE_NUM;
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[currentIndex], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void ReelManager::clearTexture() {
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[currentIndex], 0);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
GLuint ReelManager::getFBO() {
return FBO;
}
std::vector<GLuint> ReelManager::getTextures() {
std::vector<GLuint> vec = std::vector<GLuint>();
for (int i = currentIndex + 1 ; i < TEXTURE_NUM; i++) {
vec.push_back(textures[i]);
}
for (int i = 0; i <= currentIndex; i++) {
vec.push_back(textures[i]);
}
return vec;
}

View File

@@ -0,0 +1,18 @@
#version 300 es
/*this will affect all the float guys (float, vecN, matN )*/
precision mediump float;
in vec3 fragPos;
in vec2 frag_uv; //coordinate 2d di texure
uniform sampler2D inTex; //campionatore 2d
out vec4 FragColor;
void main()
{
//FragColor = vec4(1.0f, 1.0f, 0, 1);
FragColor = texture(inTex, frag_uv);
}

View File

@@ -0,0 +1,21 @@
#version 300 es
/*this will affect all the float guys (float, vecN, matN )*/
precision mediump float;
layout (location = 0) in vec3 vertPos; // Attributo Posizione
out vec3 fragPos;
out vec2 frag_uv;
void main()
{
float adjustedU = (vertPos.x + 1) / 2;
float adjustedV = (vertPos.y + 1) / 2;
frag_uv = vec2(adjustedU, adjustedV);
fragPos = vertPos;
gl_Position = vec4(fragPos, 1);
}

View File

@@ -0,0 +1,56 @@
#include "../../HEADERS/EFFECTS/Scene.h"
extern "C" {
#include "../../GLES_3_1_compatibility.h"
}
/* REPLACED
#include <GL/freeglut.h>
*/
#include <glut.h>
#include <glm/gtc/matrix_transform.hpp>
#include <string>
#include <iostream>
using namespace glm;
Scene::Scene(int left, int top, int width, int height)
: PseudoContext { left, top, width, height }
{
glGenFramebuffers(1, &FBO);
glGenRenderbuffers(1, &depthRBO);
glBindRenderbuffer(GL_RENDERBUFFER, depthRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
void Scene::draw() {
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, outputTexture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthRBO);
GLenum DrawBuffers[2] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_STENCIL_ATTACHMENT };
glDrawBuffers(2, DrawBuffers);
}
void Scene::setOutTexture(GLuint outTexture) {
outputTexture = outTexture;
}
void Scene::keyPress(unsigned char key, int x, int y) {
switch (key)
{
case 27:
exit(0);
break;
default:
break;
}
}
GLuint Scene::getFrameBuffer() {
return this->FBO;
}

View File

@@ -0,0 +1,48 @@
#include "../../HEADERS/EFFECTS/Screen.h"
extern "C" {
#include "../../GLES_3_1_compatibility.h"
#include <glut.h>
}
#include <string>
#include "../../HEADERS/UTILS/ShaderMaker.h"
Screen::Screen(int left, int top, int width, int height)
: PseudoContext{ left, top, width, height }
{
glGenVertexArrays(1, &quadVAO);
glBindVertexArray(quadVAO);
glGenBuffers(1, &vertVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVerts), quadVerts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float)/**/,(void*) 0);
glEnableVertexAttribArray(0);
string root = "C:\\Users\\BoBoBoat\\Desktop\\SCUOLA\\CGI\\GL_STUFF\\GL_STUFF\\SOURCES\\EFFECTS\\SCREEN";
string vertSh = root + "\\vertexShader_2.glsl";
string fragSh = root + "\\fragmentShader_2.glsl";
program2 = ShaderMaker::createProgram(&vertSh[0], &fragSh[0]);
shaderTexId = glGetUniformLocation(program2, "inTex");
}
void Screen::setInputTexture(GLuint inputTex) {
inputTexture = inputTex;
}
void Screen::draw() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//glViewport(left, top, width, height);
//gluOrtho2D(0, width, height, 0);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program2);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, inputTexture);
glUniform1i(shaderTexId, 0);
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glutSwapBuffers();
}

View File

@@ -0,0 +1,27 @@
#version 300 es
/*this will affect all the float guys (float, vecN, matN )*/
precision mediump float;
in vec3 fragPos;
in vec2 frag_uv; //coordinate 2d di texure
uniform sampler2D inTex1; //campionatore 2d
uniform sampler2D inTex2;
out vec4 FragColor;
vec4 max4(vec4 a, vec4 b){
return vec4( max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
}
void main()
{
float mergeFac = 0.6;
float adjFac = 1.5;
//FragColor = vec4(1.0f, 1.0f, 0, 1);
FragColor = mix(texture(inTex1, frag_uv), texture(inTex2, frag_uv), mergeFac);
FragColor = vec4( clamp( 0, 1, adjFac * FragColor.x), clamp( 0, 1, adjFac * FragColor.y), clamp( 0, 1, adjFac * FragColor.z), clamp( 0, 1, adjFac * FragColor.w));
//FragColor = 0.25 * texture(inTex1, frag_uv) + 0.75 * texture(inTex2, frag_uv);//max4(texture(inTex1, frag_uv) , texture(inTex2, frag_uv));
}

View File

@@ -0,0 +1,20 @@
#version 300 es
/*this will affect all the float guys (float, vecN, matN )*/
precision mediump float;
layout (location = 0) in vec3 vertPos; // Attributo Posizione
out vec3 fragPos;
out vec2 frag_uv;
void main()
{
float adjustedU = (vertPos.x + 1) / 2;
float adjustedV = (vertPos.y + 1) / 2;
frag_uv = vec2(adjustedU, adjustedV);
fragPos = vertPos;
gl_Position = vec4(fragPos, 1);
}

View File

@@ -0,0 +1,134 @@
#include "../../HEADERS/EFFECTS/TexMerger.h"
#include "../../HEADERS/UTILS/ShaderMaker.h"
#include <string>
#include <iostream>
#include <glm/glm.hpp>
extern "C" {
#include "../../GLES_3_1_compatibility.h"
}
TexMerger::TexMerger(GLuint texCount, unsigned int texWidth, unsigned int texHeight) {
glGenVertexArrays(1, &quadVAO);
glBindVertexArray(quadVAO);
glGenBuffers(1, &vertVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVerts), quadVerts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float)/**/, (void*)0);
glEnableVertexAttribArray(0);
std::string root = "C:\\Users\\BoBoBoat\\Desktop\\SCUOLA\\CGI\\GL_STUFF\\GL_STUFF\\SOURCES\\EFFECTS\\TEX_MERGER";
std::string vertSh = root + "\\vertexShader_merger.glsl";
std::string fragSh = root + "\\fragmentShader_merger.glsl";
program = ShaderMaker::createProgram(&vertSh[0], &fragSh[0]);
shaderTex1Id = glGetUniformLocation(program, "inTex1");
shaderTex2Id = glGetUniformLocation(program, "inTex2");
bufCount = ceil(log2((float)texCount));
mergeBuffers = (GLuint*) malloc(bufCount * sizeof(GLuint));
glGenFramebuffers(1, &FBO);
glGenTextures(bufCount, mergeBuffers);
for (unsigned int i = 0; i < bufCount; i++) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, mergeBuffers[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); //RGB o RGBA?
usageMap.push_back(false);
}
}
GLuint TexMerger::reserveBuffer() {
unsigned int index = 0;
while (index < bufCount && usageMap[index]) {
index++;
}
if (index >= bufCount)
throw "TexMerger::reserveBuffer : free buffer not found";
return mergeBuffers[index];
}
bool TexMerger::isBuffer(GLuint number) {
bool found = false;
int index = -1;
while (!found && (++index < bufCount))
if (mergeBuffers[index] == number)
found = true;
return found;
}
void TexMerger::freeBuffer(GLuint number) {
bool found = false;
int index = -1;
while (!found && (++index < bufCount))
if (mergeBuffers[index] == number)
found = true;
if (found)
usageMap[index] = false;
else
throw "TexMerger::freeBuffer : index not found";
}
/*(BUFFER è in realtà un indice)
* MERGE (START,END)
* if (END - START) == 0
* leggi la texture
* ritorna texture
* else
* MID = (START + END) / 2
* B1 = MERGE(START, MID)
* B2 = MERGE(MID + 1, END )
* scegli un BUFFER libero (bloccante)
* somma B1, B2 nel BUFFER
* sblocca tra B1 e B2 quelli che sono BUFFER
* ritorna BUFFER
*/
GLuint TexMerger::merge(std::vector<GLuint> inTextures, int startIndex, int endIndex) {
if ((endIndex - startIndex) == 0) {
return inTextures[startIndex];
}
else {
int mid = (startIndex + endIndex) / 2;
GLuint buf1;
GLuint buf2;
GLuint result;
try{
buf1 = merge(inTextures, startIndex, mid);
buf2 = merge(inTextures, mid + 1, endIndex);
result = reserveBuffer();
}
catch( std::string ex){
std::cout << ex;
}
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FBO);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, result, 0);
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
/* commented and not available
glDrawBuffers(1, DrawBuffers);
*/
//glClearColor(0, 0, 0, 0);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, buf1);
glUniform1i(shaderTex1Id, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, buf2);
glUniform1i(shaderTex2Id, 1);
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
if (isBuffer(buf1))
freeBuffer(buf1);
if (isBuffer(buf2))
freeBuffer(buf2);
return result;
}
}