Finally shifted the camera. Now to move it to the camera class.
This commit is contained in:
parent
9b5cc10c1a
commit
f364da6272
5 changed files with 39 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
||||||
|
|
||||||
project(Game)
|
project(Game)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
file(GLOB_RECURSE SOURCE_FILES
|
file(GLOB_RECURSE SOURCE_FILES
|
||||||
${CMAKE_SOURCE_DIR}/src/*.c
|
${CMAKE_SOURCE_DIR}/src/*.c
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,12 @@
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec2 aTexCoord;
|
layout (location = 1) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
uniform mat4 MVP;
|
||||||
|
|
||||||
out vec2 ourTexCoord;
|
out vec2 ourTexCoord;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(aPos, 1.0);
|
gl_Position = MVP * vec4(aPos, 1.0);
|
||||||
ourTexCoord = aTexCoord;
|
ourTexCoord = aTexCoord;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -88,3 +88,7 @@ void ShaderLoader::setInt(const std::string &name, int value) const {
|
||||||
void ShaderLoader::setFloat(const std::string &name, float value) const {
|
void ShaderLoader::setFloat(const std::string &name, float value) const {
|
||||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
void ShaderLoader::setMat4(const std::string &name, glm::mat4 value) const {
|
||||||
|
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE,
|
||||||
|
&value[0][0]);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
// File reader
|
// File reader
|
||||||
#include "Error.h"
|
#include "Error.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
@ -25,4 +26,5 @@ public:
|
||||||
void setBool(const std::string &name, bool value) const;
|
void setBool(const std::string &name, bool value) const;
|
||||||
void setInt(const std::string &name, int value) const;
|
void setInt(const std::string &name, int value) const;
|
||||||
void setFloat(const std::string &name, float value) const;
|
void setFloat(const std::string &name, float value) const;
|
||||||
|
void setMat4(const std::string &name, glm::mat4 value) const;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
33
src/main.cpp
33
src/main.cpp
|
|
@ -8,7 +8,8 @@
|
||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
#include <SDL2/SDL_opengl.h>
|
#include <SDL2/SDL_opengl.h>
|
||||||
// Not used yet
|
// Not used yet
|
||||||
//#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
// File reader
|
// File reader
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -67,9 +68,9 @@ int main(int argc, char **argv) {
|
||||||
// Create event handling struct
|
// Create event handling struct
|
||||||
SDL_Event input;
|
SDL_Event input;
|
||||||
|
|
||||||
ShaderLoader shader("../data/shaders"
|
ShaderLoader shader("./data/shaders"
|
||||||
"/vertex.glsl",
|
"/vertex.glsl",
|
||||||
"../data/shaders"
|
"./data/shaders"
|
||||||
"/fragment.glsl");
|
"/fragment.glsl");
|
||||||
|
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
|
|
@ -125,7 +126,7 @@ int main(int argc, char **argv) {
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
// Load texture image
|
// Load texture image
|
||||||
SDL_Surface *image = IMG_Load("../data"
|
SDL_Surface *image = IMG_Load("./data"
|
||||||
"/container.jpg");
|
"/container.jpg");
|
||||||
if (image == nullptr) {
|
if (image == nullptr) {
|
||||||
error.crash("SDL2_image was unable to load a texture", IMG_GetError());
|
error.crash("SDL2_image was unable to load a texture", IMG_GetError());
|
||||||
|
|
@ -151,6 +152,28 @@ int main(int argc, char **argv) {
|
||||||
SDL_FreeSurface(image);
|
SDL_FreeSurface(image);
|
||||||
image = nullptr;
|
image = nullptr;
|
||||||
|
|
||||||
|
// Mess with perspective
|
||||||
|
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
|
||||||
|
// unit <-> 100 units
|
||||||
|
float width = 4;
|
||||||
|
float height = 3;
|
||||||
|
glm::mat4 Projection = glm::perspective(
|
||||||
|
glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
|
||||||
|
|
||||||
|
// Camera matrix
|
||||||
|
glm::mat4 View = glm::lookAt(
|
||||||
|
glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
|
||||||
|
glm::vec3(0, 0, 0), // and looks at the origin
|
||||||
|
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
|
||||||
|
);
|
||||||
|
// Model matrix : an identity matrix (model will be at the origin)
|
||||||
|
glm::mat4 Model = glm::mat4(1.0f);
|
||||||
|
|
||||||
|
// Our ModelViewProjection : multiplication of our 3 matrices
|
||||||
|
glm::mat4 mvp =
|
||||||
|
Projection * View *
|
||||||
|
Model; // Remember, matrix multiplication is the other way around
|
||||||
|
|
||||||
// Game loop
|
// Game loop
|
||||||
bool running = true;
|
bool running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
|
|
@ -168,6 +191,8 @@ int main(int argc, char **argv) {
|
||||||
// Make every shader/rendering call from this point on use our shader
|
// Make every shader/rendering call from this point on use our shader
|
||||||
// glUseProgram(shaderProgram);
|
// glUseProgram(shaderProgram);
|
||||||
|
|
||||||
|
// Send our glsl shader our mvp
|
||||||
|
shader.setMat4("MVP", mvp);
|
||||||
shader.use();
|
shader.use();
|
||||||
|
|
||||||
// I think this is meant to be here but it breaks...
|
// I think this is meant to be here but it breaks...
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue