colobot/src/graphics/core/vertex.h

165 lines
3.7 KiB
C
Raw Normal View History

/*
* This file is part of the Colobot: Gold Edition source code
2021-09-11 13:52:34 +00:00
* Copyright (C) 2001-2021, Daniel Roux, EPSITEC SA & TerranovaTeam
2015-08-22 14:40:02 +00:00
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* 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 3 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; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
/**
* \file graphics/core/vertex.h
* \brief Vertex structs
*/
2012-06-20 17:34:54 +00:00
#pragma once
#include "graphics/core/color.h"
#include "graphics/core/type.h"
2012-09-19 21:50:28 +00:00
#include "math/vector.h"
2012-06-20 17:34:54 +00:00
#include <sstream>
#include <cstdint>
2021-06-06 15:03:59 +00:00
#include <glm/glm.hpp>
2012-09-19 21:50:28 +00:00
// Graphics module namespace
2015-08-02 09:40:47 +00:00
namespace Gfx
{
2012-06-20 17:34:54 +00:00
2018-05-03 17:14:55 +00:00
enum VertexType
{
VERTEX_TYPE_NORMAL,
VERTEX_TYPE_TEX2,
VERTEX_TYPE_COL,
};
2012-06-20 17:34:54 +00:00
/**
2012-06-26 20:50:55 +00:00
* \struct Vertex
* \brief Vertex of a primitive
2012-06-20 17:34:54 +00:00
*
* This structure was created as analog to DirectX's D3DVERTEX.
*
* It contains:
* - vertex coordinates (x,y,z) as Math::Vector,
* - normal coordinates (nx,ny,nz) as Math::Vector
* - texture coordinates (u,v) as glm::vec2.
2012-06-20 17:34:54 +00:00
*/
struct Vertex
{
2018-05-03 17:14:55 +00:00
static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_NORMAL;
Math::Vector coord = Math::Vector();
Math::Vector normal = Math::Vector();
glm::vec2 texCoord = { 0, 0 };
2012-06-20 17:34:54 +00:00
};
/**
* \struct VertexCol
* \brief Colored vertex
*
* It contains:
* - vertex coordinates (x,y,z) as Math::Vector,
* - RGBA color as Color
*/
struct VertexCol
{
2018-05-03 17:14:55 +00:00
static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_COL;
Math::Vector coord = Math::Vector();
Color color = Color();
};
2012-06-20 17:34:54 +00:00
/**
2012-06-26 20:50:55 +00:00
* \struct VertexTex2
* \brief Vertex with secondary texture coordinates
2012-06-20 17:34:54 +00:00
*
2012-09-19 21:50:28 +00:00
* In addition to fields from Vector, it contains
* secondary texture coordinates (u2, v2) as glm::vec2
2012-06-20 17:34:54 +00:00
*/
struct VertexTex2
2012-06-20 17:34:54 +00:00
{
2018-05-03 17:14:55 +00:00
static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_TEX2;
Math::Vector coord = Math::Vector();
Math::Vector normal = Math::Vector();
glm::vec2 texCoord = { 0, 0 };
glm::vec2 texCoord2 = { 0, 0 };
2012-09-19 21:50:28 +00:00
//! Sets the fields from Vertex with texCoord2 = (0,0)
void FromVertex(const Vertex &v)
{
coord = v.coord;
normal = v.normal;
texCoord = v.texCoord;
texCoord2 = { 0, 0 };
}
2012-06-20 17:34:54 +00:00
};
2021-06-06 15:03:59 +00:00
/**
* \struct Vertex2D
* \brief 2D vertex for interface rendering, contains UV and color
*/
struct Vertex2D
{
2021-08-08 11:54:58 +00:00
glm::vec2 position = { 0.0f, 0.0f };
2021-06-06 15:03:59 +00:00
glm::vec2 uv = { 0.0f, 0.0f };
glm::u8vec4 color = { 255, 255, 255, 255 };
};
2012-09-19 21:50:28 +00:00
2021-08-08 11:54:58 +00:00
/**
* \struct Vertex3D
* \brief 3D vertex for 3D rendering, contains UV, color and normal
*/
struct Vertex3D
{
glm::vec3 position = { 0.0f, 0.0f, 0.0f };
glm::u8vec4 color = { 255, 255, 255, 255 };
glm::vec2 uv = { 0.0f, 0.0f };
glm::vec2 uv2 = { 0.0f, 0.0f };
glm::vec3 normal = { 0.0f, 0.0f, 1.0f };
2021-08-08 11:54:58 +00:00
Vertex3D() = default;
Vertex3D(const Vertex& vertex)
: position(vertex.coord)
, uv(vertex.texCoord)
, normal(vertex.normal)
{
}
Vertex3D(const VertexTex2& vertex)
: position(vertex.coord)
, uv(vertex.texCoord)
, uv2(vertex.texCoord2)
, normal(vertex.normal)
{
}
operator Vertex() const
{
return Vertex{ position, normal, uv };
}
operator VertexTex2() const
{
return VertexTex2{ position, normal, uv, uv2 };
}
};
} // namespace Gfx