finished chapter "Colors"

This commit is contained in:
Daniel 2023-08-07 16:41:17 +02:00
parent 8ffca1f4d2
commit d474246586
8 changed files with 80 additions and 27 deletions

View File

@ -169,6 +169,10 @@
<Image Include="images\container.jpg" /> <Image Include="images\container.jpg" />
<Image Include="images\wall.jpg" /> <Image Include="images\wall.jpg" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="shaders\default\simple.frag" />
<None Include="shaders\default\simple.vert" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -83,4 +83,8 @@
<Filter>Resource Files</Filter> <Filter>Resource Files</Filter>
</Image> </Image>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="shaders\default\simple.vert" />
<None Include="shaders\default\simple.frag" />
</ItemGroup>
</Project> </Project>

View File

@ -15,8 +15,8 @@
#include "vertices.h" #include "vertices.h"
// Continue: https://learnopengl.com/Lighting/Colors // Continue: https://learnopengl.com/Lighting/Basic-Lighting
// Chapter: A lighting scene // Chapter: Not yet started
// //
// TODO: look at project->properties->VC++ Directories-> change everything to the Environment var // TODO: look at project->properties->VC++ Directories-> change everything to the Environment var
// For the "real" Nebulix Engine setup everything so it can be compiled/used with VSCode/CLion and not just VS // For the "real" Nebulix Engine setup everything so it can be compiled/used with VSCode/CLion and not just VS
@ -63,7 +63,8 @@ void ProcessInput(GLFWwindow* window)
cam.ProcessKeyboard(MovementDirection::RIGHT, deltaTime); cam.ProcessKeyboard(MovementDirection::RIGHT, deltaTime);
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
cam.ProcessKeyboard(MovementDirection::UP, deltaTime); cam.ProcessKeyboard(MovementDirection::UP, deltaTime);
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS) if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS
|| glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
cam.ProcessKeyboard(MovementDirection::DOWN, deltaTime); cam.ProcessKeyboard(MovementDirection::DOWN, deltaTime);
} }
@ -147,13 +148,17 @@ int main()
}; };
std::string vertexPath = "shaders/default/default.vert"; std::string lightingVertexPath = "shaders/default/default.vert", lampVertexPath = "shaders/default/simple.vert";
std::string fragmentPath = "shaders/default/default.frag"; std::string lightingFragmentPath = "shaders/default/default.frag", lampFragmentPath = "shaders/default/simple.frag";
std::unique_ptr<Nebulix::Shader> shader; std::shared_ptr<Nebulix::Shader> lighting, lamp;
std::vector<std::shared_ptr<Nebulix::Shader>> allShaders = std::vector<std::shared_ptr<Nebulix::Shader>>();
try try
{ {
shader = std::make_unique<Nebulix::Shader>(vertexPath, fragmentPath); lighting = std::make_shared<Nebulix::Shader>(lightingVertexPath, lightingFragmentPath);
lamp = std::make_shared<Nebulix::Shader>(lampVertexPath, lampFragmentPath);
allShaders.push_back(lighting);
allShaders.push_back(lamp);
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
@ -166,9 +171,16 @@ int main()
//Texture2D containerImage("images/container.jpg"); //Texture2D containerImage("images/container.jpg");
//Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA); //Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA);
std::vector<float> verts(std::begin(vertices_container), std::end(vertices_container)); std::vector<float> verts(std::begin(vertices_cube), std::end(vertices_cube));
std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute(GL_FLOAT, GL_FALSE, 2) }; std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute() };
Nebulix::GameObject cube = Nebulix::GameObject(verts, vertexAttribs); Nebulix::GameObject cube(verts, vertexAttribs);
vec3 lightPosition = glm::vec3(1.2f, 1.0f, 2.0f);
Nebulix::GameObject lightBulb(verts, vertexAttribs, lightPosition, glm::vec3(0.2f));
std::vector<Nebulix::GameObject> allObjects = std::vector<Nebulix::GameObject>();
allObjects.push_back(cube);
allObjects.push_back(lightBulb);
// main loop // main loop
while(!glfwWindowShouldClose(window)) while(!glfwWindowShouldClose(window))
{ {
@ -176,18 +188,31 @@ int main()
deltaTime = currentFrameTime - lastFrameTime; deltaTime = currentFrameTime - lastFrameTime;
lastFrameTime = currentFrameTime; lastFrameTime = currentFrameTime;
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ProcessInput(window); ProcessInput(window);
mat4 projectionMatrix = glm::perspective(glm::radians(cam.Fov), 800.0f / 600.0f, 0.1f, 100.0f); mat4 projectionMatrix = glm::perspective(glm::radians(cam.Fov), 800.0f / 600.0f, 0.1f, 100.0f);
for (size_t i = 0; i < allShaders.size(); i++)
{
auto shader = allShaders[i];
shader->Use(); shader->Use();
shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera
shader->SetMatrix("projectionMatrix", projectionMatrix); shader->SetMatrix("projectionMatrix", projectionMatrix);
shader->SetMatrix("modelMatrix", cube.GetModelMatrix()); shader->SetMatrix("modelMatrix", allObjects[i].GetModelMatrix());
cube.Draw(); }
lighting->Use();
lighting->SetFloat("lightColour", 1.0f, 1.0f, 1.0f);
lighting->SetFloat("objectColour", 1.0f, 0.5f, 0.31f);
for (size_t i = 0; i < allObjects.size(); i++)
{
allShaders[i]->Use();
allObjects[i].Draw();
}
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

View File

@ -40,14 +40,11 @@ namespace Nebulix
{ {
public: public:
glm::vec3 Position; glm::vec3 Position;
glm::vec3 Scale;
GameObject(std::vector<float>& vertices, std::vector<VertexAttribute> vertexAttributes) GameObject(std::vector<float>& vertices, std::vector<VertexAttribute> vertexAttributes,
: vertices{ vertices }, vertexAttributes{ vertexAttributes }, Position{glm::vec3(0.0f)} glm::vec3 position = glm::vec3(0.0f), glm::vec3 scale = glm::vec3(1.0f))
{ : vertices{ vertices }, vertexAttributes{ vertexAttributes }, Position{position}, Scale{scale}
Init();
}
GameObject(std::vector<float> &vertices, std::vector<VertexAttribute> vertexAttributes, glm::vec3& position)
: vertices{vertices}, vertexAttributes{ vertexAttributes }, Position{position}
{ {
Init(); Init();
} }
@ -57,7 +54,7 @@ namespace Nebulix
glDeleteBuffers(1, &vertexBuffer); glDeleteBuffers(1, &vertexBuffer);
} }
// NOTE: This method will only call "Shader.Use()" but will not set any properties. // NOTE: This method will not call "Shader.Use()" but will not set any properties.
// This is because I do not know the names and values of the properties. Most likely this will be possible after learning about materials // This is because I do not know the names and values of the properties. Most likely this will be possible after learning about materials
void Draw() void Draw()
{ {
@ -74,7 +71,9 @@ namespace Nebulix
{ {
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
// TODO: add properties for rotation/scale and add these to the model matrix // TODO: add properties for rotation/scale and add these to the model matrix
return glm::translate(model, Position); model = glm::translate(model, Position);
model = glm::scale(model, Scale);
return model;
} }
private: private:

View File

@ -1,7 +1,9 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
uniform vec3 lightColour, objectColour;
void main() void main()
{ {
FragColor = vec4(1.0); FragColor = vec4(lightColour * objectColour, 1.0);
} }

View File

@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0);
}

View File

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

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
float vertices_light[] = { float vertices_cube[] = {
-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,