Skip to content

Commit 0358375

Browse files
committed
WIP: adding texturing
1 parent d72ba1c commit 0358375

File tree

12 files changed

+8091
-1
lines changed

12 files changed

+8091
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ find_package(OpenGL REQUIRED)
66

77
add_executable(${PROGRAM_NAME}
88
src/glad.c
9+
src/stb_image.cpp
910
src/shader.cpp
1011
src/triangle_mesh.cpp
12+
src/material.cpp
1113
src/main.cpp)
1214

1315
target_include_directories(${PROGRAM_NAME} PRIVATE include)

img/checker-pattern-diagonal.jpg

455 KB
Loading

img/checker-pattern-horizontal.png

133 KB
Loading

img/marika_matsumoto.jpg

97.3 KB
Loading

img/mask.jpg

329 KB
Loading

include/stb/stb_image.h

Lines changed: 7988 additions & 0 deletions
Large diffs are not rendered by default.

src/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <glad/glad.h>
44
#include <GLFW/glfw3.h>
55
#include "shader.hpp"
6+
#include "material.hpp"
67
#include "triangle_mesh.hpp"
78

89

@@ -34,12 +35,25 @@ try {
3435
glViewport(0, 0, w, h);
3536

3637
TriangleMesh* triangle = new TriangleMesh();
38+
Material* material = new Material("../img/checker-pattern-horizontal.png");
39+
//Material* material = new Material("../img/checker-pattern-diagonal.jpg");
40+
//Material* material = new Material("../img/marika_matsumoto.jpg");
41+
Material* mask = new Material("../img/mask.jpg");
3742

3843
unsigned int shader = make_shader(
3944
"../src/shaders/vertex.glsl",
4045
"../src/shaders/fragment.glsl"
4146
);
4247

48+
// set the texture units
49+
glUseProgram(shader);
50+
glUniform1i(glGetUniformLocation(shader, "material"), 0);
51+
glUniform1i(glGetUniformLocation(shader, "mask"), 1);
52+
53+
// enable alpha blending
54+
glEnable(GL_BLEND);
55+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
56+
4357
while (!glfwWindowShouldClose(window)) {
4458

4559
glfwPollEvents();
@@ -48,6 +62,8 @@ try {
4862

4963
glUseProgram(shader);
5064

65+
material->use(0);
66+
mask->use(1);
5167
triangle->draw();
5268

5369
glfwSwapBuffers(window);
@@ -57,6 +73,8 @@ try {
5773
glDeleteProgram(shader);
5874

5975
delete triangle;
76+
delete material;
77+
delete mask;
6078

6179
glfwTerminate();
6280

src/material.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
#include <glad/glad.h>
3+
#include <GLFW/glfw3.h>
4+
#define STB_IMAGE_IMPLEMENTATION
5+
#include <stb/stb_image.h>
6+
#include "material.hpp"
7+
8+
Material::Material(const char* filename) {
9+
int width, height, nrChannels;
10+
unsigned char* data = stbi_load(filename, &width, &height, &nrChannels, STBI_rgb_alpha);
11+
if (data == NULL) {
12+
std::cerr << "Failed to load texture " << filename << std::endl;
13+
return;
14+
}
15+
else {
16+
std::cout << "Loaded texture " << filename << " with width " << width << " and height " << height << " nrChannels " << nrChannels << '\n';
17+
}
18+
19+
// make the texture
20+
glGenTextures(1, &texture);
21+
glBindTexture(GL_TEXTURE_2D, texture);
22+
23+
// upload the texture
24+
glTexImage2D(GL_TEXTURE_2D,
25+
0 /* MIPMAP level */,
26+
GL_RGBA,
27+
width, height,
28+
0 /* border color = black */,
29+
GL_RGBA, GL_UNSIGNED_BYTE, data);
30+
31+
// once uploaded, we can get rid of the data
32+
stbi_image_free(data);
33+
34+
// configure the texture sampler
35+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
36+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
37+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // when shrinking no interpolation
38+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // when magnifying linear interpolation
39+
}
40+
41+
Material::~Material() {
42+
glDeleteTextures(1, &texture);
43+
}
44+
45+
void Material::use(int textureUnit) {
46+
glActiveTexture(GL_TEXTURE0 + textureUnit);
47+
glBindTexture(GL_TEXTURE_2D, texture);
48+
}

src/material.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
class Material {
4+
public:
5+
Material(const char* filename);
6+
~Material();
7+
8+
void use(int textureUnit);
9+
10+
private:
11+
unsigned int texture;
12+
unsigned int shader;
13+
};

src/shaders/fragment.glsl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
#version 330 core
22

33
in vec3 fragmentColor;
4+
in vec2 fragmentTextureCoord;
45

56
out vec4 screenColor;
67

8+
uniform sampler2D material; // texture unit 0
9+
uniform sampler2D mask; // texture unit 1
10+
711
void main() {
8-
screenColor = vec4(fragmentColor, 1.0);
12+
// just color without a texture
13+
//screenColor = vec4(fragmentColor, 1.0);
14+
15+
// drawing a texture
16+
//screenColor = texture(material, fragmentTextureCoord);
17+
18+
// with a mask
19+
vec3 baseColor = texture(material, fragmentTextureCoord).rgb;
20+
float alpha = texture(mask, fragmentTextureCoord).r;
21+
screenColor = vec4(baseColor, alpha);
22+
923
}

0 commit comments

Comments
 (0)