Skip to content

Commit

Permalink
[ADD]new scene]
Browse files Browse the repository at this point in the history
  • Loading branch information
SighingSnow committed Jul 6, 2022
1 parent bbf91ab commit 83c2782
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ enum Camera_Movement {
// Default camera values
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 15.0f;
const float SENSITIVITY = 0.005f;
const float SPEED = 7.0f;
const float SENSITIVITY = 0.0005f;
const float ZOOM = 45.0f;


Expand All @@ -42,7 +42,7 @@ class Camera
float Zoom;

// constructor with vectors
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 50.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f,3.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Expand Down
30 changes: 27 additions & 3 deletions src/Shaders/Cube.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
#version 330 core
out vec4 FragColor;

uniform vec4 inputColor;
in vec3 Normal;
in vec3 FragPos;

uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;
uniform vec3 objectColor;

void main()
{
FragColor = inputColor; // set alle 4 vector values to 1.0
}
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;

// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;

// specular
float specularStrength = 0.5;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;

vec3 result = (ambient + diffuse + specular) * objectColor;
FragColor = vec4(result, 1.0);
}
9 changes: 8 additions & 1 deletion src/Shaders/Cube.vs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;

out vec3 FragPos;
out vec3 Normal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;

gl_Position = projection * view * vec4(FragPos, 1.0);
}
43 changes: 34 additions & 9 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Scene::Scene()
VAO = 0;
VBO = 0;
shader = nullptr;
camera = new Camera(glm::vec3(0.0f,0.0f,30.0f));
camera = new Camera(glm::vec3(0.0f,0.0f,3.0f));
lightPos = glm::vec3(1.2f, 1.0f, 2.0f);
prev_time = 0.0;
frame_count = 0;
Expand Down Expand Up @@ -129,6 +129,20 @@ void Scene::DrawScene()
{
int fps;
int width = 0,height = 0;

glm::vec3 cubePositions[] = {
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3( 2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3( 1.3f, -2.0f, -2.5f),
glm::vec3( 1.5f, 2.0f, -2.5f),
glm::vec3( 1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};

glfwGetWindowSize(window,&width,&height);
time_t now = time(NULL);
while (!glfwWindowShouldClose(window))
Expand All @@ -152,27 +166,38 @@ void Scene::DrawScene()

// render
// ------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
//glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// be sure to activate shader when setting uniforms/drawing objects
shader->use();
// view/projection transformations
// be sure to activate shader when setting uniforms/drawing objects
shader->setVec3("objectColor", 0.3, 0.6, 0.8);
shader->setVec3("lightColor", 1.0f, 1.0f, 1.0f);
shader->setVec3("lightPos", lightPos);
shader->setVec3("viewPos", camera->Position);

// view/projection transformations
glm::mat4 projection = glm::perspective(glm::radians(camera->Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera->GetViewMatrix();
shader->setMat4("projection", projection);
shader->setMat4("view", view);

// world transformation
glm::mat4 model = glm::mat4(1.0f);
model = glm::scale(model,glm::vec3(0.2));
shader->setMat4("model", model);
shader->setVec4("inputColor",glm::vec4(1.0));
// render the cube
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);


for(uint32_t i = 0;i < 10;i++){
glm::mat4 model = glm::mat4(1.0f);
model = glm::scale(model,glm::vec3(0.1f));
model = glm::translate(model, cubePositions[i]);
float angle = 30.0f*i;
model = glm::rotate(model,glm::radians(angle),glm::vec3(1.0f, 0.3f, 0.5f));
shader->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}

textShader->use();
glm::mat4 tproj = glm::ortho(0.0f, static_cast<GLfloat>(SCR_WIDTH), 0.0f, static_cast<GLfloat>(SCR_HEIGHT));
textShader->setMat4("projection",tproj);
Expand Down

0 comments on commit 83c2782

Please sign in to comment.