2020-07-08 00:12:22 +02:00

288 lines
10 KiB
C++

/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include <gk/gl/GLCheck.hpp>
#include <gk/resource/ResourceHandler.hpp>
#include "PlayerBox.hpp"
#include "Vertex.hpp"
constexpr int NUM_QUADS = 34;
constexpr int NUM_VERTICES_PER_QUAD = 4;
constexpr int NUM_VERTEX_ELEMENTS = 5;
static constexpr float modelCoords[NUM_QUADS * NUM_VERTICES_PER_QUAD][NUM_VERTEX_ELEMENTS] = {
// RIGHT LEG
// West
{-0.15f, -0.15f - 0.18f, 0.f, 16.f/64.f, 0.f/32.f},
{-0.15f, -0.15f - 0.18f, 0.575f, 16.f/64.f, 12.f/32.f},
{-0.15f, 0.15f - 0.18f, 0.575f, 12.f/64.f, 12.f/32.f},
{-0.15f, 0.15f - 0.18f, 0.f, 12.f/64.f, 0.f/32.f},
// East
{ 0.15f, -0.15f - 0.18f, 0.575f, 4.f/64.f, 12.f/32.f},
{ 0.15f, -0.15f - 0.18f, 0.f, 4.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f - 0.18f, 0.f, 8.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f - 0.18f, 0.575f, 8.f/64.f, 12.f/32.f},
// South
{-0.15f, -0.15f - 0.18f, 0.f, 0.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f - 0.18f, 0.f, 4.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f - 0.18f, 0.575f, 4.f/64.f, 12.f/32.f},
{-0.15f, -0.15f - 0.18f, 0.575f, 0.f/64.f, 12.f/32.f},
// North
{-0.15f, 0.15f - 0.18f, 0.575f, 12.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f - 0.18f, 0.575f, 8.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f - 0.18f, 0.f, 8.f/64.f, 0.f/32.f},
{-0.15f, 0.15f - 0.18f, 0.f, 12.f/64.f, 0.f/32.f},
// Bottom
{ 0.15f, -0.15f - 0.18f, 0.f, 8.f/64.f, 12.f/32.f},
{-0.15f, -0.15f - 0.18f, 0.f, 8.f/64.f, 16.f/32.f},
{-0.15f, 0.15f - 0.18f, 0.f, 12.f/64.f, 16.f/32.f},
{ 0.15f, 0.15f - 0.18f, 0.f, 12.f/64.f, 12.f/32.f},
// LEFT LEG
// West
{-0.15f, -0.15f + 0.18f, 0.f, 12.f/64.f, 0.f/32.f},
{-0.15f, -0.15f + 0.18f, 0.575f, 12.f/64.f, 12.f/32.f},
{-0.15f, 0.15f + 0.18f, 0.575f, 16.f/64.f, 12.f/32.f},
{-0.15f, 0.15f + 0.18f, 0.f, 16.f/64.f, 0.f/32.f},
// East
{ 0.15f, -0.15f + 0.18f, 0.575f, 8.f/64.f, 12.f/32.f},
{ 0.15f, -0.15f + 0.18f, 0.f, 8.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f + 0.18f, 0.f, 4.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f + 0.18f, 0.575f, 4.f/64.f, 12.f/32.f},
// South
{-0.15f, -0.15f + 0.18f, 0.f, 12.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f + 0.18f, 0.f, 8.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f + 0.18f, 0.575f, 8.f/64.f, 12.f/32.f},
{-0.15f, -0.15f + 0.18f, 0.575f, 12.f/64.f, 12.f/32.f},
// North
{-0.15f, 0.15f + 0.18f, 0.575f, 0.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f + 0.18f, 0.575f, 4.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f + 0.18f, 0.f, 4.f/64.f, 0.f/32.f},
{-0.15f, 0.15f + 0.18f, 0.f, 0.f/64.f, 0.f/32.f},
// Bottom
{ 0.15f, -0.15f + 0.18f, 0.f, 12.f/64.f, 12.f/32.f},
{-0.15f, -0.15f + 0.18f, 0.f, 12.f/64.f, 16.f/32.f},
{-0.15f, 0.15f + 0.18f, 0.f, 8.f/64.f, 16.f/32.f},
{ 0.15f, 0.15f + 0.18f, 0.f, 8.f/64.f, 12.f/32.f},
// BODY
// West
{-0.15f, -0.33f, 0.575f, 40.f/64.f, 0.f/32.f},
{-0.15f, -0.33f, 1.35f, 40.f/64.f, 12.f/32.f},
{-0.15f, 0.33f, 1.35f, 32.f/64.f, 12.f/32.f},
{-0.15f, 0.33f, 0.575f, 32.f/64.f, 0.f/32.f},
// East
{ 0.15f, -0.33f, 1.35f, 20.f/64.f, 12.f/32.f},
{ 0.15f, -0.33f, 0.575f, 20.f/64.f, 0.f/32.f},
{ 0.15f, 0.33f, 0.575f, 28.f/64.f, 0.f/32.f},
{ 0.15f, 0.33f, 1.35f, 28.f/64.f, 12.f/32.f},
// South
{-0.15f, -0.33f, 0.575f, 16.f/64.f, 0.f/32.f},
{ 0.15f, -0.33f, 0.575f, 20.f/64.f, 0.f/32.f},
{ 0.15f, -0.33f, 1.35f, 20.f/64.f, 12.f/32.f},
{-0.15f, -0.33f, 1.35f, 16.f/64.f, 12.f/32.f},
// North
{-0.15f, 0.33f, 1.35f, 32.f/64.f, 12.f/32.f},
{ 0.15f, 0.33f, 1.35f, 28.f/64.f, 12.f/32.f},
{ 0.15f, 0.33f, 0.575f, 28.f/64.f, 0.f/32.f},
{-0.15f, 0.33f, 0.575f, 32.f/64.f, 0.f/32.f},
// Bottom
{ 0.15f, -0.33f, 0.575f, 36.f/64.f, 12.f/32.f},
{-0.15f, -0.33f, 0.575f, 36.f/64.f, 16.f/32.f},
{-0.15f, 0.33f, 0.575f, 28.f/64.f, 16.f/32.f},
{ 0.15f, 0.33f, 0.575f, 28.f/64.f, 12.f/32.f},
// Top
{-0.15f, -0.33f, 1.35f, 20.f/64.f, 16.f/32.f},
{ 0.15f, -0.33f, 1.35f, 20.f/64.f, 12.f/32.f},
{ 0.15f, 0.33f, 1.35f, 28.f/64.f, 12.f/32.f},
{-0.15f, 0.33f, 1.35f, 28.f/64.f, 16.f/32.f},
// RIGHT ARM
// West
{-0.15f, -0.15f - 0.48f, 0.675f, 56.f/64.f, 0.f/32.f},
{-0.15f, -0.15f - 0.48f, 1.35f, 56.f/64.f, 12.f/32.f},
{-0.15f, 0.15f - 0.48f, 1.35f, 52.f/64.f, 12.f/32.f},
{-0.15f, 0.15f - 0.48f, 0.675f, 52.f/64.f, 0.f/32.f},
// East
{ 0.15f, -0.15f - 0.48f, 1.35f, 44.f/64.f, 12.f/32.f},
{ 0.15f, -0.15f - 0.48f, 0.675f, 44.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f - 0.48f, 0.675f, 48.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f - 0.48f, 1.35f, 48.f/64.f, 12.f/32.f},
// South
{-0.15f, -0.15f - 0.48f, 0.675f, 40.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f - 0.48f, 0.675f, 44.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f - 0.48f, 1.35f, 44.f/64.f, 12.f/32.f},
{-0.15f, -0.15f - 0.48f, 1.35f, 40.f/64.f, 12.f/32.f},
// North
{-0.15f, 0.15f - 0.48f, 1.35f, 52.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f - 0.48f, 1.35f, 48.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f - 0.48f, 0.675f, 48.f/64.f, 0.f/32.f},
{-0.15f, 0.15f - 0.48f, 0.675f, 52.f/64.f, 0.f/32.f},
// Bottom
{ 0.15f, -0.15f - 0.48f, 0.675f, 52.f/64.f, 12.f/32.f},
{-0.15f, -0.15f - 0.48f, 0.675f, 52.f/64.f, 16.f/32.f},
{-0.15f, 0.15f - 0.48f, 0.675f, 48.f/64.f, 16.f/32.f},
{ 0.15f, 0.15f - 0.48f, 0.675f, 48.f/64.f, 12.f/32.f},
// Top
{-0.15f, -0.15f - 0.48f, 1.35f, 44.f/64.f, 16.f/32.f},
{ 0.15f, -0.15f - 0.48f, 1.35f, 44.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f - 0.48f, 1.35f, 48.f/64.f, 12.f/32.f},
{-0.15f, 0.15f - 0.48f, 1.35f, 48.f/64.f, 16.f/32.f},
// LEFT ARM
// West
{-0.15f, -0.15f + 0.48f, 0.675f, 52.f/64.f, 0.f/32.f},
{-0.15f, -0.15f + 0.48f, 1.35f, 52.f/64.f, 12.f/32.f},
{-0.15f, 0.15f + 0.48f, 1.35f, 56.f/64.f, 12.f/32.f},
{-0.15f, 0.15f + 0.48f, 0.675f, 56.f/64.f, 0.f/32.f},
// East
{ 0.15f, -0.15f + 0.48f, 1.35f, 48.f/64.f, 12.f/32.f},
{ 0.15f, -0.15f + 0.48f, 0.675f, 48.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f + 0.48f, 0.675f, 44.f/64.f, 0.f/32.f},
{ 0.15f, 0.15f + 0.48f, 1.35f, 44.f/64.f, 12.f/32.f},
// South
{-0.15f, -0.15f + 0.48f, 0.675f, 52.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f + 0.48f, 0.675f, 48.f/64.f, 0.f/32.f},
{ 0.15f, -0.15f + 0.48f, 1.35f, 48.f/64.f, 12.f/32.f},
{-0.15f, -0.15f + 0.48f, 1.35f, 52.f/64.f, 12.f/32.f},
// North
{-0.15f, 0.15f + 0.48f, 1.35f, 40.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f + 0.48f, 1.35f, 44.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f + 0.48f, 0.675f, 44.f/64.f, 0.f/32.f},
{-0.15f, 0.15f + 0.48f, 0.675f, 40.f/64.f, 0.f/32.f},
// Bottom
{ 0.15f, -0.15f + 0.48f, 0.675f, 48.f/64.f, 12.f/32.f},
{-0.15f, -0.15f + 0.48f, 0.675f, 48.f/64.f, 16.f/32.f},
{-0.15f, 0.15f + 0.48f, 0.675f, 52.f/64.f, 16.f/32.f},
{ 0.15f, 0.15f + 0.48f, 0.675f, 52.f/64.f, 12.f/32.f},
// Top
{-0.15f, -0.15f + 0.48f, 1.35f, 48.f/64.f, 16.f/32.f},
{ 0.15f, -0.15f + 0.48f, 1.35f, 48.f/64.f, 12.f/32.f},
{ 0.15f, 0.15f + 0.48f, 1.35f, 44.f/64.f, 12.f/32.f},
{-0.15f, 0.15f + 0.48f, 1.35f, 44.f/64.f, 16.f/32.f},
// HEAD
// West
{-0.2f, -0.2f, 1.35f, 32.f/64.f, 16.f/32.f},
{-0.2f, -0.2f, 1.75f, 32.f/64.f, 24.f/32.f},
{-0.2f, 0.2f, 1.75f, 24.f/64.f, 24.f/32.f},
{-0.2f, 0.2f, 1.35f, 24.f/64.f, 16.f/32.f},
// East
{ 0.2f, -0.2f, 1.75f, 8.f/64.f, 24.f/32.f},
{ 0.2f, -0.2f, 1.35f, 8.f/64.f, 16.f/32.f},
{ 0.2f, 0.2f, 1.35f, 16.f/64.f, 16.f/32.f},
{ 0.2f, 0.2f, 1.75f, 16.f/64.f, 24.f/32.f},
// South
{-0.2f, -0.2f, 1.35f, 0.f/64.f, 16.f/32.f},
{ 0.2f, -0.2f, 1.35f, 8.f/64.f, 16.f/32.f},
{ 0.2f, -0.2f, 1.75f, 8.f/64.f, 24.f/32.f},
{-0.2f, -0.2f, 1.75f, 0.f/64.f, 24.f/32.f},
// North
{-0.2f, 0.2f, 1.75f, 24.f/64.f, 24.f/32.f},
{ 0.2f, 0.2f, 1.75f, 16.f/64.f, 24.f/32.f},
{ 0.2f, 0.2f, 1.35f, 16.f/64.f, 16.f/32.f},
{-0.2f, 0.2f, 1.35f, 24.f/64.f, 16.f/32.f},
// Bottom
{ 0.2f, -0.2f, 1.35f, 24.f/64.f, 24.f/32.f},
{-0.2f, -0.2f, 1.35f, 24.f/64.f, 32.f/32.f},
{-0.2f, 0.2f, 1.35f, 16.f/64.f, 32.f/32.f},
{ 0.2f, 0.2f, 1.35f, 16.f/64.f, 24.f/32.f},
// Top
{-0.2f, -0.2f, 1.75f, 8.f/64.f, 32.f/32.f},
{ 0.2f, -0.2f, 1.75f, 8.f/64.f, 24.f/32.f},
{ 0.2f, 0.2f, 1.75f, 16.f/64.f, 24.f/32.f},
{-0.2f, 0.2f, 1.75f, 16.f/64.f, 32.f/32.f},
};
PlayerBox::PlayerBox(const gk::Camera &camera)
: m_camera(camera),
m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>("texture-player"))
{
updateVertexBuffer();
}
void PlayerBox::updateVertexBuffer() {
Vertex vertices[NUM_QUADS * NUM_VERTICES_PER_QUAD];
for (u8 i = 0 ; i < NUM_QUADS * NUM_VERTICES_PER_QUAD ; ++i) {
vertices[i].coord3d[0] = modelCoords[i][0];
vertices[i].coord3d[1] = modelCoords[i][1];
vertices[i].coord3d[2] = modelCoords[i][2];
vertices[i].coord3d[3] = -1;
vertices[i].texCoord[0] = modelCoords[i][3];
vertices[i].texCoord[1] = 1.f - modelCoords[i][4];
}
gk::VertexBuffer::bind(&m_vbo);
m_vbo.setData(sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
gk::VertexBuffer::bind(nullptr);
}
void PlayerBox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
// Subtract the camera position - see comment in ClientWorld::draw()
gk::Vector3d cameraPosition = m_camera.getDPosition();
states.transform.translate(m_x - cameraPosition.x, m_y - cameraPosition.y, m_z - cameraPosition.z);
states.transform.rotate(m_viewAngleH, gk::Vector3{0, 0, 1});
states.transform *= getTransform();
states.texture = &m_texture;
states.vertexAttributes = VertexAttribute::All;
glCheck(glEnable(GL_CULL_FACE));
target.draw(m_vbo, GL_QUADS, 0, NUM_QUADS * NUM_VERTICES_PER_QUAD, states);
}