This commit is contained in:
RochesterX
2026-01-07 14:06:26 -05:00
commit 8b181b34e6
40 changed files with 2014836 additions and 0 deletions

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

0
src/.css Normal file
View File

1140
src/glad.c Normal file

File diff suppressed because it is too large Load Diff

110
src/line.cpp Normal file
View File

@@ -0,0 +1,110 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <stb_image.h>
#include <Line.hpp>
#include <shader.hpp>
#include <texture.hpp>
#include <iostream>
using std::vector;
using glm::vec3;
using glm::mat4;
Line::Line(const vec3 p, const vec3 r, const vec3 s, vector<float> v, vector<unsigned int> i)
{
this->position = p;
this->rotation = r;
this->scale = s;
this->vertices = v;
this->indices = i;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], 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);
}
void Line::draw(Shader s) {
/*mat4 mod = rotationMatrix();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
std::cout << mod[i][j] << "\t";
}
std::cout << std::endl;
}*/
s.setMat4("modelMatrix", &modelMatrix()[0][0]);
s.use();
glBindVertexArray(VAO);
glDrawElements(GL_LINES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
mat4 Line::translationMatrix()
{
return mat4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
position.x, position.y, position.z, 1
);
}
mat4 Line::rotationMatrix()
{
mat4 x(
1, 0, 0, 0,
0, cos(rotation.x), sin(rotation.x), 0,
0, -sin(rotation.x), cos(rotation.x), 0,
0, 0, 0, 1
);
mat4 y(
cos(rotation.y), 0, -sin(rotation.y), 0,
0, 1, 0, 0,
sin(rotation.y), 0, cos(rotation.y), 0,
0, 0, 0, 1
);
mat4 z(
cos(rotation.z), sin(rotation.z), 0, 0,
-sin(rotation.z), cos(rotation.z), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
return z * y * x;
}
mat4 Line::scaleMatrix()
{
return mat4(
scale.x, 0, 0, 0,
0, scale.y, 0, 0,
0, 0, scale.z, 0,
0, 0, 0, 1
);
}
mat4 Line::modelMatrix()
{
return translationMatrix() * rotationMatrix() * scaleMatrix();
}

370
src/main.cpp Normal file
View File

@@ -0,0 +1,370 @@
#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 <stb_image.h>
#include <shader.hpp>
#include <object.hpp>
#include <camera.hpp>
#include <iostream>
#include <vector>
using glm::vec2;
using glm::vec3;
using glm::mat4;
mat4 constructProjectionMatrix(float f, float zFar, float zNear, float aspect);
float f = 75.0f * (PI/180.0f);
float zNear = 0.1f;
float zFar = 1000;
int w = 800;
int h = 600;
vec2 mousePos(w / 2, h / 2);
unsigned int VAO;
unsigned int VBO;
unsigned int EBO;
mat4 projectionMatrix = constructProjectionMatrix(f, zFar, zNear, w / static_cast<float>(h));
vector<float> vertices {
-1, -1, -1, 0, 0, 0,
-1, -1, 1, 0, 0, 1,
1, -1, -1, 1, 0, 0,
1, -1, 1, 1, 0, 1,
-1, 1, -1, 0, 1, 0,
-1, 1, 1, 0, 1, 1,
1, 1, -1, 1, 1, 0,
1, 1, 1, 1, 1, 1,
};
/*
vector<float> darkVertices {
-1, -1, -1, 0, 0, 0,
-1, -1, 1, 0, 0, 0,
1, -1, -1, 0, 0, 0,
1, -1, 1, 0, 0, 0,
-1, 1, -1, 0.05, 0.05, 0.05,
-1, 1, 1, 0.1, 0.1, 0.1,
1, 1, -1, 0.1, 0.1, 0.1,
1, 1, 1, 0.2, 0.2, 0.2,
};
*/
vector<unsigned int> indices {
0, 2, 1,
1, 2, 3,
4, 5, 6,
5, 7, 6,
0, 4, 2,
2, 4, 6,
2, 6, 3,
3, 6, 7,
1, 5, 0,
4, 0, 5,
3, 7, 1,
1, 7, 5,
};
GLFWwindow* window;
Shader shader;
Shader gridShader;
Camera camera;
vector<Object> objects;
bool wireframe = false;
bool cull = true;
const float movementSpeed = 0.1;
const float movementSpeedMultiplier = 0.2;
const float rotationSpeed = 0.02;
const float rotationSpeedMultiplier = 0.2;
const float sensitivity = 0.002f;
bool firstMouse = true;
double timeLastFrame = 0.0;
double deltaTime = 0.0;
bool pressedLastFrame = false;
mat4 constructProjectionMatrix(float f, float zFar, float zNear, float aspect)
{
return mat4(
f / static_cast<float>(aspect), 0, 0, 0,
0, f, 0, 0,
0, 0, (zFar + zNear) / static_cast<float>(zNear - zFar), -1,
0, 0, (2 * zFar * zNear) / static_cast<float>(zNear - zFar), 0
);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
w = width;
h = height;
projectionMatrix = constructProjectionMatrix(f, zFar, zNear, w / static_cast<float>(h));
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
mousePos = vec2(xpos, ypos);
firstMouse = false;
}
vec2 offset = (vec2(xpos, ypos) - mousePos) * sensitivity;
mousePos = vec2(xpos, ypos);
camera.updateAngles(-offset.y, offset.x);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
f -= (float)yoffset;
if (f < 0.1f) f = 0.1f;
if (f > PI / 4) f = PI / 2;
projectionMatrix = constructProjectionMatrix(f, zFar, zNear, w / static_cast<float>(h));
}
void processInput(GLFWwindow *window)
{
float movementSpeedMultiplied = movementSpeed;
float rotationSpeedMultiplied = rotationSpeed;
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
movementSpeedMultiplied *= movementSpeedMultiplier;
rotationSpeedMultiplied *= rotationSpeedMultiplier;
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.movePosition(FORWARD, movementSpeedMultiplied);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.movePosition(BACKWARD, movementSpeedMultiplied);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
camera.movePosition(RIGHT, movementSpeedMultiplied);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
camera.movePosition(LEFT, movementSpeedMultiplied);
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
{
camera.updateAngles(0, rotationSpeedMultiplied);
}
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
{
camera.updateAngles(0, -rotationSpeedMultiplied);
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
{
camera.updateAngles(rotationSpeedMultiplied, 0);
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
camera.updateAngles(-rotationSpeedMultiplied, 0);
}
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS && !pressedLastFrame)
{
wireframe = !wireframe;
glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL);
cull = !cull;
if (cull)
{
glEnable(GL_CULL_FACE);
}
else
{
glDisable(GL_CULL_FACE);
}
}
pressedLastFrame = glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS;
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
cull = !cull;
if (cull)
{
glEnable(GL_CULL_FACE);
}
else
{
glDisable(GL_CULL_FACE);
}
}
}
void createShaderProgram()
{
shader = Shader("/Users/rochesterx/Documents/Programming/OpenGL/src/shaders/vertex.glsl", "/Users/rochesterx/Documents/Programming/OpenGL/src/shaders/fragment.glsl");
gridShader = Shader("/Users/rochesterx/Documents/Programming/OpenGL/src/shaders/gridvertex.glsl", "/Users/rochesterx/Documents/Programming/OpenGL/src/shaders/gridfragment.glsl");
shader.use();
}
void init()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
window = glfwCreateWindow(w, h, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
throw std::runtime_error("Failed to create GLFW window");
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
throw std::runtime_error("Failed to initialize GLAD");
}
glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL);
if (cull)
{
glEnable(GL_CULL_FACE);
}
else
{
glDisable(GL_CULL_FACE);
}
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
createShaderProgram();
Texture texture("assets/container.jpg");
Texture texture2("assets/awesomeface.png");
//Object cube(vec3(0, 0, 0), vec3(0, 0, 0), vec3(1, 1, 1), vertices, indices, texture);
objects = {
//Object(vec3(0, 0, 0), vec3(0, 0, 0), vec3(3, 3, 3), "assets/dragon.obj"),
Object(vec3(0, 4, 0), vec3(0, 0, 0), vec3(1, 1, 1), "assets/cube.obj"),
Object(vec3(0, 0, 0), vec3(0, 0, 0), vec3(3, 3, 3), "assets/sphere.obj"),
Object(vec3(5, 0, 0), vec3(0, 0, 0), vec3(0.01, 0.01, 0.01), "assets/mario.obj"),
Object(vec3(-10, 0, 5), vec3(0, 0, 0), vec3(1, 1, 1), "assets/armadillo_lowres.obj"),
Object(vec3(10, -2, 5), vec3(0, 0, 0), vec3(1, 1, 1), "assets/cubenegative.obj"),
Object(vec3(10, 0, -5), vec3(0, 0, 0), vec3(2, 2, 2), "assets/monkey.obj"),
Object(vec3(-10, 0, -5), vec3(0, 0, 0), vec3(1, 1, 1), "assets/armadillo_midres.obj"),
};
vector<float> xzV {
-100, 0, -100, 1, 1, 1,
-100, 0, 100, 1, 1, 1,
100, 0, -100, 1, 1, 1,
100, 0, 100, 1, 1, 1,
};
vector<unsigned int> xzI {
0, 1, 2,
1, 3, 2,
};
gridShader.use();
shader.use();
timeLastFrame = glfwGetTime();
}
void render()
{
mat4 viewMatrix = camera.viewMatrix();
shader.use();
shader.setMat4("viewMatrix", &viewMatrix[0][0]);
shader.setMat4("projectionMatrix", &projectionMatrix[0][0]);
int count = 0;
for (Object obj : objects)
{
switch (count)
{
case 5:
obj.rotation = vec3(0, timeLastFrame, 0);
break;
case 6:
obj.scale = vec3(0.125 + (7/16.0) - (sin(timeLastFrame * 4) * (7/16.0)), 2 + sin(timeLastFrame * 4), 2 + sin(timeLastFrame * 4));
obj.rotation = vec3(timeLastFrame, timeLastFrame, timeLastFrame);
break;
case 7:
obj.rotation = vec3(timeLastFrame, timeLastFrame, timeLastFrame);
break;
}
shader.setVec3("viewPos", camera.position);
obj.draw(shader);
count++;
}
}
int main()
{
init();
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
glfwSwapBuffers(window);
glfwPollEvents();
deltaTime = glfwGetTime() - timeLastFrame;
timeLastFrame = glfwGetTime();
}
glfwTerminate();
return 0;
}

