axmol/core/3d/Mesh.h

295 lines
8.4 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
#include <string>
#include <map>
#include "3d/Bundle3DData.h"
#include "3d/AABB.h"
#include "3d/3DProgramInfo.h"
2019-11-23 20:27:39 +08:00
#include "base/Ref.h"
#include "math/Math.h"
#include "renderer/MeshCommand.h"
#include "renderer/CustomCommand.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _3d
* @{
*/
class Texture2D;
class MeshSkin;
class MeshIndexData;
class Material;
class Renderer;
class Scene;
class Pass;
namespace backend
{
2021-12-25 10:04:45 +08:00
class Buffer;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* @brief Mesh: contains ref to index buffer, GLProgramState, texture, skin, blend function, aabb and so on
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Mesh : public Ref
2019-11-23 20:27:39 +08:00
{
friend class MeshRenderer;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
2019-11-23 20:27:39 +08:00
/**create mesh from positions, normals, and so on, single SubMesh*/
2021-12-25 10:04:45 +08:00
static Mesh* create(const std::vector<float>& positions,
const std::vector<float>& normals,
const std::vector<float>& texs,
2022-07-03 21:17:49 +08:00
const IndexArray& indices);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @lua NA
*/
2021-12-25 10:04:45 +08:00
static Mesh* create(const std::vector<float>& vertices,
int perVertexSizeInFloat,
const IndexArray& indices,
2022-07-03 21:17:49 +08:00
const std::vector<MeshVertexAttrib>& attribs);
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* create mesh
* @lua NA
*/
static Mesh* create(std::string_view name, MeshIndexData* indexData, MeshSkin* skin = nullptr);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* get vertex buffer
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* @lua NA
*/
backend::Buffer* getVertexBuffer() const;
/**
* has vertex attribute?
*
* @lua NA
*/
bool hasVertexAttrib(shaderinfos::VertexKey attrib) const;
/**get mesh vertex attribute count*/
ssize_t getMeshVertexAttribCount() const;
/**get MeshVertexAttribute by index*/
const MeshVertexAttrib& getMeshVertexAttribute(int idx);
/**get per vertex size in bytes*/
int getVertexSizeInBytes() const;
/**
2021-12-25 10:04:45 +08:00
* set texture (diffuse), which is responsible for the main appearance. It is also means main texture, you can also
* call setTexture(texPath, NTextureData::Usage::Diffuse)
2019-11-23 20:27:39 +08:00
* @param texPath texture path
*/
void setTexture(std::string_view texPath);
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* set texture (diffuse), which is responsible for the main appearance. It is also means main texture, you can also
* call setTexture(texPath, NTextureData::Usage::Diffuse)
2019-11-23 20:27:39 +08:00
* @param tex texture to be set
*/
void setTexture(Texture2D* tex);
/**
* set texture
* @param tex texture to be set
* @param usage Usage of this texture
* @param whether refresh the cache file name
*/
2021-12-25 10:04:45 +08:00
void setTexture(Texture2D* tex, NTextureData::Usage usage, bool cacheFileName = true);
2019-11-23 20:27:39 +08:00
/**
* set texture
* @param texPath texture path
* @param usage Usage of this texture
*/
void setTexture(std::string_view texPath, NTextureData::Usage usage);
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* Get texture (diffuse), which is responsible for the main appearance. It is also means main texture, you can also
* call getTexture(NTextureData::Usage::Diffuse)
2019-11-23 20:27:39 +08:00
* @return Texture used, return the texture of first mesh if multiple meshes exist
*/
Texture2D* getTexture() const;
/**
* Get texture
* @param usage Usage of returned texture
* @return The texture of this usage, return the texture of first mesh if multiple meshes exist
*/
Texture2D* getTexture(NTextureData::Usage usage);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**visible getter and setter*/
void setVisible(bool visible);
bool isVisible() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* skin getter
*
* @lua NA
*/
MeshSkin* getSkin() const { return _skin; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* mesh index data getter
*
* @lua NA
*/
MeshIndexData* getMeshIndexData() const { return _meshIndexData; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* get ProgramState
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* @lua NA
*/
backend::ProgramState* getProgramState() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**name getter */
std::string_view getName() const { return _name; }
2021-12-25 10:04:45 +08:00
void setBlendFunc(const BlendFunc& blendFunc);
const BlendFunc& getBlendFunc() const;
/**
2019-11-23 20:27:39 +08:00
* get primitive type
*
* @lua NA
*/
CustomCommand::PrimitiveType getPrimitiveType() const;
/**
* get index count
*
* @lua NA
*/
ssize_t getIndexCount() const;
/**
* get index format
*
* @lua NA
*/
CustomCommand::IndexFormat getIndexFormat() const;
/**
* set index format
*
* @lua NA
*/
void setIndexFormat(CustomCommand::IndexFormat indexFormat);
2019-11-23 20:27:39 +08:00
/**
* get index buffer
*
* @lua NA
*/
backend::Buffer* getIndexBuffer() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**get AABB*/
const AABB& getAABB() const { return _aabb; }
/** Sets a new ProgramState for the Mesh
* A new Material will be created for it
*/
void setProgramState(backend::ProgramState* programState);
/** Sets a new Material to the Mesh */
void setMaterial(Material* material);
/** Returns the Material being used by the Mesh */
Material* getMaterial() const;
2021-12-25 10:04:45 +08:00
void draw(Renderer* renderer,
float globalZ,
const Mat4& transform,
uint32_t flags,
unsigned int lightMask,
const Vec4& color,
bool forceDepthWrite,
bool wireframe);
2019-11-23 20:27:39 +08:00
/**skin setter*/
void setSkin(MeshSkin* skin);
/**Mesh index data setter*/
void setMeshIndexData(MeshIndexData* indexdata);
/**name setter*/
void setName(std::string_view name) { _name = name; }
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* calculate the AABB of the mesh
* @note the AABB is in the local space, not the world space
*/
void calculateAABB();
2021-12-25 10:04:45 +08:00
std::string getTextureFileName() { return _texFile; }
2019-11-23 20:27:39 +08:00
Mesh();
2019-11-23 20:27:39 +08:00
virtual ~Mesh();
protected:
void resetLightUniformValues();
void setLightUniforms(Pass* pass, Scene* scene, const Vec4& color, unsigned int lightmask);
void bindMeshCommand();
2021-12-25 10:04:45 +08:00
std::map<NTextureData::Usage, Texture2D*> _textures; // textures that submesh is using
MeshSkin* _skin; // skin
bool _visible; // is the submesh visible
CustomCommand::IndexFormat meshIndexFormat;
2021-12-25 10:04:45 +08:00
std::string _name;
MeshIndexData* _meshIndexData;
// GLProgramState* _glProgramState;
BlendFunc _blend;
bool _blendDirty;
Material* _material;
AABB _aabb;
std::function<void()> _visibleChanged;
std::unordered_map<std::string, std::vector<MeshCommand>> _meshCommands;
/// light parameters
std::vector<Vec3> _dirLightUniformColorValues;
std::vector<Vec3> _dirLightUniformDirValues;
2019-11-23 20:27:39 +08:00
std::vector<Vec3> _pointLightUniformColorValues;
std::vector<Vec3> _pointLightUniformPositionValues;
std::vector<float> _pointLightUniformRangeInverseValues;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::vector<Vec3> _spotLightUniformColorValues;
std::vector<Vec3> _spotLightUniformPositionValues;
std::vector<Vec3> _spotLightUniformDirValues;
std::vector<float> _spotLightUniformInnerAngleCosValues;
std::vector<float> _spotLightUniformOuterAngleCosValues;
std::vector<float> _spotLightUniformRangeInverseValues;
std::string _texFile;
};
// end of 3d group
/// @}
/// @cond
2022-07-16 10:43:05 +08:00
extern std::string AX_DLL s_uniformSamplerName[]; // uniform sampler names array
2019-11-23 20:27:39 +08:00
/// @endcond
NS_AX_END