Compare commits
2 commits
fe8a38d624
...
c591275bd1
Author | SHA1 | Date | |
---|---|---|---|
c591275bd1 | |||
7eeaa0fa42 |
12 changed files with 133 additions and 36 deletions
BIN
src/awesomeface.png
Normal file
BIN
src/awesomeface.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 58 KiB |
BIN
src/container.jpg
Normal file
BIN
src/container.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 181 KiB |
|
@ -1,8 +1,13 @@
|
||||||
// Set the GLSL version to 3.3 and use the OpenGL core profile
|
// Set the GLSL version to 3.3 and use the OpenGL core profile
|
||||||
#version 330 core
|
#version 330 core
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in vec3 ourColor;
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D texture1;
|
||||||
|
uniform sampler2D texture2;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
FragColor = vec4(ourColor, 1.0);
|
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
|
||||||
}
|
}
|
||||||
|
|
11
src/image.cc
Normal file
11
src/image.cc
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#include "image.hh"
|
||||||
|
#include <stb/stb_image.h>
|
||||||
|
|
||||||
|
Image::Image(std::string path) {
|
||||||
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Image::~Image() {
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
12
src/image.hh
Normal file
12
src/image.hh
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef IMAGE_HH
|
||||||
|
#define IMAGE_HH
|
||||||
|
#include <string>
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
class Image {
|
||||||
|
public:
|
||||||
|
int width, height, nrChannels;
|
||||||
|
unsigned char* data;
|
||||||
|
Image(std::string path);
|
||||||
|
~Image();
|
||||||
|
};
|
||||||
|
#endif
|
103
src/main.cc
103
src/main.cc
|
@ -3,9 +3,11 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "shader.hh"
|
#include "shader.hh"
|
||||||
#include "utilities.hh"
|
#include "utilities.hh"
|
||||||
|
#include "image.hh"
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||||
void process_input(GLFWwindow* window);
|
void process_input(GLFWwindow* window);
|
||||||
|
@ -34,10 +36,10 @@ struct GlRenderObjects {
|
||||||
GlRenderObjects* triangle() {
|
GlRenderObjects* triangle() {
|
||||||
// An array of verticies containing data for a triangle.
|
// An array of verticies containing data for a triangle.
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
// positions // colors
|
// positions // colors // texture coords
|
||||||
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||||
0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f,
|
0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f, 1.0f, 0.0f,
|
||||||
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int vertexBufferObject, vertexArrayObject;
|
unsigned int vertexBufferObject, vertexArrayObject;
|
||||||
|
@ -58,14 +60,18 @@ GlRenderObjects* triangle() {
|
||||||
// This will pass the position attribute to the location "0". We tell OpenGL the size of each position attribute is 3 floats.
|
// This will pass the position attribute to the location "0". We tell OpenGL the size of each position attribute is 3 floats.
|
||||||
// We tell OpenGL that we're using floating point types.
|
// We tell OpenGL that we're using floating point types.
|
||||||
// We tell OpenGL we don't want our data to be normalized as its already normalized.
|
// We tell OpenGL we don't want our data to be normalized as its already normalized.
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
// Pass the color attribute data to the location 1.
|
// Pass the color attribute data to the location 1.
|
||||||
// We tell it to offset the data per "vertex" by 3 floats to get the color data instead of the position data.
|
// We tell it to offset the data per "vertex" by 3 floats to get the color data instead of the position data.
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
// Process the texture coord data.
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
@ -74,11 +80,11 @@ GlRenderObjects* triangle() {
|
||||||
|
|
||||||
GlRenderObjects* rectangle() {
|
GlRenderObjects* rectangle() {
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
// positions // colors
|
// positions // colors // texture coords
|
||||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||||
0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f,
|
0.5f, -0.5f, 0.0f, 0.0f,1.0f, 0.0f, 1.0f, 0.0f,
|
||||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||||
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f
|
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int indices[] = {
|
unsigned int indices[] = {
|
||||||
|
@ -107,13 +113,17 @@ GlRenderObjects* rectangle() {
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
// Tell OpenGL on how to process the verticies.
|
// Tell OpenGL on how to process the verticies.
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
// Process the color data.
|
// Process the color data.
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3*sizeof(float)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3*sizeof(float)));
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
// Process the texture coord data.
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
// Unbind the current buffers and arrays from the state.
|
// Unbind the current buffers and arrays from the state.
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
@ -125,6 +135,7 @@ GlRenderObjects* rectangle() {
|
||||||
int main() {
|
int main() {
|
||||||
glfwInit();
|
glfwInit();
|
||||||
|
|
||||||
|
{
|
||||||
// Set OpenGL version to 3.3.
|
// Set OpenGL version to 3.3.
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
@ -153,10 +164,56 @@ int main() {
|
||||||
std::string currentPath = utilities::getCurrentPath();
|
std::string currentPath = utilities::getCurrentPath();
|
||||||
|
|
||||||
// Create a shader class.
|
// Create a shader class.
|
||||||
Shader* shader = new Shader(std::format("{}/vertex.glsl", currentPath).c_str(), std::format("{}/fragment.glsl", currentPath).c_str());
|
Shader shader(std::format("{}/vertex.glsl", currentPath).c_str(), std::format("{}/fragment.glsl", currentPath).c_str());
|
||||||
|
|
||||||
GlRenderObjects* triangleRenderObjs = triangle();
|
std::unique_ptr<GlRenderObjects> triangleRenderObjs(triangle());
|
||||||
GlRenderObjects* rectangleRenderObjs = rectangle();
|
std::unique_ptr<GlRenderObjects> rectangleRenderObjs(rectangle());
|
||||||
|
|
||||||
|
unsigned int texture1, texture2;
|
||||||
|
|
||||||
|
{
|
||||||
|
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_MIPMAP_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
|
Image texture(utilities::getCurrentPath("container.jpg"));
|
||||||
|
if (texture.data) {
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture.width, texture.height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture.data);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
} else {
|
||||||
|
std::cout << "Failed to load texture." << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
Image texture(utilities::getCurrentPath("awesomeface.png"));
|
||||||
|
|
||||||
|
if (texture.data) {
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.data);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
} else {
|
||||||
|
std::cout << "Failed to load texture." << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// activate the shader
|
||||||
|
shader.activate();
|
||||||
|
|
||||||
|
// tell the shader what texture unit the textures are on
|
||||||
|
shader.setInt("texture1", 0);
|
||||||
|
shader.setInt("texture2", 1);
|
||||||
|
|
||||||
// Create a render loop, which keeps the program open until glfw tells the loop that the window should close.
|
// Create a render loop, which keeps the program open until glfw tells the loop that the window should close.
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
@ -170,8 +227,12 @@ int main() {
|
||||||
// Clear the screen and use the color from the state.
|
// Clear the screen and use the color from the state.
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
// activate the shader
|
glActiveTexture(GL_TEXTURE0);
|
||||||
shader->activate();
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||||
|
glActiveTexture(GL_TEXTURE1);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
||||||
|
|
||||||
|
shader.activate();
|
||||||
|
|
||||||
if (!rectangleToggled) {
|
if (!rectangleToggled) {
|
||||||
// Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies.
|
// Bind the state to use the vertexArrayObject object so OpenGL knows what to do with the verticies.
|
||||||
|
@ -191,11 +252,7 @@ int main() {
|
||||||
// Check if any events are triggered.
|
// Check if any events are triggered.
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Clean up objects from memory.
|
|
||||||
delete triangleRenderObjs;
|
|
||||||
delete rectangleRenderObjs;
|
|
||||||
delete shader;
|
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
sources = [
|
sources = [
|
||||||
'main.cc',
|
'main.cc',
|
||||||
'shader.cc',
|
'shader.cc',
|
||||||
'utilities.cc'
|
'utilities.cc',
|
||||||
|
'image.cc'
|
||||||
]
|
]
|
||||||
|
|
||||||
copy = find_program('cp')
|
copy = find_program('cp')
|
||||||
|
|
||||||
fragment = custom_target('fragment', output: 'fragment.glsl', input: 'fragment.glsl', command: [copy, '@INPUT@', '@OUTPUT@'])
|
fragment = custom_target('fragment', output: 'fragment.glsl', input: 'fragment.glsl', command: [copy, '@INPUT@', '@OUTPUT@'])
|
||||||
vertex = custom_target('vertex', output: 'vertex.glsl', input: 'vertex.glsl', command: [copy, '@INPUT@', '@OUTPUT@'])
|
vertex = custom_target('vertex', output: 'vertex.glsl', input: 'vertex.glsl', command: [copy, '@INPUT@', '@OUTPUT@'])
|
||||||
|
container = custom_target('container', output: 'container.jpg', input: 'container.jpg', command: [copy, '@INPUT@', '@OUTPUT@'])
|
||||||
|
awesomeface = custom_target('awesomeface', output: 'awesomeface.png', input: 'awesomeface.png', command: [copy, '@INPUT@', '@OUTPUT@'])
|
||||||
|
|
||||||
|
executable('main', [sources, fragment, vertex, container, awesomeface], dependencies: [glfw, libepoxy])
|
||||||
executable('main', [sources] + [fragment, vertex], dependencies: [glfw, libepoxy])
|
|
||||||
executable('fixedfunction', ['fixedfunction.cc'], dependencies: [glfw, libepoxy])
|
executable('fixedfunction', ['fixedfunction.cc'], dependencies: [glfw, libepoxy])
|
||||||
|
|
|
@ -94,12 +94,12 @@ void Shader::activate() {
|
||||||
glUseProgram(id);
|
glUseProgram(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setBool(const std::string &name, bool value) const {
|
void Shader::setBool(const char* name, bool value) const {
|
||||||
glUniform1f(glGetUniformLocation(id, name.c_str()), (int)value);
|
glUniform1i(glGetUniformLocation(id, name), (int)value);
|
||||||
}
|
}
|
||||||
void Shader::setInt(const std::string &name, int value) const {
|
void Shader::setInt(const char* name, int value) {
|
||||||
glUniform1f(glGetUniformLocation(id, name.c_str()), value);
|
glUniform1i(glGetUniformLocation(id, name), value);
|
||||||
}
|
}
|
||||||
void Shader::setFloat(const std::string &name, float value) const {
|
void Shader::setFloat(const char* name, float value) const {
|
||||||
glUniform1f(glGetUniformLocation(id, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(id, name), value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,9 @@ class Shader {
|
||||||
// activate the shader.
|
// activate the shader.
|
||||||
void activate();
|
void activate();
|
||||||
// utility uniform functions
|
// utility uniform functions
|
||||||
void setBool(const std::string &name, bool value) const;
|
void setBool(const char* name, bool value) const;
|
||||||
void setInt(const std::string &name, int value) const;
|
void setInt(const char* name, int value) ;
|
||||||
void setFloat(const std::string &name, float value) const;
|
void setFloat(const char* name, float value) const;
|
||||||
private:
|
private:
|
||||||
uint compileShader(std::string code, GLenum type, std::string shaderTypeName);
|
uint compileShader(std::string code, GLenum type, std::string shaderTypeName);
|
||||||
uint compileVertexShader(std::string code);
|
uint compileVertexShader(std::string code);
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
std::string utilities::getCurrentPath() {
|
std::string utilities::getCurrentPath() {
|
||||||
char result[PATH_MAX];
|
char result[PATH_MAX];
|
||||||
|
@ -14,6 +15,10 @@ std::string utilities::getCurrentPath() {
|
||||||
return utilities::combineString(stringVector, '/');
|
return utilities::combineString(stringVector, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string utilities::getCurrentPath(std::string file) {
|
||||||
|
return std::format("{}/{}", getCurrentPath(), file);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> utilities::splitString(std::string string, char delimiter) {
|
std::vector<std::string> utilities::splitString(std::string string, char delimiter) {
|
||||||
size_t last = 0;
|
size_t last = 0;
|
||||||
size_t next = 0;
|
size_t next = 0;
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
namespace utilities {
|
namespace utilities {
|
||||||
std::string getCurrentPath();
|
std::string getCurrentPath();
|
||||||
|
std::string getCurrentPath(std::string file);
|
||||||
std::vector<std::string> splitString(std::string string, char delimiter);
|
std::vector<std::string> splitString(std::string string, char delimiter);
|
||||||
std::string combineString(std::vector<std::string> stringVector, char delimiter);
|
std::string combineString(std::vector<std::string> stringVector, char delimiter);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,13 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec3 aColor;
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec2 aTexCoord;
|
||||||
|
|
||||||
out vec3 ourColor;
|
out vec3 ourColor;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(aPos, 1.0);
|
gl_Position = vec4(aPos, 1.0);
|
||||||
ourColor = aColor;
|
ourColor = aColor;
|
||||||
|
TexCoord = aTexCoord;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue