summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2022-11-21 04:03:29 -0500
committerrealtradam <[email protected]>2022-11-21 04:03:29 -0500
commit35d550df84ede11e7e2cd07c491944222491cd0e (patch)
treef0a41067945bd89d60face7178ed60033b7cdbf1 /src
parente413ffe66c404889a7607eaddb92f37bfaa56b69 (diff)
downloadOgle-35d550df84ede11e7e2cd07c491944222491cd0e.tar.gz
Ogle-35d550df84ede11e7e2cd07c491944222491cd0e.zip
textures :D
Diffstat (limited to 'src')
-rw-r--r--src/compile_flags.txt1
-rw-r--r--src/input.cpp4
-rw-r--r--src/input.hpp (renamed from src/input.h)2
-rw-r--r--src/main.cpp83
-rw-r--r--src/shader.cpp2
-rw-r--r--src/shader.hpp2
-rw-r--r--src/shaders/default.frag9
-rw-r--r--src/shaders/default.vert7
8 files changed, 91 insertions, 19 deletions
diff --git a/src/compile_flags.txt b/src/compile_flags.txt
index 42dbe59..b0924cf 100644
--- a/src/compile_flags.txt
+++ b/src/compile_flags.txt
@@ -1,3 +1,4 @@
-I../glfw/build/include
-I../glad/include
+-I../stb
-I./
diff --git a/src/input.cpp b/src/input.cpp
index ef5f410..827581c 100644
--- a/src/input.cpp
+++ b/src/input.cpp
@@ -1,5 +1,5 @@
-#include "input.h"
-#include <GLFW/glfw3.h>
+#include "input.hpp"
+#include "GLFW/glfw3.h"
void processInput(GLFWwindow *window)
{
diff --git a/src/input.h b/src/input.hpp
index 274489c..39d4e9e 100644
--- a/src/input.h
+++ b/src/input.hpp
@@ -1,4 +1,4 @@
#pragma once
-#include <GLFW/glfw3.h>
+#include "GLFW/glfw3.h"
void processInput(GLFWwindow *window);
diff --git a/src/main.cpp b/src/main.cpp
index 35e4fb6..831fd47 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,8 +1,10 @@
-#include <glad/glad.h>
-#include <GLFW/glfw3.h>
+#include "glad/glad.h"
+#include "GLFW/glfw3.h"
-#include "input.h"
+#include "input.hpp"
#include "shader.hpp"
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
#include <iostream>
#include <cmath>
@@ -10,11 +12,11 @@
//#include <stdio.h>
float vertices[] = {
- // positions // colors
- 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // top right
- 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
- -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
- -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f // top left
+ // positions // colors // texture coords
+ 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top right
+ 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
+ -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, // bottom left
+ -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
@@ -68,11 +70,14 @@ int main() {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// position attribute
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); // configure how the vertex array looks
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); // configure how the vertex array looks
glEnableVertexAttribArray(0);
// color attribute
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); // configure how the vertex array looks
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); // configure how the vertex array looks
glEnableVertexAttribArray(1);
+ //texture attribute
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); // configure how the vertex array looks
+ glEnableVertexAttribArray(2);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // wireframe mode
@@ -82,6 +87,56 @@ int main() {
// std::cout << "Maximum nr of vertex attributes supported: " << nrAttributes << std::endl;
//}
+ // Image/Texture stuffs
+ unsigned int texture1, texture2;
+ glGenTextures(1, &texture1);
+ glBindTexture(GL_TEXTURE_2D, texture1);
+ // set the texture wrapping/filtering options (on the currently bound texture object)
+ 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);
+ // load and generate the texture
+ {
+ int width, height, nrChannels;
+ unsigned char *data = stbi_load("assets/container.jpg", &width, &height, &nrChannels, 0);
+ if (data)
+ {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+ glGenerateMipmap(GL_TEXTURE_2D);
+ }
+ else
+ {
+ std::cout << "Failed to load texture" << std::endl;
+ }
+ stbi_image_free(data);
+ }
+ glGenTextures(2, &texture2);
+ glBindTexture(GL_TEXTURE_2D, texture2);
+ // set the texture wrapping/filtering options (on the currently bound texture object)
+ 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);
+ // load and generate the texture
+ {
+ int width, height, nrChannels;
+ unsigned char *data = stbi_load("assets/awesomeface.png", &width, &height, &nrChannels, 0);
+ if (data)
+ {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ glGenerateMipmap(GL_TEXTURE_2D);
+ }
+ else
+ {
+ std::cout << "Failed to load texture" << std::endl;
+ }
+ stbi_image_free(data);
+ }
+ shader.use();
+ shader.setInt("texture1", 0);
+ shader.setInt("texture2", 1);
+
// game loop
while(!glfwWindowShouldClose(window))
{
@@ -96,10 +151,14 @@ int main() {
float timeValue = glfwGetTime();
float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
- //int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
- //glUniform4f(vertexColorLocation, 1.0f - greenValue, greenValue, (greenValue / 2.0f) + 0.25f, 1.0f);
shader.set4f("ourColor", 1.0f - greenValue, greenValue, (greenValue / 2.0f) + 0.25f, 1.0f);
+ shader.set1f("offset_x", greenValue - 0.5);
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, texture1);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO); // activate the preconfigured settings
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // :)
glBindVertexArray(0);
diff --git a/src/shader.cpp b/src/shader.cpp
index 25b2fdc..08da2a8 100644
--- a/src/shader.cpp
+++ b/src/shader.cpp
@@ -5,7 +5,7 @@
#include <sstream>
#include <iostream>
-#include <glad/glad.h>
+#include "glad/glad.h"
Shader::Shader(const char* vertexPath, const char* fragmentPath)
{
diff --git a/src/shader.hpp b/src/shader.hpp
index 8fba3df..7e4f25d 100644
--- a/src/shader.hpp
+++ b/src/shader.hpp
@@ -2,7 +2,7 @@
#include <string>
-#include <glad/glad.h>
+#include "glad/glad.h"
class Shader
{
diff --git a/src/shaders/default.frag b/src/shaders/default.frag
index 36ed030..17b84a0 100644
--- a/src/shaders/default.frag
+++ b/src/shaders/default.frag
@@ -1,9 +1,16 @@
#version 330 core
out vec4 FragColor;
+
in vec3 ourColor;
+in vec2 TexCoord;
+
+uniform sampler2D texture1;
+uniform sampler2D texture2;
void main()
{
- FragColor = vec4(ourColor, 1.0);
+ vec2 texInvert = vec2(TexCoord.x, -TexCoord.y);
+ FragColor = mix(texture(texture1, texInvert), texture(texture2, texInvert), 0.2);
+ //FragColor = texture(ourTexture, TexCoord) * vec4(ourColor.xyz, 1.0);
}
diff --git a/src/shaders/default.vert b/src/shaders/default.vert
index 2e9b994..5adf795 100644
--- a/src/shaders/default.vert
+++ b/src/shaders/default.vert
@@ -1,11 +1,16 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
+layout (location = 2) in vec2 aTexCoord;
+
+uniform float offset_x;
out vec3 ourColor;
+out vec2 TexCoord;
void main()
{
- gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
+ gl_Position = vec4(aPos.x + offset_x, aPos.yz, 1.0);
ourColor = aColor;
+ TexCoord = aTexCoord;
}