finished first chapter of Textures section

This commit is contained in:
Daniel 2023-07-24 20:06:59 +02:00
parent 6aa1460068
commit 5d149d8b3c
10 changed files with 8068 additions and 9 deletions

View File

@ -140,6 +140,7 @@
<ClCompile Include="..\..\..\libraries\glad\src\glad.c" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Shaders\Shader.cpp" />
<ClCompile Include="util\stb_image.cpp" />
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Shaders\default\default.frag">
@ -156,6 +157,11 @@
<ClInclude Include="mesh\mesh.h" />
<ClInclude Include="Shaders\Enums.h" />
<ClInclude Include="Shaders\Shader.h" />
<ClInclude Include="util\stb_image.h" />
</ItemGroup>
<ItemGroup>
<Image Include="images\container.jpg" />
<Image Include="images\wall.jpg" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -24,6 +24,9 @@
<ClCompile Include="Shaders\Shader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="util\stb_image.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Shaders\Shader.h">
@ -44,6 +47,9 @@
<ClInclude Include="mesh\mesh.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util\stb_image.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Shaders\default\default.frag" />
@ -51,4 +57,12 @@
<Filter>Resource Files</Filter>
</CopyFileToFolders>
</ItemGroup>
<ItemGroup>
<Image Include="images\wall.jpg">
<Filter>Resource Files</Filter>
</Image>
<Image Include="images\container.jpg">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 KiB

BIN
src/Engine/images/wall.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB

View File

@ -5,9 +5,10 @@
#include <string>
#include "shaders/Shader.h"
#include "util/stb_image.h"
// Continue: https://learnopengl.com/Getting-started/Textures
// Chapter: Not yet started
// Chapter: Texture Units
//
@ -63,11 +64,11 @@ int main()
OnWindowResize(window, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(window, OnWindowResize);
GLfloat vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f // top left
GLfloat vertices[] = { // world coordinate, colour, uv coordinates
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f // top left
};
GLuint faces[]{
0, 1, 3,
@ -90,11 +91,14 @@ int main()
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// colour (or more general, the second vertex Attribute)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// uv-coordinates (or more general the third vertex attribute)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem
std::string vertexPath = "shaders/default/default.vert";
@ -112,6 +116,34 @@ int main()
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// TEXTURES
int width, height, nrChannels;
unsigned char* image = stbi_load("images/container.jpg", &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;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
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
};
//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))
{
@ -125,6 +157,7 @@ int main()
//float greenValue = (sin(time) / 2.0f) + 0.5f;
//shader->setFloat("ourColour", 0.0f, greenValue, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, textureID);
glBindVertexArray(vertexArrayObject);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

View File

@ -2,8 +2,11 @@
out vec4 FragColor;
in vec3 ourColour;
in vec2 texCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor = vec4(ourColour, 1.0);
FragColor = texture(ourTexture, texCoord) * vec4(ourColour, 1.0);
}

View File

@ -1,11 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColour;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColour;
out vec2 texCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColour = aColour;
texCoord = aTexCoord;
}

View File

@ -0,0 +1,11 @@
#pragma once
// Maybe make a base "Texture" class and make a Texture2D and Texture3D child class. look into what would make more sense
class Texture2D
{
public:
Texture2D();
~Texture2D();
private:
};

View File

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

7987
src/Engine/util/stb_image.h Normal file

File diff suppressed because it is too large Load Diff