Files
rez_demo/Main.cpp

502 lines
14 KiB
C++
Executable File

/* C++ is picky about the linking of C
see
https://stackoverflow.com/questions/38761620/undefined-reference-to-error-while-linking-object-files
*/
extern "C" {
#include <GL_STUFF/GLES_3_1_compatibility.h>
#include <glut.h>
}
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <GL_STUFF/HEADERS/CURVES/CurvesLoader.hpp>
#include <GL_STUFF/HEADERS/SCENE/InstancesLoader.h>
#include <GL_STUFF/HEADERS/CURVES/CurveIterator.hpp>
#include <GL_STUFF/HEADERS/SCENE/Camera.hpp>
#include <GL_STUFF/HEADERS/SCENE/Instance3D.h>
#include <GL_STUFF/HEADERS/SCENE/ShadingHelper.h>
#include <GL_STUFF/HEADERS/UTILS/ResourceCache.h>
#include <GL_STUFF/HEADERS/UTILS/ShaderMaker.h>
#include <GL_STUFF/HEADERS/UTILS/ClockIterator.hpp>
/* EXCLUDED BC NO COMPUTE SHADERS IN GLES2
#include <GL_STUFF/HEADERS/EFFECTS/CRT_shader.h>
*/
#include <GL_STUFF/HEADERS/EFFECTS/ReelManager.h>
#include <GL_STUFF/HEADERS/EFFECTS/TexMerger.h>
#include <GL_STUFF/HEADERS/EFFECTS/Scene.h>
#include <GL_STUFF/HEADERS/EFFECTS/Screen.h>
using namespace glm;
using namespace std;
const char *RES_FOLDER = "/CGI_DEMO/RESOURCES/";
const int TARGET_FPS = 15;
static int ONE_TICK_MS = 1000 / TARGET_FPS;
float viewSize = 2.0f;
float aspect_ratio;
int steps = 60;
GLuint linesShader;
GLuint curveVAO;
int nCurvePoints = 10000;
GLuint controlPointsVAO;
int nControlPoints;
GLuint curvePointsVBO;
GLuint controlPointsVBO;
mat4 projection;
int baseClock = 8000;
ClockIterator cubesClock = ClockIterator(baseClock);
vector<Instance3D*> columns = vector<Instance3D*>();
ShadingHelper* columnsShading;
Instance3D* cube;
ShadingHelper* cubeShading;
vector<TextureHelper> cubeTextures;
vector<Curve*> curves = vector<Curve*>();
vector<CurveIterator*> iterators = vector<CurveIterator*>();
CameraController *camera;
Scene* sceneBuffer;
ReelManager* reel;
TexMerger* merger;
Screen* canvas;
/*was false and triggerable via keypress but i have no input capturing*/
bool runIterator = true;
/* don't want to implement */
/* also removed
glEnable(GL_ALPHA_TEST);
glDisable(GL_ALPHA_TEST);
*/
GLuint GLUT_COMPATIBILITY_PROFILE = 0 ;
GLuint GLUT_SINGLE = 0 ;
GLuint GLUT_RGBA = 0 ;
const GLuint GLUT_SCREEN_WIDTH = 0;
const GLuint GLUT_SCREEN_HEIGHT = 1;
void glutInitContextVersion( int maj, int min){}
void glutInitContextProfile( GLuint profile){}
void glutInitDisplayMode( GLuint OR_ed_FLAGS){}
int glutGet(GLuint dimension){
switch( dimension) {
case GLUT_SCREEN_WIDTH :
return 1920;
break;
case GLUT_SCREEN_HEIGHT :
return 1156;
break;
default:
exit(1);
}
}
vector<GLuint> antiLogSelect(vector<GLuint> frames, float increment) {
vector<int> indices = vector<int>();
assert(increment > 1);
float gap = 1;
for (int index = 0; index < frames.size(); index += gap) {
gap *= increment;
indices.push_back(index);
}
vector<GLuint> selected = vector<GLuint>();
for (int i = indices.size() - 1; i >= 0; i--) {
selected.push_back(frames[frames.size() - 1 - indices[i]]);
}
return selected;
}
void drawAxis() {
/*
glUseProgram(linesShader);
//AXIS
glUniform3f(glGetUniformLocation(linesShader, "baseColor"), 1, 0, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(5, 0, 0);
glEnd();
glUniform3f(glGetUniformLocation(linesShader, "baseColor"), 0, 0, 1);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 5, 0);
glEnd();
glUniform3f(glGetUniformLocation(linesShader, "baseColor"), 0, 1, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glEnd();
*/
}
void drawCurve() {
glUseProgram(linesShader);
//CURVE
glUniform3f(glGetUniformLocation(linesShader, "baseColor"), 0, 1, 0);
GLint localTransformUniform = glGetUniformLocation(linesShader, "Model");
glUniformMatrix4fv(localTransformUniform, 1, GL_FALSE, value_ptr(glm::mat4(1.0)));
GLint projectionUniform = glGetUniformLocation(linesShader, "Projection");
glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, value_ptr(projection));
GLint viewUniform = glGetUniformLocation(linesShader, "View");
glUniformMatrix4fv(viewUniform, 1, GL_FALSE, value_ptr(camera->getCamera()->getViewMat()));
glBindVertexArray(curveVAO);
glDrawArrays(GL_POINTS, 0, 2 * nCurvePoints);
glBindVertexArray(0);
/* DISABLED
glUniform3f(glGetUniformLocation(linesShader, "baseColor"), 1, 0, 0);
glPointSize(10.0);
glBegin(GL_POINTS);
vec3 p = iterators[0]->evaluation();
glVertex3f(p.x, p.y, p.z);
glEnd();
glPointSize(1.0);
glUniform3f(glGetUniformLocation(linesShader, "baseColor"), 0, 0, 1);
glBegin(GL_LINES);
vec3 d = p + iterators[0]->derivation();
glVertex3f(p.x, p.y, p.z);
glVertex3f(d.x, d.y, d.z);
glEnd();
*/
glUniform3f(glGetUniformLocation(linesShader, "baseColor"), 1, 0, 0);
glBindVertexArray(controlPointsVAO);
glDrawArrays(GL_LINES, 0, 2 * nControlPoints);
glBindVertexArray(0);
}
//funky stuff ahead
void renderLoop() {
/* DISABLED
reel->nextTexture();
reel->clearTexture();
vector<GLuint> frames = reel->getTextures();
sceneBuffer->setOutTexture(frames.back());
sceneBuffer->draw();
*/
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDepthFunc(GL_LESS);
//glEnable(GL_BLEND); //Attiva il blending
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
cubeShading->useProgram();
cubeShading->bindTexture2D(1, cubeTextures[0].texture, cubeTextures[0].uniformName);
cubeShading->bindTexture2D(2, cubeTextures[1].texture, cubeTextures[1].uniformName);
cubeShading->bindViewUniforms(projection, camera->getCamera()->getViewMat());
cubeShading->bindLightUniforms(vec3(0, 100, 0), vec3(1.0f), camera->getCamera()->getPosition());
cubeShading->bindModelUniforms(cube->getGlobalTransform(), cube->getLocalTransform());
cube->getModel()->draw();
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
/*
glEnable(GL_ALPHA_TEST);
*/
glEnable(GL_BLEND); //Attiva il blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_MAX); //evita i brutti artefatti
drawAxis();
drawCurve();
columnsShading->useProgram();
glUniform1f(glGetUniformLocation(columnsShading->getProgram() , "time"), cubesClock.getPercentage() / 100.0f);
columnsShading->bindViewUniforms(projection, camera->getCamera()->getViewMat());
columnsShading->bindLightUniforms( vec3(0, 100, 0), vec3(1.0f), camera->getCamera()->getPosition());
vec3 LBN = vec3(0,10000,0);
vec3 RTF = vec3(0,-10000,0);
for (Instance3D* inst : columns) {
LBN = (LBN.y < inst->getModel()->getLBN().y) ? LBN : inst->getModel()->getLBN();
RTF = (RTF.y > inst->getModel()->getRTF().y) ? RTF : inst->getModel()->getRTF();
}
for(Instance3D* inst : columns){
glUniform3fv(glGetUniformLocation(columnsShading->getProgram(), "boundingBoxLBN"),1, &LBN[0]);
glUniform3fv(glGetUniformLocation(columnsShading->getProgram(), "boundingBoxRTF"),1, &RTF[0]);
columnsShading->bindModelUniforms(inst->getGlobalTransform(), inst->getLocalTransform());
inst->getModel()->draw();
}
/*
glDisable(GL_ALPHA_TEST);
*/
glDisable(GL_BLEND);
/* DISABLED
vector<GLuint> selectedFrames = antiLogSelect(frames, 1.01 + (1 - cubesClock.getPercentage() / 100.0f) * 5);
GLuint merged = merger->merge(selectedFrames, 0, selectedFrames.size() - 1);
*/
//GLuint merged = selectedFrames.back();
/*
canvas->setInputTexture(merged);
canvas->draw();
*/
/* DISABLED
sceneBuffer->setOutTexture(merged);
sceneBuffer->draw();
CRT_Shader::get().loadColorFromFramebuffer(sceneBuffer->getFrameBuffer());
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
CRT_Shader::get().draw(ONE_TICK_MS, 0);
*/
glutSwapBuffers();
}
void timer(int value) {
cubesClock.step(ONE_TICK_MS);
cube->rotate(vec3(0, 1, 0), 1);
if (runIterator) {
//cube->setPosition(iterators[0]->evaluation());
//cube->alignTo(iterators[0]->derivation());
camera->getCamera()->setPosition(iterators[0]->evaluation());
camera->getCamera()->alignTo(iterators[0]->derivation());
for(int i = 0; i < 10; i++)
iterators[0]->nextStep();
/*
std::cout << "eval at" << iterators[0]->getStep() << "\n";
*/
}
glutTimerFunc(ONE_TICK_MS, timer, 0);
glutPostRedisplay();
}
void keypressed(unsigned char key, int x, int y) {
if (key >= '0' && key <= '9') {
int prog = cubesClock.getPosition();
cubesClock = ClockIterator(baseClock / pow( 3 , (key - '0') / 2.0f ));
cubesClock.step(prog);
//for (CurveIterator* it : iterators)
// it->setProgress((key - '0')/ 10.0f);
}
else if (key == 32) {
runIterator = !runIterator;
}else if (key == 27) {
exit(0);
}
else {
camera->keyPress(key);
}
}
void mousemoved(int x, int y) {
//camera.mouseMotion(x, y);
}
void emplaceModels() {
char buf_1[256];
char buf_2[256];
snprintf(buf_1, sizeof(buf_1), "%s%s", RES_FOLDER, "SCENE/scene.txt");
printf("would load %s\n", buf_1);
loadInstances( buf_1, columns, false);
snprintf(buf_1, sizeof(buf_1), "%s%s", RES_FOLDER, "vertexShader_pulse.glsl");
snprintf(buf_2, sizeof(buf_2), "%s%s", RES_FOLDER, "fragmentShader_pulse.glsl");
printf("would load %s | %s\n", buf_1, buf_2);
columnsShading = new ShadingHelper(ResourceCache::get().getShader(&buf_1[0], &buf_2[0]));
snprintf(buf_1, sizeof(buf_1), "%s%s", RES_FOLDER, "ico.obj");
printf("would load %s\n", buf_1);
Model3D* cubeModel = new Model3D(buf_1, true);
snprintf(buf_1, sizeof(buf_1), "%s%s", RES_FOLDER, "vertexShader_gourard.glsl");
snprintf(buf_2, sizeof(buf_2), "%s%s", RES_FOLDER, "fragmentShader_gourard.glsl");
cubeShading = new ShadingHelper(ResourceCache::get().getShader(&buf_1[0], &buf_2[0]));
cube = new Instance3D(cubeModel , vec3(10,0,0));
TextureHelper col = TextureHelper();
snprintf(buf_1, sizeof(buf_1), "%s%s", RES_FOLDER, "waito.png");
printf("would load %s\n", buf_1);
col.texture = ResourceCache::get().getImage( buf_1);
col.uniformName = "colorMap";
TextureHelper nor = TextureHelper();
snprintf(buf_1, sizeof(buf_1), "%s%s", RES_FOLDER, "cube3.png");
printf("would load %s\n", buf_1);
nor.texture = ResourceCache::get().getImage( buf_1);
nor.uniformName = "normalMap";
cubeTextures.push_back(col);
cubeTextures.push_back(nor);
}
void curveSetup() {
char buf_1[256];
char buf_2[256];
snprintf(buf_1, sizeof( buf_1), "%s%s", RES_FOLDER, "curve5.txt");
printf("would load %s\n", buf_1);
CurvesLoader::loadCurves( buf_1, curves);
//CurvesLoader::loadCurves("C:\\Users\\BoBoBoat\\Documents\\curve4cyclic.txt", curves);
for (Curve* curve : curves)
iterators.push_back( new CurveIterator(curve, nCurvePoints, CurveIterationMode::LENGTH));
snprintf(buf_1, sizeof(buf_1), "%s%s", RES_FOLDER, "vertexShader_lines.glsl");
snprintf(buf_2, sizeof(buf_2), "%s%s", RES_FOLDER, "fragmentShader_lines.glsl");
printf("would load %s | %s\n", buf_1, buf_2);
linesShader = ShaderMaker::createProgram( &buf_1[0], &buf_2[0]);
glGenVertexArrays(1, &curveVAO);
glBindVertexArray(curveVAO);
vector<glm::vec3> curvePoints = vector<glm::vec3>();
for (int i = 0; i < nCurvePoints; i++) {
curvePoints.push_back(iterators[0]->evaluation());
iterators[0]->nextStep();
curvePoints.push_back(iterators[0]->evaluation());
}
glGenBuffers(1, &curvePointsVBO);
glBindBuffer(GL_ARRAY_BUFFER, curvePointsVBO);
glBufferData(GL_ARRAY_BUFFER, 2 * nCurvePoints * sizeof(vec3), &curvePoints[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
glGenVertexArrays(1, &controlPointsVAO);
glBindVertexArray(controlPointsVAO);
vector<vec3> controlPoints = vector<vec3>();
vector<vec3> cp = *iterators[0]->getCurve()->getControlPoints();
controlPoints.push_back(cp[0]);
for (int i = 1; i < cp.size() - 1; i++) {
controlPoints.push_back(cp[i]);
controlPoints.push_back(cp[i]);
}
controlPoints.push_back(cp[cp.size() - 1]);
nControlPoints = controlPoints.size();
glGenBuffers(1, &controlPointsVBO);
glBindBuffer(GL_ARRAY_BUFFER, controlPointsVBO);
glBufferData(GL_ARRAY_BUFFER, nControlPoints * sizeof(vec3), &controlPoints[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
int scrWidth = glutGet(GLUT_SCREEN_WIDTH); //* 5 / 6;
int scrHeight = glutGet(GLUT_SCREEN_HEIGHT); //* 5 / 6;
//scrWidth = 640;
//scrHeight = 480;
aspect_ratio = ( (float) scrWidth) / scrHeight;
glutInitWindowSize( scrWidth , scrHeight );
glutInitWindowPosition( 0, 0);
glutCreateWindow("TriDi");
/*
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
std::cout << "Error " << glewGetErrorString(err) << "\n";
*/
//glutSetCursor(GLUT_CURSOR_NONE);
glutTimerFunc(ONE_TICK_MS, timer, 0);
glutDisplayFunc(renderLoop);
glutKeyboardFunc(keypressed);
glutPassiveMotionFunc(mousemoved);
/*get the pointers to the missing functions*/
GLES_3_1_compatibility_init();
printf(
"GL info :\nversion : %s\nvendor: %s\nrenderer : %s\nshading language version: %s\n",
glGetString(GL_VERSION),
glGetString(GL_VENDOR),
glGetString(GL_RENDERER),
glGetString(GL_SHADING_LANGUAGE_VERSION)
);
projection = glm::perspective( glm::radians(30.0f), aspect_ratio, 0.1f, 1000.0f);
camera = new CameraController( new Camera(vec3( 0, 0, 2.0f),vec3(0,0,1.0f), vec3(0,1.0f,0)));
camera->setWindowData( 0, scrWidth, 0, scrHeight);
camera->getCamera()->setPosition(vec3(0, 0, 2));
curveSetup();
emplaceModels();
/*
printf("begin effects setup\n");
sceneBuffer = new Scene( 0, 0, scrWidth, scrHeight);
reel = new ReelManager(4 * TARGET_FPS, scrWidth, scrHeight);
merger = new TexMerger(2 * TARGET_FPS, scrWidth, scrHeight);
canvas = new Screen(0, 0, scrWidth, scrHeight);
printf("end effects setup\n");
*/
/* DISABLED
CRT_Shader::setup(scrWidth, scrHeight, TRUE, SIM_PRECISION::ROUGH);
CRT_Shader::get();
*/
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
glutMainLoop();
}