Got libraries installed and started working the boilerplate code. This code currently sucks, but it is more to make sure everything is working before I put a bunch of time into making a good system.

This commit is contained in:
nc5432 2025-05-23 15:35:55 -04:00
parent b2bba2d11f
commit 537e0b71d8
8 changed files with 139 additions and 1 deletions

View File

@ -1,3 +1,5 @@
# learn-opengl
I want to learn more about opengl, so here we go
I want to learn more about opengl, so here we go.
Learning from https://learnopengl.com

View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

BIN
learning Executable file

Binary file not shown.

112
learning.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;
const char *VERTEX_SHADER = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main(){\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *FRAG_SHADER = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main(){\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0";
void resizeWindow(GLFWwindow *window, int width, int height){
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window){
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
glfwSetWindowShouldClose(window, true);
}
}
int main(){
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Learning OpenGL", NULL, NULL);
if (window == NULL){
write(1, "Failed to create GLFW window\n", 29);
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)){
write(1, "Failed to initialize GLAD\n", 26);
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, resizeWindow);
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &VERTEX_SHADER, NULL);
glCompileShader(vertexShader);
int success;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success){
char infoLog[512];
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR: Vertex shader compilation failed\n" << infoLog;
}
unsigned int fragmentShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(fragmentShader, 1, &FRAG_SHADER, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success){
char infoLog[512];
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR: Fragment shader compilation failed\n" << infoLog;
}
unsigned int shader;
shader = glCreateProgram();
glAttachShader(shader, vertexShader);
glAttachShader(shader, fragmentShader);
glLinkProgram(shader);
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success){
char infoLog[512];
glGetProgramInfoLog(shader, 512, NULL, infoLog);
std::cout << "ERROR: Failed to link shader\n" << infoLog;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
while (!glfwWindowShouldClose(window)){
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}

BIN
lib/glad.o Normal file

Binary file not shown.

4
makefile Normal file
View File

@ -0,0 +1,4 @@
all:
gcc -Wall learning.cpp -o learning lib/glad.o -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl
clean:
rm -f learning

6
shaders/default.frag Normal file
View File

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

6
shaders/default.vert Normal file
View File

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