Animated texture demo

This commit is contained in:
2024-06-04 09:23:13 -04:00
parent 7071050158
commit 970301a5e7
12 changed files with 37 additions and 21 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

@@ -37,7 +37,7 @@ public:
void setError(ENGINE_ERROR_CODE code, bool critical);
EngineError getError();
bool fullscreen;
bool debug = true;
bool debug = false;
bool useVBO = true;
u64 tickCount = 0;
u64 frameCount = 0;

View File

@@ -26,5 +26,14 @@ Cube::Cube() {
}
void Cube::loadTexture() {
MultiTexture texture(this, texturePath.c_str(), true);
//MultiTexture texture(this, texturePath.c_str(), true);
std::vector<std::string> textures;
textures.emplace_back("assets/textures/motion/default.png");
textures.emplace_back("assets/textures/motion/1.png");
textures.emplace_back("assets/textures/motion/2.png");
textures.emplace_back("assets/textures/motion/3.png");
textures.emplace_back("assets/textures/motion/4.png");
textures.emplace_back("assets/textures/motion/5.png");
textures.emplace_back("assets/textures/motion/6.png");
MotionTexture texture(this, textures, 250, true);
}

View File

@@ -79,11 +79,15 @@ VertexArray* Entity::getGeometry() {
void Entity::render() {
bool multiTexture = false;
bool motionTexture = false;
//Done to avoid using the dynamic cast more than once because it's comparatively slow.
if (auto* t = dynamic_cast<MultiTexture*>(this->getTexture()))
if (auto* t = dynamic_cast<MultiTexture*>(getTexture()))
multiTexture = true;
if (!multiTexture)
if (auto* t = dynamic_cast<MotionTexture*>(getTexture()))
motionTexture = true;
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glRotatef(angle.x, 1.0f, 0.0f, 0.0f);
@@ -98,7 +102,7 @@ void Entity::render() {
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
glBindTexture(GL_TEXTURE_2D, motionTexture ? ((MotionTexture*)getTexture())->current()->id : getTexture()->id);
if (!engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x),
@@ -109,7 +113,6 @@ void Entity::render() {
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
if (!multiTexture)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND),
getGeometry()->draw();
if (multiTexture) {

View File

@@ -86,26 +86,30 @@ MotionTexture::MotionTexture(Entity* entity, const std::vector<std::string>& tex
}
Texture* MotionTexture::current() {
//TODO this will look stupid if their frameDelta is greater than the animation speed. In that event, skip to the frame we should be on.
//If animation is disabled.
if (!doAnim)
return this;
///If it's our first time getting what the current texture should be.
//If it's our first time getting the current texture
if (lastUpdate == std::chrono::high_resolution_clock::time_point()) {
lastUpdate = std::chrono::high_resolution_clock::now();
lastDisplayedIndex = -1;
lastDisplayedIndex = 0;
return this;
}
///If the time between now and the last time the texture was drawn is greater than lastUpdate+msDelay.
if ((std::chrono::high_resolution_clock::now().time_since_epoch()) - lastUpdate.time_since_epoch() > (lastUpdate.time_since_epoch() + std::chrono::milliseconds(msDelay))) {
auto now = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastUpdate).count();
if (elapsed >= msDelay) {
lastDisplayedIndex++;
///If we'd be getting an object outside the bounds of the vector then we'd reset the loop to the base.
if (lastDisplayedIndex >= motion.size()) {
lastUpdate = std::chrono::high_resolution_clock::now();
lastDisplayedIndex = -1;
return this;
}
lastUpdate = std::chrono::high_resolution_clock::now();
return &motion[lastDisplayedIndex];
if (lastDisplayedIndex > motion.size())
lastDisplayedIndex = 0;
lastUpdate = now;
}
}
if (lastDisplayedIndex == 0)
return this;
return &motion[lastDisplayedIndex - 1];
}

View File

@@ -122,7 +122,7 @@ OBB VertexArray::getCachedOBB(const Vector3& rotation) const {
return {cachedOBB.pos, cachedOBB.r, axis0, axis1, axis2};
}
OBB VertexArray::getCachedOBB(const EulerAngle &rotation) const {
OBB VertexArray::getCachedOBB(const EulerAngle& rotation) const {
Matrix3x3 rotationMatrix = Matrix3x3::FromQuat(Quaternion( rotation));
Vector3 axis0 = {rotationMatrix[0][0], rotationMatrix[1][0], rotationMatrix[2][0]};
Vector3 axis1 = {rotationMatrix[0][1], rotationMatrix[1][1], rotationMatrix[2][1]};