Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
186 lines (151 loc) · 4.69 KB
/
Copy pathmain.cpp
File metadata and controls
186 lines (151 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include "glframework/core.h"
#include "glframework/shader .h"
#include <string>
#include <assert.h>
#include "wrapper/checkError.h"
#include "application/Application.h"
#include "glframework/texture.h"
//引入相机+控制器
#include "application/camera/perspectiveCamera.h"
#include "application/camera/orthographicCamera.h"
#include "application/camera/trackBallCameraControl.h"
#include "application/camera/gameCameraControl.h"
#include "glframework/geometry.h"
/*
漫反射相关:
1 为Geometry类中的box sphere以及plane加入法线属性
验证normal的是否正确加入:可以将normal的xyz当作rgb输出 其中负的处理一下 让其输出0.0f
2 设置光源的参数,加入到shader中,并且验证
设置光源参数
将光源参数传输到shader中,以uniform变量形式
3 将diffuse(漫反射)加入到fragmentshader当中
高光反射:
1 计算ViewDirection,计算每个像素的世界坐标位置,将相机的
的位置以uniform变量的形式传入fs
在vs里面加入了out变量 worldPosition
在fs里面加入了in变量,接纳了worldPosition
在fs里面加入了cameraPosition这种uniform变量
2 计算反射方向 lightReflect
3 计算lightReflect跟viewDirection之间的夹角cos值
处理光线从面的背面照射的情况
4 通过对specular进行pow计算,控制光斑的大小
5 加入specularIntensity来控制高光反射的强度
6 加入环境光,照亮死黑的地方
法线矩阵:
1 加入NormalMatrix
求逆变换在glsl非常耗费性能,所以在CPU端提前计算好NormalMatrix,作为uniform传输到vs中
*/
//平行光:参数(方向,光强)
glm::vec3 lightDirection = glm::vec3(-1.0f, -1.0f, -1.0f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
//specular
float specularIntensity = 0.5f;
//ambient
glm::vec3 ambientColor = glm::vec3(0.2f, 0.2f, 0.2f);
Geometry* geometry = nullptr;
Shader* shader = nullptr;
Texture* texture = nullptr;
glm::mat4 transform(1.0f);
PerspectiveCamera* camera = nullptr;
//OrthographicCamera* camera = nullptr;
CameraControl* cameraControl = nullptr;
void OnResize(int width,int height) {
GL_CALL(glViewport(0, 0, width, height));
std::cout << "OnResize" << std::endl;
}
void OnKey(int key, int action, int mods) {
cameraControl->onKey(key, action, mods);
}
void OnMouse(int button, int action, int mods) {
double x, y;
app->getCursorPosition(&x, &y);
cameraControl->onMouse(button, action, x, y);
}
void OnCursor(double xpos, double ypos) {
cameraControl->onCursor(xpos, ypos);
}
//鼠标滚轮
void OnScroll(double offset) {
cameraControl->onScroll(offset);
}
void prepareVAO() {
geometry = Geometry::createSphere(3.0f);
}
void prepareShader() {
shader = new Shader("assets/shaders/vertex.glsl", "assets/shaders/fragment.glsl");
}
void prepareTexture() {
texture = new Texture("assets/textures/earth.png", 0);
}
void prepareCamera() {
float size = 6.0f;
//camera = new OrthographicCamera(-size, size, size, -size, size, -size);
camera = new PerspectiveCamera(
60.0f,
(float)app->getWidth() / (float)app->getHeight(),
0.1f,
1000.0f
);
cameraControl = new TrackBallCameraControl();
//cameraControl = new GameCameraControl();
cameraControl->setCamera(camera);
cameraControl->setSensitivity(0.4f);
}
void prepareState() {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void doTransform() {
transform = glm::rotate(transform, 0.005f, glm::vec3(0.0f, 1.0f, 0.0f));
}
void render() {
//执行opengl画布清理操作 这里注意处理清理颜色还要清理深度
GL_CALL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
//绑定当前的program
shader->begin();
shader->setInt("sampler", 0);
shader->setMatrix4x4("modelMatrix", transform);
shader->setMatrix4x4("viewMatrix", camera->getViewMatrix());
shader->setMatrix4x4("projectionMatrix", camera->getProjectionMatrix());
//计算normalMatrix
auto normalMatrix = glm::transpose(glm::inverse(glm::mat3(transform)));
shader->setMatrix3x3("normalMatrix", normalMatrix);
//光源参数的uniform更新
shader->setVector3("lightDirection", lightDirection);
shader->setVector3("lightColor", lightColor);
shader->setFloat("specularIntensity", specularIntensity);
shader->setVector3("ambientColor", ambientColor);
shader->setVector3("cameraPosition", camera->mPosition);
// 绑定当前的vao
glBindVertexArray(geometry->getVao());
//发出绘制指令
GL_CALL(glDrawElements(GL_TRIANGLES, geometry->getIndicesCount(), GL_UNSIGNED_INT, 0));
GL_CALL(glBindVertexArray(0));
shader->end();
}
int main() {
if (!app->init(1600, 1200)) {
return -1;
}
app->setResizeCallback(OnResize);
app->SetKeyBoardCallback(OnKey);
app->setMouseCallback(OnMouse);
app->setCursorCallback(OnCursor);
app->setScrollCallback(OnScroll);
//设置视口以及清理颜色
glViewport(0, 0, 1600, 1200);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
prepareShader();
prepareVAO();
prepareTexture();
prepareCamera();
prepareState();
while (app->update()) {
doTransform();
cameraControl->update();
render();
}
app->destroy();
return 0;
}