Merge pull request 'bubbles' (#1) from bubbles into master

Reviewed-on: #1
This commit is contained in:
Bartosz Małaszewski 2022-01-19 18:02:29 +01:00
commit 59a4681cbe
8 changed files with 120 additions and 9 deletions

View File

View File

@ -11,6 +11,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Bubble.cpp" />
<ClCompile Include="src\Camera.cpp" />
<ClCompile Include="src\main_6_1.cpp" />
<ClCompile Include="src\Render_Utils.cpp" />
@ -22,6 +23,7 @@
<ClCompile Include="src\Texture.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Bubble.h" />
<ClInclude Include="src\Camera.h" />
<ClInclude Include="src\objload.h" />
<ClInclude Include="src\Render_Utils.h" />

View File

@ -48,6 +48,9 @@
<ClCompile Include="src\main_6_1.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Bubble.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\objload.h">
@ -83,6 +86,9 @@
<ClInclude Include="src\Texture.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\Bubble.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="shaders\shader_color.frag">

View File

@ -1,6 +1,6 @@
#version 410 core
uniform vec3 objectColor;
uniform vec4 objectColor;
uniform vec3 lightPos;
uniform vec3 cameraPos;
@ -16,5 +16,5 @@ void main()
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,normalize(lightDir)));
gl_FragColor = vec4(mix(objectColor,objectColor*diffuse+vec3(1)*specular,0.9), 1.0);
gl_FragColor = vec4(mix(objectColor.rgb,objectColor.rgb*diffuse+vec3(1)*specular,0.9), objectColor.a);
}

40
cw 6/src/Bubble.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "Bubble.h"
Bubble::Bubble(){}
Bubble::Bubble(float newRadius, float newX, float newZ) {
x = newX;
z = newZ;
y = 0.0f;
maxY = 10.0f;
elevationSpeed = 0.0005f;
radius = newRadius;
}
float Bubble::getAndElevateY() {
y += elevationSpeed * (1/radius);
if (y > maxY) {
y = 0.0f;
}
return y;
}
float Bubble::getX() {
return x;
}
float Bubble::getY() {
return y;
}
float Bubble::getZ() {
return z;
}
glm::vec3 Bubble::getPosition() {
return glm::vec3(getX(), getY(), getZ());
}
glm::mat4 Bubble::getModelMatrix() {
return glm::translate(glm::vec3(getX(), getAndElevateY(), getZ())) * glm::scale(glm::vec3(radius));
}

27
cw 6/src/Bubble.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include "glm.hpp"
#include "ext.hpp"
class Bubble {
private:
float x;
float y;
float z;
float radius;
float maxY;
float elevationSpeed;
float getAndElevateY();
public:
Bubble();
Bubble(float newRadius, float newX, float newZ);
float getX();
float getY();
float getZ();
glm::vec3 getPosition();
glm::mat4 getModelMatrix();
};

View File

@ -12,6 +12,5 @@ namespace Core
// up - wektor "w gore" kamery (jednostkowy)
// up i forward musza byc ortogonalne!
glm::mat4 createViewMatrix(glm::vec3 position, glm::vec3 forward, glm::vec3 up);
glm::mat4 createViewMatrixQuat(glm::vec3 position, glm::quat rotation);
}

View File

@ -12,6 +12,7 @@
#include "Render_Utils.h"
#include "Camera.h"
#include "Texture.h"
#include "Bubble.h"
GLuint programColor;
GLuint programTexture;
@ -33,6 +34,8 @@ glm::vec3 lightDir = glm::vec3(0.0f, 100.0f, 0.0f);
glm::quat rotation = glm::quat(1, 0, 0, 0);
std::vector<glm::vec3> planetsCoords;
std::vector<Bubble> bubbles;
float mouseX = 0;
float mouseY = 0;
float prevX = 0;
@ -76,13 +79,13 @@ glm::mat4 createCameraMatrix()
return Core::createViewMatrixQuat(cameraPos,rotation);
}
void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::vec4 color)
{
GLuint program = programColor;
glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z);
glUniform4f(glGetUniformLocation(program, "objectColor"), color.r, color.g, color.b, color.a);
glUniform3f(glGetUniformLocation(program, "lightPos"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
@ -113,6 +116,16 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin
glUseProgram(0);
}
void drawBubbles()
{
for (Bubble& bubble : bubbles) {
drawObjectColor(sphereContext, bubble.getModelMatrix(), glm::vec4(0.0f, 0.0f, 1.0f, 0.3f));
}
}
void renderScene()
{
// Aktualizacja macierzy widoku i rzutowania
@ -120,18 +133,22 @@ void renderScene()
perspectiveMatrix = Core::createPerspectiveMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.1f, 0.3f, 1.0f);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 shipInitialTransformation = glm::translate(glm::vec3(0,-1.0f,-1.0f)) * glm::rotate(glm::radians(180.0f), glm::vec3(0,1,0)) * glm::scale(glm::vec3(0.25f));
glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.5f) * glm::mat4_cast(glm::inverse(rotation)) * shipInitialTransformation;
drawObjectTexture(sharkModel, shipModelMatrix, sharkTexture);
for (auto& coords : planetsCoords) {
drawObjectColor(sphereContext, glm::translate(coords), coords);
}
drawObjectTexture(sharkModel, glm::mat4() * glm::scale(glm::vec3(0.25f)), sharkTexture);
drawBubbles();
//drawObjectColor(sphereContext, glm::translate(glm::vec3(1.0f, 0.0f, 0.0f)), glm::vec4(0.0f, 0.0f, 1.0f, 0.3f));
//for (auto& coords : planetsCoords) {
// drawObjectColor(sphereContext, glm::translate(coords), coords);
//}
glutSwapBuffers();
}
@ -148,15 +165,35 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
context.initFromAssimpMesh(scene->mMeshes[0]);
}
void initBubbles() {
bubbles.insert(bubbles.end(), {
Bubble(0.7f, 1.0f, 1.0f),
Bubble(0.5f, 3.0f, 2.3f),
Bubble(0.2f, 5.7f, 1.2f),
Bubble(0.5f, 7.0f, 4.0f),
Bubble(0.4f, 4.7f, 3.7f),
Bubble(0.1f, 1.0f, 2.1f),
Bubble(0.3f, 2.6f, 8.4f),
Bubble(0.4f, 1.3f, 0.3f),
Bubble(0.6f, 5.2f, 2.1f),
Bubble(0.5f, 4.0f, 1.2f)
}
);
}
void init()
{
srand(time(0));
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
programColor = shaderLoader.CreateProgram("shaders/shader_color.vert", "shaders/shader_color.frag");
programTexture = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
loadModelToContext("models/orca.obj", sharkModel);
loadModelToContext("models/sphere.obj", sphereContext);
sharkTexture = Core::LoadTexture("textures/Orca_Diffuse.jpg");
initBubbles();
for (int i = 0; i < 10; i++) {
float r = (float)(rand()) / (float)(RAND_MAX/20.0);
planetsCoords.push_back(glm::ballRand(r));