Skip to content

Commit 2662652

Browse files
author
Ilya Makarov
committed
Added task 3
1 parent 86c742c commit 2662652

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3005
-0
lines changed

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ add_subdirectory(sayfudinova)
1818
add_subdirectory(Antonova)
1919
add_subdirectory(Biryuchkov)
2020
add_subdirectory(Bondarchuk)
21+
add_subdirectory(kolodkin)

src/kolodkin/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(task3)

src/kolodkin/task3/CMakeLists.txt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
set(SRC_LIST
2+
cameraview.cpp
3+
cameraview.hpp
4+
dialog.cpp
5+
dialog.hpp
6+
dialog.ui
7+
direcltylight.cpp
8+
direcltylight.hpp
9+
GuroSceneObject.frag
10+
GuroSceneObject.vert
11+
keyboard.cpp
12+
keyboard.hpp
13+
lightsource.hpp
14+
LightSource.vert
15+
main.cpp
16+
mainwindow.cpp
17+
mainwindow.hpp
18+
mainwindow.ui
19+
material.hpp
20+
materialfactory.hpp
21+
mesh.hpp
22+
meshfactory.cpp
23+
meshfactory.hpp
24+
meshobject.cpp
25+
meshobject.hpp
26+
object.hpp
27+
objectsgrid.cpp
28+
objectsgrid.hpp
29+
pointlightsource.cpp
30+
pointlightsource.hpp
31+
renderobject.hpp
32+
scene.cpp
33+
scene.hpp
34+
sceneobject.cpp
35+
sceneobject.hpp
36+
scenewidget.cpp
37+
scenewidget.hpp
38+
shaders.qrc
39+
spotlightsource.cpp
40+
spotlightsource.hpp
41+
vertex.hpp)
42+
43+
add_executable(kolodkin-task3 ${SRC_LIST})
44+
45+
find_package(Qt5 COMPONENTS Widgets REQUIRED)
46+
47+
target_link_libraries(kolodkin-task3 PRIVATE Qt5::Widgets)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 400
2+
in lowp vec4 vertexColor;
3+
4+
out vec4 outColor;
5+
6+
void main()
7+
{
8+
outColor = vertexColor;
9+
}
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#version 400
2+
uniform highp mat4 model;
3+
uniform highp mat4 normModel;
4+
uniform highp mat4 projView;
5+
uniform highp vec3 cameraPos;
6+
7+
struct Material
8+
{
9+
vec3 ambient;
10+
vec3 diffuse;
11+
vec3 specular;
12+
float shininess;
13+
};
14+
15+
struct DirLightSource
16+
{
17+
lowp vec3 color;
18+
float intensity;
19+
highp vec3 direction;
20+
};
21+
22+
struct PointLightSource
23+
{
24+
lowp vec3 color;
25+
highp vec3 position;
26+
27+
float intensity;
28+
float constFactor;
29+
float linFactor;
30+
float quadFactor;
31+
};
32+
33+
struct SpotLightSource
34+
{
35+
lowp vec3 color;
36+
highp vec3 position;
37+
highp vec3 direction;
38+
39+
float cutOff;
40+
float outerCutOff;
41+
42+
float intensity;
43+
float constFactor;
44+
float linFactor;
45+
float quadFactor;
46+
};
47+
48+
uniform DirLightSource dirLights[10];
49+
uniform int dirLightsCount;
50+
51+
uniform PointLightSource pointLights[10];
52+
uniform int pointLightsCount;
53+
54+
uniform SpotLightSource spotLights[10];
55+
uniform int spotLightsCount;
56+
57+
uniform Material material;
58+
59+
vec3 CalcDirLight(DirLightSource light, vec3 normal, vec3 toEye)
60+
{
61+
vec3 lightDir = normalize(-light.direction);
62+
// diffuse shading
63+
float diff = max(dot(normal, lightDir), 0.0);
64+
// specular shading
65+
vec3 reflectDir = reflect(-lightDir, normal);
66+
float spec = pow(max(dot(toEye, reflectDir), 0.0), material.shininess) * sign(diff);
67+
// combine results
68+
vec3 ambient = material.ambient;
69+
vec3 diffuse = diff * material.diffuse;
70+
vec3 specular = spec * material.specular;
71+
return light.intensity * light.color * (ambient + diffuse + specular);
72+
}
73+
74+
vec3 CalcPointLight(PointLightSource light, vec3 normal, vec3 vertexPos, vec3 toEye)
75+
{
76+
vec3 lightDir = normalize(light.position - vertexPos);
77+
// diffuse shading
78+
float diff = max(dot(normal, lightDir), 0.0);
79+
// specular shading
80+
vec3 reflectDir = reflect(-lightDir, normal);
81+
float spec = pow(max(dot(toEye, reflectDir), 0.0), 256) * sign(diff);
82+
// attenuation
83+
float distance = length(light.position - vertexPos);
84+
float attenuation = 1.0 / (light.constFactor + light.linFactor * distance +
85+
light.quadFactor * distance * distance);
86+
// combine results
87+
vec3 diffuse = diff * material.diffuse;
88+
vec3 specular = spec * material.specular;
89+
90+
return light.intensity * attenuation * (diffuse + specular) * light.color;
91+
}
92+
93+
vec3 CalcSpotLight(SpotLightSource light, vec3 normal, vec3 vertexPos, vec3 toEye)
94+
{
95+
vec3 lightDir = normalize(light.position - vertexPos);
96+
// diffuse shading
97+
float diff = max(dot(normal, lightDir), 0.0);
98+
// specular shading
99+
vec3 reflectDir = reflect(-lightDir, normal);
100+
float spec = pow(max(dot(toEye, reflectDir), 0.0), material.shininess) * sign(diff);
101+
// attenuation
102+
float distance = length(light.position - vertexPos);
103+
float attenuation = 1.0 / (light.constFactor + light.linFactor * distance +
104+
light.quadFactor * distance * distance);
105+
// spotlight intensity
106+
float theta = acos(dot(lightDir, normalize(-light.direction)));
107+
float epsilon = light.cutOff - light.outerCutOff;
108+
float intensity = light.intensity * clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
109+
// combine results
110+
vec3 diffuse = diff * material.diffuse;
111+
vec3 specular = spec * material.specular;
112+
113+
return attenuation * intensity * (diffuse + specular) * light.color;
114+
}
115+
116+
layout (location = 0) in highp vec3 position;
117+
layout (location = 1) in highp vec3 normal;
118+
119+
out lowp vec4 vertexColor;
120+
121+
void main()
122+
{
123+
vec3 norm = normalize(mat3(normModel) * normal);
124+
vec4 worldVertexPos = model * vec4(position, 1.f);
125+
vec3 toEye = normalize(cameraPos - worldVertexPos.xyz);
126+
vec3 resultCol = vec3(0.0);
127+
128+
for (int i = 0; i < min(dirLightsCount, 10); ++i)
129+
{
130+
resultCol += CalcDirLight(dirLights[i], norm, toEye);
131+
}
132+
for (int i = 0; i < min(pointLightsCount, 10); ++i)
133+
{
134+
resultCol += CalcPointLight(pointLights[i], norm, worldVertexPos.xyz, toEye);
135+
}
136+
for (int i = 0; i < min(spotLightsCount, 10); ++i)
137+
{
138+
resultCol += CalcSpotLight(spotLights[i], norm, worldVertexPos.xyz, toEye);
139+
}
140+
141+
vertexColor = vec4(resultCol, 1.0);
142+
gl_Position = projView * worldVertexPos;
143+
}

