额
这个应该是多重渲染目标(MRT)吧,反正就这么命名了 r,g,b,d依次是red,green,blue,depth(其实是距离) 除100是因为太亮了,很容易就会达到1,而100是我视体的远平面 话说纹理竟然是反的
片段着色器代码
#version 330 core
in vec3 v_position;
in vec3 v_normal;
in vec3 v_normal2;
in vec2 v_texCoord;
layout(location = 0)out vec4 o_red;
layout(location = 1)out vec4 o_green;
layout(location = 2)out vec4 o_blue;
layout(location = 3)out vec4 o_depth;
uniform vec3 u_cameraposition;
uniform sampler2D s_sampler;
vec4 lighting()
{
vec3 light_direction = normalize(v_position - u_cameraposition);
vec3 view_direction = normalize(v_position - u_cameraposition);
float ambient = 0.1;
float diffuse = max(dot(v_normal, -light_direction), 0.0);
float diffuse2 = max(dot(v_normal2, -light_direction), 0.0);
vec3 reflect_direction = reflect(light_direction, v_normal);
float specular = pow(max(dot(-view_direction, reflect_direction), 0.0), 32.0);
vec3 reflect_direction2 = reflect(light_direction, v_normal2);
float specular2 = pow(max(dot(-view_direction, reflect_direction2), 0.0), 32.0);
return (ambient + diffuse + specular + diffuse2 + specular2) * texture(s_sampler,v_texCoord);
}
void main()
{
vec4 finalColor = lighting();
vec3 distance = (v_position - u_cameraposition) / 100.0;
o_red = vec4(finalColor.r,0.0,0.0,1.0);
o_green = vec4(0.0,finalColor.g,0.0,1.0);
o_blue = vec4(0.0,0.0,finalColor.b,1.0);
o_depth = vec4(distance.x,distance.y,distance.z,1.0);
}
宿主语言代码
初始化帧缓冲 p.s.其实我觉得RBO不需要
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* videomode = glfwGetVideoMode(monitor);
int width = videomode->width;
int height = videomode->height;
GLuint fbo;
GLuint rbo;
GLuint r_texture, g_texture, b_texture, d_texture;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenTextures(1, &r_texture);
glBindTexture(GL_TEXTURE_2D, r_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &g_texture);
glBindTexture(GL_TEXTURE_2D, g_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &b_texture);
glBindTexture(GL_TEXTURE_2D, b_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &d_texture);
glBindTexture(GL_TEXTURE_2D, d_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, r_texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, g_texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, b_texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, d_texture, 0);
GLuint attachments[4] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, attachments);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std::cout << "Framebuffer is not complete." << std::endl;
return -1;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
渲染代码
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
data->program_m.useProgram();
data->program_m.setUniform("u_projection", data->camera.getProjectionMatrix());
data->program_m.setUniform("u_view", data->camera.getViewMatrix());
data->program_m.setUniform("u_cameraposition", data->camera.getPosition());
glActiveTexture(GL_TEXTURE0);
data->texture.bindTexture();
data->model.render(data->program_m);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
data->program_t.useProgram();
data->program_t.setUniform("u_projection", glm::mat4(1.0f));
data->program_t.setUniform("u_view", glm::mat4(1.0f));
data->program_t.setUniform("u_cameraposition", data->camera.getPosition());
data->target.setPosition(target_pos_r);
data->program_t.setUniform("u_model", data->target.getModelMatrix());
glBindTexture(GL_TEXTURE_2D, r_texture);
data->target.render(data->program_t);
data->target.setPosition(target_pos_g);
data->program_t.setUniform("u_model", data->target.getModelMatrix());
glBindTexture(GL_TEXTURE_2D, g_texture);
data->target.render(data->program_t);
data->target.setPosition(target_pos_b);
data->program_t.setUniform("u_model", data->target.getModelMatrix());
glBindTexture(GL_TEXTURE_2D, b_texture);
data->target.render(data->program_t);
data->target.setPosition(target_pos_d);
data->program_t.setUniform("u_model", data->target.getModelMatrix());
glBindTexture(GL_TEXTURE_2D, d_texture);
data->target.render(data->program_t);
glBindTexture(GL_TEXTURE_2D, 0);
最后删除缓冲区
glDeleteFramebuffers(1, &fbo);
glDeleteRenderbuffers(1, &rbo);
glDeleteTextures(1, &r_texture);
glDeleteTextures(1, &g_texture);
glDeleteTextures(1, &b_texture);
glDeleteTextures(1, &d_texture);
运行结果
懒得放图:)
|