Commit 58ac9e22 authored by Sadman Kazi's avatar Sadman Kazi 🎸

Add texture loading support

parent 7105c87d
Pipeline #120 passed with stage
in 3 minutes and 42 seconds
#version 330
in vec4 color;
in vec2 texCoord0;
out vec4 fragColor;
uniform sampler2D u_diffuse;
void main () {
fragColor = color;
fragColor = texture2D(u_diffuse, texCoord0.xy);
}
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
out vec4 color;
out vec2 texCoord0;
uniform mat4 u_transform;
void main () {
color = vec4(clamp(position, 0.0, 1.0), 1.0);
gl_Position = u_transform * vec4(1.5*position, 1.0);
texCoord0 = texCoord;
}
......@@ -20,10 +20,10 @@
Game::Game()
{
Vertex data[] = { Vertex(glm::vec3(-0.5, -0.5, 0)),
Vertex(glm::vec3(0, 0.5, 0)),
Vertex(glm::vec3(0.5, -0.5, 0)),
Vertex(glm::vec3(0, -0.5, 0.5))};
Vertex data[] = { Vertex(glm::vec3(-0.5, -0.5, 0), glm::vec2(0, 0)),
Vertex(glm::vec3(0, 0.5, 0), glm::vec2(0,0)),
Vertex(glm::vec3(0.5, -0.5, 0),glm::vec2(0,0)),
Vertex(glm::vec3(0, -0.5, 0.5),glm::vec2(0,0))};
unsigned int indices[] = {0, 1, 3,
3, 1, 2,
......@@ -32,9 +32,11 @@ Game::Game()
m_shader = new Shader("../res/basicShader");
//mesh = new Mesh(data, sizeof(data)/sizeof(data[0]), indices, sizeof(indices)/sizeof(indices[0]));
//m_mesh = new Mesh(data, sizeof(data)/sizeof(data[0]), indices, sizeof(indices)/sizeof(indices[0]));
m_mesh = new Mesh("../res/monkey.obj");
m_texture = new Texture("../res/checker_pattern.jpg");
m_transform = new Transform();
m_camera = new Camera(glm::vec3(0.0f, 0.0f, -2.0f), 70.0f, (float)WIDTH/HEIGHT, 1.0f, 100.0f);
......@@ -59,6 +61,7 @@ void Game::input() {
void Game::render() {
m_shader->bind();
m_texture->bind(0);
m_mesh->draw();
/*if (Input::getKeyDown(SDLK_UP)) {
std::cout << "You have pressed up!" << std::endl;
......@@ -71,8 +74,7 @@ void Game::render() {
if (Input::getMouseDown(SDL_BUTTON_LEFT)) {
std::cout << "X: " << Input::getMousePos().x << " Y: " << Input::getMousePos().y << std::endl;
}
else if (Input::getMouseUp(SDL_BUTTON_LEFT)) {
std::cout << "You released the left mouse button!" << std::endl;
else if (Input::getMouseUp(SDL_BUTTON_LEFT)) { std::cout << "You released the left mouse button!" << std::endl;
}
*/
}
......@@ -84,8 +86,12 @@ void Game::update() {
float absSinCounter = abs(sinCounter);
//transform->GetPos().x = sinCounter;
m_transform->getRot().y = sinCounter;
//m_transform->getRot().y = sinCounter;
//transform->GetRot().z = sinCounter;
m_shader->setUniformMat4("u_transform", m_transform->getProjectedModel(m_camera));
}
void Game::loadMesh(const std::string& model_path) {
}
......@@ -16,6 +16,7 @@
#define GAME_H_
#include "mesh.h"
#include "texture.h"
#include "shader.h"
#include "transform.h"
#include "camera.h"
......@@ -33,6 +34,7 @@ public:
void input();
void update();
void render();
void loadMesh(const std::string& model_path);
virtual ~Game();
......@@ -41,6 +43,7 @@ private:
Shader* m_shader;
Transform* m_transform;
Camera* m_camera;
Texture* m_texture;
float m_counter;
};
......
#include "mesh.h"
#include <vector>
Mesh::Mesh(const std::string& fileName) {
IndexedModel model = OBJModel(fileName).ToIndexedModel();
......@@ -13,10 +14,22 @@ Mesh::Mesh(Vertex* vertices, unsigned int numVertices, unsigned int* indices, un
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
// retrieve the data from the vertices
std::vector<glm::vec3*> posCoords;
std::vector<glm::vec2*> texCoords;
posCoords.reserve(numVertices);
texCoords.reserve(numVertices);
for (unsigned int i = 0; i < numVertices; i++) {
posCoords.push_back(vertices[i].getPos());
texCoords.push_back(vertices[i].getTexCoord());
}
//writing it to the GPU using buffers
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(posCoords[0]), &posCoords[0], GL_STATIC_DRAW);
//assigning the data a location in the GPU memory
glEnableVertexAttribArray(0);
......@@ -25,11 +38,26 @@ Mesh::Mesh(Vertex* vertices, unsigned int numVertices, unsigned int* indices, un
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[INDEX_VB]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(indices[0]), indices, GL_STATIC_DRAW);
/*
//glEnableVertexAttribArray(1);
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0); //unbind the ARRAY from the GL handler
glBindVertexArray(1); //unbind the ARRAY from the GL handler
*/
glBindVertexArray(0);
//writing it to the GPU using buffers
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(texCoords[0]), &texCoords[0], GL_STATIC_DRAW);
//assigning the data a location in the GPU memory
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(1);
}
void Mesh::draw() {
......@@ -62,7 +90,6 @@ void Mesh::InitMesh(const IndexedModel& model) {
glEnableVertexAttribArray(0); //look at it as an array
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
/*
//for the texture coordinates
//get some buffers to work with
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
......@@ -71,10 +98,10 @@ void Mesh::InitMesh(const IndexedModel& model) {
//take data and put it in the buffer, from program to GPU memory
glBufferData(GL_ARRAY_BUFFER, model.positions.size() * sizeof(model.texCoords[0]), &model.texCoords[0], GL_STATIC_DRAW); //static draw means that the data is not gonna change
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
/*
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[NORMAL_VB]);
//take data and put it in the buffer, from program to GPU memory
glBufferData(GL_ARRAY_BUFFER, model.normals.size() * sizeof(model.normals[0]), &model.normals[0], GL_STATIC_DRAW); //static draw means that the data is not gonna change
......
......@@ -31,6 +31,7 @@ private:
enum {
POSITION_VB,
INDEX_VB,
TEXCOORD_VB,
NUM_BUFFERS
};
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
#include "texture.h"
#include "stb_image.h" //for loading the textures
#include <cassert>
#include <iostream>
Texture::Texture(const std::string& fileName) {
int width, height, numComponents; //returned by the stb loader
unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
if (imageData == NULL)
std::cerr << "Texture loading failed for texture: " << fileName << std::endl;
glGenTextures(1, &m_texture); //generate space for 1 texture and store it in m_texture
glBindTexture(GL_TEXTURE_2D, m_texture);
//wrapping if the mesh has more pixels than the texture, repeat makes the texture repeat in this case
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//for interpolation
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//send the texture to the GPU
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);//free the image data from the CPU once we dont need it anymore
}
Texture::~Texture() {
glDeleteTextures(1, &m_texture); //delete the texture
}
void Texture::bind(unsigned int unit) {
assert(unit >= 0 && unit <= 31);
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, m_texture);
}
#ifndef TEXTURE_H_
#define TEXTURE_H_
#include <string>
#include <GL/glew.h>
class Texture {
public:
Texture(const std::string& fileName);
void bind(unsigned int unit);
virtual ~Texture();
private:
Texture(const Texture& texture) {}
void operator=(const Texture& texture) {}
GLuint m_texture;
};
#endif // TEXTURE_H_
......@@ -6,13 +6,15 @@
class Vertex
{
public:
Vertex(const glm::vec3& pos) {
this->pos = pos;
}
Vertex(const glm::vec3& pos, const glm::vec2& texCoord) :
m_pos(pos),
m_texCoord(texCoord) {}
inline glm::vec3* getPos() { return &pos; }
inline glm::vec3* getPos() { return &m_pos; }
inline glm::vec2* getTexCoord() { return &m_texCoord; }
private:
glm::vec3 pos;
glm::vec3 m_pos;
glm::vec2 m_texCoord;
};
#endif // VERTEX_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment