Add specular map and abstract loading textures
This commit is contained in:
parent
cc1f6afb26
commit
fbda4821ee
BIN
images/container2_specular.png
Normal file
BIN
images/container2_specular.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 141 KiB |
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
struct Material {
|
struct Material {
|
||||||
sampler2D diffuse;
|
sampler2D diffuse;
|
||||||
vec3 specular;
|
sampler2D specular;
|
||||||
float shininess;
|
float shininess;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ void main() {
|
|||||||
vec3 diff_tex = vec3(texture(material.diffuse, uv_coords));
|
vec3 diff_tex = vec3(texture(material.diffuse, uv_coords));
|
||||||
vec3 ambient = light.ambient * diff_tex;
|
vec3 ambient = light.ambient * diff_tex;
|
||||||
vec3 diffuse = light.diffuse * (diff * diff_tex);
|
vec3 diffuse = light.diffuse * (diff * diff_tex);
|
||||||
vec3 specular = light.specular * (spec * material.specular);
|
vec3 specular = light.specular * (spec * vec3(texture(material.specular, uv_coords)));
|
||||||
|
|
||||||
color = vec4(ambient + diffuse + specular, 1.0);
|
color = vec4(ambient + diffuse + specular, 1.0);
|
||||||
};
|
};
|
||||||
|
73
src/main.cc
73
src/main.cc
@ -66,6 +66,20 @@ class Shader {
|
|||||||
static const char *get_shader_type_string(GLenum shader_type);
|
static const char *get_shader_type_string(GLenum shader_type);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Texture2D {
|
||||||
|
public:
|
||||||
|
Texture2D(const char *filename, GLint texture_unit);
|
||||||
|
~Texture2D();
|
||||||
|
void activate();
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int channels;
|
||||||
|
private:
|
||||||
|
GLuint texture;
|
||||||
|
GLint texture_unit;
|
||||||
|
GLint format;
|
||||||
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||||
return EXIT_CODE_SDL_INIT_FAILED;
|
return EXIT_CODE_SDL_INIT_FAILED;
|
||||||
@ -181,28 +195,10 @@ int main() {
|
|||||||
Shader main_shader {"shaders/vert.glsl", "shaders/frag.glsl"};
|
Shader main_shader {"shaders/vert.glsl", "shaders/frag.glsl"};
|
||||||
Shader light_shader {"shaders/vert.glsl", "shaders/light_frag.glsl"};
|
Shader light_shader {"shaders/vert.glsl", "shaders/light_frag.glsl"};
|
||||||
|
|
||||||
int width, height, channels;
|
Texture2D diffuse_map = Texture2D("images/container2.png", GL_TEXTURE0);
|
||||||
uint8_t *diffuse_map = stbi_load("images/container2.png", &width, &height, &channels, 0);
|
Texture2D specular_map = Texture2D("images/container2_specular.png", GL_TEXTURE1);
|
||||||
GLuint diffuse_texture = 0;
|
|
||||||
if (diffuse_map) {
|
|
||||||
glGenTextures(1, &diffuse_texture);
|
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, diffuse_texture);
|
|
||||||
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, diffuse_map);
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
|
|
||||||
main_shader.set_int("material.diffuse", 0);
|
main_shader.set_int("material.diffuse", 0);
|
||||||
|
main_shader.set_int("material.specular", 1);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
stbi_image_free(diffuse_map);
|
|
||||||
}
|
|
||||||
|
|
||||||
const float camera_speed = 25.0f;
|
const float camera_speed = 25.0f;
|
||||||
glm::vec3 camera_position = glm::vec3(-2.0f, 0.0f, 6.0f);
|
glm::vec3 camera_position = glm::vec3(-2.0f, 0.0f, 6.0f);
|
||||||
@ -323,8 +319,8 @@ int main() {
|
|||||||
main_shader.set_vec3("light.diffuse", light_diffuse);
|
main_shader.set_vec3("light.diffuse", light_diffuse);
|
||||||
main_shader.set_mat4("model", model);
|
main_shader.set_mat4("model", model);
|
||||||
main_shader.set_mat3("normal_mat", normal_mat);
|
main_shader.set_mat3("normal_mat", normal_mat);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
diffuse_map.activate();
|
||||||
glBindTexture(GL_TEXTURE_2D, diffuse_texture);
|
specular_map.activate();
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, (void *)0);
|
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, (void *)0);
|
||||||
|
|
||||||
@ -477,3 +473,34 @@ const char *Shader::get_shader_type_string(GLenum shader_type) {
|
|||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Texture2D::Texture2D(const char *filename, GLint texture_unit) : texture_unit(texture_unit) {
|
||||||
|
uint8_t *image = stbi_load(filename, &width, &height, &channels, 0);
|
||||||
|
if (!image) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO (Abdelrahman): This doesn't handle all formats
|
||||||
|
format = channels > 3 ? GL_RGBA : GL_RGB;
|
||||||
|
|
||||||
|
glGenTextures(1, &texture);
|
||||||
|
activate();
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, image);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
stbi_image_free(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture2D::~Texture2D() {}
|
||||||
|
|
||||||
|
void Texture2D::activate() {
|
||||||
|
glActiveTexture(texture_unit);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user