This commit is contained in:
Vladislav Khorev 2025-12-05 21:27:57 +03:00
parent 05bf63d447
commit 2b19a3eaba
21 changed files with 309 additions and 0 deletions

3
.gitattributes vendored Normal file
View File

@ -0,0 +1,3 @@
*.bmp filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text

View File

@ -44,6 +44,25 @@ void Game::setup() {
renderer.shaderManager.AddShaderFromFiles("default", "./shaders/default.vertex", "./shaders/default_desktop.fragment", CONST_ZIP_FILE);
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./shaders/defaultColor.vertex", "./shaders/defaultColor_desktop.fragment", CONST_ZIP_FILE);
#endif
renderer.shaderManager.AddShaderFromFiles("env", "./shaders/env.vertex", "./shaders/env.fragment", CONST_ZIP_FILE);
// std::array<std::string, 6> cubemapTextureNightStr = {
//"../resources/sky/space_rt.bmp", "../resources/sky/space_lf.bmp", "../resources/sky/space_up.bmp", "../resources/sky/space_dn.bmp", "../resources/sky/space_bk.bmp", "../resources/sky/space_ft.bmp"
// };
cubemapTexture = std::make_shared<Texture>(
std::array<TextureDataStruct, 6>{
CreateTextureDataFromBmp24("./resources/sky/space_rt.bmp"),
CreateTextureDataFromBmp24("./resources/sky/space_lf.bmp"),
CreateTextureDataFromBmp24("./resources/sky/space_up.bmp"),
CreateTextureDataFromBmp24("./resources/sky/space_dn.bmp"),
CreateTextureDataFromBmp24("./resources/sky/space_bk.bmp"),
CreateTextureDataFromBmp24("./resources/sky/space_ft.bmp")
});
cubemap.data = ZL::CreateCubemap(1000);
cubemap.RefreshVBO();
//Load texture
spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/sship001x.png"));
@ -59,6 +78,8 @@ void Game::setup() {
void Game::drawScene() {
static const std::string defaultShaderName = "default";
static const std::string envShaderName = "env";
static const std::string vPositionName = "vPosition";
static const std::string vTexCoordName = "vTexCoord";
static const std::string textureUniformName = "Texture";
@ -70,6 +91,31 @@ void Game::drawScene() {
CheckGlError();
renderer.shaderManager.PushShader(envShaderName);
renderer.RenderUniform1i(textureUniformName, 0);
renderer.EnableVertexAttribArray(vPositionName);
renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5,
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
1, 1000);
renderer.PushMatrix();
renderer.LoadIdentity();
CheckGlError();
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture->getTexID());
renderer.DrawVertexRenderStruct(cubemap);
CheckGlError();
renderer.PopMatrix();
renderer.PopProjectionMatrix();
renderer.DisableVertexAttribArray(vPositionName);
renderer.shaderManager.PopShader();
CheckGlError();
renderer.shaderManager.PushShader(defaultShaderName);
renderer.RenderUniform1i(textureUniformName, 0);
renderer.EnableVertexAttribArray(vPositionName);

3
Game.h
View File

@ -33,8 +33,11 @@ private:
static const size_t CONST_MAX_TIME_INTERVAL = 1000;
std::shared_ptr<Texture> spaceshipTexture;
std::shared_ptr<Texture> cubemapTexture;
VertexDataStruct spaceshipBase;
VertexRenderStruct spaceship;
VertexRenderStruct cubemap;
};
} // namespace ZL

View File

