OpenMiner/source/client/graphics/CelestialObject.cpp

77 lines
2.3 KiB
C++
Raw Normal View History

2020-07-14 16:13:53 +02:00
/*
* =====================================================================================
*
* 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
*
* =====================================================================================
*/
2020-07-14 16:26:37 +02:00
#include <gk/core/GameClock.hpp>
2020-07-14 16:13:53 +02:00
#include <gk/gl/GLCheck.hpp>
2020-07-14 16:26:37 +02:00
#include "CelestialObject.hpp"
2020-07-14 16:13:53 +02:00
#include "Vertex.hpp"
2020-07-14 16:26:37 +02:00
CelestialObject::CelestialObject() {
2020-07-14 16:13:53 +02:00
updateVertexBuffer();
}
2020-07-14 16:26:37 +02:00
void CelestialObject::updateVertexBuffer() const {
2020-07-14 16:13:53 +02:00
float width = 20.f;
float height = 20.f;
Vertex vertices[4] = {
// Rectangle vertices
{{0, width, 0, -1}},
{{0, 0, 0, -1}},
{{0, 0, height, -1}},
{{0, width, height, -1}},
};
for (u8 i = 0 ; i < 4 ; ++i) {
2020-07-14 16:26:37 +02:00
vertices[i].color[0] = m_color.r;
vertices[i].color[1] = m_color.g;
vertices[i].color[2] = m_color.b;
vertices[i].color[3] = m_color.a;
2020-07-14 16:13:53 +02:00
}
gk::VertexBuffer::bind(&m_vbo);
m_vbo.setData(sizeof(vertices), vertices, GL_STATIC_DRAW);
gk::VertexBuffer::bind(nullptr);
}
2020-07-14 16:26:37 +02:00
void CelestialObject::draw(gk::RenderTarget &target, gk::RenderStates states) const {
2020-07-14 18:24:28 +02:00
states.transform.rotate(-fmod(gk::GameClock::getInstance().getTicks() * 4 / 1000.f, 360), {0, 1, 0});
2020-07-14 16:26:37 +02:00
states.transform *= getTransform();
2020-07-14 16:13:53 +02:00
states.vertexAttributes = VertexAttribute::All;
// glCheck(glEnable(GL_CULL_FACE));
// glCheck(glEnable(GL_DEPTH_TEST));
static const GLubyte indices[] = {
0, 1, 3,
3, 1, 2
};
target.drawElements(m_vbo, GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices, states);
}