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"]
path = extern/stb
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)
#### Sources
file(GLOB SOURCES src/*.cpp headers/*.hpp)
add_executable(Hohmann ${SOURCES})
# GLFW submodule
#### Libs
# GLFW
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
@ -14,10 +17,13 @@ add_subdirectory(lib/glfw)
# GLAD
add_subdirectory(lib/glad)
# External headers & sources
# GLM
add_subdirectory(lib/glm)
#### External headers & sources
file(GLOB EXTERN_SOURCES extern/*)
include_directories(${EXTERN_SOURCES})
link_directories(${EXTERN_SOURCES})
# Linking
target_link_libraries(Hohmann PRIVATE glfw glad)
#### Linking
target_link_libraries(Hohmann PRIVATE glfw glad glm)

@ -1,3 +1,4 @@
#include "glm/fwd.hpp"
#include <glad/glad.h>
#include <string>
#include <vector>
@ -18,5 +19,9 @@ namespace Shaders {
void setBool(const std::string &name, bool val) const;
void setInt(const std::string &name, int 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;
Texture2D(const char* t_src);
void load();
void use();
void bind();
private:
const char* texture_src;
int width, height, nrChannels;

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

@ -6,8 +6,15 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor;
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() {
gl_Position = vec4(aPos, 1.0);
gl_Position = transform * vec4(aPos, 1.0);
VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord;
}

@ -1,5 +1,8 @@
#include <glad/glad.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 <vector>
#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)));
glEnableVertexAttribArray(2);
Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE);
Textures::Texture2D texture(RUSTY_METAL_TEXTURE);
return RenderObj {EBO, shader, texture};
}
void renderCallback(RenderObj ro) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
void renderCallback() {
// Make background
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f;
ro.shader.use();
ro.shader.setFloat("r", gVal);
ro.shader.setFloat("g", gVal);
ro.shader.setFloat("b", gVal);
void renderObject(RenderObj ro, glm::mat4 T) {
// Bind the texture
ro.texture.bind();
ro.texture.use();
ro.shader.setMat4("transform", T);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@ -116,9 +114,9 @@ int main() {
float verts[] = {
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, 0.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,
-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
};
// Vert struc: x y z r g b tx ty
@ -140,7 +138,31 @@ int main() {
processInput(win);
// 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
glfwSwapBuffers(win);

@ -7,6 +7,8 @@
#include "../headers/shaders.hpp"
#include <glm/gtc/type_ptr.hpp>
namespace Shaders {
std::string loadSourceFromFile(const char* fp) {
std::string src;
@ -88,14 +90,23 @@ namespace Shaders {
void Shader::use() { glUseProgram(id); }
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 {
glUniform1f(glGetUniformLocation(id, name.c_str()), val);
glUniform1f(uniLocation(name), val);
}
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);
}
void Texture2D::use() {
void Texture2D::bind() {
glBindTexture(GL_TEXTURE_2D, id);
}
}

Loading…
Cancel
Save