Textures and colors passed to shader via Uniforms
This commit is contained in:
parent
e243a666f1
commit
5e8d693758
@ -37,7 +37,10 @@ swig_add_library(couchlua
|
||||
target_link_libraries(couchlua ${LUA_LIBRARIES})
|
||||
target_link_libraries(couch couchlua)
|
||||
|
||||
add_subdirectory(thirdparty)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(couch glfw3dll)
|
||||
target_link_libraries(couch ssp)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
|
||||
endif(WIN32)
|
||||
|
BIN
container.png
Normal file
BIN
container.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 413 KiB |
@ -3,15 +3,15 @@
|
||||
Ball::Ball() {
|
||||
// It's a cube really
|
||||
// Front
|
||||
vertices.push_back(Vertex(1.0f, 1.0f, 1.0f));
|
||||
vertices.push_back(Vertex(1.0f, -1.0f, 1.0f));
|
||||
vertices.push_back(Vertex(-1.0f, 1.0f, 1.0f));
|
||||
vertices.push_back(Vertex(-1.0f, -1.0f, 1.0f));
|
||||
vertices.push_back(Vertex(1.0f, 1.0f, 1.0f, 0.0f, 1.0f));
|
||||
vertices.push_back(Vertex(1.0f, -1.0f, 1.0f, 0.0f, 0.0f));
|
||||
vertices.push_back(Vertex(-1.0f, 1.0f, 1.0f, 1.0f, 1.0f));
|
||||
vertices.push_back(Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f));
|
||||
// Back
|
||||
vertices.push_back(Vertex(1.0f, 1.0f, -1.0f));
|
||||
vertices.push_back(Vertex(1.0f, -1.0f, -1.0f));
|
||||
vertices.push_back(Vertex(-1.0f, 1.0f, -1.0f));
|
||||
vertices.push_back(Vertex(-1.0f, -1.0f, -1.0f));
|
||||
vertices.push_back(Vertex(1.0f, 1.0f, -1.0f, 0.0f, 0.0f));
|
||||
vertices.push_back(Vertex(1.0f, -1.0f, -1.0f, 0.0f, 1.0f));
|
||||
vertices.push_back(Vertex(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f));
|
||||
vertices.push_back(Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f));
|
||||
|
||||
//Front
|
||||
indices.push_back(Index(0, 1, 2));
|
||||
|
@ -2,11 +2,13 @@
|
||||
#define DRAWABLE_H
|
||||
|
||||
#include "Node.h"
|
||||
#include "Material.h"
|
||||
|
||||
class Drawable : virtual public Node {
|
||||
public:
|
||||
virtual bool IsDrawable() const {return true;}
|
||||
virtual void Draw() = 0;
|
||||
Material *material;
|
||||
};
|
||||
|
||||
#endif /* DRAWABLE_H */
|
||||
|
47
core/Material.cpp
Normal file
47
core/Material.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "Material.h"
|
||||
|
||||
Color::Color() {
|
||||
this->r = 0.0f;
|
||||
this->g = 0.0f;
|
||||
this->b = 0.0f;
|
||||
}
|
||||
|
||||
Color::Color(cfloat r, cfloat g, cfloat b) {
|
||||
this->r = r;
|
||||
this->g = g;
|
||||
this->b = b;
|
||||
}
|
||||
|
||||
Texture::Texture() {}
|
||||
|
||||
Texture Texture::FromFile(const char *filename) {
|
||||
// Lotta hocus pocus
|
||||
// https://learnopengl.com/Getting-started/Textures
|
||||
Texture tex;
|
||||
glGenTextures(1, &tex.id);
|
||||
glBindTexture(GL_TEXTURE_2D, tex.id);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
int nrChannels;
|
||||
unsigned char* data = stbi_load(filename, &tex.width, &tex.height, &nrChannels, 0);
|
||||
if (data) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
} else {
|
||||
std::cout << "Error loading texture file: " << filename << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return tex;
|
||||
}
|
||||
|
||||
Material::Material() {
|
||||
usesColor = false;
|
||||
usesTex = false;
|
||||
}
|
34
core/Material.h
Normal file
34
core/Material.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef MATERIAL_H
|
||||
#define MATERIAL_H
|
||||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include "stb_image.h"
|
||||
|
||||
#include "types.h"
|
||||
|
||||
struct Color {
|
||||
cfloat r, g, b;
|
||||
Color();
|
||||
Color(cfloat r, cfloat g, cfloat b);
|
||||
};
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
int width, height;
|
||||
Id id;
|
||||
static Texture FromFile(const char *filename);
|
||||
Texture();
|
||||
};
|
||||
|
||||
struct Material {
|
||||
Color color;
|
||||
bool usesColor;
|
||||
Texture tex;
|
||||
bool usesTex;
|
||||
Material();
|
||||
};
|
||||
|
||||
#endif /* MATERIAL_H */
|
@ -1,6 +1,12 @@
|
||||
#include "Mesh.h"
|
||||
|
||||
Mesh::Mesh() {}
|
||||
Mesh::Mesh() {
|
||||
material = new Material();
|
||||
}
|
||||
|
||||
Mesh::~Mesh() {
|
||||
delete material;
|
||||
}
|
||||
|
||||
Mesh::Mesh(VertexList vertices, IndexList indices) {
|
||||
this->vertices = vertices;
|
||||
@ -24,7 +30,10 @@ void Mesh::SetupMesh() {
|
||||
// Vertex positions
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
|
||||
// TODO normals, uv
|
||||
// Vertex UV
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
|
||||
// TODO normals
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ public:
|
||||
VertexList vertices;
|
||||
IndexList indices;
|
||||
Mesh();
|
||||
~Mesh();
|
||||
Mesh(VertexList vertices, IndexList indices);
|
||||
virtual void Draw();
|
||||
virtual void SetupMesh();
|
||||
|
@ -67,8 +67,6 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) {
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Shader::Use() {
|
||||
glUseProgram(id);
|
||||
}
|
||||
@ -85,6 +83,23 @@ void Shader::UpdateProjection(Matrix projection) {
|
||||
glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection));
|
||||
}
|
||||
|
||||
void Shader::UpdateColor(bool usesColor) {
|
||||
glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) usesColor);
|
||||
}
|
||||
|
||||
void Shader::UpdateColor(bool usesColor, Color color) {
|
||||
glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) usesColor);
|
||||
glUniform3f(glGetUniformLocation(id, "material.color"), color.r, color.g, color.b);
|
||||
}
|
||||
|
||||
void Shader::UpdateTex(bool usesTex) {
|
||||
glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) usesTex);
|
||||
}
|
||||
void Shader::UpdateTex(bool usesTex, Texture tex) {
|
||||
glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) usesTex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex.id);
|
||||
}
|
||||
|
||||
Name Shader::GetName() const {
|
||||
return "Unnamed Shader";
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "types.h"
|
||||
#include "Material.h"
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
@ -19,6 +20,10 @@ public:
|
||||
void UpdateView(Matrix view);
|
||||
void UpdateModel(Matrix model);
|
||||
void UpdateProjection(Matrix projection);
|
||||
void UpdateColor(bool usesColor);
|
||||
void UpdateColor(bool usesColor, Color color);
|
||||
void UpdateTex(bool usesTex);
|
||||
void UpdateTex(bool usesTex, Texture tex);
|
||||
|
||||
virtual Name GetName() const;
|
||||
};
|
||||
|
@ -4,10 +4,22 @@ Vertex::Vertex() {
|
||||
x = 0.0f;
|
||||
y = 0.0f;
|
||||
z = 0.0f;
|
||||
u = 0.0f;
|
||||
v = 0.0f;
|
||||
}
|
||||
|
||||
Vertex::Vertex(cfloat x, cfloat y, cfloat z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->u = 0.0f;
|
||||
this->v = 0.0f;
|
||||
}
|
||||
|
||||
Vertex::Vertex(cfloat x, cfloat y, cfloat z, cfloat u, cfloat v) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->u = u;
|
||||
this->v = v;
|
||||
}
|
||||
|
@ -8,7 +8,9 @@
|
||||
struct Vertex {
|
||||
Vertex();
|
||||
Vertex(cfloat x, cfloat y, cfloat z);
|
||||
Vertex(cfloat x, cfloat y, cfloat z, cfloat u, cfloat v);
|
||||
cfloat x, y, z;
|
||||
cfloat u, v;
|
||||
};
|
||||
|
||||
typedef std::vector<Vertex> VertexList;
|
||||
|
@ -15,6 +15,10 @@
|
||||
#include "Node.h"
|
||||
#include "Lua.h"
|
||||
|
||||
// Thirdparty Includes
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
Window *window;
|
||||
|
||||
const int width = 800;
|
||||
@ -33,6 +37,16 @@ void render(Node *curr, Shader *shader, Matrix model) {
|
||||
shader->UpdateModel(model);
|
||||
}
|
||||
Drawable *drawable = dynamic_cast<Drawable*>(curr);
|
||||
if (drawable->material->usesColor) {
|
||||
shader->UpdateColor(true, drawable->material->color);
|
||||
} else {
|
||||
shader->UpdateColor(false);
|
||||
}
|
||||
if (drawable->material->usesTex) {
|
||||
shader->UpdateTex(true, drawable->material->tex);
|
||||
} else {
|
||||
shader->UpdateTex(false);
|
||||
}
|
||||
drawable->Draw();
|
||||
}
|
||||
for (Node *child : curr->children) {
|
||||
@ -66,6 +80,7 @@ int main() {
|
||||
}
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
root = Node::GetRoot();
|
||||
|
||||
@ -76,7 +91,6 @@ int main() {
|
||||
|
||||
FlatShader *shader = new FlatShader();
|
||||
shader->Use();
|
||||
shader->UpdateColor(Vector3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
|
||||
shader->UpdateProjection(projection);
|
||||
@ -90,7 +104,7 @@ int main() {
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
lua->Update(delta);
|
||||
|
||||
|
8
main.lua
8
main.lua
@ -20,15 +20,23 @@ local cam_rot_y = 0.0
|
||||
|
||||
local SPEED = 30
|
||||
|
||||
local RED = couch.Color(1.0, 0.0, 0.0)
|
||||
local BLUE = couch.Color(0.0, 0.0, 1.0)
|
||||
|
||||
function init()
|
||||
camera = couch.Camera()
|
||||
camera:MakeCurrent()
|
||||
camera.transform:Translate(0.0, 0.0, 10.0)
|
||||
ball = couch.Ball()
|
||||
ball:SetupMesh()
|
||||
ball.material.color = RED
|
||||
ball.material.usesColor = true
|
||||
couch.Node.GetRoot().children:Append(ball)
|
||||
ball1 = couch.Ball()
|
||||
ball1:SetupMesh()
|
||||
ball1.material.tex = couch.Texture.FromFile("container.png")
|
||||
ball1.material.usesTex = true
|
||||
print(ball1.material.tex.width, ball1.material.tex.height)
|
||||
couch.Node.GetRoot().children:Append(ball1)
|
||||
|
||||
ball1.transform:Translate(0.0, 3.0, 0.0)
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "Transform.h"
|
||||
#include "Spatial.h"
|
||||
#include "Drawable.h"
|
||||
#include "Material.h"
|
||||
#include "Mesh.h"
|
||||
#include "Ball.h"
|
||||
#include "Camera.h"
|
||||
@ -36,6 +37,7 @@ public:
|
||||
%include "Spatial.h"
|
||||
%include "Transform.h"
|
||||
%include "Drawable.h"
|
||||
%include "Material.h"
|
||||
%include "Mesh.h"
|
||||
%include "Ball.h"
|
||||
%include "Camera.h"
|
||||
|
@ -1,9 +1,24 @@
|
||||
#version 330 core
|
||||
|
||||
uniform vec3 color;
|
||||
in vec2 UV;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
struct Material {
|
||||
vec3 color;
|
||||
bool usesColor;
|
||||
sampler2D tex;
|
||||
bool usesTex;
|
||||
};
|
||||
|
||||
uniform Material material;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(color, 1.0);
|
||||
FragColor = vec4(0.0);
|
||||
if (material.usesColor) {
|
||||
FragColor += vec4(material.color, 1.0);
|
||||
}
|
||||
if (material.usesTex) {
|
||||
FragColor += texture(material.tex, UV);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,15 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 pos;
|
||||
layout (location = 1) in vec2 uv;
|
||||
|
||||
uniform mat4 MODEL;
|
||||
uniform mat4 VIEW;
|
||||
uniform mat4 PROJECTION;
|
||||
|
||||
out vec2 UV;
|
||||
|
||||
void main() {
|
||||
gl_Position = PROJECTION * VIEW * MODEL * vec4(pos, 1.0);
|
||||
UV = uv;
|
||||
}
|
||||
|
7
thirdparty/CMakeLists.txt
vendored
Normal file
7
thirdparty/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
project(Couch)
|
||||
|
||||
target_include_directories(couch
|
||||
PUBLIC stb)
|
||||
|
||||
target_include_directories(couchlua
|
||||
PUBLIC stb)
|
1
thirdparty/stb/.#CMakeLists.txt
vendored
Symbolic link
1
thirdparty/stb/.#CMakeLists.txt
vendored
Symbolic link
@ -0,0 +1 @@
|
||||
dane@sweetdee.1314:1610908086
|
1
thirdparty/stb/CMakeLists.txt
vendored
Normal file
1
thirdparty/stb/CMakeLists.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
Project(stb)
|
7762
thirdparty/stb/stb_image.h
vendored
Normal file
7762
thirdparty/stb/stb_image.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user