Transformations etc

pull/2/head
E. Almqvist 2 years ago
parent c85d14e65c
commit 73ff2b5619
  1. 3
      .gitmodules
  2. 14
      CMakeLists.txt
  3. 5
      headers/shaders.hpp
  4. 2
      headers/textures.hpp
  5. 1
      lib/glm
  6. 9
      shaders/vertex.glsl
  7. 58
      src/main.cpp
  8. 17
      src/shaders.cpp
  9. 2
      src/textures.cpp

3
.gitmodules vendored

@ -7,3 +7,6 @@
[submodule "extern/stb"] [submodule "extern/stb"]
path = extern/stb path = extern/stb
url = https://github.com/nothings/stb.git url = https://github.com/nothings/stb.git
[submodule "lib/glm"]
path = lib/glm
url = https://github.com/g-truc/glm.git

@ -2,10 +2,13 @@ cmake_minimum_required(VERSION 3.24.2)
project(Hohmann VERSION 1.0) project(Hohmann VERSION 1.0)
#### Sources
file(GLOB SOURCES src/*.cpp headers/*.hpp) file(GLOB SOURCES src/*.cpp headers/*.hpp)
add_executable(Hohmann ${SOURCES}) add_executable(Hohmann ${SOURCES})
# GLFW submodule
#### Libs
# GLFW
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
@ -14,10 +17,13 @@ add_subdirectory(lib/glfw)
# GLAD # GLAD
add_subdirectory(lib/glad) add_subdirectory(lib/glad)
# External headers & sources # GLM
add_subdirectory(lib/glm)
#### External headers & sources
file(GLOB EXTERN_SOURCES extern/*) file(GLOB EXTERN_SOURCES extern/*)
include_directories(${EXTERN_SOURCES}) include_directories(${EXTERN_SOURCES})
link_directories(${EXTERN_SOURCES}) link_directories(${EXTERN_SOURCES})
# Linking #### Linking
target_link_libraries(Hohmann PRIVATE glfw glad) target_link_libraries(Hohmann PRIVATE glfw glad glm)

@ -1,3 +1,4 @@
#include "glm/fwd.hpp"
#include <glad/glad.h> #include <glad/glad.h>
#include <string> #include <string>
#include <vector> #include <vector>
@ -18,5 +19,9 @@ namespace Shaders {
void setBool(const std::string &name, bool val) const; void setBool(const std::string &name, bool val) const;
void setInt(const std::string &name, int val) const; void setInt(const std::string &name, int val) const;
void setFloat(const std::string &name, float val) const; void setFloat(const std::string &name, float val) const;
void setMat4(const std::string &name, glm::mat4 val) const;
private:
unsigned int uniLocation(const std::string &name) const;
}; };
} }

@ -7,7 +7,7 @@ namespace Textures {
unsigned int id; unsigned int id;
Texture2D(const char* t_src); Texture2D(const char* t_src);
void load(); void load();
void use(); void bind();
private: private:
const char* texture_src; const char* texture_src;
int width, height, nrChannels; int width, height, nrChannels;

@ -0,0 +1 @@
Subproject commit 0a6d3334ea747b0023d232572d3aff61dc6b48e6

@ -6,8 +6,15 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor; out vec4 VertexColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 transform = mat4(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
void main() { void main() {
gl_Position = vec4(aPos, 1.0); gl_Position = transform * vec4(aPos, 1.0);
VertexColor = vec4(aColor, 1.0); VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord; TexCoord = aTexCoord;
} }

@ -1,5 +1,8 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdio.h> #include <stdio.h>
#include <vector> #include <vector>
#include <math.h> #include <math.h>
@ -68,35 +71,30 @@ RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count,
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE); Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE);
Textures::Texture2D texture(RUSTY_METAL_TEXTURE); Textures::Texture2D texture(RUSTY_METAL_TEXTURE);
return RenderObj {EBO, shader, texture}; return RenderObj {EBO, shader, texture};
} }
void renderCallback(RenderObj ro) { void renderCallback() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Make background
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
}
float time = glfwGetTime(); void renderObject(RenderObj ro, glm::mat4 T) {
float gVal = (sin(time) / 1.5f) + 0.5f; // Bind the texture
ro.texture.bind();
ro.shader.use();
ro.shader.setFloat("r", gVal);
ro.shader.setFloat("g", gVal);
ro.shader.setFloat("b", gVal);
ro.texture.use(); ro.shader.setMat4("transform", T);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
} }
int main() { int main() {
glfwInit(); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@ -116,9 +114,9 @@ int main() {
float verts[] = { float verts[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f
}; };
// Vert struc: x y z r g b tx ty // Vert struc: x y z r g b tx ty
@ -140,7 +138,31 @@ int main() {
processInput(win); processInput(win);
// rendering // rendering
renderCallback(ro); renderCallback();
/* OBJECT RENDERING */
float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f;
// Coloring uniform
ro.shader.use();
// ro.shader.setFloat("r", gVal);
// ro.shader.setFloat("g", gVal);
// ro.shader.setFloat("b", gVal);
// Transformation
float rotang = time;
glm::mat4 T = glm::mat4(1.0f);
T = glm::rotate(T, rotang, glm::vec3(0.0, 0.707, 0.707));
T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5));
renderObject(ro, T);
glm::mat4 T2 = glm::mat4(1.0f);
T2 = glm::translate(T2, glm::vec3(-0.5, 0.4, 0.0));
T2 = glm::rotate(T2, rotang, glm::vec3(0.707, 0.707, 0.0));
T2 = glm::scale(T2, glm::vec3(0.5, 0.5, 0.2));
renderObject(ro, T2);
// glfw // glfw
glfwSwapBuffers(win); glfwSwapBuffers(win);

@ -7,6 +7,8 @@
#include "../headers/shaders.hpp" #include "../headers/shaders.hpp"
#include <glm/gtc/type_ptr.hpp>
namespace Shaders { namespace Shaders {
std::string loadSourceFromFile(const char* fp) { std::string loadSourceFromFile(const char* fp) {
std::string src; std::string src;
@ -88,14 +90,23 @@ namespace Shaders {
void Shader::use() { glUseProgram(id); } void Shader::use() { glUseProgram(id); }
void Shader::setInt(const std::string &name, int val) const { void Shader::setInt(const std::string &name, int val) const {
glUniform1i(glGetUniformLocation(id, name.c_str()), val); glUniform1i(uniLocation(name), val);
} }
void Shader::setFloat(const std::string &name, float val) const { void Shader::setFloat(const std::string &name, float val) const {
glUniform1f(glGetUniformLocation(id, name.c_str()), val); glUniform1f(uniLocation(name), val);
} }
void Shader::setBool(const std::string &name, bool val) const { void Shader::setBool(const std::string &name, bool val) const {
glUniform1i(glGetUniformLocation(id, name.c_str()), (int)val) ; glUniform1i(uniLocation(name), (int)val);
}
void Shader::setMat4(const std::string &name, glm::mat4 val) const {
glUniformMatrix4fv(uniLocation(name), 1, GL_FALSE, glm::value_ptr(val));
};
// Private stuff
unsigned int Shader::uniLocation(const std::string &name) const {
return glGetUniformLocation(id, name.c_str());
} }
} }

@ -40,7 +40,7 @@ namespace Textures {
stbi_image_free(data); stbi_image_free(data);
} }
void Texture2D::use() { void Texture2D::bind() {
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
} }
} }

Loading…
Cancel
Save