Added color cube rotating in 3d

This commit is contained in:
Vladislav Khorev 2025-02-22 20:58:34 +03:00
parent 2876446ec8
commit c792e50f1b
9 changed files with 242 additions and 14 deletions

View File

@ -33,6 +33,9 @@ namespace ZL
VertexDataStruct pipeMesh;
VertexDataStruct gameOverMesh;
VertexDataStruct colorCubeMesh;
VertexDataStruct colorCubeMeshMutable;
}
@ -225,6 +228,7 @@ namespace ZL
void GameState::UpdateScene(size_t tickCountDiff)
{
rotateTimer += tickCountDiff * 0.001f;
if (isGameOver)
{
@ -238,6 +242,8 @@ namespace ZL
UpdateBackgroundPos(tickCountDiff);
UpdatePhysics(tickCountDiff);
}
void GameState::BirdJump()

5
Game.h
View File

@ -76,6 +76,8 @@ namespace ZL
bool isGameOver = false;
float rotateTimer = 0.0;
std::vector<PipePairConfig> pipePairArr;
EllipsePhysicsObject birdEllipse;
@ -103,5 +105,8 @@ namespace ZL
extern VertexDataStruct pipeMesh;
extern VertexDataStruct gameOverMesh;
extern VertexDataStruct colorCubeMesh;
extern VertexDataStruct colorCubeMeshMutable;
}
}

View File

@ -201,6 +201,30 @@ namespace ZL {
}
Vector4f QuatFromRotateAroundX(float angle)
{
Vector4f result;
result.v[0] = sinf(angle * 0.5f);
result.v[1] = 0.f;
result.v[2] = 0.f;
result.v[3] = cosf(angle * 0.5f);
return result;
}
Vector4f QuatFromRotateAroundY(float angle)
{
Vector4f result;
result.v[0] = 0.f;
result.v[1] = sinf(angle * 0.5f);
result.v[2] = 0.f;
result.v[3] = cosf(angle * 0.5f);
return result;
}
Vector4f QuatFromRotateAroundZ(float angle)
{
Vector4f result;
@ -334,5 +358,26 @@ namespace ZL {
return r;
}
Vector3f operator*(Vector3f v, float scale)
{
Vector3f r = v;
r.v[0] = v.v[0] * scale;
r.v[1] = v.v[1] * scale;
r.v[2] = v.v[2] * scale;
return r;
}
Vector3f MultVectorMatrix(Vector3f v, Matrix3f mt)
{
Vector3f r;
r.v[0] = v.v[0] * mt.m[0] + v.v[1] * mt.m[1] + v.v[2] * mt.m[2];
r.v[1] = v.v[0] * mt.m[3] + v.v[1] * mt.m[4] + v.v[2] * mt.m[5];
r.v[2] = v.v[0] * mt.m[6] + v.v[1] * mt.m[7] + v.v[2] * mt.m[8];
return r;
}
};

6
Math.h
View File

@ -53,6 +53,12 @@ namespace ZL {
Matrix3f QuatToMatrix(const Vector4f& q);
Vector4f QuatFromRotateAroundX(float angle);
Vector4f QuatFromRotateAroundY(float angle);
Vector4f QuatFromRotateAroundZ(float angle);
Vector3f operator*(Vector3f v, float scale);
Vector3f MultVectorMatrix(Vector3f v, Matrix3f mt);
};

View File

