2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 14.01.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Screen.h"
|
|
|
|
#include "utils/Time.h"
|
|
|
|
#include <utility>
|
|
|
|
#include "utils/Log.h"
|
|
|
|
#include "ResourceManager.h"
|
2021-10-22 19:42:32 +03:00
|
|
|
#include <SFML/OpenGL.hpp>
|
|
|
|
#include <cmath>
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
void Screen::open(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, sf::Uint32 style) {
|
2021-09-19 15:44:31 +03:00
|
|
|
_title = name;
|
2021-09-19 11:25:10 +03:00
|
|
|
_w = screenWidth;
|
|
|
|
_h = screenHeight;
|
|
|
|
_background = background;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
sf::ContextSettings settings;
|
2021-10-22 19:42:32 +03:00
|
|
|
settings.depthBits = 24;
|
2021-09-13 15:53:43 +03:00
|
|
|
settings.antialiasingLevel = 8;
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window = std::make_shared<sf::RenderWindow>();
|
|
|
|
_window->create(sf::VideoMode(_w, _h), name, style, settings);
|
|
|
|
_window->setVerticalSyncEnabled(verticalSync);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::display() {
|
|
|
|
sf::Event event{};
|
2021-09-19 15:44:31 +03:00
|
|
|
while (_window->pollEvent(event)) {
|
2021-09-13 15:53:43 +03:00
|
|
|
if (event.type == sf::Event::Closed) {
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->close();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)";
|
|
|
|
_window->setTitle(title);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->display();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::clear() {
|
2021-10-22 19:42:32 +03:00
|
|
|
// Clear the depth buffer
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->clear(_background);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
void Screen::drawTriangle(const Triangle& triangle)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
|
|
|
sf::Vertex tris[3] =
|
|
|
|
{
|
2021-10-09 13:41:12 +03:00
|
|
|
sf::Vertex(sf::Vector2f((float)triangle[0].x(), (float)triangle[0].y()), triangle.color()),
|
|
|
|
sf::Vertex(sf::Vector2f((float)triangle[1].x(), (float)triangle[1].y()), triangle.color()),
|
|
|
|
sf::Vertex(sf::Vector2f((float)triangle[2].x(), (float)triangle[2].y()), triangle.color())
|
2021-09-13 15:53:43 +03:00
|
|
|
};
|
2021-10-22 19:42:32 +03:00
|
|
|
|
|
|
|
_window->pushGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(tris, 3, sf::Triangles);
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->popGLStates();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Screen::setTitle(const std::string& title) {
|
|
|
|
_title = title;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Screen::isOpen() {
|
2021-09-19 15:44:31 +03:00
|
|
|
return _window->isOpen();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::close() {
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->close();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Screen::debugText(const std::string& text) {
|
|
|
|
sf::Text t;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-16 20:22:55 +03:00
|
|
|
t.setFont(*ResourceManager::loadFont(Consts::THIN_FONT));
|
2021-09-19 15:44:31 +03:00
|
|
|
t.setString(text);
|
|
|
|
t.setCharacterSize(30);
|
|
|
|
t.setFillColor(sf::Color::Black);
|
2021-10-22 19:42:32 +03:00
|
|
|
t.setPosition(10, 50);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->pushGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(t);
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->popGLStates();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Screen::drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, const Vec2D &p4, sf::Color color) {
|
2021-09-19 15:44:31 +03:00
|
|
|
sf::ConvexShape polygon;
|
|
|
|
polygon.setPointCount(4);
|
|
|
|
polygon.setPoint(0, sf::Vector2f((float)p1.x(), (float)p1.y()));
|
|
|
|
polygon.setPoint(1, sf::Vector2f((float)p2.x(), (float)p2.y()));
|
|
|
|
polygon.setPoint(2, sf::Vector2f((float)p3.x(), (float)p3.y()));
|
|
|
|
polygon.setPoint(3, sf::Vector2f((float)p4.x(), (float)p4.y()));
|
|
|
|
polygon.setFillColor(color);
|
2021-10-22 19:42:32 +03:00
|
|
|
|
|
|
|
_window->pushGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(polygon);
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->popGLStates();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Screen::drawText(const std::string& string, const Vec2D &position, int size, sf::Color color) {
|
2021-09-19 15:44:31 +03:00
|
|
|
sf::Text text;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-16 20:22:55 +03:00
|
|
|
text.setFont(*ResourceManager::loadFont(Consts::MEDIUM_FONT));
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
text.setCharacterSize(size);
|
|
|
|
text.setFillColor(color);
|
|
|
|
text.setStyle(sf::Text::Italic);
|
|
|
|
text.setPosition((float)position.x(), (float)position.y());
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
text.setString(string);
|
2021-10-22 19:42:32 +03:00
|
|
|
|
|
|
|
_window->pushGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(text);
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->popGLStates();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Screen::drawSprite(const sf::Sprite &sprite) {
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->pushGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(sprite);
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->popGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Screen::drawText(const sf::Text &text) {
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->pushGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(text);
|
2021-10-22 19:42:32 +03:00
|
|
|
_window->popGLStates();
|
2021-09-19 15:44:31 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Screen::attachMouse(std::shared_ptr<Mouse> mouse) {
|
|
|
|
mouse->setWindow(_window);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
2021-10-22 19:42:32 +03:00
|
|
|
|
|
|
|
// OpenGL functions
|
|
|
|
void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, size_t count) {
|
|
|
|
// OpenGL:
|
|
|
|
// Make the window the active window for OpenGL calls
|
|
|
|
_window->setActive(true);
|
|
|
|
|
|
|
|
glEnable(GL_CULL_FACE); // enable culling face
|
|
|
|
glCullFace(GL_BACK); // cull faces from back
|
|
|
|
glFrontFace(GL_CCW); // vertex order (counter clock wise)
|
|
|
|
|
|
|
|
// Enable Z-buffer read and write
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
glClearDepth(1.f);
|
|
|
|
|
|
|
|
// Disable lighting
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
|
|
|
|
// enable alpha channel:
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
// Configure the viewport (the same size as the window)
|
|
|
|
glViewport(0, 0, _window->getSize().x, _window->getSize().y);
|
|
|
|
|
|
|
|
// Setup a perspective projection
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
GLfloat ratio = static_cast<float>(_window->getSize().x) / _window->getSize().y;
|
|
|
|
glFrustum(-ratio, ratio, -1.f, 1.f, 1.0f, 500.f);
|
|
|
|
|
|
|
|
// Enable position and texture coordinates vertex components
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), geometry);
|
|
|
|
glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), geometry + 3);
|
|
|
|
|
|
|
|
// Disable normal and color vertex components
|
|
|
|
glDisableClientState(GL_NORMAL_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
|
|
|
|
// Apply some transformations
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
glLoadMatrixf(view);
|
|
|
|
|
|
|
|
// Draw the mesh
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
|
|
|
|
|
|
|
// Make the window no longer the active window for OpenGL calls
|
|
|
|
_window->setActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLfloat* Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D& cameraPosition) {
|
|
|
|
Vec3D pos = mesh->position();
|
|
|
|
std::vector<Triangle>& triangles = mesh->triangles();
|
|
|
|
|
|
|
|
auto* geometry = (GLfloat*)malloc(7*3*triangles.size()*sizeof(GLfloat));
|
|
|
|
|
|
|
|
for(int i = 0; i < triangles.size(); i++) {
|
|
|
|
int stride = 21*i;
|
|
|
|
|
|
|
|
double dot = triangles[i].norm().dot((pos + Vec3D(triangles[i][0]) - cameraPosition).normalized());
|
|
|
|
sf::Color color = triangles[i].color();
|
|
|
|
sf::Color ambientColor = sf::Color((sf::Uint8)(color.r * (0.3 * std::abs(dot) + 0.7)),
|
|
|
|
(sf::Uint8)(color.g * (0.3 * std::abs(dot) + 0.7)),
|
|
|
|
(sf::Uint8)(color.b * (0.3 * std::abs(dot) + 0.7)),
|
|
|
|
(sf::Uint8)color.a);
|
|
|
|
|
|
|
|
|
|
|
|
geometry[stride + 0] = (GLfloat)triangles[i][0].x() + pos.x();
|
|
|
|
geometry[stride + 1] = (GLfloat)triangles[i][0].y() + pos.y();
|
|
|
|
geometry[stride + 2] = (GLfloat)triangles[i][0].z() + pos.z();
|
|
|
|
|
|
|
|
geometry[stride + 3] = (GLfloat)ambientColor.r/255.0f;
|
|
|
|
geometry[stride + 4] = (GLfloat)ambientColor.g/255.0f;
|
|
|
|
geometry[stride + 5] = (GLfloat)ambientColor.b/255.0f;
|
|
|
|
geometry[stride + 6] = (GLfloat)ambientColor.a/255.0f;
|
|
|
|
|
|
|
|
geometry[stride + 7] = (GLfloat)triangles[i][1].x() + pos.x();
|
|
|
|
geometry[stride + 8] = (GLfloat)triangles[i][1].y() + pos.y();
|
|
|
|
geometry[stride + 9] = (GLfloat)triangles[i][1].z() + pos.z();
|
|
|
|
|
|
|
|
geometry[stride + 10] = (GLfloat)ambientColor.r/255.0f;
|
|
|
|
geometry[stride + 11] = (GLfloat)ambientColor.g/255.0f;
|
|
|
|
geometry[stride + 12] = (GLfloat)ambientColor.b/255.0f;
|
|
|
|
geometry[stride + 13] = (GLfloat)ambientColor.a/255.0f;
|
|
|
|
|
|
|
|
geometry[stride + 14] = (GLfloat)triangles[i][2].x() + pos.x();
|
|
|
|
geometry[stride + 15] = (GLfloat)triangles[i][2].y() + pos.y();
|
|
|
|
geometry[stride + 16] = (GLfloat)triangles[i][2].z() + pos.z();
|
|
|
|
|
|
|
|
geometry[stride + 17] = (GLfloat)ambientColor.r/255.0f;
|
|
|
|
geometry[stride + 18] = (GLfloat)ambientColor.g/255.0f;
|
|
|
|
geometry[stride + 19] = (GLfloat)ambientColor.b/255.0f;
|
|
|
|
geometry[stride + 20] = (GLfloat)ambientColor.a/255.0f;
|
|
|
|
}
|
|
|
|
return geometry;
|
|
|
|
}
|