1er commit
This commit is contained in:
74
src/render/loader/GLLoader.cpp
Normal file
74
src/render/loader/GLLoader.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* GLLoader.cpp
|
||||
*
|
||||
* Created on: 4 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
|
||||
#include "render/loader/GLLoader.h"
|
||||
#include "glbinding/gl/gl.h"
|
||||
|
||||
using namespace gl;
|
||||
|
||||
namespace GL{
|
||||
|
||||
VAO::~VAO(){
|
||||
if(m_ID != 0)
|
||||
glDeleteVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
VAO::VAO(unsigned int vertexCount) : m_VertexCount(vertexCount){
|
||||
glGenVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
void VAO::bind() const{
|
||||
glBindVertexArray(m_ID);
|
||||
}
|
||||
|
||||
void VAO::unbind() const{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void VAO::bindVBO(VBO& vbo){
|
||||
vbo.bind();
|
||||
vbo.bindVertexAttribs();
|
||||
m_Vbos.push_back(std::move(vbo));
|
||||
}
|
||||
|
||||
VBO::~VBO(){
|
||||
if(m_ID != 0)
|
||||
glDeleteBuffers(1, &m_ID);
|
||||
}
|
||||
|
||||
VBO::VBO(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride){
|
||||
glGenBuffers(1, &m_ID);
|
||||
bind();
|
||||
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, data.size() * sizeof(float), data.data());
|
||||
unbind();
|
||||
}
|
||||
|
||||
void VBO::bind() const{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
||||
}
|
||||
|
||||
void VBO::unbind() const{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void VBO::addVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset){
|
||||
VertexAttribPointer pointer;
|
||||
pointer.m_Index = index;
|
||||
pointer.m_Size = coordinateSize;
|
||||
pointer.m_Offset = offset;
|
||||
m_VertexAttribs.push_back(pointer);
|
||||
}
|
||||
|
||||
void VBO::bindVertexAttribs() const{
|
||||
for(const VertexAttribPointer& pointer : m_VertexAttribs){
|
||||
glEnableVertexAttribArray(pointer.m_Index);
|
||||
glVertexAttribPointer(pointer.m_Index, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*) pointer.m_Offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
46
src/render/loader/TextureLoader.cpp
Normal file
46
src/render/loader/TextureLoader.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* TextureLoader.cpp
|
||||
*
|
||||
* Created on: 15 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include "render/loader/TextureLoader.h"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "render/loader/stb_image.h"
|
||||
#include <iostream>
|
||||
#include <glbinding/gl/gl.h>
|
||||
|
||||
using namespace gl;
|
||||
|
||||
namespace TextureLoader {
|
||||
const unsigned int loadGLTexture(const char* fileName) {
|
||||
|
||||
int width, height, comp;
|
||||
|
||||
const unsigned char* image = stbi_load(fileName, &width, &height, &comp, STBI_rgb_alpha);
|
||||
|
||||
if (image == nullptr) {
|
||||
std::cerr << "Erreur lors du chargement de la texture !" << std::endl;
|
||||
throw(std::runtime_error("Failed to load texture"));
|
||||
}
|
||||
|
||||
GLuint textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
if (comp == 3)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
else if (comp == 4)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
stbi_image_free((void*) image);
|
||||
return textureID;
|
||||
}
|
||||
}
|
||||
161
src/render/loader/WorldLoader.cpp
Normal file
161
src/render/loader/WorldLoader.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include "render/loader/WorldLoader.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
GL::VAO loadMobModel(){
|
||||
std::vector<float> positions = {
|
||||
-0.5, -0.5,
|
||||
0.5, -0.5,
|
||||
-0.5, 0.5,
|
||||
|
||||
0.5, -0.5,
|
||||
-0.5, 0.5,
|
||||
0.5, 0.5
|
||||
};
|
||||
|
||||
float yellowFloat;
|
||||
int yellow = 255 << 24 | 255 << 16 | 255;
|
||||
memcpy(&yellowFloat, &yellow, sizeof(int));
|
||||
|
||||
std::vector<float> colors = {
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
yellowFloat
|
||||
};
|
||||
|
||||
GL::VBO positionVBO(positions, 2);
|
||||
positionVBO.addVertexAttribPointer(0, 2, 0);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
colorVBO.addVertexAttribPointer(1, 1, 0);
|
||||
|
||||
GL::VAO mobVao(colors.size()); // each pos = 1 color
|
||||
mobVao.bind();
|
||||
mobVao.bindVBO(positionVBO);
|
||||
mobVao.bindVBO(colorVBO);
|
||||
mobVao.unbind();
|
||||
return mobVao;
|
||||
}
|
||||
|
||||
GL::VAO loadWorldModel(const td::game::World* world){
|
||||
std::vector<float> positions;
|
||||
std::vector<float> colors;
|
||||
|
||||
for (const auto& chunkInfo : world->getChunks()){
|
||||
const td::game::ChunkCoord& coords = chunkInfo.first;
|
||||
td::game::ChunkPtr chunk = chunkInfo.second;
|
||||
|
||||
std::int32_t chunkX = coords.first * td::game::Chunk::ChunkWidth;
|
||||
std::int32_t chunkY = coords.second * td::game::Chunk::ChunkHeight;
|
||||
|
||||
for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++){
|
||||
for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++){
|
||||
int tileNumber = tileY * td::game::Chunk::ChunkWidth + tileX;
|
||||
td::game::TileIndex tileIndex = chunk->getTileIndex(tileNumber);
|
||||
td::game::TilePtr tile = world->getTilePtr(tileIndex);
|
||||
|
||||
if (tile == nullptr)
|
||||
continue;
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
(float)chunkX + tileX, (float)chunkY + tileY,
|
||||
(float)chunkX + tileX + 1, (float)chunkY + tileY,
|
||||
(float)chunkX + tileX, (float)chunkY + tileY + 1,
|
||||
|
||||
(float)chunkX + tileX + 1, (float)chunkY + tileY,
|
||||
(float)chunkX + tileX, (float)chunkY + tileY + 1,
|
||||
(float)chunkX + tileX + 1, (float)chunkY + tileY + 1,
|
||||
});
|
||||
|
||||
/*positions.push_back(chunkX + tileX);
|
||||
positions.push_back(chunkY + tileY);
|
||||
|
||||
positions.push_back(chunkX + tileX + 1);
|
||||
positions.push_back(chunkY + tileY);
|
||||
|
||||
positions.push_back(chunkX + tileX);
|
||||
positions.push_back(chunkY + tileY + 1);
|
||||
|
||||
|
||||
|
||||
positions.push_back(chunkX + tileX + 1);
|
||||
positions.push_back(chunkY + tileY);
|
||||
|
||||
positions.push_back(chunkX + tileX);
|
||||
positions.push_back(chunkY + tileY + 1);
|
||||
|
||||
positions.push_back(chunkX + tileX + 1);
|
||||
positions.push_back(chunkY + tileY + 1);*/
|
||||
|
||||
const td::game::Color& tileColor = world->getTileColor(tile);
|
||||
|
||||
for (int i = 0; i < 6; i++){
|
||||
int color = 255;
|
||||
color |= tileColor.r << 24;
|
||||
color |= tileColor.g << 16;
|
||||
color |= tileColor.b << 8;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int spawnColor = 0; spawnColor < 2; spawnColor++){
|
||||
const game::Spawn& spawn = world->getSpawn(game::TeamColor(spawnColor));
|
||||
float fromX = spawn.x - 2, toX = spawn.x + 3;
|
||||
float fromY = spawn.y - 2, toY = spawn.y + 3;
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
fromX, fromY,
|
||||
fromX, toY,
|
||||
toX, fromY,
|
||||
|
||||
toX, toY,
|
||||
fromX, toY,
|
||||
toX, fromY,
|
||||
});
|
||||
|
||||
for (int i = 0; i < 6; i++){
|
||||
int color = 255;
|
||||
color |= world->getSpawnColor(game::TeamColor(spawnColor)).r << 24;
|
||||
color |= world->getSpawnColor(game::TeamColor(spawnColor)).g << 16;
|
||||
color |= world->getSpawnColor(game::TeamColor(spawnColor)).b << 8;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
|
||||
GL::VBO positionVBO(positions, 2);
|
||||
positionVBO.addVertexAttribPointer(0, 2, 0);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
colorVBO.addVertexAttribPointer(1, 1, 0);
|
||||
|
||||
GL::VAO worldVao(positions.size() / 2); // each pos = 2 vertecies
|
||||
worldVao.bind();
|
||||
worldVao.bindVBO(positionVBO);
|
||||
worldVao.bindVBO(colorVBO);
|
||||
worldVao.unbind();
|
||||
return worldVao;
|
||||
}
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user