0
src/object.cpp Normal file
View File

129
src/shader.old Normal file
View File

@@ -0,0 +1,129 @@
#ifndef SHADER_H
#define SHADER_H
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
unsigned int ID;
Shader(const char* vertexPath, const char* fragmentPath)
{
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std:: ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std:: ifstream::badbit);
try
{
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderStream.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
vShaderCode = "#version 330 core"
"layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec3 aColor;"
"out vec3 ourColor;"
"void main()"
"{"
" gl_Position = vec4(aPos, 1.0);"
" ourColor = aColor;"
"}";
std::cout << vShaderCode << std::endl << fShaderCode << std::endl;
unsigned int vertex, fragment;
int success;
char infoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(ID, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
glDeleteShader(vertex);
glDeleteShader(fragment);
}
}
void use()
{
glUseProgram(ID);
}
void setBool(const std::string &name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
}
void setInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
void setFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
};
#endif

42
src/shaders/fragment.glsl Normal file
View File

@@ -0,0 +1,42 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec3 Normal;
in vec3 FragPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform sampler2D texture1;
void main()
{
float ambientStrength = 0.1;
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;
float specularStrength = 0.5;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * objectColor;
FragColor = vec4(result, 1.0);
//FragColor = vec4(ourColor * gl_FragCoord.w, 1);
}

