This commit is contained in:
2026-03-07 09:49:00 +10:00
parent 57433d688a
commit 4839dd8eb2
9 changed files with 9879 additions and 0 deletions

4
Makefile Normal file
View File

@@ -0,0 +1,4 @@
run:
g++ *.c *.cpp -o app -lglfw -lGL && ./app
build:
g++ *.c *.cpp -o app -lglfw -lGL

33
Shaders/OG_F.glsl Normal file
View File

@@ -0,0 +1,33 @@
#version 330 core
out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
void main()
{
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
// specular
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);
}

17
Shaders/OG_V.glsl Normal file
View File

@@ -0,0 +1,17 @@
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 texCoords;
layout(location = 2) in vec3 aNormals;
out vec3 FragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec2 TexC;
out vec3 Normal;
void main()
{
FragPos = vec3(model * vec4(aPos,1.0));
Normal = mat3(transpose(inverse(model))) * aNormals;
TexC = vec2(texCoords.x,texCoords.y);
gl_Position = projection * view * vec4(FragPos,1.0);
}

BIN
app Executable file

Binary file not shown.

9466
glad.c Normal file

File diff suppressed because one or more lines are too long

187
main.cpp Normal file
View File

@@ -0,0 +1,187 @@
#include"misc/headers.hpp"
#include "shaderReader.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#define w 1280
#define h 720
void input(GLFWwindow *window);
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,0.0f,-1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,0.0f,-1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,0.0f,-1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,0.0f,-1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.0f,-1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,0.0f,-1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.0f,1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,0.0f,1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -1.0f,0.0f,0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -1.0f,0.0f,0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -1.0f,0.0f,0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -1.0f,0.0f,0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -1.0f,0.0f,0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -1.0f,0.0f,0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,0.0f,0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f,0.0f,0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,0.0f,0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,0.0f,0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,0.0f,0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,0.0f,0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,-1.0f,0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,-1.0f,0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,-1.0f,0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,-1.0f,0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,-1.0f,0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,-1.0f,0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,1.0f,0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,1.0f,0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,1.0f,0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f,1.0f,0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,1.0f,0.0f
};
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f), glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f), glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.5f)};
int main() {
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(w, h, "title", nullptr, nullptr);
glfwMakeContextCurrent(window);
gladLoadGL();
Shader s("Shaders/OG_V.glsl", "Shaders/OG_F.glsl");
s.use();
uint32_t VAO, VBO, EBO, lightVAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE, 8 * sizeof(float),(void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE, 8 * sizeof(float), (void*)(5 * sizeof(float)));
glEnableVertexAttribArray(2);
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
uint32_t tex1;
glGenTextures(1,&tex1);
glBindTexture(GL_TEXTURE_2D,tex1);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load("test.png", &width,&height,&nrChannels,0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "failed to load test.png" << std::endl;
}
stbi_image_free(data);
glEnable(GL_DEPTH_TEST);
s.use();
s.setInt("texture0", 0);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
input(window);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE0, tex1);
s.use();
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
view = glm::translate(view,glm::vec3(0,0,-3));
projection = glm::perspective(glm::radians(45.0f),(float)w/(float)h,0.1f,100.0f);
s.setMat4("view", view);
s.setMat4("projection", projection);
s.setVec3("lightPos", glm::vec3(cos(glfwGetTime()),0,sin(glfwGetTime())));
s.setVec3("lightColor", glm::vec3(1,1,1));
s.setVec3("objectColor",glm::vec3(0.2,0.2,0.2));
s.setVec3("viewPos",glm::vec3(0,0,-3));
glBindVertexArray(VAO);
for (uint16_t i = 0; i < 20; i++)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model,glm::radians(angle),glm::vec3(1,0.3,0.5));
s.setMat4("model",model);
glDrawArrays(GL_TRIANGLES,0,36);
}
s.use();
glBindVertexArray(lightVAO);
glm::mat4 model2 = glm::mat4(1.0f);
model2 = glm::translate(model2, glm::vec3(cos(glfwGetTime()), 0, sin(glfwGetTime())));
s.setMat4("model",model2);
glDrawArrays(GL_TRIANGLES,0,36);
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void input(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}

3
misc/headers.hpp Normal file
View File

@@ -0,0 +1,3 @@
#include<glad/glad.h>
#include<GLFW/glfw3.h>
// this file is to stop helix "autoformatting" the headers

169
shaderReader.hpp Normal file
View File

@@ -0,0 +1,169 @@
#ifndef SHADER_H
#define SHADER_H
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const char* vertexPath, const char* fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
// 2. compile shaders
unsigned int vertex, fragment;
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
// shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
checkCompileErrors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// activate the shader
// ------------------------------------------------------------------------
void use() const
{
glUseProgram(ID);
}
// utility uniform functions
// ------------------------------------------------------------------------
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);
}
// ------------------------------------------------------------------------
void setVec2(const std::string &name, const glm::vec2 &value) const
{
glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec2(const std::string &name, float x, float y) const
{
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
}
// ------------------------------------------------------------------------
void setVec3(const std::string &name, const glm::vec3 &value) const
{
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec3(const std::string &name, float x, float y, float z) const
{
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
}
// ------------------------------------------------------------------------
void setVec4(const std::string &name, const glm::vec4 &value) const
{
glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec4(const std::string &name, float x, float y, float z, float w) const
{
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
}
// ------------------------------------------------------------------------
void setMat2(const std::string &name, const glm::mat2 &mat) const
{
glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat3(const std::string &name, const glm::mat3 &mat) const
{
glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat4(const std::string &name, const glm::mat4 &mat) const
{
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
void checkCompileErrors(GLuint shader, std::string type)
{
GLint success;
GLchar infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
};
#endif

BIN
test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 KiB