finished texture unit chapter (and therefore textures section)

This commit is contained in:
Daniel 2023-07-27 09:16:56 +02:00
parent 5d149d8b3c
commit deb2a7de0b
5 changed files with 34 additions and 14 deletions

View File

@ -157,6 +157,7 @@
<ClInclude Include="mesh\mesh.h" />
<ClInclude Include="Shaders\Enums.h" />
<ClInclude Include="Shaders\Shader.h" />
<ClInclude Include="textures\Texture2D.h" />
<ClInclude Include="util\stb_image.h" />
</ItemGroup>
<ItemGroup>

View File

@ -50,6 +50,9 @@
<ClInclude Include="util\stb_image.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="textures\Texture2D.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Shaders\default\default.frag" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -7,8 +7,8 @@
#include "shaders/Shader.h"
#include "util/stb_image.h"
// Continue: https://learnopengl.com/Getting-started/Textures
// Chapter: Texture Units
// Continue: https://learnopengl.com/Getting-started/Transformations
// Chapter: Not started
//
@ -117,6 +117,7 @@ int main()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// TEXTURES
stbi_set_flip_vertically_on_load(true); // because an image has 0 at top of y axis and opengl expects it to be on the botton, so if this is omitted, the image will be flipped
int width, height, nrChannels;
unsigned char* image = stbi_load("images/container.jpg", &width, &height, &nrChannels, 0);
if (!image)
@ -125,25 +126,33 @@ int main()
stbi_image_free(image);
return -1; // it would be better to just use a default error image instead and make the error message a warning message instead
}
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
unsigned int textureID_container;
glGenTextures(1, &textureID_container);
glBindTexture(GL_TEXTURE_2D, textureID_container);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(image);
GLfloat texCoords[] = {
0.0f, 0.0f, // lower-left corner
1.0f, 0.0f, // lower-right corner
0.5f, 1.0f // top-center corner
};
image = stbi_load("images/awesomeface.png", &width, &height, &nrChannels, 0);
if (!image)
{
std::cerr << "ERROR::IO::LOAD_TEXTURE";
stbi_image_free(image);
return -1; // it would be better to just use a default error image instead and make the error message a warning message instead
}
unsigned int textureID_face;
glGenTextures(1, &textureID_face);
glBindTexture(GL_TEXTURE_2D, textureID_face);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(image);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // use nearest neighbour when zooming out
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // use bilinear when zooming in
// main loop
while(!glfwWindowShouldClose(window))
{
@ -153,11 +162,17 @@ int main()
ProcessInput(window);
shader->Use();
shader->setInt("ourTexture1", 0);
shader->setInt("ourTexture2", 1);
//float time = glfwGetTime();
//float greenValue = (sin(time) / 2.0f) + 0.5f;
//shader->setFloat("ourColour", 0.0f, greenValue, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, textureID);
glActiveTexture(GL_TEXTURE0); // before binding texture, activate correct textre Unit (some drivers might show nothing if this is omitted)
glBindTexture(GL_TEXTURE_2D, textureID_container);
glActiveTexture(GL_TEXTURE0 + 1); // GL_TEXTURE0 + 1 == GL_TEXTURE1, this means looping over multiple units is easily possible
glBindTexture(GL_TEXTURE_2D, textureID_face);
glBindVertexArray(vertexArrayObject);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

View File

@ -4,9 +4,10 @@ out vec4 FragColor;
in vec3 ourColour;
in vec2 texCoord;
uniform sampler2D ourTexture;
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;
void main()
{
FragColor = texture(ourTexture, texCoord) * vec4(ourColour, 1.0);
FragColor = mix(texture(ourTexture1, texCoord), texture(ourTexture2, texCoord), 0.2) * vec4(ourColour, 1.0);
}