Camera point at & more tests

pull/4/head
Elias Almqvist 2 years ago
parent efc205acb5
commit cd0b229767
  1. 13
      headers/renderer.hpp
  2. 9
      shaders/vertex.glsl
  3. 14
      src/main.cpp
  4. 23
      src/renderer.cpp

@ -25,26 +25,24 @@ namespace Renderer {
Object(glm::vec3 pos); Object(glm::vec3 pos);
Object(glm::vec3 pos, glm::vec3 angle); Object(glm::vec3 pos, glm::vec3 angle);
glm::mat4 getModelTransform();
void transform(glm::mat4 T); void transform(glm::mat4 T);
void scale(glm::vec3 vscale); void scale(glm::vec3 vscale);
glm::mat4 getPositionTransform();
void setPosition(glm::vec3 pos); void setPosition(glm::vec3 pos);
void translate(glm::vec3 dpos); void translate(glm::vec3 dpos);
glm::mat4 getRotationTransform();
void setRotation(glm::vec3 angle); void setRotation(glm::vec3 angle);
void rotate(glm::vec3 dangle); void rotate(glm::vec3 dangle);
private: protected:
void updatePositionTransform(); void updatePositionTransform();
void updateRotationTransform(); void updateRotationTransform();
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 angle = glm::vec3(0.0f, 0.0f, 0.0f);
glm::mat4 positionTransform = glm::mat4(1.0f); glm::mat4 positionTransform = glm::mat4(1.0f);
glm::mat4 rotationTransform = glm::mat4(1.0f); glm::mat4 rotationTransform = glm::mat4(1.0f);
glm::mat4 modelTransform = glm::mat4(1.0f); glm::mat4 modelTransform = glm::mat4(1.0f);
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 angle = glm::vec3(0.0f, 0.0f, 0.0f);
}; };
class Camera : public Object { class Camera : public Object {
@ -54,7 +52,10 @@ namespace Renderer {
Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle); Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle);
void setFOV(float deg); void setFOV(float deg);
void pointAt(glm::vec3 target);
glm::mat4 projection = glm::mat4(1.0f); glm::mat4 projection = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
private: private:
GLFWwindow* window; GLFWwindow* window;
}; };

@ -10,13 +10,12 @@ uniform mat4 modelRotation = mat4(1.0);
uniform mat4 modelPosition = mat4(1.0); uniform mat4 modelPosition = mat4(1.0);
uniform mat4 model = mat4(1.0); uniform mat4 model = mat4(1.0);
uniform mat4 camPos = mat4(1.0); uniform mat4 view = mat4(1.0);
uniform mat4 camRot = mat4(1.0); uniform mat4 projection = mat4(1.0);
uniform mat4 camProjection = mat4(1.0);
void main() { void main() {
// projection { view } { model } // projection view { model }
gl_Position = camProjection * (camRot * camPos) * (modelRotation * modelPosition * model) * vec4(aPos, 1.0); gl_Position = projection * view * (modelRotation * modelPosition * model) * vec4(aPos, 1.0);
VertexColor = vec4(aColor, 1.0); VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord; TexCoord = aTexCoord;
} }

