Textures (not working)

pull/2/head
E. Almqvist 2 years ago
parent 7f74a78a82
commit fa59c32e24
  1. 15
      headers/textures.hpp
  2. 9
      shaders/fragment.glsl
  3. 7
      shaders/vertex.glsl
  4. 41
      src/main.cpp
  5. 2
      src/shaders.cpp
  6. 46
      src/textures.cpp

@ -1,8 +1,17 @@
#ifndef TEXTURES_HPP
#define TEXTURES_HPP
#define STB_IMAGE_IMPLEMENTATION
#include "../extern/stb/stb_image.h"
namespace Textures {
class Texture2D {
public:
unsigned int id;
Texture2D(const char* t_src);
void load();
void use();
private:
const char* texture_src;
int width, height, nrChannels;
};
}
#endif

@ -1,11 +1,16 @@
#version 330 core
out vec4 FragColor;
in vec4 vertexColor;
in vec4 VertexColor;
in vec2 TexCoord;
uniform sampler2D inpTexture;
uniform float r;
uniform float g;
uniform float b;
void main() {
FragColor = vertexColor + vec4(r, g, b, 1.0f);
FragColor = texture(inpTexture, TexCoord) * (VertexColor + vec4(r, g, b, 1.0f));
}

@ -1,10 +1,13 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec4 vertexColor;
out vec4 VertexColor;
out vec2 TexCoord;
void main() {
gl_Position = vec4(aPos, 1.0);
vertexColor = vec4(aColor, 1.0);
VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord;
}

@ -5,6 +5,7 @@
#include <math.h>
#include "../headers/shaders.hpp"
#include "../headers/textures.hpp"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
@ -28,11 +29,10 @@ void processInput(GLFWwindow *win) {
struct RenderObj {
unsigned int EBO;
Shaders::Shader shader;
Textures::Texture2D texture;
};
RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count, float verts[], unsigned int verts_count) {
Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE);
// Vertex buffer object
unsigned int VBO;
glGenBuffers(1, &VBO);
@ -47,36 +47,49 @@ RenderObj preRenderCallback(unsigned int indices[], unsigned int indices_count,
glBufferData(GL_ARRAY_BUFFER, verts_count, verts, GL_DYNAMIC_DRAW); // for moving stuff
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_count, indices, GL_STATIC_DRAW);
// Set attrib pointers
#define VERTEX_ATTRIB_PTR_SIZE 8 * sizeof(float)
// Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0);
glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int EBO;
glGenBuffers(1, &EBO);
// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_count, indices, GL_STATIC_DRAW);
return RenderObj {EBO, shader};
Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE);
Textures::Texture2D texture(RUSTY_METAL_TEXTURE);
return RenderObj {EBO, shader, texture};
}
void renderCallback(RenderObj ro) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f;
ro.shader.use();
ro.shader.setFloat("r", gVal);
ro.shader.setFloat("g", gVal);
ro.shader.setFloat("b", gVal);
ro.texture.use();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
@ -123,14 +136,6 @@ int main() {
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
// Textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
// "blending"
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

@ -38,7 +38,7 @@ namespace Shaders {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, infolog);
printf("Shader Compilation Error:\n%s\n", infolog);
printf("[%s] Shader Compilation Error:\n%s\n", shaderSource, infolog);
}
return shader;

@ -0,0 +1,46 @@
#include <stdio.h>
#include "../headers/textures.hpp"
#include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION
#include "../extern/stb/stb_image.h"
namespace Textures {
Texture2D::Texture2D(const char* t_src) {
texture_src = t_src;
// Bind this texture
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
// Repeat/bounded etc
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
// Mipmap "blending"
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load texture source etc
load();
}
void Texture2D::load() {
printf("[%i] \"%s\" - ", id, texture_src);
unsigned char* data = stbi_load(texture_src, &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);
printf("Loaded!\n");
} else {
printf("[ERROR] Failed to load texture \"%s\"\n", texture_src);
}
stbi_image_free(data);
}
void Texture2D::use() {
glBindTexture(GL_TEXTURE_2D, id);
}
}
Loading…
Cancel
Save