src/kolodkin/task3/LightSource.vert

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#version 400
2+
uniform highp mat4 model;
3+
uniform highp mat4 normModel;
4+
uniform highp mat4 projView;
5+
6+
uniform lowp vec3 lightColor;
7+
8+
in highp vec3 position;
9+
in highp vec3 normal;
10+
11+
out lowp vec4 vertexColor;
12+
13+
void main()
14+
{
15+
vertexColor = vec4(lightColor, 1.0);
16+
gl_Position = projView * model * vec4(position, 1.f);
17+
}

src/kolodkin/task3/cameraview.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "cameraview.hpp"
2+
#include <algorithm>
3+
#include <cmath>
4+
5+
void CameraView::updateVectors() {
6+
front_.setX(std::cos(yaw_) * std::cos(pitch_));
7+
front_.setY(-std::sin(pitch_));
8+
front_.setZ(std::sin(yaw_) * std::cos(pitch_));
9+
front_.normalize();
10+
right_ = QVector3D::crossProduct(front_, worldUp_).normalized();
11+
up_ = QVector3D::crossProduct(right_, front_).normalized();
12+
}
13+
14+
CameraView::CameraView(const QVector3D &eye_pos, const QVector3D &front,
15+
const QVector3D &up)
16+
: eyePos_(eye_pos), front_(front), worldUp_(up) {
17+
yaw_ = std::atan2(front_.z(), front_.x());
18+
pitch_ = -std::asin(front_.y());
19+
updateVectors();
20+
}
21+
22+
QMatrix4x4 const &CameraView::getViewMatrix() {
23+
view_.setToIdentity();
24+
view_.lookAt(eyePos_, eyePos_ + front_, up_);
25+
return view_;
26+
}
27+
28+
const QMatrix4x4 &CameraView::getProjectionMatrix() {
29+
projection_.setToIdentity();
30+
projection_.perspective(fov_, aspectRatio_, zNear_, zFar_);
31+
return projection_;
32+
}
33+
34+
const QMatrix4x4 &CameraView::getProjViewMatrix() {
35+
projView_ = getProjectionMatrix() * getViewMatrix();
36+
return projView_;
37+
}
38+
39+
const QVector3D &CameraView::cameraPosition() const { return eyePos_; }
40+
41+
const QVector3D &CameraView::front() const { return front_; }
42+
43+
const QVector3D &CameraView::up() const { return up_; }
44+
45+
const QVector3D &CameraView::right() const { return right_; }
46+
47+
void CameraView::setProjection(float fov, float aspectRatio, float zNear,
48+
float zFar) {
49+
setFOV(fov);
50+
setAspectRatio(aspectRatio);
51+
setZNear(zNear);
52+
setZFar(zFar);
53+
}
54+
55+
void CameraView::setFOV(float fov) { fov_ = fov; }
56+
57+
void CameraView::setAspectRatio(float ratio) { aspectRatio_ = ratio; }
58+
59+
void CameraView::setZNear(float zNear) { zNear_ = zNear; }
60+
61+
void CameraView::setZFar(float zFar) { zFar_ = zFar; }
62+
63+
void CameraView::offsetMove(const QVector3D &offset) {
64+
eyePos_ += front_ * offset.z();
65+
eyePos_ += up_ * offset.y();
66+
eyePos_ += right_ * offset.x();
67+
}
68+
69+
void CameraView::moveTo(const QVector3D &position) { eyePos_ = position; }
70+
71+
void CameraView::rotate(float yaw, float pitch) {
72+
yaw_ += yaw;
73+
pitch_ += pitch;
74+
75+
if constexpr (constrainPitch) {
76+
pitch_ = std::clamp(pitch_, -static_cast<float>(M_PI) / 2.2f,
77+
static_cast<float>(M_PI) / 2.2f);
78+
}
79+
updateVectors();
80+
}

