lighting and imgui
This commit is contained in:
4
.gitmodules
vendored
Normal file
4
.gitmodules
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
[submodule "imgui"]
|
||||
path = imgui
|
||||
url = https://github.com/ocornut/imgui.git
|
||||
branch = docking
|
||||
@@ -71,7 +71,7 @@
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<IncludePath>C:\Development Projects\LearningOpenGL\Include\glm;C:\Development Projects\LearningOpenGL\Include\glfw-3.4.bin.WIN64\include;C:\Development Projects\LearningOpenGL\Include\glad\include;$(IncludePath)</IncludePath>
|
||||
<IncludePath>C:\Development Projects\LearningOpenGL\imgui\backends;C:\Development Projects\LearningOpenGL\imgui;C:\Development Projects\LearningOpenGL\Include\glm;C:\Development Projects\LearningOpenGL\Include\glfw-3.4.bin.WIN64\include;C:\Development Projects\LearningOpenGL\Include\glad\include;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>C:\Development Projects\LearningOpenGL\Include\glfw-3.4.bin.WIN64\lib-vc2022;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
@@ -137,10 +137,17 @@
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="cube.frag" />
|
||||
<None Include="cube.vert" />
|
||||
<None Include="fragment.frag" />
|
||||
<None Include="light.frag" />
|
||||
<None Include="light.vert" />
|
||||
<None Include="vertex.vert" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="camera.h" />
|
||||
<ClInclude Include="cube.h" />
|
||||
<ClInclude Include="cubeFactory.h" />
|
||||
<ClInclude Include="shader.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
||||
@@ -25,10 +25,23 @@
|
||||
<ItemGroup>
|
||||
<None Include="fragment.frag" />
|
||||
<None Include="vertex.vert" />
|
||||
<None Include="cube.vert" />
|
||||
<None Include="cube.frag" />
|
||||
<None Include="light.vert" />
|
||||
<None Include="light.frag" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="shader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="cube.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="camera.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="cubeFactory.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
115
camera.h
Normal file
115
camera.h
Normal file
@@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
enum Camera_Movement
|
||||
{
|
||||
FORWARD,
|
||||
BACKWARD,
|
||||
LEFT,
|
||||
RIGHT
|
||||
};
|
||||
|
||||
const float YAW = -90.0f;
|
||||
const float PITCH = 0.0f;
|
||||
const float SPEED = 2.5f;
|
||||
const float SENSITIVITY = 0.1f;
|
||||
const float ZOOM = 45.0f;
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Front;
|
||||
glm::vec3 Up;
|
||||
glm::vec3 Right;
|
||||
glm::vec3 WorldUp;
|
||||
|
||||
float Yaw;
|
||||
float Pitch;
|
||||
|
||||
float MovementSpeed;
|
||||
float MouseSensitivity;
|
||||
float Zoom;
|
||||
|
||||
Camera(
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
float yaw = YAW,
|
||||
float pitch = PITCH)
|
||||
:
|
||||
Front(glm::vec3(0.0f, 0.0f, -1.0f)),
|
||||
MovementSpeed(SPEED),
|
||||
MouseSensitivity(SENSITIVITY),
|
||||
Zoom(ZOOM)
|
||||
{
|
||||
Position = position;
|
||||
WorldUp = up;
|
||||
Yaw = yaw;
|
||||
Pitch = pitch;
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
glm::mat4 GetViewMatrix()
|
||||
{
|
||||
return glm::lookAt(Position, Position + Front, Up);
|
||||
}
|
||||
|
||||
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
|
||||
{
|
||||
float velocity = MovementSpeed * deltaTime;
|
||||
if (direction == FORWARD)
|
||||
Position += Front * velocity;
|
||||
if (direction == BACKWARD)
|
||||
Position -= Front * velocity;
|
||||
if (direction == LEFT)
|
||||
Position -= Right * velocity;
|
||||
if (direction == RIGHT)
|
||||
Position += Right * velocity;
|
||||
}
|
||||
|
||||
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
|
||||
{
|
||||
xoffset *= MouseSensitivity;
|
||||
yoffset *= MouseSensitivity;
|
||||
|
||||
Yaw += xoffset;
|
||||
Pitch += yoffset;
|
||||
|
||||
if (constrainPitch)
|
||||
{
|
||||
if (Pitch > 89.0f)
|
||||
Pitch = 89.0f;
|
||||
if (Pitch < -89.0f)
|
||||
Pitch = -89.0f;
|
||||
}
|
||||
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void ProcessMouseScroll(float yoffset)
|
||||
{
|
||||
Zoom -= (float)yoffset;
|
||||
if (Zoom < 1.0f)
|
||||
Zoom = 1.0f;
|
||||
if (Zoom > 45.0f)
|
||||
Zoom = 45.0f;
|
||||
}
|
||||
private:
|
||||
void updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
front.y = sin(glm::radians(Pitch));
|
||||
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
|
||||
Front = glm::normalize(front);
|
||||
Right = glm::normalize(glm::cross(Front, WorldUp));
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
23
cube.frag
Normal file
23
cube.frag
Normal file
@@ -0,0 +1,23 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 objectColor;
|
||||
uniform vec3 lightColor;
|
||||
uniform vec3 lightPos;
|
||||
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
|
||||
void main()
|
||||
{
|
||||
float ambientStrength = 0.1f;
|
||||
vec3 ambient = ambientStrength * lightColor;
|
||||
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(lightPos - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * lightColor;
|
||||
|
||||
vec3 result = (ambient + diffuse) * objectColor;
|
||||
FragColor = vec4(result, 1.0f);
|
||||
}
|
||||
100
cube.h
Normal file
100
cube.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef CUBE_H
|
||||
#define CUBE_H
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
class Cube
|
||||
{
|
||||
private:
|
||||
glm::vec3 position;
|
||||
glm::vec3 scale;
|
||||
glm::vec3 rotation;
|
||||
glm::vec3 color;
|
||||
|
||||
//const float CUBE_COLORS[24]{
|
||||
// 1.0f, 0.0f, 0.0f,
|
||||
// 0.0f, 1.0f, 0.0f,
|
||||
// 0.0f, 0.0f, 1.0f,
|
||||
// 1.0f, 1.0f, 0.0f,
|
||||
// 0.0f, 1.0f, 1.0f,
|
||||
// 1.0f, 0.0f, 1.0f,
|
||||
// 1.0f, 1.0f, 1.0f,
|
||||
// 0.0f, 0.0f, 0.0f
|
||||
//};
|
||||
|
||||
public:
|
||||
|
||||
Cube(glm::vec3 position, glm::vec3 scale, glm::vec3 rotation, glm::vec3 color)
|
||||
{
|
||||
this->position = position;
|
||||
this->scale = scale;
|
||||
this->rotation = rotation;
|
||||
this->color = color;
|
||||
}
|
||||
|
||||
//void draw(unsigned int& modelLoc, unsigned int& VAO)
|
||||
//{
|
||||
// glBindVertexArray(VAO);
|
||||
// glm::mat4 model = getModelMatrix();
|
||||
// glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
|
||||
// glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); // 36 indices for the cube
|
||||
// glBindVertexArray(0);
|
||||
//}
|
||||
|
||||
glm::mat4 getModelMatrix() const
|
||||
{
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, position);
|
||||
model = glm::rotate(model, glm::radians(rotation.x), glm::vec3(1, 0, 0));
|
||||
model = glm::rotate(model, glm::radians(rotation.y), glm::vec3(0, 1, 0));
|
||||
model = glm::rotate(model, glm::radians(rotation.z), glm::vec3(0, 0, 1));
|
||||
model = glm::scale(model, scale);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
void setPosition(glm::vec3 position)
|
||||
{
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
void setScale(glm::vec3 scale)
|
||||
{
|
||||
this->scale = scale;
|
||||
}
|
||||
|
||||
void setRotation(glm::vec3 rotation)
|
||||
{
|
||||
this->rotation = rotation;
|
||||
}
|
||||
|
||||
void setColor(glm::vec3 color)
|
||||
{
|
||||
this->color = color;
|
||||
}
|
||||
|
||||
glm::vec3 getPosition() const
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
glm::vec3 getScale() const
|
||||
{
|
||||
return scale;
|
||||
}
|
||||
|
||||
glm::vec3 getRotation() const
|
||||
{
|
||||
return rotation;
|
||||
}
|
||||
|
||||
glm::vec3 getColor() const
|
||||
{
|
||||
return color;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
19
cube.vert
Normal file
19
cube.vert
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
|
||||
gl_Position = projection * view * vec4(FragPos, 1.0);
|
||||
}
|
||||
162
cubeFactory.h
Normal file
162
cubeFactory.h
Normal file
@@ -0,0 +1,162 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef CUBE_FACTORY_H
|
||||
#define CUBE_FACTORY_H
|
||||
|
||||
#include <list>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <shader.h>
|
||||
#include <cube.h>
|
||||
|
||||
class CubeFactory
|
||||
{
|
||||
private:
|
||||
Shader shader;
|
||||
|
||||
unsigned int VAO, VBO, EBO;
|
||||
|
||||
std::list<Cube> cubes;
|
||||
|
||||
const unsigned int CUBE_INDICES[36]{
|
||||
0, 1, 2, 0, 2, 3, // Front face
|
||||
4, 5, 6, 4, 6, 7, // Back face
|
||||
0, 1, 5, 0, 5, 4, // Bottom face
|
||||
3, 2, 6, 3, 6, 7, // Top face
|
||||
0, 3, 7, 0, 7, 4, // Left face
|
||||
1, 2, 6, 1, 6, 5 // Right face
|
||||
};
|
||||
|
||||
void setupBuffers()
|
||||
{
|
||||
float CUBE_VERTICES[] = {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(CUBE_VERTICES), CUBE_VERTICES, GL_STATIC_DRAW);
|
||||
|
||||
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CUBE_INDICES), CUBE_INDICES, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
//glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
public:
|
||||
CubeFactory() : shader (Shader("cube.vert", "cube.frag"))
|
||||
{
|
||||
std::cout << "Setting up cube factory" << std::endl;
|
||||
setupBuffers();
|
||||
//std::cout << VAO << " " << VBO << " " << EBO << std::endl;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteBuffers(1, &EBO);
|
||||
}
|
||||
|
||||
void addCube(glm::vec3 position, glm::vec3 scale, glm::vec3 rotation, glm::vec3 color)
|
||||
{
|
||||
cubes.push_back(Cube(position, scale, rotation, color));
|
||||
}
|
||||
|
||||
void render(glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float deltaTime)
|
||||
{
|
||||
//std::cout << "Rendering cubes" << std::endl;
|
||||
shader.use();
|
||||
shader.setMat4("projection", projection);
|
||||
shader.setMat4("view", view);
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
for (Cube& cube : cubes)
|
||||
{
|
||||
//std::cout << "Cube position: (" << cube.getColor().x << "," << cube.getColor().y << "," << cube.getColor().z << ")" << std::endl;
|
||||
cube.setRotation(cube.getRotation() + (glm::vec3(5.0f, 10.0f, 20.0f))*deltaTime);
|
||||
glm::mat4 model = cube.getModelMatrix();
|
||||
glm::vec3 color = cube.getColor();
|
||||
shader.setMat4("model", model);
|
||||
shader.setVec3("objectColor", color);
|
||||
shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
||||
shader.setVec3("lightPos", lightPos);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
|
||||
//shader.setVec3("objectColor", 0.0f, 0.0f, 0.0f);
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
//glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
}
|
||||
|
||||
std::list<Cube> getCubes()
|
||||
{
|
||||
return cubes;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,8 +1,10 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
in vec3 ourColor;
|
||||
|
||||
uniform vec3 objectColor;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(ourColor, 1.0f);
|
||||
FragColor = vec4(objectColor * lightColor, 1.0f);
|
||||
}
|
||||
1
imgui
Submodule
1
imgui
Submodule
Submodule imgui added at 22503bfe75
7
light.frag
Normal file
7
light.frag
Normal file
@@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0); // set all 4 vector values to 1.0
|
||||
}
|
||||
12
light.vert
Normal file
12
light.vert
Normal file
@@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
||||
|
||||
199
main.cpp
199
main.cpp
@@ -1,40 +1,49 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <shader.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_glfw.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
|
||||
#include <camera.h>
|
||||
#include <cubeFactory.h>
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void processInput(GLFWwindow* window);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
|
||||
int SCR_WIDTH = 800;
|
||||
int SCR_HEIGHT = 800;
|
||||
|
||||
float triangleVertices[] = {
|
||||
-0.5f,-0.5f,0.0f, 1.0f,0.0f,0.0f,
|
||||
0.5f,-0.5f,0.0f, 0.0f,1.0f,0.0f,
|
||||
0.0f, 0.5f,0.0f, 0.0f,0.0f,1.0f
|
||||
};
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 20.0f));
|
||||
float lastX = SCR_WIDTH / 2.0f;
|
||||
float lastY = SCR_HEIGHT / 2.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
float squareVertices[] = {
|
||||
-0.8f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
-0.8f, 0.8f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
-1.0f, 0.8f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float elapsedTime = 0.0f;
|
||||
|
||||
unsigned int indices[] = {
|
||||
0,1,3,
|
||||
1,2,3
|
||||
};
|
||||
//glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
||||
|
||||
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
@@ -46,106 +55,128 @@ int main() {
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Shader ourShader("vertex.vert", "fragment.frag");
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
|
||||
std::cout << "Loaded GLFW context, OpenGL 3.3" << std::endl;
|
||||
|
||||
unsigned int VAO_1, VAO_2, VBO_1, VBO_2, EBO;
|
||||
glGenVertexArrays(1, &VAO_1);
|
||||
glGenVertexArrays(1, &VAO_2);
|
||||
glGenBuffers(1, &VBO_1);
|
||||
glGenBuffers(1, &VBO_2);
|
||||
glGenBuffers(1, &EBO);
|
||||
CubeFactory cubeFactory;
|
||||
|
||||
glBindVertexArray(VAO_1);
|
||||
float offset = 2.0f;
|
||||
int xAmount = 10;
|
||||
int yAmount = 10;
|
||||
int zAmount = 10;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO_1);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
for (int x = 0; x < xAmount; x++)
|
||||
{
|
||||
for (int y = 0; y < yAmount; y++)
|
||||
{
|
||||
for (int z = 0; z < zAmount; z++)
|
||||
{
|
||||
glm::vec3 position = glm::vec3( (x * offset) - (xAmount * offset / 2.0f),
|
||||
(y * offset) - (yAmount * offset / 2.0f),
|
||||
(z * offset) - (zAmount * offset / 2.0f));
|
||||
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 color = glm::vec3(x/10.0f, y/10.0f, z/10.0f);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
cubeFactory.addCube(position, scale, rotation, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glBindVertexArray(VAO_2);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO_2);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
//std::cout << "Amount of cubes: " << cubeFactory.getCubes().size() << std::endl;
|
||||
|
||||
float r = 0.0f, g = 0.0f, b = 0.0f;
|
||||
float r = 0.05f, g = 0.05f, b = 0.05f;
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
//timing
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
elapsedTime += deltaTime;
|
||||
|
||||
//input
|
||||
processInput(window);
|
||||
|
||||
//rendering
|
||||
if (r < 1) {
|
||||
r = r + 0.01f;
|
||||
}
|
||||
else if (g < 1) {
|
||||
g = g + 0.01f;
|
||||
}
|
||||
else if (b < 1) {
|
||||
b = b + 0.01f;
|
||||
}
|
||||
else {
|
||||
r = 0;
|
||||
g = 0;
|
||||
b = 0;
|
||||
}
|
||||
|
||||
glClearColor(r, g, b, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
//transforms
|
||||
glm::mat4 defaultTransform = glm::mat4(1.0f);
|
||||
glm::mat4 rotatedTransform = glm::rotate(defaultTransform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
// view/projection transformations
|
||||
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
|
||||
//draw
|
||||
ourShader.use();
|
||||
unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
|
||||
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(rotatedTransform));
|
||||
glBindVertexArray(VAO_1);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glm::vec3 lightPos = glm::vec3(100.2f, 100.0f, 200.0f);
|
||||
|
||||
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(defaultTransform));
|
||||
glBindVertexArray(VAO_2);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
cubeFactory.render(view, projection, lightPos, deltaTime);
|
||||
|
||||
//check and call
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
//cubeFactory.clear();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window) {
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
}
|
||||
|
||||
const float cameraSpeed = 5.0f * deltaTime;
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
//std::cout << "Mouse callback" << std::endl;
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos;
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
10
vertex.vert
10
vertex.vert
@@ -1,13 +1,11 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
|
||||
out vec3 ourColor;
|
||||
|
||||
uniform mat4 transform;
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = transform * vec4(aPos, 1.0);
|
||||
ourColor = aColor;
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user