lets use opengl instead

This commit is contained in:
Nathan Singer 2025-04-21 22:16:28 -04:00
parent 59e838aabb
commit 48c90cd5bf
24 changed files with 13103 additions and 50 deletions

View File

@ -1,7 +1,8 @@
CC=g++ CC=g++
CFLAGS= -c -g -Wall $(shell pkg-config --cflags gtkmm-4.0) CFLAGS= -c -g -Wall
LDLIBS = $(shell pkg-config --libs gtkmm-4.0)
LDLIBS = -lglfw
TARGET := untap TARGET := untap
@ -9,16 +10,24 @@ BUILD_DIR := ./build
SRC_DIRS := ./src SRC_DIRS := ./src
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp') SRCS := $(shell find $(SRC_DIRS) -name '*.cpp')
SRCS += $(shell find $(SRC_DIRS) -name '*.c')
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
$(BUILD_DIR)/$(TARGET): $(OBJS) $(BUILD_DIR)/$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $@ $(LDLIBS) $(CC) $(OBJS) -o $@ $(LDLIBS)
cp -r $(SRC_DIRS)/shaders $(BUILD_DIR)/
cp -r $(SRC_DIRS)/textures $(BUILD_DIR)/
$(BUILD_DIR)/%.cpp.o: %.cpp $(BUILD_DIR)/%.cpp.o: %.cpp
mkdir -p $(dir $@) mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/%.c.o: %.c
mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: clean .PHONY: clean
clean: clean:
rm -rf $(BUILD_DIR) rm -rf $(BUILD_DIR)

14
build/shaders/shader.fs Normal file
View File

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

15
build/shaders/shader.vs Normal file
View File

@ -0,0 +1,15 @@
#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;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}

BIN
build/src/glad.c.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
build/src/stb_image.cpp.o Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

BIN
build/textures/wg.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

Binary file not shown.

1140
src/glad.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +0,0 @@
#include "include/helloworld.h"
#include <iostream>
HelloWorld::HelloWorld()
: m_button("Hello world!")
{
m_button.set_margin(10);
m_button.signal_clicked().connect(sigc::mem_fun(*this,
&HelloWorld::on_button_clicked));
set_child(m_button);
}
HelloWorld::~HelloWorld()
{
}
void HelloWorld::on_button_clicked()
{
std::cout << "Hello, World!" << std::endl;
}

3611
src/include/glad.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +0,0 @@
#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H
#include <gtkmm/button.h>
#include <gtkmm/window.h>
class HelloWorld : public Gtk::Window
{
public:
HelloWorld();
~HelloWorld() override;
protected:
void on_button_clicked();
Gtk::Button m_button;
};
#endif

114
src/include/shader.h Normal file
View File

@ -0,0 +1,114 @@
#ifndef SHADER_H
#define SHADER_H
#include "glad.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
// program ID
unsigned int ID;
// constructor reads and builds the shader
Shader(const char* vertexPath, const char* fragmentPath)
{
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// setup exceptions for bad files
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 buffers 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_SUCCESFULLY_READ:" << e.what() << "\n";
}
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
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 << "\n";
}
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 << "\n";
}
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 << "\n";
}
// delete shaders, no longer needed
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// use/activate the shader
void use()
{
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);
}
};
#endif

7988
src/include/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,184 @@
#include "include/helloworld.h" #include "include/glad.h"
#include "include/shader.h"
#include "include/stb_image.h"
#include <gtkmm/application.h> #include <GLFW/glfw3.h>
#include <iostream>
#include <math.h>
int main(int argc, char* argv[]) void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void setupGLFW(GLFWwindow** window);
typedef struct {
int width, height, nrChannels;
unsigned char* data;
} texture_t;
float vertices[] = {
// positions colours texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // bottom left
};
unsigned int indices[] = {
0, 1, 3,
1, 2, 3
};
bool wireframe = false;
int main()
{ {
auto app = Gtk::Application::create("org.gtkmm.example"); GLFWwindow* window;
setupGLFW(&window);
Shader myShader("shaders/shader.vs", "shaders/shader.fs");
texture_t tex;
tex.data = stbi_load("textures/container.jpg", &tex.width, &tex.height, &tex.nrChannels, 0);
unsigned int texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
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);
if (tex.data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Failed to load texture\n";
}
stbi_image_free(tex.data);
stbi_set_flip_vertically_on_load(true);
tex.data = stbi_load("textures/wg.jpg", &tex.width, &tex.height, &tex.nrChannels, 0);
unsigned int texture2;
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
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);
if (tex.data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Failed to load texture\n";
}
stbi_image_free(tex.data);
unsigned int VAO;
glGenVertexArrays(1, &VAO);
unsigned int VBO;
glGenBuffers(1, &VBO);
unsigned int EBO;
glGenBuffers(1, &EBO);
// bind Vertex Array Object
glBindVertexArray(VAO);
// copy our vertices into a buffer for opengl to use
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// copy our indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// set the vertex attributes pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) (3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) (6 * sizeof(float)));
glEnableVertexAttribArray(2);
myShader.use();
myShader.setInt("texture1", 0);
myShader.setInt("texture2", 1);
while(!glfwWindowShouldClose(window)) {
//input
processInput(window);
//rendering
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
myShader.use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); // unbinds the VAO so its ready to be bound again for the next render
// check and call events and swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void setupGLFW(GLFWwindow** window)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
*window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window\n";
glfwTerminate();
exit(-1);
}
glfwMakeContextCurrent(*window);
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "Failed to init GLAD\n";
exit(-1);
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(*window, framebuffer_size_callback);
}
void framebuffer_size_callback(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);
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) // triggers wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) // disables wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
return app->make_window_and_run<HelloWorld>(argc, argv);
} }

14
src/shaders/shader.fs Normal file
View File

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

15
src/shaders/shader.vs Normal file
View File

@ -0,0 +1,15 @@
#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;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}

2
src/stb_image.cpp Normal file
View File

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "include/stb_image.h"

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

BIN
src/textures/container.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

BIN
src/textures/wg.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB