Commit 3b71257a authored by Adam Štěpánek's avatar Adam Štěpánek
Browse files

Fix normal maps behaviour with multiple lights

parent e10ecd3f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -72,7 +72,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);
		glm::vec4 emissiveColor(material.emission[0], material.emission[1], material.emission[2], 0.0);
		glm::vec4 emissiveColor(material.emission[0], material.emission[1], material.emission[2], 1.0);
		_materials.push_back(std::make_unique<Material>(ambientColor, diffuseColor, specularColor, emissiveColor, diffuseTexture, normalTexture));
	}

+5 −5
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ layout(location = 0) out vec4 finalColor;
void main()
{
	vec3 lightSum = vec3(0.0f);
	for(int i = 0; i < lights.length(); i++) {
	for(int i = 0; i <= lights.length(); i++) {
		Light light = lights[i];

		vec3 lightVector = fs_TBN * light.position.xyz - fs_Position * light.position.w;
@@ -59,11 +59,11 @@ void main()
		}
		float NdotH = max(dot(N, H), 0.0);

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

		vec3 ambient = material.ambientColor.rgb * lights[0].ambientColor.rgb;
		vec3 diffuse = texture(diffuseTexture, fs_TextureCoord).rgb * material.diffuseColor.rgb * lights[0].diffuseColor.rgb;
		vec3 specular = material.specularColor.rgb * lights[0].specularColor.rgb;
		vec3 ambient = material.ambientColor.rgb * light.ambientColor.rgb;
		vec3 diffuse = texture(diffuseTexture, fs_TextureCoord).rgb * material.diffuseColor.rgb * light.diffuseColor.rgb;
		vec3 specular = material.specularColor.rgb * light.specularColor.rgb;

		float spec = pow(NdotH, material.specularColor.w);
		if (toon.isEnabled > 0) {
+1 −1
Original line number Diff line number Diff line
@@ -114,7 +114,7 @@ void Application::render() {
	glBindBufferBase(GL_UNIFORM_BUFFER, 3, toonBuffer);

	// Lights
	glNamedBufferSubData(lightsBuffer, 0, sizeof(LightUBO) * lights.size(), lights.data());
	glNamedBufferSubData(lightsBuffer, 0, lights.size() * sizeof(LightUBO), lights.data());

	// --------------------------------------------------------------------------
	// Draw the scene
+5 −1
Original line number Diff line number Diff line
@@ -36,7 +36,10 @@ int main(void) {
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
	GLFWwindow *window = glfwCreateWindow(INITIAL_WIDTH, INITIAL_HEIGHT, "The Almighty Jupiteapot", NULL, NULL);

	//glfwWindowHint(GLFW_SAMPLES, 4);
	    GLFWwindow *window
	    = glfwCreateWindow(INITIAL_WIDTH, INITIAL_HEIGHT, "The Almighty Jupiteapot", NULL, NULL);
	if (!window) {
		glfwTerminate();
		return ERR_GLFW_OTHER;
@@ -48,6 +51,7 @@ int main(void) {
		std::cerr << "Could not initialize OpenGL context!" << std::endl;
		return ERR_GLAD_LOAD;
	}
	//glEnable(GL_MULTISAMPLE);
	glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
	glDebugMessageCallback(onGlDebugMessage, nullptr);

+13 −2
Original line number Diff line number Diff line
@@ -16,11 +16,11 @@ Probe::Probe(const std::string& objPath,

	_scene = std::make_unique<Scene>(objPath);
	for (auto& mesh : _scene->meshes()) {
		if (mesh->material() && mesh->material()->emissiveColor() != glm::vec4(0.0f)) {
		if (mesh->material() && glm::vec3(mesh->material()->emissiveColor()) != glm::vec3(0.0f)) {
			lights.push_back(LightUBO { glm::vec4(mesh->center(), 1.0f),
			    glm::vec4(0.0f),
			    mesh->material()->emissiveColor(),
			    glm::vec4(0.1f) });
			    glm::vec4(0.0f) });
			++_lightsCount;
			_lightsStartPositions.push_back(mesh->center());
		}
@@ -39,3 +39,14 @@ void Probe::update() {
	}
	_lastTime = _time;
}

void Probe::draw() {
	for (auto& mesh : _scene->meshes()) {
		GLuint program = mesh->material() && mesh->material()->normalTexture()
		    ? _normalMappedObjectProgram.id()
		    : _texturedObjectProgram.id();
		glUseProgram(program);
		glProgramUniformMatrix4fv(program, 0, 1, GL_FALSE, glm::value_ptr(_modelMatrix));
		mesh->draw();
	}
}
 No newline at end of file
Loading