Skip to content

Commit

Permalink
use ubo for camera
Browse files Browse the repository at this point in the history
  • Loading branch information
mohlek committed Sep 26, 2021
1 parent 6f8fc30 commit dab1bcc
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion engine
2 changes: 1 addition & 1 deletion include/shader/displayNormal.gs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ R"(

const float MAGNITUDE = 3.0;

layout(std140) uniform camera {
layout(std140, binding = 0) uniform camera {
mat4 view;
mat4 projection;
};
Expand Down
2 changes: 1 addition & 1 deletion include/shader/displayNormal.vs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ R"(
} vs_out;

uniform mat4 model;
layout(std140) uniform camera {
layout(std140, binding = 0) uniform camera {
mat4 view;
mat4 projection;
};
Expand Down
4 changes: 2 additions & 2 deletions include/shader/main.vs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ R"(
layout(location = 4) in vec3 in_bitangent;

uniform mat4 model;
layout(std140) uniform camera {
layout(std140, binding = 0) uniform camera {
mat4 view;
mat4 projection;
};
Expand Down Expand Up @@ -37,7 +37,7 @@ R"(
mat3 TBN = transpose(mat3(T, B, N));
vs_out.TBN = TBN;

vec3 lightDir = normalize(vec3(sin(time * 0.001), 1.0, cos(time * 0.001)));
vec3 lightDir = normalize(vec3(sin(float(time) * 0.001), 1.0, cos(float(time) * 0.001)));
vs_out.tangentLightDir = TBN * lightDir;
vs_out.tangentViewPos = TBN * transpose(view)[3].xyz;
vs_out.tangentFragPos = TBN * vs_out.fragPos;
Expand Down
12 changes: 3 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ int main(int argc, char** argv) {
Engine::Camera camera;

struct CameraUBO {
glm::mat4 projection;
glm::mat4 view;
glm::mat4 projection;
} cameraUbo;

fprintf(stdout, "%d\n", sizeof(CameraUBO));
Expand Down Expand Up @@ -223,6 +223,7 @@ int main(int argc, char** argv) {
Engine::VertexArrayObject vao;
vao.setBuffer(0, vbo);
vao.setBuffer(0, elements);
vao.setBuffer(0, cameraBuffer);
vao.setBuffer(1, norm);
vao.setBuffer(2, uvs);
vao.setBuffer(3, tangent);
Expand Down Expand Up @@ -271,10 +272,8 @@ int main(int argc, char** argv) {
GLint uniformModelHouse = program.getUniformLocation("model");
GLint uniformProjHouse = program.getUniformLocation("projection");
GLint uniformTime = program.getUniformLocation("time");
GLint uniformCamera = program.getUniformBlockIndex("camera");

glUniformMatrix4fv(uniformModelHouse, 1, GL_FALSE, glm::value_ptr(modelHouse));
glUniformBlockBinding(program.getProgramId(), uniformCamera, 0);

Engine::ShaderProgram displayNormalShader;
Engine::Shader displayNormalVertex(GL_VERTEX_SHADER, Shader::normalDisplayVert);
Expand All @@ -287,9 +286,7 @@ int main(int argc, char** argv) {
displayNormalShader.use();

GLint uniformModelDisplayNormal = displayNormalShader.getUniformLocation("model");
GLint uniformCameraDisplayNormal = displayNormalShader.getUniformBlockIndex("camera");
glUniformMatrix4fv(uniformModelDisplayNormal, 1, GL_FALSE, glm::value_ptr(modelHouse));
glUniformBlockBinding(displayNormalShader.getProgramId(), uniformCameraDisplayNormal, 0);

Engine::ShaderProgram programTerrain;
Engine::Shader vertexShaderTerrain(GL_VERTEX_SHADER, Shader::terrainVertex);
Expand All @@ -307,11 +304,8 @@ int main(int argc, char** argv) {
camera.position.y += 200;

GLint uniformModelTerrain = programTerrain.getUniformLocation("model");
GLint uniformCameraTerrain = programTerrain.getUniformBlockIndex("camera");
glBindBufferBase(GL_UNIFORM_BUFFER, 0, cameraBuffer._bufferId);

glUniformMatrix4fv(uniformModelTerrain, 1, GL_FALSE, glm::value_ptr(modelTerrain));
glUniformBlockBinding(programTerrain.getProgramId(), uniformCameraTerrain, 0);

double factor = 0.14;
Engine::Terrain terrain(500, 500, projection, programTerrain);
Expand All @@ -335,7 +329,7 @@ int main(int argc, char** argv) {
float yaw, pitch;
float lastX = 400, lastY = 300;
bool moveCamera = false;
ImVec4 clearColor(1.0, 1.0, 1.0, 1.0);
ImVec4 clearColor(0.0, 0.7, 1.0, 1.0);

do {
SDL_Event event;
Expand Down

0 comments on commit dab1bcc

Please sign in to comment.