src/kolodkin/task3/cameraview.hpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
3+
#include "object.hpp"
4+
5+
#include <QMatrix4x4>
6+
#include <QVector3D>
7+
#include <cmath>
8+
9+
class CameraView final : public Object {
10+
QMatrix4x4 view_{};
11+
QMatrix4x4 projection_{};
12+
QMatrix4x4 projView_{};
13+
14+
QVector3D eyePos_;
15+
QVector3D up_;
16+
QVector3D front_;
17+
QVector3D right_;
18+
QVector3D worldUp_;
19+
20+
float aspectRatio_;
21+
float zNear_;
22+
float zFar_;
23+
float fov_;
24+
25+
float yaw_ = -M_PI / 2;
26+
float pitch_ = 0.f;
27+
28+
void updateVectors();
29+
30+
public:
31+
inline static constexpr bool constrainPitch = true;
32+
33+
CameraView(QVector3D const &eye_pos = {},
34+
QVector3D const &front = {0.f, 0.f, -1.f},
35+
QVector3D const &up = {0.f, 1.f, 0.f});
36+
QMatrix4x4 const &getViewMatrix();
37+
QMatrix4x4 const &getProjectionMatrix();
38+
QMatrix4x4 const &getProjViewMatrix();
39+
const QVector3D &cameraPosition() const;
40+
const QVector3D &front() const;
41+
const QVector3D &up() const;
42+
const QVector3D &right() const;
43+
44+
void setProjection(float fov, float aspectRatio, float zNear, float zFar);
45+
void setFOV(float fov);
46+
void setAspectRatio(float ratio);
47+
void setZNear(float zNear);
48+
void setZFar(float zFar);
49+
50+
void offsetMove(QVector3D const &offset) override;
51+
void moveTo(QVector3D const &position) override;
52+
53+
void rotate(float yaw, float pitch);
54+
};

0 commit comments

Comments
 (0)