Semi-functional water caustics

This commit is contained in:
Mateusz Drewniak 2022-02-01 02:14:48 +01:00
parent ac8330324c
commit e49f4ae225
4 changed files with 6868 additions and 6872 deletions

View File

@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Physical
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
#include "Texture.h"
#include <fstream>
#include <fstream>
#include <iterator>
#include <vector>
#include "SOIL/SOIL.h"
@ -14,12 +14,12 @@ GLuint Core::LoadTexture( const char * filepath )
glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
int w, h;
unsigned char* image = SOIL_load_image(filepath, &w, &h, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);

View File

@ -37,7 +37,7 @@ int bubblePosition[100][3];
float firstFishSpreadFactor = 15.0;
GLuint caustics[16];
float causticScale = 1.5;
float causticScale = 2;
int causticIndex = 0;
float causticCounter = 0;
@ -199,58 +199,32 @@ void secondRenderingPass() {
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR, GL_ONE);
/* The 0.03 in the Y column is just to shift the texture coordinates
a little based on Y (depth in the water) so that vertical faces
(like on the cube) do not get totally vertical caustics. */
GLfloat sPlane[4] = { 0.05, 0.03, 0.0, 0.0 };
GLfloat tPlane[4] = { 0.0, 0.03, 0.05, 0.0 };
/* The causticScale determines how large the caustic "ripples" will
be. See the "Increate/Decrease ripple size" menu options. */
sPlane[0] = 0.05 * causticScale;
sPlane[1] = 0.03 * causticScale;
tPlane[1] = 0.03 * causticScale;
tPlane[2] = 0.05 * causticScale;
/* Set current color to "white" and disable lighting
to emulate OpenGL 1.1's GL_REPLACE texture environment. */
glColor3f(1.0, 1.0, 1.0);
glDisable(GL_LIGHTING);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, sPlane);
glTexGenfv(GL_T, GL_OBJECT_PLANE, tPlane);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glColor3f(.8, .6, .6);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, caustics[causticIndex]);
// glColor3f(1, 1, 1);
// glTranslatef(5, .6, -2);
glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.5f) * glm::mat4_cast(glm::inverse(cameraRotation)) * shipInitialTransformation;
drawObjectColor(shipContext, shipModelMatrix, glm::vec3(0.6f));
drawObjectTexture(seafloorContext, glm::translate(glm::vec3(0, -15, 0)) * glm::scale(glm::vec3(5.0f, 1.0f, 5.0f)), textureSand);
drawObjectTexture(skyboxContext, glm::translate(cameraPos) * glm::translate(glm::vec3(0, -15, 0)) * glm::scale(glm::vec3(1.0f, 1.0f, 1.0f)), textureSkybox);
drawObjectTexture(shipContext, shipModelMatrix, caustics[causticIndex]);
drawObjectTexture(seafloorContext, glm::translate(glm::vec3(0,-15,0)) * glm::scale(glm::vec3(10.f)), caustics[causticIndex]);
// Animals
drawObjectTexture(seaTurtleContext, glm::translate(glm::vec3(0, cos(time) - 3, 0)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.015f)), textureSeaTurtle);
drawObjectTexture(crabContext, glm::translate(glm::vec3(cos(time / 7) * 10, -13, 0)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.2f)), textureCrab);
for (int i = 0; i < 15; i++) drawObjectTexture(firstFishContext, glm::translate(glm::vec3(firstFishPositions[i][0], firstFishPositions[i][1], firstFishPositions[i][2])) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(-20, 0, 0)) * glm::rotate(glm::radians(cos(8 * time) * 8), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.6f)), textureFirstFish);
for (int i = 0; i < 15; i++) drawObjectTexture(secondFishContext, glm::translate(glm::vec3(secondFishPositions[i][0], secondFishPositions[i][1], secondFishPositions[i][2])) * glm::eulerAngleY(float(-time / 1.75)) * glm::translate(glm::vec3(20, 0, 0)) * glm::rotate(glm::radians(cos(10 * time) * 12), glm::vec3(0, 1, 0)) * glm::rotate(glm::radians(90.f), glm::vec3(0, 1, 0)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.1f)), textureSecondFish);
for (int i = 0; i < 15; i++) drawObjectTexture(thirdFishContext, glm::translate(glm::vec3(thirdFishPositions[i][0], thirdFishPositions[i][1], thirdFishPositions[i][2])) * glm::eulerAngleY(float(time / 2.5)) * glm::translate(glm::vec3(-20, 0, 0)) * glm::rotate(glm::radians(cos(8 * time) * 8), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.5f)), textureThirdFish);
drawObjectTexture(seaTurtleContext, glm::translate(glm::vec3(0, cos(time) - 3, 0)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.015f)), caustics[causticIndex]);
drawObjectTexture(crabContext, glm::translate(glm::vec3(cos(time / 7) * 10, -13, 0)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.2f)), caustics[causticIndex]);
for (int i = 0; i < 15; i++) drawObjectTexture(firstFishContext, glm::translate(glm::vec3(firstFishPositions[i][0], firstFishPositions[i][1], firstFishPositions[i][2])) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(-20, 0, 0)) * glm::rotate(glm::radians(cos(8 * time) * 8), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.6f)), caustics[causticIndex]);
for (int i = 0; i < 15; i++) drawObjectTexture(secondFishContext, glm::translate(glm::vec3(secondFishPositions[i][0], secondFishPositions[i][1], secondFishPositions[i][2])) * glm::eulerAngleY(float(-time / 1.75)) * glm::translate(glm::vec3(20, 0, 0)) * glm::rotate(glm::radians(cos(10 * time) * 12), glm::vec3(0, 1, 0)) * glm::rotate(glm::radians(90.f), glm::vec3(0, 1, 0)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.1f)), caustics[causticIndex]);
for (int i = 0; i < 15; i++) drawObjectTexture(thirdFishContext, glm::translate(glm::vec3(thirdFishPositions[i][0], thirdFishPositions[i][1], thirdFishPositions[i][2])) * glm::eulerAngleY(float(time / 2.5)) * glm::translate(glm::vec3(-20, 0, 0)) * glm::rotate(glm::radians(cos(8 * time) * 8), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.5f)), caustics[causticIndex]);
// Plants
drawObjectColor(coralTreeContext, glm::translate(glm::vec3(0, -14, 2)) * glm::scale(glm::vec3(0.5f)), glm::vec3(0.5802f, 0.1406f, 0.74f));
drawObjectColor(coralContext, glm::translate(glm::vec3(0, -14, 8)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.1f)), glm::vec3(0.85f, 0.0255f, 0.6027f));
drawObjectColor(gorgonianCoralContext, glm::translate(glm::vec3(0, -14, 16)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.07f)), glm::vec3(0.f, 1.f, 0.f));
drawObjectTexture(coralTreeContext, glm::translate(glm::vec3(0, -14, 2)) * glm::scale(glm::vec3(0.5f)), caustics[causticIndex]);
drawObjectTexture(coralContext, glm::translate(glm::vec3(0, -14, 8)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.1f)), caustics[causticIndex]);
drawObjectTexture(gorgonianCoralContext, glm::translate(glm::vec3(0, -14, 16)) * glm::rotate(glm::radians(270.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.07f)), caustics[causticIndex]);
drawObjectTexture(shipwreckContext, glm::translate(glm::vec3(20, -15, 0)) * glm::rotate(glm::radians(255.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.003f)), caustics[causticIndex]);
drawObjectTexture(shipwreckContext, glm::translate(glm::vec3(20, -15, 0)) * glm::rotate(glm::radians(255.f), glm::vec3(1, 0, 0)) * glm::scale(glm::vec3(0.003f)), textureBoards);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glDisable(GL_BLEND);
}
void firstRenderingPass() {
@ -289,7 +263,7 @@ void renderScene()
glClearColor(skyColor.x, skyColor.y, skyColor.z, 1.0f);
firstRenderingPass();
// secondRenderingPass();
secondRenderingPass();
// Render the scene
glFlush();
@ -389,13 +363,10 @@ void shutdown()
void idle()
{
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
causticCounter += time;
if (causticCounter >= .05) {
//Update caustic texture
causticIndex = (causticIndex + 1)%16;
causticCounter = 0;
}
float time = glutGet(GLUT_ELAPSED_TIME) / 100.f;
// Update caustic texture
causticIndex = (int)time % 16;
causticCounter = 0;
glutPostRedisplay();
}