Prepared everything for the lighting section

This commit is contained in:
Daniel 2023-08-07 12:50:48 +02:00
parent 42db40144e
commit e874dae218
6 changed files with 104 additions and 84 deletions

View File

@ -162,6 +162,7 @@
<ClInclude Include="textures\Texture2D.h" />
<ClInclude Include="util\property.h" />
<ClInclude Include="util\stb_image.h" />
<ClInclude Include="vertices.h" />
</ItemGroup>
<ItemGroup>
<Image Include="images\awesomeface.png" />

View File

@ -62,6 +62,9 @@
<ClInclude Include="object\game_object.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="vertices.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Shaders\default\default.frag" />

View File

@ -13,6 +13,8 @@
#include "util/camera/camera.h"
#include "object/game_object.h"
#include "vertices.h"
// Continue: https://learnopengl.com/Lighting/Colors
// Chapter: A lighting scene
//
@ -123,52 +125,6 @@ int main()
Configure(window);
float vertices[] = { // object coordinates, uv-coordinates
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute(GL_FLOAT, GL_FALSE, 2) };
GLuint faces[]{
0, 1, 3,
1, 2, 3
@ -196,7 +152,7 @@ int main()
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_container), vertices_container, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW);
@ -225,10 +181,11 @@ int main()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// TEXTURES
Texture2D containerImage("images/container.jpg");
Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA);
//Texture2D containerImage("images/container.jpg");
//Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA);
std::vector<float> verts(std::begin(vertices), std::end(vertices));
std::vector<float> verts(std::begin(vertices_container), std::end(vertices_container));
std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute(GL_FLOAT, GL_FALSE, 2) };
Nebulix::GameObject cube = Nebulix::GameObject(verts, vertexAttribs);
// main loop
while(!glfwWindowShouldClose(window))
@ -245,26 +202,10 @@ int main()
mat4 projectionMatrix = glm::perspective(glm::radians(cam.Fov), 800.0f / 600.0f, 0.1f, 100.0f);
shader->Use();
shader->SetInt("ourTexture1", 0);
shader->SetInt("ourTexture2", 1);
shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera
shader->SetMatrix("projectionMatrix", projectionMatrix);
containerImage.BindTexture();
faceImage.BindTexture(GL_TEXTURE0 + 1); // GL_TEXTURE0 + 1 == GL_TEXTURE1, this means looping over multiple texture units is easily possible
for (int i = 0; i < 10; i++)
{
cube.Position = cubePositions[i];
mat4 model = cube.GetModelMatrix();
float angle = (20.0f * i);
if (i % 3 == 0)
angle = currentFrameTime * 25;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
shader->SetMatrix("modelMatrix", model);
cube.Draw();
}
shader->SetMatrix("modelMatrix", cube.GetModelMatrix());
cube.Draw();
glfwSwapBuffers(window);
glfwPollEvents();

View File

@ -1,13 +1,7 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColour;
in vec2 texCoord;
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;
void main()
{
FragColor = mix(texture(ourTexture1, texCoord), texture(ourTexture2, texCoord), 0.2) * vec4(ourColour, 1.0);
FragColor = vec4(1.0);
}

View File

@ -1,21 +1,12 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec3 ourColour;
out vec2 texCoord;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
//uniform mat4 transform;
void main()
{
// note that we read the multiplication from right to left
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPos, 1.0);
//gl_Position = transform * vec4(aPos, 1.0);
ourColour = vec3(1.0);
texCoord = aTexCoord;
}

90
src/Engine/vertices.h Normal file
View File

@ -0,0 +1,90 @@
#pragma once
float vertices_light[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f
};
float vertices_container[] = { // object coordinates, uv-coordinates
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 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