Implement alpha blending

This commit is contained in:
Mateusz Drewniak 2022-01-23 17:36:19 +01:00
parent 770cdb4b47
commit c223db50c6
4 changed files with 21 additions and 3 deletions

View File

@ -11,9 +11,11 @@ in float visibility;
void main()
{
vec2 modifiedTexCoord = vec2(interpTexCoord.x, 1.0 - interpTexCoord.y); // Poprawka dla tekstur Ziemi, ktore bez tego wyswietlaja sie 'do gory nogami'
vec3 color = texture2D(textureSampler, modifiedTexCoord).rgb;
vec4 rgba = texture2D(textureSampler, modifiedTexCoord).rgba;
if(rgba.a < 0.1) discard; // don't render the pixel if it's transparent
vec3 normal = normalize(interpNormal);
float diffuse = max(dot(normal, -lightDir), 0.0);
gl_FragColor = vec4(color * diffuse, 1.0);
gl_FragColor = rgba * diffuse;
gl_FragColor = mix(vec4(skyColor, 1.0), gl_FragColor, visibility);
}

View File

@ -56,6 +56,7 @@ GLuint textureSecondFish;
GLuint textureThirdFish;
GLuint textureSeaTurtle;
GLuint textureCrab;
GLuint bubbleTexture;
void keyboard(unsigned char key, int x, int y)
{
@ -143,6 +144,15 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin
glUseProgram(0);
}
void drawTranslucentObjects() {
glEnable(GL_BLEND); // enable blending
drawObjectTexture(sphereContext, glm::translate(glm::vec3(0, 5, 0)) * glm::scale(glm::vec3(5.0f, 5.0f, 5.0f)), bubbleTexture);
glDisable(GL_BLEND); // disable blending
}
void renderScene()
{
// Aktualizacja macierzy widoku i rzutowania
@ -150,7 +160,7 @@ void renderScene()
perspectiveMatrix = Core::createPerspectiveMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.1f, 0.3f, 1.0f);
glClearColor(skyColor.x, skyColor.y, skyColor.z, 1.0f);
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
@ -174,6 +184,8 @@ void renderScene()
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));
drawTranslucentObjects();
glutSwapBuffers();
}
@ -194,8 +206,11 @@ void init()
{
srand(time(0));
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
programColor = shaderLoader.CreateProgram("shaders/shader_color.vert", "shaders/shader_color.frag");
programTexture = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
loadModelToContext("models/sphere.obj", sphereContext);
loadModelToContext("models/ship.obj", shipContext);
loadModelToContext("models/seafloor.obj", seafloorContext);
loadModelToContext("models/skybox.obj", skyboxContext);
@ -207,6 +222,7 @@ void init()
loadModelToContext("models/coral_tree.obj", coralTreeContext);
loadModelToContext("models/coral.obj", coralContext);
loadModelToContext("models/gorgonian_coral.obj", gorgonianCoralContext);
bubbleTexture = Core::LoadTexture("textures/bubble.png");
textureSkybox = Core::LoadTexture("textures/skybox.png");
textureFirstFish = Core::LoadTexture("textures/first_fish.png");
textureSecondFish = Core::LoadTexture("textures/second_fish.png");

BIN
cw 6/textures/bubble.pdn Normal file

Binary file not shown.

BIN
cw 6/textures/bubble.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 KiB