Add sprite, draw_op and vertex classes

This commit is contained in:
Jyrki Vesterinen 2018-04-05 19:57:38 +03:00
parent 73caa6da64
commit 0992605c5d
7 changed files with 221 additions and 0 deletions

View file

@ -2530,6 +2530,7 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)OGL\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\ogl\sprite.cpp" />
<ClCompile Include="..\..\src\ogl\texture.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)OGL\</ObjectFileName>
@ -3885,8 +3886,11 @@
<ClInclude Include="..\..\src\multiplayer_error_codes.hpp" />
<ClInclude Include="..\..\src\network_asio.hpp" />
<ClInclude Include="..\..\src\ogl\context.hpp" />
<ClInclude Include="..\..\src\ogl\draw_op.hpp" />
<ClInclude Include="..\..\src\ogl\sprite.hpp" />
<ClInclude Include="..\..\src\ogl\texture.hpp" />
<ClInclude Include="..\..\src\ogl\utils.hpp" />
<ClInclude Include="..\..\src\ogl\vertex.hpp" />
<ClInclude Include="..\..\src\overlay.hpp" />
<ClInclude Include="..\..\src\pathfind\pathfind.hpp" />
<ClInclude Include="..\..\src\pathfind\teleport.hpp" />

View file

@ -1549,6 +1549,9 @@
<ClCompile Include="..\..\src\ogl\texture.cpp">
<Filter>OGL</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ogl\sprite.cpp">
<Filter>OGL</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\addon\client.hpp">
@ -3011,6 +3014,15 @@
<ClInclude Include="..\..\src\ogl\texture.hpp">
<Filter>OGL</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\vertex.hpp">
<Filter>OGL</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\draw_op.hpp">
<Filter>OGL</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\sprite.hpp">
<Filter>OGL</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">

View file

@ -1,4 +1,5 @@
ogl/context.cpp
ogl/sprite.cpp
ogl/texture.cpp
ogl/utils.cpp
sdl/exception.cpp

32
src/ogl/draw_op.hpp Normal file
View file

@ -0,0 +1,32 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "ogl/vertex.hpp"
#include <GL/glew.h>
#include <array>
namespace gl
{
/** Represents a single draw operation.
The shape is always a triangle. */
struct draw_op
{
std::array<vertex, 3> vertices;
GLuint texture_name;
};
}

30
src/ogl/sprite.cpp Normal file
View file

@ -0,0 +1,30 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "ogl/sprite.hpp"
namespace gl
{
void sprite::normalize_coordinates(const std::pair<int, int>& texture_size)
{
x_min = static_cast<float>(x_min_px) / static_cast<float>(texture_size.first);
x_max = static_cast<float>(x_max_px) / static_cast<float>(texture_size.first);
y_min = static_cast<float>(y_min_px) / static_cast<float>(texture_size.second);
y_max = static_cast<float>(y_max_px) / static_cast<float>(texture_size.second);
half_width = (x_max_px - x_min_px) / 2.0f;
half_height = (y_max_px - y_min_px) / 2.0f;
}
}

111
src/ogl/sprite.hpp Normal file
View file

@ -0,0 +1,111 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "ogl/draw_op.hpp"
#include "ogl/texture.hpp"
#include <GL/glew.h>
#include <utility>
namespace gl
{
class sprite
{
public:
/** Texture name. */
const GLuint texture_name;
/** X coordinate of left edge. Normalized to the range [0, 1]. */
float x_min;
/** X coordinate of right edge. Normalized to the range [0, 1]. */
float x_max;
/** Y coordinate of bottom edge. Normalized to the range [0, 1]. */
float y_min;
/** Y coordinate of top edge. Normalized to the range [0, 1]. */
float y_max;
/** X coordinate of left edge as pixels. */
unsigned int x_min_px;
/** X coordinate of right edge as pixels. */
unsigned int x_max_px;
/** Y coordinate of bottom edge as pixels. */
unsigned int y_min_px;
/** Y coordinate of top edge as pixels. */
unsigned int y_max_px;
/** Half of the width of the sprite, in pixels. */
float half_width;
/** Half of the height of the sprite, in pixels. */
float half_height;
/** Constructor. */
sprite(const texture& tex) : texture_name(tex.get_name())
{
}
/** Calculates normalized texture coordinates from pixel coordinates and texture size. */
void normalize_coordinates(const std::pair<int, int>& texture_size);
/** Generates draw operations which draw the sprite.
The sprite is anchored to center.
@param x X coordinate for center of the sprite.
@param y Y coordinate for center of the sprite.
@param out An output iterator for the container where the draw operations will be pushed.
@todo Scaling support. */
template<typename T>
void to_draw_ops(float x, float y, T out) const
{
draw_op op;
op.texture_name = texture_name;
op.vertices[0].x = x - half_width;
op.vertices[0].y = y - half_height;
op.vertices[1].x = x + half_width;
op.vertices[1].y = y - half_height;
op.vertices[2].x = x + half_width;
op.vertices[2].y = y + half_height;
op.vertices[0].u = x_min;
op.vertices[0].v = y_min;
op.vertices[1].u = x_max;
op.vertices[1].v = y_min;
op.vertices[2].u = x_max;
op.vertices[2].v = y_max;
// Assigning to the iterator pushes the draw operation.
// It pushes a copy, allowing us to modify the original.
out = op;
op.vertices[0].x = x - half_width;
op.vertices[0].y = y - half_height;
op.vertices[1].x = x + half_width;
op.vertices[1].y = y + half_height;
op.vertices[2].x = x - half_width;
op.vertices[2].y = y + half_height;
op.vertices[0].u = x_min;
op.vertices[0].v = y_min;
op.vertices[1].u = x_max;
op.vertices[1].v = y_max;
op.vertices[2].u = x_min;
op.vertices[2].v = y_max;
out = op;
}
};
}

31
src/ogl/vertex.hpp Normal file
View file

@ -0,0 +1,31 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include <GL/glew.h>
namespace gl
{
struct vertex
{
// Vertex coordinates
GLfloat x;
GLfloat y;
// Texture coordinates
GLfloat u;
GLfloat v;
};
}