@ -121,6 +121,82 @@ namespace ZL {
}
VertexDataStruct CreateCube3D(float scale)
{
std::array<std::array<Vector3f, 4>, 6> cubeSides;
std::array<Vector3f, 6> cubeColors;
cubeSides[0][0] = { -1, -1, -1 };
cubeSides[0][1] = { -1, 1, -1 };
cubeSides[0][2] = { 1, 1, -1 };
cubeSides[0][3] = { 1, -1, -1 };
cubeSides[1][0] = { -1, -1, 1 };
cubeSides[1][1] = { -1, 1, 1 };
cubeSides[1][2] = { 1, 1, 1 };
cubeSides[1][3] = { 1, -1, 1 };
//------------
cubeSides[2][0] = { -1, -1, -1 };
cubeSides[2][1] = { -1, -1, 1 };
cubeSides[2][2] = { 1, -1, 1 };
cubeSides[2][3] = { 1, -1, -1 };
cubeSides[3][0] = { -1, 1, -1 };
cubeSides[3][1] = { -1, 1, 1 };
cubeSides[3][2] = { 1, 1, 1 };
cubeSides[3][3] = { 1, 1, -1 };
//------------
cubeSides[4][0] = { -1, -1, -1 };
cubeSides[4][1] = { -1, -1, 1 };
cubeSides[4][2] = { -1, 1, 1 };
cubeSides[4][3] = { -1, 1, -1 };
cubeSides[5][0] = { 1, -1, -1 };
cubeSides[5][1] = { 1, -1, 1 };
cubeSides[5][2] = { 1, 1, 1 };
cubeSides[5][3] = { 1, 1, -1 };
//-----------
cubeColors[0] = Vector3f{ 1, 0, 0 };
cubeColors[1] = Vector3f{ 0, 1, 0 };
cubeColors[2] = Vector3f{ 0, 0, 1 };
cubeColors[3] = Vector3f{ 1, 1, 0 };
cubeColors[4] = Vector3f{ 0, 1, 1 };
cubeColors[5] = Vector3f{ 1, 0, 1 };
//-----------
VertexDataStruct result;
for (int i = 0; i < 6; i++)
{
result.PositionData.push_back(cubeSides[i][0] * scale);
result.PositionData.push_back(cubeSides[i][1] * scale);
result.PositionData.push_back(cubeSides[i][2] * scale);
result.PositionData.push_back(cubeSides[i][2] * scale);
result.PositionData.push_back(cubeSides[i][3] * scale);
result.PositionData.push_back(cubeSides[i][0] * scale);
result.ColorData.push_back(cubeColors[i]);
result.ColorData.push_back(cubeColors[i]);
result.ColorData.push_back(cubeColors[i]);
result.ColorData.push_back(cubeColors[i]);
result.ColorData.push_back(cubeColors[i]);
result.ColorData.push_back(cubeColors[i]);
}
result.RefreshVBO();
return result;
}
void VertexDataStruct::RefreshVBO()
{
//Check if main thread, check if data is not empty...
@ -204,6 +280,44 @@ namespace ZL {
}
}
void VertexDataStruct::RotateByMatrix(Matrix3f m)
{
for (int i = 0; i < PositionData.size(); i++)
{
PositionData[i] = MultVectorMatrix(PositionData[i], m);
}
for (int i = 0; i < NormalData.size(); i++)
{
NormalData[i] = MultVectorMatrix(NormalData[i], m);
}
for (int i = 0; i < TangentData.size(); i++)
{
TangentData[i] = MultVectorMatrix(TangentData[i], m);
}
for (int i = 0; i < BinormalData.size(); i++)
{
BinormalData[i] = MultVectorMatrix(BinormalData[i], m);
}
RefreshVBO();
}
void VertexDataStruct::AssignFrom(const VertexDataStruct& v)
{
PositionData = v.PositionData;
NormalData = v.NormalData;
TangentData = v.TangentData;
BinormalData = v.BinormalData;
TexCoordData = v.TexCoordData;
ColorData = v.ColorData;
RefreshVBO();
}
void Renderer::InitOpenGL()
{
ModelviewMatrixStack.push(Matrix4f::Identity());
@ -499,22 +613,22 @@ namespace ZL {
if (vertexDataStruct.NormalData.size() > 0)
{
glBindBuffer(GL_ARRAY_BUFFER, vertexDataStruct.normalVBO->getBuffer());
VertexAttribPointer2fv(vNormal, 0, NULL);
VertexAttribPointer3fv(vNormal, 0, NULL);
}
if (vertexDataStruct.TangentData.size() > 0)
{
glBindBuffer(GL_ARRAY_BUFFER, vertexDataStruct.tangentVBO->getBuffer());
VertexAttribPointer2fv(vTangent, 0, NULL);
VertexAttribPointer3fv(vTangent, 0, NULL);
}
if (vertexDataStruct.BinormalData.size() > 0)
{
glBindBuffer(GL_ARRAY_BUFFER, vertexDataStruct.binormalVBO->getBuffer());
VertexAttribPointer2fv(vBinormal, 0, NULL);
VertexAttribPointer3fv(vBinormal, 0, NULL);
}
if (vertexDataStruct.ColorData.size() > 0)
{
glBindBuffer(GL_ARRAY_BUFFER, vertexDataStruct.colorVBO->getBuffer());
VertexAttribPointer2fv(vColor, 0, NULL);
VertexAttribPointer3fv(vColor, 0, NULL);
}
if (vertexDataStruct.TexCoordData.size() > 0)
{

View File

@ -58,11 +58,15 @@ namespace ZL {
std::shared_ptr<VBOHolder> binormalVBO;
std::shared_ptr<VBOHolder> colorVBO;
void RefreshVBO();
void RotateByMatrix(Matrix3f m);
void AssignFrom(const VertexDataStruct& v);
};
VertexDataStruct CreateRect2D(Vector2f center, Vector2f halfWidthHeight, float zLevel);
VertexDataStruct CreateRectHorizontalSections2D(Vector2f center, Vector2f halfWidthHeight, float zLevel, size_t sectionCount);
//VertexDataStruct CreateRect2D(Vector2f center, Vector2f halfWidthHeight, float zLevel);
VertexDataStruct CreateCube3D(float scale);
class Renderer

8
defaultColor.fragment Normal file
View File

@ -0,0 +1,8 @@
precision mediump float;
varying vec3 color;
void main()
{
gl_FragColor = vec4(color, 1.0);
}

11
defaultColor.vertex Normal file
View File

@ -0,0 +1,11 @@
attribute vec3 vPosition;
attribute vec3 vColor;
varying vec3 color;
uniform mat4 ProjectionModelViewMatrix;
void main()
{
gl_Position = ProjectionModelViewMatrix * vec4(vPosition.xyz, 1.0);
color = vColor;
}

View File

@ -119,37 +119,62 @@ namespace ZL
void DrawScene()
{
static const std::string defaultShaderName = "default";
static const std::string colorShaderName = "defaultColor";
static const std::string vPositionName = "vPosition";
static const std::string vTexCoordName = "vTexCoord";
static const std::string vColorName = "vColor";
static const std::string textureUniformName = "Texture";
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClearColor(0.0f, 0.1f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glViewport(0, 0, Env::width, Env::height);
renderer.shaderManager.PushShader(defaultShaderName);
renderer.shaderManager.PushShader(colorShaderName);
renderer.RenderUniform1i(textureUniformName, 0);
//renderer.RenderUniform1i(textureUniformName, 0);
renderer.EnableVertexAttribArray(vPositionName);
renderer.EnableVertexAttribArray(vTexCoordName);
renderer.EnableVertexAttribArray(vColorName);
renderer.PushProjectionMatrix(static_cast<float>(Env::width), static_cast<float>(Env::height));
//renderer.PushPerspectiveProjectionMatrix(1.0 / 6.0, static_cast<float>(Env::width)/ static_cast<float>(Env::height), 10, 10000);
renderer.PushPerspectiveProjectionMatrix(1.0 / 6.0, static_cast<float>(Env::width)/ static_cast<float>(Env::height), 10, 10000);
renderer.PushMatrix();
renderer.LoadIdentity();
//renderer.RotateMatrix(QuatFromRotateAroundZ(gs.rotateTimer * M_PI / 3.0));
renderer.TranslateMatrix({ 0,0, -1000 });
//
renderer.RotateMatrix(QuatFromRotateAroundX(-M_PI / 3.0));
GameObjects::colorCubeMeshMutable.AssignFrom(GameObjects::colorCubeMesh);
GameObjects::colorCubeMeshMutable.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundZ(gs.rotateTimer * M_PI / 3.0)));
renderer.DrawVertexDataStruct(GameObjects::colorCubeMeshMutable);
renderer.PopMatrix();
/*
DrawBackground();
DrawPipes();
DrawBird();
DrawUi();
DrawUi();*/
renderer.PopProjectionMatrix();
renderer.DisableVertexAttribArray(vTexCoordName);
renderer.DisableVertexAttribArray(vColorName);
renderer.DisableVertexAttribArray(vPositionName);
renderer.shaderManager.PopShader();
@ -206,7 +231,8 @@ namespace ZL
//Load shaders:
renderer.shaderManager.AddShaderFromFiles("default", "./default.vertex", "./default.fragment");
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./defaultColor.vertex", "./defaultColor.fragment");
//Load textures
GameObjects::birdTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp32("./bird.bmp32"));
GameObjects::backgroundTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp24("./background.bmp"));
@ -240,6 +266,9 @@ namespace ZL
GameObjects::gameOverMesh = CreateRect2D({ GAMEOVER_WIDTH * gameOverTextureScale * 0.5f, GAMEOVER_HEIGHT * gameOverTextureScale * 0.5f }, { GAMEOVER_WIDTH * gameOverTextureScale * 0.5f, GAMEOVER_HEIGHT * gameOverTextureScale * 0.5f }, 0.1f);
GameObjects::colorCubeMesh = CreateCube3D(5.0);
GameObjects::colorCubeMeshMutable = CreateCube3D(5.0);
//Set some game values
Env::birdStartPos = { Env::width * 0.2f, Env::getActualClientHeight() * 0.5f };