2014-05-19 05:49:16 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2014-05-19 05:49:16 +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.
|
|
|
|
****************************************************************************/
|
2019-01-30 09:35:17 +08:00
|
|
|
#pragma once
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
#include <string>
|
2015-12-01 10:37:53 +08:00
|
|
|
#include <map>
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-06-17 10:49:52 +08:00
|
|
|
#include "3d/CCBundle3DData.h"
|
2014-08-22 13:52:03 +08:00
|
|
|
#include "3d/CCAABB.h"
|
2019-01-30 09:35:17 +08:00
|
|
|
#include "3d/CC3DProgramInfo.h"
|
2014-06-17 10:49:52 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
#include "base/CCRef.h"
|
|
|
|
#include "math/CCMath.h"
|
2014-08-22 13:52:03 +08:00
|
|
|
#include "renderer/CCMeshCommand.h"
|
2019-01-30 09:35:17 +08:00
|
|
|
#include "renderer/CCCustomCommand.h"
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2015-03-27 14:10:41 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup _3d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
class Texture2D;
|
|
|
|
class MeshSkin;
|
|
|
|
class MeshIndexData;
|
2015-05-06 04:07:32 +08:00
|
|
|
class Material;
|
|
|
|
class Renderer;
|
|
|
|
class Scene;
|
2015-05-13 10:58:09 +08:00
|
|
|
class Pass;
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
namespace backend
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
class Buffer;
|
2019-01-30 09:35:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2015-03-27 14:10:41 +08:00
|
|
|
* @brief Mesh: contains ref to index buffer, GLProgramState, texture, skin, blend function, aabb and so on
|
2014-08-22 13:52:03 +08:00
|
|
|
*/
|
2014-10-09 09:30:23 +08:00
|
|
|
class CC_DLL Mesh : public Ref
|
2014-08-22 13:52:03 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
friend class MeshRenderer;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
public:
|
2022-07-03 17:24:10 +08:00
|
|
|
|
2015-09-09 11:37:41 +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,
|
|
|
|
const IndexArray& indices);
|
|
|
|
|
2015-03-30 15:47:47 +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,
|
|
|
|
const std::vector<MeshVertexAttrib>& attribs);
|
|
|
|
|
|
|
|
/**
|
2015-03-30 15:47:47 +08:00
|
|
|
* create mesh
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static Mesh* create(std::string_view name, MeshIndexData* indexData, MeshSkin* skin = nullptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* get vertex buffer
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
|
|
|
*/
|
2019-01-30 09:35:17 +08:00
|
|
|
backend::Buffer* getVertexBuffer() const;
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* has vertex attribute?
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2019-01-30 09:35:17 +08:00
|
|
|
bool hasVertexAttrib(shaderinfos::VertexKey attrib) const;
|
2014-08-22 13:52:03 +08:00
|
|
|
/**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;
|
|
|
|
|
2015-12-01 10:37:53 +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)
|
2015-12-01 10:37:53 +08:00
|
|
|
* @param texPath texture path
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setTexture(std::string_view texPath);
|
2015-12-01 10:37:53 +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)
|
2015-12-01 10:37:53 +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
|
2015-12-17 16:53:38 +08:00
|
|
|
* @param whether refresh the cache file name
|
2015-12-01 10:37:53 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setTexture(Texture2D* tex, NTextureData::Usage usage, bool cacheFileName = true);
|
2015-12-01 10:37:53 +08:00
|
|
|
/**
|
|
|
|
* set texture
|
|
|
|
* @param texPath texture path
|
|
|
|
* @param usage Usage of this texture
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setTexture(std::string_view texPath, NTextureData::Usage usage);
|
2015-12-01 10:37:53 +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)
|
2015-12-01 10:37:53 +08:00
|
|
|
* @return Texture used, return the texture of first mesh if multiple meshes exist
|
|
|
|
*/
|
|
|
|
Texture2D* getTexture() const;
|
|
|
|
/**
|
2015-12-09 00:45:39 +08:00
|
|
|
* Get texture
|
2015-12-01 10:37:53 +08:00
|
|
|
* @param usage Usage of returned texture
|
2015-12-09 00:45:39 +08:00
|
|
|
* @return The texture of this usage, return the texture of first mesh if multiple meshes exist
|
2015-12-01 10:37:53 +08:00
|
|
|
*/
|
|
|
|
Texture2D* getTexture(NTextureData::Usage usage);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
/**visible getter and setter*/
|
|
|
|
void setVisible(bool visible);
|
2015-05-06 04:07:32 +08:00
|
|
|
bool isVisible() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* skin getter
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-08-22 13:52:03 +08:00
|
|
|
MeshSkin* getSkin() const { return _skin; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* mesh index data getter
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-08-22 13:52:03 +08:00
|
|
|
MeshIndexData* getMeshIndexData() const { return _meshIndexData; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
2019-01-30 09:35:17 +08:00
|
|
|
* get ProgramState
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2015-03-30 15:47:47 +08:00
|
|
|
* @lua NA
|
|
|
|
*/
|
2019-01-30 09:35:17 +08:00
|
|
|
backend::ProgramState* getProgramState() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
/**name getter */
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getName() const { return _name; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void setBlendFunc(const BlendFunc& blendFunc);
|
|
|
|
const BlendFunc& getBlendFunc() const;
|
|
|
|
|
|
|
|
/**
|
2015-03-30 15:47:47 +08:00
|
|
|
* get primitive type
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2019-01-30 09:35:17 +08:00
|
|
|
CustomCommand::PrimitiveType getPrimitiveType() const;
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* get index count
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-08-22 13:52:03 +08:00
|
|
|
ssize_t getIndexCount() const;
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* get index format
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2019-01-30 09:35:17 +08:00
|
|
|
CustomCommand::IndexFormat getIndexFormat() const;
|
2022-07-01 02:44:37 +08:00
|
|
|
/**
|
|
|
|
* set index format
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
void setIndexFormat(CustomCommand::IndexFormat indexFormat);
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* get index buffer
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2019-01-30 09:35:17 +08:00
|
|
|
backend::Buffer* getIndexBuffer() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-22 14:02:51 +08:00
|
|
|
/**get AABB*/
|
|
|
|
const AABB& getAABB() const { return _aabb; }
|
2014-08-22 13:52:03 +08:00
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
/** Sets a new ProgramState for the Mesh
|
2015-05-06 04:07:32 +08:00
|
|
|
* A new Material will be created for it
|
2015-03-20 17:20:22 +08:00
|
|
|
*/
|
2019-01-30 09:35:17 +08:00
|
|
|
void setProgramState(backend::ProgramState* programState);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
/** 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);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
/**skin setter*/
|
|
|
|
void setSkin(MeshSkin* skin);
|
|
|
|
/**Mesh index data setter*/
|
|
|
|
void setMeshIndexData(MeshIndexData* indexdata);
|
|
|
|
/**name setter*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setName(std::string_view name) { _name = name; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-20 17:20:22 +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
|
|
|
|
2015-07-01 14:24:22 +08:00
|
|
|
/**
|
2022-07-05 14:48:46 +08:00
|
|
|
* force set this Mesh renderer to 2D render queue
|
2015-07-01 14:24:22 +08:00
|
|
|
*/
|
|
|
|
void setForce2DQueue(bool force2D) { _force2DQueue = force2D; }
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string getTextureFileName() { return _texFile; }
|
2015-11-13 17:44:23 +08:00
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
Mesh();
|
2015-05-06 04:07:32 +08:00
|
|
|
virtual ~Mesh();
|
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
protected:
|
2015-09-16 18:04:52 +08:00
|
|
|
void resetLightUniformValues();
|
2015-05-13 10:58:09 +08:00
|
|
|
void setLightUniforms(Pass* pass, Scene* scene, const Vec4& color, unsigned int lightmask);
|
2015-05-06 04:07:32 +08:00
|
|
|
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
|
|
|
|
bool _isTransparent; // is this mesh transparent, it is a property of material in fact
|
|
|
|
bool _force2DQueue; // add this mesh to 2D render queue
|
2022-07-01 02:44:37 +08:00
|
|
|
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;
|
|
|
|
|
2015-09-16 18:04:52 +08:00
|
|
|
std::vector<Vec3> _pointLightUniformColorValues;
|
|
|
|
std::vector<Vec3> _pointLightUniformPositionValues;
|
|
|
|
std::vector<float> _pointLightUniformRangeInverseValues;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-16 18:04:52 +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;
|
2015-11-13 17:44:23 +08:00
|
|
|
|
|
|
|
std::string _texFile;
|
2014-08-22 13:52:03 +08:00
|
|
|
};
|
2014-06-16 20:58:13 +08:00
|
|
|
|
2015-04-01 10:23:53 +08:00
|
|
|
// end of 3d group
|
2015-03-27 14:10:41 +08:00
|
|
|
/// @}
|
|
|
|
|
2015-12-01 10:37:53 +08:00
|
|
|
/// @cond
|
2021-12-25 10:04:45 +08:00
|
|
|
extern std::string CC_DLL s_uniformSamplerName[]; // uniform sampler names array
|
2015-12-01 10:37:53 +08:00
|
|
|
/// @endcond
|
2015-03-27 14:10:41 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|