@ -196,6 +196,71 @@ namespace ZL {
return result;
}
VertexDataStruct CreateCubemap(float scale)
{
VertexDataStruct cubemapVertexDataStruct;
// +x
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, scale });
// -x
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, scale });
// +y
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, scale });
// -y
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, scale });
// +z
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, scale });
// -z
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, -scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ scale, scale, -scale });
cubemapVertexDataStruct.PositionData.push_back({ -scale, scale, -scale });
return cubemapVertexDataStruct;
}
void VertexRenderStruct::RefreshVBO()
{
//Check if main thread, check if data is not empty...

View File

@ -75,6 +75,7 @@ namespace ZL {
VertexDataStruct CreateRect2D(Vector2f center, Vector2f halfWidthHeight, float zLevel);
VertexDataStruct CreateRectHorizontalSections2D(Vector2f center, Vector2f halfWidthHeight, float zLevel, size_t sectionCount);
VertexDataStruct CreateCube3D(float scale);
VertexDataStruct CreateCubemap(float scale = 1000.f);
class Renderer

View File

@ -46,6 +46,77 @@ namespace ZL
}
Texture::Texture(const std::array<TextureDataStruct, 6>& texDataArray)
{
// Ïðîâåðêà, ÷òî âñå ãðàíè èìåþò îäèíàêîâûå ðàçìåðû
width = texDataArray[0].width;
height = texDataArray[0].height;
for (size_t i = 1; i < 6; ++i) {
if (texDataArray[i].width != width || texDataArray[i].height != height) {
throw std::runtime_error("Cubemap faces must have the same dimensions");
}
}
glGenTextures(1, &texID);
if (texID == 0)
{
throw std::runtime_error("glGenTextures did not work for cubemap");
}
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
CheckGlError();
// Íàñòðîéêà ïàðàìåòðîâ äëÿ Cubemap
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Îáÿçàòåëüíûå ïàðàìåòðû îáåðòêè äëÿ Cubemap
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
CheckGlError();
// Çàãðóçêà äàííûõ äëÿ êàæäîé èç 6 ãðàíåé
// GL_TEXTURE_CUBE_MAP_POSITIVE_X + i äàåò ãðàíè:
// +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
for (int i = 0; i < 6; ++i)
{
GLint internalFormat;
GLenum format;
if (texDataArray[i].bitSize == TextureDataStruct::BS_24BIT)
{
internalFormat = GL_RGB;
format = GL_RGB;
}
else // BS_32BIT
{
internalFormat = GL_RGBA;
format = GL_RGBA;
}
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, // Öåëåâàÿ ãðàíü
0, // Óðîâåíü MIP-òåêñòóðû
internalFormat, // Âíóòðåííèé ôîðìàò (íàïðèìåð, GL_RGB)
static_cast<GLsizei>(width),
static_cast<GLsizei>(height),
0, // Ãðàíèöà (âñåãäà 0)
format, // Ôîðìàò èñõîäíûõ äàííûõ (íàïðèìåð, GL_RGB)
GL_UNSIGNED_BYTE, // Òèï äàííûõ
texDataArray[i].data.data() // Óêàçàòåëü íà äàííûå
);
CheckGlError();
}
// Ñíèìàåì ïðèâÿçêó äëÿ ÷èñòîòû
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
Texture::~Texture()
{
glDeleteTextures(1, &texID);
@ -282,4 +353,6 @@ namespace ZL
return texData;
}
#endif
}

View File

@ -29,6 +29,9 @@ namespace ZL
Texture(const TextureDataStruct& texData);
//Cubemap texture:
Texture(const std::array<TextureDataStruct, 6>& texDataArray);
~Texture();
GLuint getTexID();
@ -44,4 +47,6 @@ namespace ZL
#ifdef PNG_ENABLED
TextureDataStruct CreateTextureDataFromPng(const std::string& fullFileName);
#endif
}

BIN
resources/sky/space_bk.bmp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
resources/sky/space_dn.bmp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
resources/sky/space_ft.bmp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
resources/sky/space_lf.bmp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
resources/sky/space_rt.bmp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
resources/sky/space_up.bmp (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 653 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 599 KiB

13
shaders/env.fragment Normal file
View File

@ -0,0 +1,13 @@
uniform samplerCube Texture;
varying vec3 dir;
void main(){
gl_FragColor = textureCube(Texture, normalize(dir));
//if (dir.z < 0)
//{
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
//}
//gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

12
shaders/env.vertex Normal file
View File

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

View File

@ -0,0 +1,21 @@
uniform samplerCube Env;
varying vec3 dir;
void main(){
gl_FragColor = textureCube(Env, normalize(dir));
vec4 fogColor = vec4(0.25, 0.55, 1.0, 1.0);
float coef = dir.y+1.0;
coef = clamp(coef, 0.0, 1.0);
gl_FragColor = mix(gl_FragColor,fogColor, coef);
//if (dir.z < 0)
//{
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
//}
//gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

View File

@ -0,0 +1,14 @@
attribute vec3 vPosition;
uniform mat4 ProjectionMatrix;
uniform mat3 ModelRotateMatrix;
uniform vec3 ModelTranslateVector;
varying vec3 dir;
void main(){
vec4 realVertexPos = vec4(ModelRotateMatrix * vPosition.xyz + ModelTranslateVector, 1.0);
gl_Position = ProjectionMatrix * realVertexPos;
dir = -vPosition;
}

View File

@ -0,0 +1,21 @@
uniform samplerCube Env;
varying vec3 dir;
void main(){
gl_FragColor = textureCube(Env, normalize(dir));
vec4 fogColor = vec4(0.05, 0.05, 0.1, 1.0);
float coef = dir.y+1.0;
coef = clamp(coef, 0.0, 1.0);
gl_FragColor = mix(gl_FragColor,fogColor, coef);
//if (dir.z < 0)
//{
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
//}
//gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

View File

@ -0,0 +1,14 @@
attribute vec3 vPosition;
uniform mat4 ProjectionMatrix;
uniform mat3 ModelRotateMatrix;
uniform vec3 ModelTranslateVector;
varying vec3 dir;
void main(){
vec4 realVertexPos = vec4(ModelRotateMatrix * vPosition.xyz + ModelTranslateVector, 1.0);
gl_Position = ProjectionMatrix * realVertexPos;
dir = -vPosition;
}