Commit 8165b5e3 authored by Adam Štěpánek's avatar Adam Štěpánek
Browse files

Finish the belt

parent 642e0a50
Loading
Loading
Loading
Loading
+45 −16
Original line number Diff line number Diff line
@@ -4,16 +4,24 @@
#include <glad/glad.h>
#include <glm/glm.hpp>

struct MaterialUBO {
	glm::vec4 ambientColor;
	glm::vec4 diffuseColor;
	glm::vec4 specularColor;
};

class Material {
private:
	struct MaterialUBO {
		glm::vec4 ambientColor;
		glm::vec4 diffuseColor;
		glm::vec4 specularColor;
	};

public:
	Material(MaterialUBO ubo, Texture* diffuseTexture = nullptr, Texture* normalTexture = nullptr)
	    : _ubo(ubo)
	Material()
	    : Material(glm::vec4(0.0f), glm::vec4(1.0f), glm::vec4(1.0f)) {}

	Material(const glm::vec4& ambientColor,
	    const glm::vec4& diffuseColor,
	    const glm::vec4& specularColor,
	    Texture* diffuseTexture = nullptr,
	    Texture* normalTexture = nullptr)
	    : _ubo(MaterialUBO { ambientColor, diffuseColor, specularColor })
	    , _diffuseTexture(diffuseTexture)
	    , _normalTexture(normalTexture) {
		glCreateBuffers(1, &_buffer);
@@ -24,15 +32,30 @@ public:
		glDeleteBuffers(1, &_buffer);
	}

private:
	MaterialUBO _ubo { glm::vec4(0.0f), glm::vec4(1.0f), glm::vec4(1.0f) };
	Texture* _diffuseTexture = nullptr;
	Texture* _normalTexture = nullptr;
	GLuint _buffer = 0;
	const glm::vec4& ambientColor() const {
		return _ubo.ambientColor;
	}

public:
	MaterialUBO ubo() const {
		return _ubo;
	void ambientColor(const glm::vec4& value) {
		_ubo.ambientColor = value;
		glNamedBufferSubData(_buffer, 0, sizeof(MaterialUBO), &_ubo);
	}

	const glm::vec4& diffuseColor() const {
		return _ubo.diffuseColor;
	}

	void diffuseColor(const glm::vec4& value) {
		_ubo.diffuseColor = value;
		glNamedBufferSubData(_buffer, 0, sizeof(MaterialUBO), &_ubo);
	}

	const glm::vec4& specularColor() const {
		return _ubo.specularColor;
	}
	void specularColor(const glm::vec4& value) {
		_ubo.specularColor = value;
		glNamedBufferSubData(_buffer, 0, sizeof(MaterialUBO), &_ubo);
	}

	Texture* diffuseTexture() {
@@ -46,4 +69,10 @@ public:
	GLuint buffer() const {
		return _buffer;
	}

private:
	MaterialUBO _ubo { glm::vec4(0.0f), glm::vec4(1.0f), glm::vec4(1.0f) };
	Texture* _diffuseTexture = nullptr;
	Texture* _normalTexture = nullptr;
	GLuint _buffer = 0;
};
 No newline at end of file
+1 −4
Original line number Diff line number Diff line
@@ -68,9 +68,7 @@ Scene::Scene(const std::string& objPath) {
		glm::vec4 ambientColor(material.ambient[0], material.ambient[1], material.ambient[2], 0.0);
		glm::vec4 diffuseColor(material.diffuse[0], material.diffuse[1], material.diffuse[2], 0.0);
		glm::vec4 specularColor(material.specular[0], material.specular[1], material.specular[2], material.shininess);
		_materials.emplace_back(std::make_unique<Material>(MaterialUBO { ambientColor, diffuseColor, specularColor },
		    diffuseTexture,
		    normalTexture));
		_materials.push_back(std::make_unique<Material>(ambientColor, diffuseColor, specularColor, diffuseTexture, normalTexture));
	}

	for (const tinyobj::shape_t& shape : shapes) {
@@ -85,7 +83,6 @@ Scene::Scene(const std::string& objPath) {
			for (size_t v = 0; v < 3; v++) {
				tinyobj::index_t idx = shape.mesh.indices[f * 3 + v];


				// unwrap vertices and optionally normals
				for (int i = 0; i < 3; i++) {
					vertices.push_back(attrib.vertices[3 * (size_t)idx.vertex_index + i]);

images/rings.png

0 → 100644LFS
+128 B
Loading image diff...
+12 −10
Original line number Diff line number Diff line
@@ -17,42 +17,44 @@ layout(binding = 1, std430) buffer Lights {
	Light lights[];
};

layout(binding = 2, std140) uniform Object {
	mat4 modelMatrix;
layout(binding = 2, std140) uniform Material {
	vec4 ambientColor;
	vec4 diffuseColor;
	vec4 specularColor;
} object;
} material;

layout(binding = 0) uniform sampler2D diffuseTexture;

layout(location = 0) in vec3 fs_Position;
layout(location = 1) in vec3 fs_Normal;
layout(location = 2) in vec4 fs_RingColor;

layout(location = 0) out vec4 finalColor;

void main()
{
	vec3 lightsSum = vec3(0.0);
	for(int i = 0; i < 1; i++) {
	for(int i = 0; i < lights.length(); i++) {
		Light light = lights[i];

		vec3 lightVector = light.position.xyz - fs_Position * light.position.w;
		vec3 L = normalize(lightVector);
		vec3 N = normalize(fs_Normal);
		vec3 E = -normalize(camera.position - fs_Position); 
		vec3 E = normalize(camera.position - fs_Position); 
		vec3 H = normalize(L + E);

		float NdotL = max(dot(N, L), 0.0);
		float NdotH = max(dot(N, H), 0.000001);
		float NdotH = max(dot(N, H), 0.0);

		float distance2 = light.position.w == 1.0 ? pow(length(lightVector), 2) : 1.0;

		vec3 ambient = object.ambientColor.rgb * light.ambientColor.rgb;
		vec3 diffuse = object.diffuseColor.rgb * light.diffuseColor.rgb;
		vec3 specular = object.specularColor.rgb * light.specularColor.rgb;
		vec3 ambient = material.ambientColor.rgb * light.ambientColor.rgb;
		vec3 diffuse = fs_RingColor.rgb * material.diffuseColor.rgb * light.diffuseColor.rgb;
		vec3 specular = material.specularColor.rgb * light.specularColor.rgb;

		vec3 color = ambient.rgb
			+ NdotL * diffuse.rgb
			+ pow(NdotH, object.specularColor.w) * specular;
			+ pow(NdotH, material.specularColor.w) * specular;
		color /= distance2;

		lightsSum += color;
+41 −21
Original line number Diff line number Diff line
@@ -96,38 +96,58 @@ layout(binding = 1, std430) buffer Lights {
	Light lights[];
};

layout(binding = 2, std140) uniform Object {
	mat4 modelMatrix;
layout(binding = 2, std140) uniform Material {
	vec4 ambientColor;
	vec4 diffuseColor;
	vec4 specularColor;
} object;
} material;

layout(location = 0) uniform double magic[256];
layout(binding = 3, std140) uniform Belt {
	double magic[64];
};

layout(location = 0) uniform mat4 modelMatrix;
layout(location = 1) uniform double time;

layout(binding = 0) uniform sampler2D diffuseTexture;

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;

layout(location = 0) out vec3 fs_Position;
layout(location = 1) out vec3 fs_Normal;
layout(location = 2) out vec4 fs_RingColor;

void main()
{
	float rotationSeed = snoise(vec2(gl_InstanceID, 0.0)) * 360;
	mat4 rotation = mat4(
		cos(rotationSeed), 0.0, sin(rotationSeed), 0.0,
		0.0, 1.0, 0.0, 0.0,
		-sin(rotationSeed), 0.0, cos(rotationSeed), 0.0,
		0.0, 0.0, 0.0, 1.0);

	fs_Position = vec3(rotation * object.modelMatrix * vec4(position, 1.0));
	fs_Normal = transpose(inverse(mat3(object.modelMatrix) * mat3(rotation))) * normal;

	float distanceFromCenter = 2.3 + snoise(vec2(gl_InstanceID, 0.0)) / 2.0;
	vec2 direction = vec2(cos(gl_InstanceID), sin(gl_InstanceID));
	direction *= distanceFromCenter;
	vec4 finalPosition = vec4(position * 0.01, 1.0);
	finalPosition.xz += direction;
	finalPosition.y += float(magic[int((distanceFromCenter - 2.3) * 256) % 256]) * 0.1;
	gl_Position = camera.projection * camera.view * rotation * object.modelMatrix * finalPosition;
	float angleY = snoise(vec2(gl_InstanceID, 0.0f) * 100.0f) * 3.1415f;
	angleY += sign(angleY) * float(time) * 10.0f;
	mat4 modelRotationY = mat4(
		cos(angleY), 0.0f, -sin(angleY), 0.0f,
		0.0f, 1.0f, 0.0f, 0.0f,
		sin(angleY), 0.0f, cos(angleY), 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f);

	float dist = snoise(vec2(gl_InstanceID, 0.0f));
	float music = sqrt(float(magic[int(dist * 32.0f + 16.0f) ]));
	vec2 direction = vec2(cos(gl_InstanceID), sin(gl_InstanceID)) * (15.0f + dist * 5.0f);
	mat4 modelTranslation = mat4(
		1.0f, 0.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f, 0.0f,
		0.0f, 0.0f, 1.0f, 0.0f,
		direction.x, music, direction.y, 1.0f);

	float angleRing = float(time) / 10.0f;
	mat4 modelRotationRing = mat4(
		cos(angleRing), 0.0f, -sin(angleRing), 0.0f,
		0.0f, 1.0f, 0.0f, 0.0f,
		sin(angleRing), 0.0f, cos(angleRing), 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f);

	mat4 model = modelRotationRing * modelTranslation * modelRotationY * modelMatrix;

	fs_Position = vec3(model * vec4(position, 1.0));
	fs_Normal = transpose(inverse(mat3(model))) * normal;
	fs_RingColor = texture(diffuseTexture, vec2(0.0f, dist));
	gl_Position = camera.projection * camera.view * model * vec4(position, 1.0f);
}
Loading