@ -27,9 +27,9 @@ void processInput(GLFWwindow *win) {
void renderCallback() { void renderCallback() {
// Make background // Make background
// glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
// glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }
@ -152,10 +152,10 @@ int main() {
Renderer::Scene scene(win); Renderer::Scene scene(win);
Renderer::TexturedObject ro(verts, indices); Renderer::TexturedObject ro(verts, indices);
ro.setPosition(glm::vec3(0.2f, -1.0f, -8.0f)); // ro.setPosition(glm::vec3(0.2f, -1.0f, -8.0f));
Renderer::TexturedObject ro2(verts, indices); Renderer::TexturedObject ro2(verts, indices);
ro2.setPosition(glm::vec3(0.5f, 0.0, -8.0f)); // ro2.setPosition(glm::vec3(0.5f, 0.0, -8.0f));
ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug
ro.setTexture(RUSTY_METAL_TEXTURE); ro.setTexture(RUSTY_METAL_TEXTURE);
@ -174,13 +174,17 @@ int main() {
float gVal = sin(time); float gVal = sin(time);
// Move the camera left and right // Move the camera left and right
scene.camera.setRotation(glm::vec3(gVal * 5, 5.0f * gVal, gVal * 10.0f)); // scene.camera.setRotation(glm::vec3(gVal * 5, 5.0f * gVal, gVal * 20.0f));
// scene.camera.translate(glm::vec3(0.0f, 0.0f, 0.02f + gVal/100.0f)); // scene.camera.translate(glm::vec3(0.0f, 0.0f, 0.02f + gVal/100.0f));
scene.camera.pointAt(glm::vec3(0.0f, 0.0f, 0.0f));
scene.camera.setPosition(glm::vec3(cos(time)*10.0f, 0.0f, gVal*10.0f));
// Move the objects & stuff // Move the objects & stuff
float rotang = time; float rotang = time;
ro.rotate(glm::vec3(0.0f, 0.0f, gVal)); ro.rotate(glm::vec3(0.0f, 0.0f, gVal));
ro2.setPosition(glm::vec3(0.0f, gVal * 2.0f, 1.0f + gVal*5.0f));
// Render new frame // Render new frame
scene.render(); scene.render();

@ -22,8 +22,6 @@ namespace Renderer {
setRotation(angle); setRotation(angle);
} }
glm::mat4 Object::getModelTransform() { return modelTransform; }
void Object::scale(glm::vec3 vscale) { void Object::scale(glm::vec3 vscale) {
glm::mat4 T = glm::mat4(1.0f); glm::mat4 T = glm::mat4(1.0f);
T = glm::scale(T, vscale); T = glm::scale(T, vscale);
@ -34,8 +32,6 @@ namespace Renderer {
modelTransform = T; modelTransform = T;
} }
glm::mat4 Object::getPositionTransform() { return positionTransform; }
void Object::updatePositionTransform() { void Object::updatePositionTransform() {
glm::mat4 T = glm::mat4(1.0f); glm::mat4 T = glm::mat4(1.0f);
positionTransform = glm::translate(T, position); positionTransform = glm::translate(T, position);
@ -51,14 +47,13 @@ namespace Renderer {
updatePositionTransform(); updatePositionTransform();
} }
glm::mat4 Object::getRotationTransform() { return rotationTransform; }
void Object::updateRotationTransform() { void Object::updateRotationTransform() {
float x_Ang, y_Ang, z_Ang; float x_Ang, y_Ang, z_Ang;
x_Ang = angle[0]; x_Ang = angle[0];
y_Ang = angle[1]; y_Ang = angle[1];
z_Ang = angle[2]; z_Ang = angle[2];
// TODO: make better
glm::mat4 T = glm::mat4(1.0f); glm::mat4 T = glm::mat4(1.0f);
T = glm::rotate(T, glm::radians(x_Ang), glm::vec3(1.0f, 0.0f, 0.0f)); T = glm::rotate(T, glm::radians(x_Ang), glm::vec3(1.0f, 0.0f, 0.0f));
T = glm::rotate(T, glm::radians(y_Ang), glm::vec3(0.0f, 1.0f, 0.0f)); T = glm::rotate(T, glm::radians(y_Ang), glm::vec3(0.0f, 1.0f, 0.0f));
@ -120,6 +115,11 @@ namespace Renderer {
projection = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE); projection = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE);
} }
void Camera::pointAt(glm::vec3 target) {
view = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f));
}
// RenderObject // RenderObject
RenderObject::RenderObject(std::vector<float> verts, std::vector<unsigned int> indices) RenderObject::RenderObject(std::vector<float> verts, std::vector<unsigned int> indices)
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) { : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) {
@ -164,13 +164,12 @@ namespace Renderer {
// TODO: Make prerender instead of render // TODO: Make prerender instead of render
void RenderObject::render(GLFWwindow* win, Camera cam) { void RenderObject::render(GLFWwindow* win, Camera cam) {
shader.setMat4("modelPosition", getPositionTransform()); shader.setMat4("modelPosition", positionTransform);
shader.setMat4("modelRotation", getRotationTransform()); shader.setMat4("modelRotation", rotationTransform);
shader.setMat4("model", getModelTransform()); shader.setMat4("model", modelTransform);
shader.setMat4("camPos", cam.getPositionTransform()); shader.setMat4("view", cam.view);
shader.setMat4("camRot", cam.getRotationTransform()); shader.setMat4("projection", cam.projection);
shader.setMat4("camProjection", cam.projection);
shader.use(); shader.use();

Loading…
Cancel
Save