Working version, no shadows
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "peripherals_glfw.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include "demo_data.h"
|
||||
|
||||
BEGIN_NAMESPACE
|
||||
|
||||
@@ -28,7 +29,7 @@ bool peripherals_glfw::init() {
|
||||
|
||||
glfwSetErrorCallback(error_callback);
|
||||
|
||||
window = glfwCreateWindow(1024, 768, "Demo", nullptr, nullptr);
|
||||
window = glfwCreateWindow(demo_data::screen_width, demo_data::screen_height, "Demo", nullptr, nullptr);
|
||||
if (window == nullptr) {
|
||||
std::cerr << "Unable to create window!" << std::endl;
|
||||
return false;
|
||||
|
||||
+91
-10
@@ -10,7 +10,7 @@
|
||||
BEGIN_NAMESPACE
|
||||
|
||||
static const char *vs = R"(
|
||||
#version 150 core
|
||||
#version 330 core
|
||||
|
||||
uniform mat4 mmtx;
|
||||
uniform mat4 wlmtx;
|
||||
@@ -26,7 +26,7 @@ static const char *vs = R"(
|
||||
)";
|
||||
|
||||
static const char *ps = R"(
|
||||
#version 150 core
|
||||
#version 330 core
|
||||
|
||||
out vec4 colorOut;
|
||||
|
||||
@@ -36,7 +36,7 @@ static const char *ps = R"(
|
||||
)";
|
||||
|
||||
static const char *vs2 = R"(
|
||||
#version 150 core
|
||||
#version 330 core
|
||||
|
||||
uniform mat4 mmtx;
|
||||
uniform mat4 vmtx;
|
||||
@@ -71,7 +71,7 @@ static const char *vs2 = R"(
|
||||
)";
|
||||
|
||||
static const char *ps2 = R"(
|
||||
#version 150 core
|
||||
#version 330 core
|
||||
out vec4 outColor;
|
||||
|
||||
uniform sampler2DShadow shadowMap1;
|
||||
@@ -191,6 +191,87 @@ static const char *ps2 = R"(
|
||||
}
|
||||
)";
|
||||
|
||||
static const char *vs3 = R"(
|
||||
#version 330 core
|
||||
|
||||
uniform mat4 mmtx;
|
||||
uniform mat4 vmtx;
|
||||
uniform mat3 nmtx;
|
||||
uniform mat4 pmtx;
|
||||
|
||||
in vec3 position;
|
||||
in vec3 normal;
|
||||
in vec2 texCoords;
|
||||
|
||||
out vec3 vEyeSpaceNormal;
|
||||
out vec3 vEyeSpacePosition;
|
||||
out vec2 vTexCoords;
|
||||
|
||||
void main() {
|
||||
gl_Position = pmtx * vmtx * mmtx * vec4(position, 1);
|
||||
vEyeSpacePosition = (vmtx * mmtx * vec4(position, 1)).xyz;
|
||||
vEyeSpaceNormal = normalize(nmtx * normal);
|
||||
vTexCoords = texCoords;
|
||||
})";
|
||||
|
||||
static const char *ps3 = R"(
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 lPosEye[4];
|
||||
uniform vec4 matAmbientCol;
|
||||
uniform vec4 matDiffuseCol;
|
||||
uniform vec4 matSpecularCol;
|
||||
uniform float matShininess;
|
||||
uniform int numLights;
|
||||
|
||||
uniform sampler2D diffuseMap;
|
||||
|
||||
struct Light {
|
||||
int type;
|
||||
vec3 position;
|
||||
vec3 target;
|
||||
float spotCutoff;
|
||||
float spotExponent;
|
||||
vec3 attenuation;
|
||||
vec4 ambient;
|
||||
vec4 diffuse;
|
||||
vec4 specular;
|
||||
};
|
||||
|
||||
uniform Light light[4];
|
||||
|
||||
const vec2 texmapscale = vec2(1.0f/1024.0f, 1.0f/1024.0f);
|
||||
|
||||
in vec3 vEyeSpaceNormal;
|
||||
in vec3 vEyeSpacePosition;
|
||||
in vec2 vTexCoords;
|
||||
|
||||
void main() {
|
||||
vec4 finalColor = vec4(0);
|
||||
vec3 normal = normalize(vEyeSpaceNormal);
|
||||
vec4 diffuseColor = matDiffuseCol * texture(diffuseMap, vTexCoords);
|
||||
|
||||
for (int i = 0; i < numLights; i++) {
|
||||
vec3 L = (light[i].position - vEyeSpacePosition);
|
||||
float d = length(L);
|
||||
L = normalize(L);
|
||||
float diffuse = max(0.0, dot(normal, L));
|
||||
|
||||
vec3 E = normalize(-vEyeSpacePosition);
|
||||
vec3 R = normalize(-reflect(L, vEyeSpaceNormal));
|
||||
float specular = pow(max(dot(R, E), 0.0), 0.3*matShininess);
|
||||
|
||||
float shadow = 1.0f;
|
||||
finalColor += matAmbientCol * matDiffuseCol;
|
||||
finalColor += diffuseColor * diffuse * light[i].diffuse * shadow;
|
||||
finalColor += matSpecularCol * specular * shadow;
|
||||
}
|
||||
|
||||
FragColor = finalColor;
|
||||
}
|
||||
)";
|
||||
|
||||
scene_renderer::scene_renderer()
|
||||
: fbo_id{0} {
|
||||
|
||||
@@ -206,7 +287,7 @@ scene_renderer::scene_renderer()
|
||||
first_pass_shadow_material->cull_faces = true;
|
||||
first_pass_shadow_material->cull_front_faces = false;
|
||||
|
||||
second_pass_shadow_shader = std::make_shared<shader>(vs2, ps2);
|
||||
second_pass_shadow_shader = std::make_shared<shader>(vs3, ps3);
|
||||
|
||||
second_pass_shadow_material = std::make_shared<material>();
|
||||
second_pass_shadow_material->shader_ = second_pass_shadow_shader;
|
||||
@@ -225,11 +306,11 @@ void scene_renderer::attach_depth_texture(texture &text) const {
|
||||
}
|
||||
|
||||
void scene_renderer::render(const scene_tree &scene, const std::string &cameraName) {
|
||||
for (auto &node: scene.lights) {
|
||||
light_node &light = node->as_light_node();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
render_shadow_map(scene, light);
|
||||
}
|
||||
// for (auto &node: scene.lights) {
|
||||
// light_node &light = node->as_light_node();
|
||||
// glActiveTexture(GL_TEXTURE0);
|
||||
// render_shadow_map(scene, light);
|
||||
// }
|
||||
|
||||
std::shared_ptr<scene_node> camNode = scene.nodeByNameAndType(cameraName, scene_node_type::camera);
|
||||
camera_node &camera = camNode->as_camera_node();
|
||||
|
||||
+60
-17
@@ -3,32 +3,32 @@
|
||||
|
||||
BEGIN_NAMESPACE
|
||||
|
||||
void check_compile_error(GLuint shaderId) {
|
||||
void check_compile_error(GLuint shader_id) {
|
||||
GLint isCompiled = 0;
|
||||
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &isCompiled);
|
||||
if (isCompiled == GL_FALSE) {
|
||||
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &isCompiled);
|
||||
if (!isCompiled) {
|
||||
GLint maxLength = 0;
|
||||
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
|
||||
//The maxLength includes the NULL character
|
||||
char errorLog[maxLength];
|
||||
glGetShaderInfoLog(shaderId, maxLength, &maxLength, &errorLog[0]);
|
||||
glGetShaderInfoLog(shader_id, maxLength, &maxLength, &errorLog[0]);
|
||||
|
||||
LOG(ERROR) << "Shader compile error: " << "(id: " << shaderId << ", code: " << isCompiled << ", bytes: "
|
||||
LOG(ERROR) << "Shader compile error: " << "(id: " << shader_id << ", code: " << isCompiled << ", bytes: "
|
||||
<< maxLength << ") - " << std::string(errorLog, maxLength);
|
||||
|
||||
//Provide the infolog in whatever manor you deem best.
|
||||
//Exit with failure.
|
||||
glDeleteShader(shaderId); //Don't leak the shader.
|
||||
glDeleteShader(shader_id); //Don't leak the shader.
|
||||
} else {
|
||||
LOG(INFO) << "Shader successfully compiled";
|
||||
LOG(INFO) << "Shader " << shader_id << " successfully compiled";
|
||||
}
|
||||
}
|
||||
|
||||
void check_link_error(GLuint program_id) {
|
||||
GLint is_linked = 0;
|
||||
glGetShaderiv(program_id, GL_LINK_STATUS, &is_linked);
|
||||
if (is_linked == GL_FALSE) {
|
||||
if (!is_linked) {
|
||||
GLint maxLength = 0;
|
||||
glGetShaderiv(program_id, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
|
||||
@@ -43,35 +43,78 @@ void check_link_error(GLuint program_id) {
|
||||
//Exit with failure.
|
||||
glDeleteShader(program_id); //Don't leak the program shader.
|
||||
} else {
|
||||
LOG(INFO) << "Shader program successfully linked";
|
||||
LOG(INFO) << "Shader program " << program_id << " successfully linked";
|
||||
}
|
||||
}
|
||||
|
||||
shader::shader(const char *vs_content, const char *ps_content)
|
||||
: shader_ids{0, 0},
|
||||
program_id{0} {
|
||||
|
||||
|
||||
shader::shader(const char *vs_content, const char *ps_content) {
|
||||
shader_ids[0] = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(shader_ids[0], 1, &vs_content, nullptr);
|
||||
glShaderSource(shader_ids[0], 1, &vs_content, NULL);
|
||||
glCompileShader(shader_ids[0]);
|
||||
check_compile_error(shader_ids[0]);
|
||||
int success;
|
||||
char infoLog[512];
|
||||
glGetShaderiv(shader_ids[0], GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(shader_ids[0], 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
shader_ids[1] = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(shader_ids[1], 1, &ps_content, nullptr);
|
||||
glShaderSource(shader_ids[1], 1, &ps_content, NULL);
|
||||
glCompileShader(shader_ids[1]);
|
||||
check_compile_error(shader_ids[1]);
|
||||
glGetShaderiv(shader_ids[1], GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(shader_ids[1], 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
program_id = glCreateProgram();
|
||||
glAttachShader(program_id, shader_ids[0]);
|
||||
glAttachShader(program_id, shader_ids[1]);
|
||||
|
||||
// TODO Adrian: these should come from an external enumeration
|
||||
glBindAttribLocation(program_id, 0, "position");
|
||||
glBindAttribLocation(program_id, 1, "normal");
|
||||
glBindAttribLocation(program_id, 2, "texCoords");
|
||||
|
||||
glLinkProgram(program_id);
|
||||
check_link_error(program_id);
|
||||
|
||||
glGetProgramiv(program_id, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetProgramInfoLog(program_id, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::PROGRAM::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glUseProgram(program_id);
|
||||
// glDeleteShader(shader_ids[0]);
|
||||
// glDeleteShader(shader_ids[1]);
|
||||
|
||||
// shader_ids[0] = glCreateShader(GL_VERTEX_SHADER);
|
||||
// glShaderSource(shader_ids[0], 1, &vs_content, nullptr);
|
||||
// glCompileShader(shader_ids[0]);
|
||||
// check_compile_error(shader_ids[0]);
|
||||
//
|
||||
// shader_ids[1] = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
// glShaderSource(shader_ids[1], 1, &ps_content, nullptr);
|
||||
// glCompileShader(shader_ids[1]);
|
||||
// check_compile_error(shader_ids[1]);
|
||||
//
|
||||
// program_id = glCreateProgram();
|
||||
// glAttachShader(program_id, shader_ids[0]);
|
||||
// glAttachShader(program_id, shader_ids[1]);
|
||||
|
||||
// TODO Adrian: these should come from an external enumeration
|
||||
// glBindAttribLocation(program_id, 0, "position");
|
||||
// glBindAttribLocation(program_id, 1, "normal");
|
||||
// glBindAttribLocation(program_id, 2, "texCoords");
|
||||
|
||||
// glLinkProgram(program_id);
|
||||
// check_link_error(program_id);
|
||||
|
||||
// glUseProgram(program_id);
|
||||
}
|
||||
|
||||
shader::~shader() {
|
||||
|
||||
@@ -140,25 +140,25 @@ public:
|
||||
type = shader_constant_type::decimal;
|
||||
}
|
||||
|
||||
void apply_to(shader &shader, const char *uniform_name) {
|
||||
void apply_to(shader &sh, const char *uniform_name) {
|
||||
switch (type) {
|
||||
case shader_constant_type::vec3:
|
||||
shader.setVec3Uniform(glm::value_ptr(vec3Val), uniform_name);
|
||||
sh.setVec3Uniform(glm::value_ptr(vec3Val), uniform_name);
|
||||
break;
|
||||
case shader_constant_type::vec4:
|
||||
shader.setVec4Uniform(glm::value_ptr(vec4Val), uniform_name);
|
||||
sh.setVec4Uniform(glm::value_ptr(vec4Val), uniform_name);
|
||||
break;
|
||||
case shader_constant_type::mat3:
|
||||
shader.setMatrix3Uniform(glm::value_ptr(mat3Val), uniform_name);
|
||||
sh.setMatrix3Uniform(glm::value_ptr(mat3Val), uniform_name);
|
||||
break;
|
||||
case shader_constant_type::mat4:
|
||||
shader.setMatrix4Uniform(glm::value_ptr(mat4Val), uniform_name);
|
||||
sh.setMatrix4Uniform(glm::value_ptr(mat4Val), uniform_name);
|
||||
break;
|
||||
case shader_constant_type::integer:
|
||||
shader.setIntUniform(intVal, uniform_name);
|
||||
sh.setIntUniform(intVal, uniform_name);
|
||||
break;
|
||||
case shader_constant_type::decimal:
|
||||
shader.setFloatUniform(floatVal, uniform_name);
|
||||
sh.setFloatUniform(floatVal, uniform_name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user