View File

@@ -0,0 +1,13 @@
#version 330 core
out vec4 FragColor;
in vec3 color;
in vec3 position;
void main()
{
FragColor = abs(position.x - round(position.x)) <= 0.1 ? vec4(1, 1, 1, 1) : vec4(0, 0, 0, 0);
FragColor = abs(position.z - round(position.z)) <= 0.1 ? vec4(1, 1, 1, 1) : vec4(0, 0, 0, 0);
}

View File

@@ -0,0 +1,21 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
out vec3 position;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
void main()
{
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
position = aPos;
}

52
src/shaders/orbitv.glsl Normal file
View File

@@ -0,0 +1,52 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 ourColor;
uniform float f;
uniform float aspect;
uniform float zFar;
uniform float zNear;
uniform float theta;
uniform vec3 cameraPosition;
uniform vec3 cameraRotation;
void main()
{
/* mat4 realprojectionMatrix = mat4(
f / aspect, 0, 0, 0,
0, f, 0, 0,
0, 0, (zFar + zNear) / (zNear - zFar), -1,
0, 0, (2 * zFar * zNear) / (zNear - zFar), 0
);
*/
mat4 projectionMatrix = mat4(
3 / (800.0 / 600.0), 0, 0, 0,
0, 3, 0, 0,
0, 0, (10 + 1) / (1 - 10), -1,
0, 0, (2 * 10 * 1) / (1 - 10), 0
);
mat4 cameraRotationMatrix = mat4(
cos(theta), 0, -sin(theta), 0,
0, 1, 0, 0,
sin(theta), 0, cos(theta), 0,
0, 0, 0, 1
);
mat4 cameraPositionMatrix = mat4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
10 * sin(theta), 0, -10 * cos(theta), 1
);
mat4 viewMatrix = cameraRotationMatrix * cameraPositionMatrix;
vec4 projectedVertex = projectionMatrix * cameraRotationMatrix * cameraPositionMatrix * vec4(aPos, 1.0);
gl_Position = projectedVertex / projectedVertex.w;
ourColor = aColor;
}

View File

@@ -0,0 +1,14 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D texture1;
void main()
{
FragColor = texture(texture1, TexCoord);
}

22
src/shaders/vertex.glsl Normal file
View File

@@ -0,0 +1,22 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec3 aNormal;
out vec3 ourColor;
out vec3 Normal;
out vec3 FragPos;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform mat4 normalMatrix;
void main()
{
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPos, 1.0);
FragPos = vec3(modelMatrix * vec4(aPos, 1.0));
ourColor = aColor;
Normal = mat3(normalMatrix) * aNormal;
}

3
src/stb_image.cpp Normal file
View File

@@ -0,0 +1,3 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

7
src/vertex.glsl Normal file
View File

@@ -0,0 +1,7 @@
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}