From 57a7b3ea9db5f0d63df028377bc205f4f3b799a0 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Mon, 1 Jun 2026 10:42:02 +0800 Subject: [PATCH 1/5] Update asset paths and refactor TesselationShaderScene - Switch from external to internal storage for native assets in `GLActivity`. - Rename tessellation shaders and update asset paths for models and textures. - Initialize `rez` and explicitly bind the heightmap texture in `TesselationShaderScene`. - Update tessellation shader constants from `int` to `float` and adjust background clear color. - Refactor model paths in `AsteroidScene` from `models/` to `objects/`. --- .../{8.3.gpuheight.frag => gpuheight.frag} | 0 .../{8.3.gpuheight.tesc => gpuheight.tesc} | 22 +++---- .../{8.3.gpuheight.tese => gpuheight.tese} | 4 +- .../{8.3.gpuheight.vert => gpuheight.vert} | 0 tutorial/src/main/cpp/basic/AsteroidScene.cpp | 4 +- .../main/cpp/basic/TesselationShaderScene.cpp | 64 +++++++++---------- .../main/cpp/basic/TesselationShaderScene.h | 2 +- .../com/minininja/learngles/GLActivity.kt | 2 +- 8 files changed, 48 insertions(+), 50 deletions(-) rename tutorial/src/main/assets/shaders/tesselation_shaders/{8.3.gpuheight.frag => gpuheight.frag} (100%) rename tutorial/src/main/assets/shaders/tesselation_shaders/{8.3.gpuheight.tesc => gpuheight.tesc} (52%) rename tutorial/src/main/assets/shaders/tesselation_shaders/{8.3.gpuheight.tese => gpuheight.tese} (88%) rename tutorial/src/main/assets/shaders/tesselation_shaders/{8.3.gpuheight.vert => gpuheight.vert} (100%) diff --git a/tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.frag b/tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.frag similarity index 100% rename from tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.frag rename to tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.frag diff --git a/tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.tesc b/tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.tesc similarity index 52% rename from tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.tesc rename to tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.tesc index 97672d4f..f7782357 100644 --- a/tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.tesc +++ b/tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.tesc @@ -15,8 +15,8 @@ void main() if(gl_InvocationID == 0) { - const int MIN_TESS_LEVEL = 4; - const int MAX_TESS_LEVEL = 64; + const float MIN_TESS_LEVEL = 4.; + const float MAX_TESS_LEVEL = 64.; const float MIN_DISTANCE = 20.; const float MAX_DISTANCE = 800.; @@ -26,15 +26,15 @@ void main() vec4 eyeSpacePos11 = view * model * gl_in[3].gl_Position; // "distance" from camera scaled between 0 and 1 - float distance00 = clamp( (abs(eyeSpacePos00.z) - MIN_DISTANCE) / (MAX_DISTANCE-MIN_DISTANCE), 0.0, 1.0 ); - float distance01 = clamp( (abs(eyeSpacePos01.z) - MIN_DISTANCE) / (MAX_DISTANCE-MIN_DISTANCE), 0.0, 1.0 ); - float distance10 = clamp( (abs(eyeSpacePos10.z) - MIN_DISTANCE) / (MAX_DISTANCE-MIN_DISTANCE), 0.0, 1.0 ); - float distance11 = clamp( (abs(eyeSpacePos11.z) - MIN_DISTANCE) / (MAX_DISTANCE-MIN_DISTANCE), 0.0, 1.0 ); - - float tessLevel0 = mix( MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance10, distance00) ); - float tessLevel1 = mix( MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance00, distance01) ); - float tessLevel2 = mix( MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance01, distance11) ); - float tessLevel3 = mix( MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance11, distance10) ); + float distance00 = clamp((abs(eyeSpacePos00.z) - MIN_DISTANCE) / (MAX_DISTANCE - MIN_DISTANCE), 0.0, 1.0); + float distance01 = clamp((abs(eyeSpacePos01.z) - MIN_DISTANCE) / (MAX_DISTANCE - MIN_DISTANCE), 0.0, 1.0); + float distance10 = clamp((abs(eyeSpacePos10.z) - MIN_DISTANCE) / (MAX_DISTANCE - MIN_DISTANCE), 0.0, 1.0); + float distance11 = clamp((abs(eyeSpacePos11.z) - MIN_DISTANCE) / (MAX_DISTANCE - MIN_DISTANCE), 0.0, 1.0); + + float tessLevel0 = mix(MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance10, distance00)); + float tessLevel1 = mix(MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance00, distance01)); + float tessLevel2 = mix(MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance01, distance11)); + float tessLevel3 = mix(MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance11, distance10)); gl_TessLevelOuter[0] = tessLevel0; gl_TessLevelOuter[1] = tessLevel1; diff --git a/tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.tese b/tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.tese similarity index 88% rename from tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.tese rename to tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.tese index 03e23681..90fafaff 100644 --- a/tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.tese +++ b/tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.tese @@ -1,5 +1,5 @@ #version 320 es -layout(quads, fractional_odd_spacing, ccw) in; +layout (quads, fractional_odd_spacing, ccw) in; uniform sampler2D heightMap; uniform mat4 model; @@ -33,7 +33,7 @@ void main() vec4 uVec = p01 - p00; vec4 vVec = p10 - p00; - vec4 normal = normalize( vec4(cross(vVec.xyz, uVec.xyz), 0.) ); + vec4 normal = normalize(vec4(cross(vVec.xyz, uVec.xyz), 0.)); vec4 p0 = (p01 - p00) * u + p00; vec4 p1 = (p11 - p10) * u + p10; diff --git a/tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.vert b/tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.vert similarity index 100% rename from tutorial/src/main/assets/shaders/tesselation_shaders/8.3.gpuheight.vert rename to tutorial/src/main/assets/shaders/tesselation_shaders/gpuheight.vert diff --git a/tutorial/src/main/cpp/basic/AsteroidScene.cpp b/tutorial/src/main/cpp/basic/AsteroidScene.cpp index 9e43a0dc..84b1c467 100644 --- a/tutorial/src/main/cpp/basic/AsteroidScene.cpp +++ b/tutorial/src/main/cpp/basic/AsteroidScene.cpp @@ -22,8 +22,8 @@ void AsteroidScene::init() { // load models // ----------- - m_pRockModel = new Model("models/rock/rock.obj"); - m_pPlanetModel = new Model("models/planet/planet.obj"); + m_pRockModel = new Model("objects/rock/rock.obj"); + m_pPlanetModel = new Model("objects/planet/planet.obj"); // generate a large list of semi-random model transformation matrices // ------------------------------------------------------------------ diff --git a/tutorial/src/main/cpp/basic/TesselationShaderScene.cpp b/tutorial/src/main/cpp/basic/TesselationShaderScene.cpp index b56aa22e..d3b9cf8e 100644 --- a/tutorial/src/main/cpp/basic/TesselationShaderScene.cpp +++ b/tutorial/src/main/cpp/basic/TesselationShaderScene.cpp @@ -24,15 +24,15 @@ void TesselationShaderScene::init() { // build and compile our shader program // ------------------------------------ - tessHeightMapShader = new Shader("shaders/tesselation_shaders/8.3.gpuheight.vert", - "shaders/tesselation_shaders/8.3.gpuheight.frag", + tessHeightMapShader = new Shader("shaders/tesselation_shaders/gpuheight.vert", + "shaders/tesselation_shaders/gpuheight.frag", nullptr, - "shaders/tesselation_shaders/8.3.gpuheight.tesc", - "shaders/tesselation_shaders/8.3.gpuheight.tese"); + "shaders/tesselation_shaders/gpuheight.tesc", + "shaders/tesselation_shaders/gpuheight.tese"); // load and create a texture // ------------------------- - texture = loadTexture("heightmaps/iceland_heightmap.png"); + texture = loadTexture("textures/heightmaps/iceland_heightmap.png"); tessHeightMapShader->use(); tessHeightMapShader->setInt("heightMap", 0); @@ -48,33 +48,31 @@ void TesselationShaderScene::resize(int width, int height) { // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ std::vector vertices; - for(unsigned i = 0; i <= rez-1; i++) - { - for(unsigned j = 0; j <= rez-1; j++) - { - vertices.push_back(-width/2.0f + width*i/(float)rez); // v.x + for (unsigned i = 0; i <= rez - 1; i++) { + for (unsigned j = 0; j <= rez - 1; j++) { + vertices.push_back(-width / 2.0f + width * i / (float) rez); // v.x vertices.push_back(0.0f); // v.y - vertices.push_back(-height/2.0f + height*j/(float)rez); // v.z - vertices.push_back(i / (float)rez); // u - vertices.push_back(j / (float)rez); // v + vertices.push_back(-height / 2.0f + height * j / (float) rez); // v.z + vertices.push_back(i / (float) rez); // u + vertices.push_back(j / (float) rez); // v - vertices.push_back(-width/2.0f + width*(i+1)/(float)rez); // v.x + vertices.push_back(-width / 2.0f + width * (i + 1) / (float) rez); // v.x vertices.push_back(0.0f); // v.y - vertices.push_back(-height/2.0f + height*j/(float)rez); // v.z - vertices.push_back((i+1) / (float)rez); // u - vertices.push_back(j / (float)rez); // v + vertices.push_back(-height / 2.0f + height * j / (float) rez); // v.z + vertices.push_back((i + 1) / (float) rez); // u + vertices.push_back(j / (float) rez); // v - vertices.push_back(-width/2.0f + width*i/(float)rez); // v.x + vertices.push_back(-width / 2.0f + width * i / (float) rez); // v.x vertices.push_back(0.0f); // v.y - vertices.push_back(-height/2.0f + height*(j+1)/(float)rez); // v.z - vertices.push_back(i / (float)rez); // u - vertices.push_back((j+1) / (float)rez); // v + vertices.push_back(-height / 2.0f + height * (j + 1) / (float) rez); // v.z + vertices.push_back(i / (float) rez); // u + vertices.push_back((j + 1) / (float) rez); // v - vertices.push_back(-width/2.0f + width*(i+1)/(float)rez); // v.x + vertices.push_back(-width / 2.0f + width * (i + 1) / (float) rez); // v.x vertices.push_back(0.0f); // v.y - vertices.push_back(-height/2.0f + height*(j+1)/(float)rez); // v.z - vertices.push_back((i+1) / (float)rez); // u - vertices.push_back((j+1) / (float)rez); // v + vertices.push_back(-height / 2.0f + height * (j + 1) / (float) rez); // v.z + vertices.push_back((i + 1) / (float) rez); // u + vertices.push_back((j + 1) / (float) rez); // v } } @@ -83,9 +81,9 @@ void TesselationShaderScene::resize(int width, int height) { glGenBuffers(1, &terrainVBO); glBindBuffer(GL_ARRAY_BUFFER, terrainVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertices.size(), &vertices[0], GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(sizeof(float) * 3)); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) (sizeof(float) * 3)); glEnableVertexAttribArray(1); glPatchParameteri(GL_PATCH_VERTICES, NUM_PATCH_PTS); @@ -93,7 +91,7 @@ void TesselationShaderScene::resize(int width, int height) { void TesselationShaderScene::draw() { m_camera->update(); - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClearColor(0.65f, 0.65f, 0.65f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (tessHeightMapShader) { @@ -104,7 +102,7 @@ void TesselationShaderScene::draw() { tessHeightMapShader->setMat4("view", view); glm::mat4 model = glm::mat4(1.0f); tessHeightMapShader->setMat4("model", model); - + tessHeightMapShader->setTexture("heightMap", texture, 0); glBindVertexArray(terrainVAO); glDrawArrays(GL_PATCHES, 0, NUM_PATCH_PTS * rez * rez); } @@ -134,12 +132,12 @@ std::map TesselationShaderScene::propertyEvent(std::map &event) { - auto* targetCamera = dynamic_cast(m_camera); + auto *targetCamera = dynamic_cast(m_camera); if (!targetCamera) return; if (auto it = event.find("single_touching"); it != event.end()) { if (it->second.type() == typeid(std::vector)) { - const auto& val = std::any_cast&>(it->second); + const auto &val = std::any_cast &>(it->second); if (val.size() >= 4) { targetCamera->onSingleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3])); } @@ -148,10 +146,10 @@ void TesselationShaderScene::parseTargetCameraEvent(std::mapsecond.type() == typeid(std::vector)) { - const auto& val = std::any_cast&>(it->second); + const auto &val = std::any_cast &>(it->second); if (val.size() >= 8) { targetCamera->onDoubleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3]), - glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); + glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); } } } diff --git a/tutorial/src/main/cpp/basic/TesselationShaderScene.h b/tutorial/src/main/cpp/basic/TesselationShaderScene.h index 163e460a..b04c7598 100644 --- a/tutorial/src/main/cpp/basic/TesselationShaderScene.h +++ b/tutorial/src/main/cpp/basic/TesselationShaderScene.h @@ -28,7 +28,7 @@ public : Shader* tessHeightMapShader = nullptr; unsigned int terrainVAO = 0u, terrainVBO = 0u; unsigned int texture = 0u; - unsigned int rez = 0u; + unsigned int rez = 20u; int m_width = 0; int m_height = 0; diff --git a/tutorial/src/main/java/com/minininja/learngles/GLActivity.kt b/tutorial/src/main/java/com/minininja/learngles/GLActivity.kt index 808af31b..6bc2239c 100644 --- a/tutorial/src/main/java/com/minininja/learngles/GLActivity.kt +++ b/tutorial/src/main/java/com/minininja/learngles/GLActivity.kt @@ -58,7 +58,7 @@ open class GLActivity : ComponentActivity() { super.onCreate(savedInstanceState) enableEdgeToEdge() NativeHelper.setupNativeAsset(assets) - NativeHelper.setupInternalPath(getExternalFilesDir("files")?.path) + NativeHelper.setupInternalPath(filesDir.path) setContent { LearnGLESTheme { OpenGLContainer( From fc87f94272e9e494ec469ee991ceffedbf3dd563 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Mon, 1 Jun 2026 11:06:50 +0800 Subject: [PATCH 2/5] Update asset paths and refactor TesselationShaderScene - Switch from external to internal storage for native assets in `GLActivity`. - Rename tessellation shaders and update asset paths for models and textures. - Initialize `rez` and explicitly bind the heightmap texture in `TesselationShaderScene`. - Update tessellation shader constants from `int` to `float` and adjust background clear color. - Refactor model paths in `AsteroidScene` from `models/` to `objects/`. --- tutorial/src/main/cpp/model/Model.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tutorial/src/main/cpp/model/Model.cpp b/tutorial/src/main/cpp/model/Model.cpp index 0bb4921a..2514f7e3 100644 --- a/tutorial/src/main/cpp/model/Model.cpp +++ b/tutorial/src/main/cpp/model/Model.cpp @@ -12,7 +12,7 @@ using std::string; using std::vector; -extern char *g_internalPath; +extern std::string g_internalPath; extern AAssetManager* mgr; // constructor, expects a filepath to a 3D model. @@ -33,7 +33,7 @@ void Model::loadModel(string const &path) { // read file via ASSIMP Assimp::Importer importer; - Assimp::AndroidJNIIOSystem *ioSystem = new Assimp::AndroidJNIIOSystem (g_internalPath, mgr); + Assimp::AndroidJNIIOSystem *ioSystem = new Assimp::AndroidJNIIOSystem (g_internalPath.data(), mgr); importer.SetIOHandler(ioSystem); const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace); From 726b36c5d6ecb970785d257d3865b3dccf024d3f Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Mon, 1 Jun 2026 11:12:17 +0800 Subject: [PATCH 3/5] Enable face culling and flip textures vertically on load - Enabled `GL_CULL_FACE` in `ModelScene` for back-face culling. - Added `stbi_set_flip_vertically_on_load(1)` to ensure textures align correctly with OpenGL's coordinate system. --- tutorial/src/main/cpp/basic/ModelScene.cpp | 1 + tutorial/src/main/cpp/model/Model.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/tutorial/src/main/cpp/basic/ModelScene.cpp b/tutorial/src/main/cpp/basic/ModelScene.cpp index 1c776e06..2aa1cc42 100644 --- a/tutorial/src/main/cpp/basic/ModelScene.cpp +++ b/tutorial/src/main/cpp/basic/ModelScene.cpp @@ -12,6 +12,7 @@ void ModelScene::init() { m_camera = new TargetCamera; // configure global opengl state // ----------------------------- + glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); // build and compile our shader zprogram diff --git a/tutorial/src/main/cpp/model/Model.cpp b/tutorial/src/main/cpp/model/Model.cpp index 2514f7e3..740baaa7 100644 --- a/tutorial/src/main/cpp/model/Model.cpp +++ b/tutorial/src/main/cpp/model/Model.cpp @@ -191,6 +191,7 @@ vector Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, unsigned int TextureFromFile(const char *path, const string &directory, bool gamma) { + stbi_set_flip_vertically_on_load(1); string filename = string(path); filename = directory + '/' + filename; From dab5bfdd8544bd9bae1e666d71b4b8aebd081fd7 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Mon, 1 Jun 2026 15:58:15 +0800 Subject: [PATCH 4/5] Refactor shader naming and update BasicLightingScene lighting parameters - Rename shader assets to remove numeric prefixes and update corresponding paths in `BasicLightingScene` and `MultiLightsScene`. - Reorganize vertex data in `BasicLightingScene` with clearer face labeling. - Adjust lighting and material properties in `BasicLightingScene`, including ambient/diffuse intensities and shininess calculation. - Update clear color and integrate OpenGL error checking using `check_gl_error()`. - Refine code formatting and pointer syntax for consistency. --- .../basic_lighting/6.multiple_lights.frag | 148 ------------------ .../{6.light_cube.frag => light_cube.frag} | 2 +- .../{6.light_cube.vert => light_cube.vert} | 2 +- .../shaders/basic_lighting/lighting_maps.frag | 50 ++++++ ...ultiple_lights.vert => lighting_maps.vert} | 3 +- .../main/cpp/lighting/BasicLightingScene.cpp | 97 ++++++------ .../main/cpp/lighting/MultiLightsScene.cpp | 4 +- 7 files changed, 108 insertions(+), 198 deletions(-) delete mode 100644 tutorial/src/main/assets/shaders/basic_lighting/6.multiple_lights.frag rename tutorial/src/main/assets/shaders/basic_lighting/{6.light_cube.frag => light_cube.frag} (98%) rename tutorial/src/main/assets/shaders/basic_lighting/{6.light_cube.vert => light_cube.vert} (98%) create mode 100644 tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag rename tutorial/src/main/assets/shaders/basic_lighting/{6.multiple_lights.vert => lighting_maps.vert} (99%) diff --git a/tutorial/src/main/assets/shaders/basic_lighting/6.multiple_lights.frag b/tutorial/src/main/assets/shaders/basic_lighting/6.multiple_lights.frag deleted file mode 100644 index bd1af29d..00000000 --- a/tutorial/src/main/assets/shaders/basic_lighting/6.multiple_lights.frag +++ /dev/null @@ -1,148 +0,0 @@ -#version 320 es -precision mediump float; -out vec4 FragColor; - -struct Material { - sampler2D diffuse; - sampler2D specular; - float shininess; -}; - -struct DirLight { - vec3 direction; - - vec3 ambient; - vec3 diffuse; - vec3 specular; -}; - -struct PointLight { - vec3 position; - - float constant; - float linear; - float quadratic; - - vec3 ambient; - vec3 diffuse; - vec3 specular; -}; - -struct SpotLight { - vec3 position; - vec3 direction; - float cutOff; - float outerCutOff; - - float constant; - float linear; - float quadratic; - - vec3 ambient; - vec3 diffuse; - vec3 specular; -}; - -#define NR_POINT_LIGHTS 4 - -in vec3 FragPos; -in vec3 Normal; -in vec2 TexCoords; - -uniform vec3 viewPos; -uniform DirLight dirLight; -uniform PointLight pointLights[NR_POINT_LIGHTS]; -uniform SpotLight spotLight; -uniform Material material; - -// function prototypes -vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir); -vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir); -vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir); - -void main() -{ - // properties - vec3 norm = normalize(Normal); - vec3 viewDir = normalize(viewPos - FragPos); - - // == ===================================================== - // Our lighting is set up in 3 phases: directional, point lights and an optional flashlight - // For each phase, a calculate function is defined that calculates the corresponding color - // per lamp. In the main() function we take all the calculated colors and sum them up for - // this fragment's final color. - // == ===================================================== - // phase 1: directional lighting - vec3 result = CalcDirLight(dirLight, norm, viewDir); - // phase 2: point lights - for(int i = 0; i < NR_POINT_LIGHTS; i++) - result += CalcPointLight(pointLights[i], norm, FragPos, viewDir); - // phase 3: spot light - result += CalcSpotLight(spotLight, norm, FragPos, viewDir); - - FragColor = vec4(result, 1.0); -} - -// calculates the color when using a directional light. -vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir) -{ - vec3 lightDir = normalize(-light.direction); - // diffuse shading - float diff = max(dot(normal, lightDir), 0.0); - // specular shading - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); - // combine results - vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); - vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); - vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords)); - return (ambient + diffuse + specular); -} - -// calculates the color when using a point light. -vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) -{ - vec3 lightDir = normalize(light.position - fragPos); - // diffuse shading - float diff = max(dot(normal, lightDir), 0.0); - // specular shading - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); - // attenuation - float distance = length(light.position - fragPos); - float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); - // combine results - vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); - vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); - vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords)); - ambient *= attenuation; - diffuse *= attenuation; - specular *= attenuation; - return (ambient + diffuse + specular); -} - -// calculates the color when using a spot light. -vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir) -{ - vec3 lightDir = normalize(light.position - fragPos); - // diffuse shading - float diff = max(dot(normal, lightDir), 0.0); - // specular shading - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); - // attenuation - float distance = length(light.position - fragPos); - float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); - // spotlight intensity - float theta = dot(lightDir, normalize(-light.direction)); - float epsilon = light.cutOff - light.outerCutOff; - float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0); - // combine results - vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); - vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); - vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords)); - ambient *= attenuation * intensity; - diffuse *= attenuation * intensity; - specular *= attenuation * intensity; - return (ambient + diffuse + specular); -} diff --git a/tutorial/src/main/assets/shaders/basic_lighting/6.light_cube.frag b/tutorial/src/main/assets/shaders/basic_lighting/light_cube.frag similarity index 98% rename from tutorial/src/main/assets/shaders/basic_lighting/6.light_cube.frag rename to tutorial/src/main/assets/shaders/basic_lighting/light_cube.frag index 2291f6fd..3707fa2a 100644 --- a/tutorial/src/main/assets/shaders/basic_lighting/6.light_cube.frag +++ b/tutorial/src/main/assets/shaders/basic_lighting/light_cube.frag @@ -5,4 +5,4 @@ out vec4 FragColor; void main() { FragColor = vec4(1.0); // set all 4 vector values to 1.0 -} +} \ No newline at end of file diff --git a/tutorial/src/main/assets/shaders/basic_lighting/6.light_cube.vert b/tutorial/src/main/assets/shaders/basic_lighting/light_cube.vert similarity index 98% rename from tutorial/src/main/assets/shaders/basic_lighting/6.light_cube.vert rename to tutorial/src/main/assets/shaders/basic_lighting/light_cube.vert index a349d935..54efa4cc 100644 --- a/tutorial/src/main/assets/shaders/basic_lighting/6.light_cube.vert +++ b/tutorial/src/main/assets/shaders/basic_lighting/light_cube.vert @@ -8,4 +8,4 @@ uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0); -} +} \ No newline at end of file diff --git a/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag b/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag new file mode 100644 index 00000000..e533e288 --- /dev/null +++ b/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag @@ -0,0 +1,50 @@ +#version 320 es +precision mediump float; +out vec4 FragColor; + +struct Material { + sampler2D diffuse; + sampler2D specular; + sampler2D emission; + float shininess; +}; + +struct Light { + vec3 position; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +in vec3 FragPos; +in vec3 Normal; +in vec2 TexCoords; + +uniform vec3 viewPos; +uniform Material material; +uniform Light light; + +void main() +{ + // ambient + vec3 ambient = light.ambient * texture(material.diffuse, TexCoords).rgb; + + // diffuse + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(light.position - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = light.diffuse * diff * texture(material.diffuse, TexCoords).rgb; + + // specular + vec3 viewDir = normalize(viewPos - FragPos); + vec3 reflectDir = reflect(-lightDir, norm); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb; + + // emission + vec3 emission = texture(material.emission, TexCoords).rgb; + + vec3 result = ambient + diffuse + specular + emission; + FragColor = vec4(specular, 1.0); +} \ No newline at end of file diff --git a/tutorial/src/main/assets/shaders/basic_lighting/6.multiple_lights.vert b/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.vert similarity index 99% rename from tutorial/src/main/assets/shaders/basic_lighting/6.multiple_lights.vert rename to tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.vert index 3fbae3a6..81d781f2 100644 --- a/tutorial/src/main/assets/shaders/basic_lighting/6.multiple_lights.vert +++ b/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.vert @@ -1,4 +1,5 @@ #version 320 es + layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords; @@ -18,4 +19,4 @@ void main() TexCoords = aTexCoords; gl_Position = projection * view * vec4(FragPos, 1.0); -} +} \ No newline at end of file diff --git a/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp b/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp index 1c3b4f48..ea451ea0 100644 --- a/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp +++ b/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp @@ -4,8 +4,10 @@ #include "Camera.h" #include "TargetCamera.h" #include "Texture.h" +#include "glerror.h" BasicLightingScene::BasicLightingScene() { + m_lightPos = glm::vec3(1.2f, 1.0f, 2.0f); } void BasicLightingScene::init() { @@ -17,27 +19,30 @@ void BasicLightingScene::init() { // build and compile our shader zprogram // ------------------------------------ - m_pLightingShader = new Shader("shaders/basic_lighting/4.4.lighting_maps.vert", "shaders/basic_lighting/4.4.lighting_maps.frag"); - m_pLightCubeShader = new Shader("shaders/basic_lighting/4.4.light_cube.vert", "shaders/basic_lighting/4.4.light_cube.frag"); + m_pLightingShader = new Shader("shaders/basic_lighting/lighting_maps.vert", "shaders/basic_lighting/lighting_maps.frag"); + m_pLightCubeShader = new Shader("shaders/basic_lighting/light_cube.vert", "shaders/basic_lighting/light_cube.frag"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ float vertices[] = { // positions // normals // texture coords - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - + // front face -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + // back face + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + + // left face -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, @@ -45,24 +50,27 @@ void BasicLightingScene::init() { -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -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f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }; @@ -74,11 +82,11 @@ void BasicLightingScene::init() { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindVertexArray(m_cubeVAO); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) 0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (3 * sizeof(float))); glEnableVertexAttribArray(1); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (6 * sizeof(float))); glEnableVertexAttribArray(2); // second, configure the light's VAO (VBO stays the same; the vertices are the same for the light object which is also a 3D cube) @@ -87,7 +95,7 @@ void BasicLightingScene::init() { glBindBuffer(GL_ARRAY_BUFFER, m_VBO); // note that we update the lamp's position attribute's stride to reflect the updated buffer data - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) 0); glEnableVertexAttribArray(0); // load textures @@ -100,6 +108,8 @@ void BasicLightingScene::init() { m_pLightingShader->setInt("material.diffuse", 0); m_pLightingShader->setInt("material.specular", 1); m_pLightingShader->setInt("material.emission", 2); + + check_gl_error(); } void BasicLightingScene::resize(int width, int height) { @@ -111,23 +121,22 @@ void BasicLightingScene::draw() { m_camera->update(); // render // ------ - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClearColor(0.65f, 0.65f, 0.65f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects - if(m_pLightingShader) - { + if (m_pLightingShader) { m_pLightingShader->use(); m_pLightingShader->setVec3("light.position", m_lightPos); m_pLightingShader->setVec3("viewPos", m_camera->getPosition()); // light properties - m_pLightingShader->setVec3("light.ambient", 0.2f, 0.2f, 0.2f); - m_pLightingShader->setVec3("light.diffuse", 0.5f, 0.5f, 0.5f); + m_pLightingShader->setVec3("light.ambient", 0.5f, 0.5f, 0.5f); + m_pLightingShader->setVec3("light.diffuse", 1.f, 1.f, 1.f); m_pLightingShader->setVec3("light.specular", 1.0f, 1.0f, 1.0f); // material properties - m_pLightingShader->setFloat("material.shininess", 64.0f); + m_pLightingShader->setFloat("material.shininess", 64.f * 64.f); // view/projection transformations glm::mat4 projection = m_camera->getProjectionMatrix(); @@ -154,8 +163,7 @@ void BasicLightingScene::draw() { glDrawArrays(GL_TRIANGLES, 0, 36); // also draw the lamp object - if(m_pLightCubeShader) - { + if (m_pLightCubeShader) { m_pLightCubeShader->use(); m_pLightCubeShader->setMat4("projection", projection); m_pLightCubeShader->setMat4("view", view); @@ -168,27 +176,26 @@ void BasicLightingScene::draw() { glDrawArrays(GL_TRIANGLES, 0, 36); } } + check_gl_error(); } void BasicLightingScene::destroy() { glDeleteVertexArrays(1, &m_cubeVAO); glDeleteVertexArrays(1, &m_lightCubeVAO); glDeleteBuffers(1, &m_VBO); - if (m_pLightingShader) - { + if (m_pLightingShader) { delete m_pLightingShader; m_pLightingShader = nullptr; } - if (m_pLightCubeShader) - { + if (m_pLightCubeShader) { delete m_pLightCubeShader; m_pLightCubeShader = nullptr; } delete m_camera; + check_gl_error(); } BasicLightingScene::~BasicLightingScene() { - } std::map BasicLightingScene::propertyEvent(std::map &map) { @@ -203,12 +210,12 @@ std::map BasicLightingScene::propertyEvent(std::map &event) { - auto* targetCamera = dynamic_cast(m_camera); + auto *targetCamera = dynamic_cast(m_camera); if (!targetCamera) return; if (auto it = event.find("single_touching"); it != event.end()) { if (it->second.type() == typeid(std::vector)) { - const auto& val = std::any_cast&>(it->second); + const auto &val = std::any_cast &>(it->second); if (val.size() >= 4) { targetCamera->onSingleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3])); } @@ -217,10 +224,10 @@ void BasicLightingScene::parseTargetCameraEvent(std::map if (auto it = event.find("double_touching"); it != event.end()) { if (it->second.type() == typeid(std::vector)) { - const auto& val = std::any_cast&>(it->second); + const auto &val = std::any_cast &>(it->second); if (val.size() >= 8) { targetCamera->onDoubleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3]), - glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); + glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); } } } diff --git a/tutorial/src/main/cpp/lighting/MultiLightsScene.cpp b/tutorial/src/main/cpp/lighting/MultiLightsScene.cpp index 05734813..4740fe98 100644 --- a/tutorial/src/main/cpp/lighting/MultiLightsScene.cpp +++ b/tutorial/src/main/cpp/lighting/MultiLightsScene.cpp @@ -30,8 +30,8 @@ void MultiLightsScene::init() { // build and compile our shader zprogram // ------------------------------------ - m_pLightingShader = new Shader("shaders/multiple_lights/6.multiple_lights.vert", "shaders/multiple_lights/6.multiple_lights.frag"); - m_pLightCubeShader = new Shader("shaders/multiple_lights/6.light_cube.vert", "shaders/multiple_lights/6.light_cube.frag"); + m_pLightingShader = new Shader("shaders/multiple_lights/multiple_lights.vert", "shaders/multiple_lights/multiple_lights.frag"); + m_pLightCubeShader = new Shader("shaders/multiple_lights/light_cube.vert", "shaders/multiple_lights/light_cube.frag"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ From 5e99d405c6cd21328350ed380b7d7e4497a7800d Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Mon, 1 Jun 2026 16:07:28 +0800 Subject: [PATCH 5/5] Refactor shader naming and update BasicLightingScene lighting parameters - Rename shader assets to remove numeric prefixes and update corresponding paths in `BasicLightingScene` and `MultiLightsScene`. - Reorganize vertex data in `BasicLightingScene` with clearer face labeling. - Adjust lighting and material properties in `BasicLightingScene`, including ambient/diffuse intensities and shininess calculation. - Update clear color and integrate OpenGL error checking using `check_gl_error()`. - Refine code formatting and pointer syntax for consistency. --- .../shaders/basic_lighting/lighting_maps.frag | 4 +- .../main/cpp/lighting/BasicLightingScene.cpp | 99 +++++++++---------- 2 files changed, 49 insertions(+), 54 deletions(-) diff --git a/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag b/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag index e533e288..eea1c870 100644 --- a/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag +++ b/tutorial/src/main/assets/shaders/basic_lighting/lighting_maps.frag @@ -46,5 +46,5 @@ void main() vec3 emission = texture(material.emission, TexCoords).rgb; vec3 result = ambient + diffuse + specular + emission; - FragColor = vec4(specular, 1.0); -} \ No newline at end of file + FragColor = vec4(result, 1.0); +} \ No newline at end of file diff --git a/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp b/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp index ea451ea0..79733d9e 100644 --- a/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp +++ b/tutorial/src/main/cpp/lighting/BasicLightingScene.cpp @@ -4,10 +4,8 @@ #include "Camera.h" #include "TargetCamera.h" #include "Texture.h" -#include "glerror.h" BasicLightingScene::BasicLightingScene() { - m_lightPos = glm::vec3(1.2f, 1.0f, 2.0f); } void BasicLightingScene::init() { @@ -19,30 +17,29 @@ void BasicLightingScene::init() { // build and compile our shader zprogram // ------------------------------------ - m_pLightingShader = new Shader("shaders/basic_lighting/lighting_maps.vert", "shaders/basic_lighting/lighting_maps.frag"); - m_pLightCubeShader = new Shader("shaders/basic_lighting/light_cube.vert", "shaders/basic_lighting/light_cube.frag"); + m_pLightingShader = new Shader("shaders/basic_lighting/lighting_maps.vert", + "shaders/basic_lighting/lighting_maps.frag"); + m_pLightCubeShader = new Shader("shaders/basic_lighting/light_cube.vert", + "shaders/basic_lighting/light_cube.frag"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ float vertices[] = { // positions // normals // texture coords - // front face - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - - // back face -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - // left face + -0.5f, -0.5f, 0.5f, 0.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() { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindVertexArray(m_cubeVAO); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) 0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (3 * sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (6 * sizeof(float))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); // second, configure the light's VAO (VBO stays the same; the vertices are the same for the light object which is also a 3D cube) @@ -95,7 +89,7 @@ void BasicLightingScene::init() { glBindBuffer(GL_ARRAY_BUFFER, m_VBO); // note that we update the lamp's position attribute's stride to reflect the updated buffer data - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) 0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // load textures @@ -108,8 +102,6 @@ void BasicLightingScene::init() { m_pLightingShader->setInt("material.diffuse", 0); m_pLightingShader->setInt("material.specular", 1); m_pLightingShader->setInt("material.emission", 2); - - check_gl_error(); } void BasicLightingScene::resize(int width, int height) { @@ -121,22 +113,23 @@ void BasicLightingScene::draw() { m_camera->update(); // render // ------ - glClearColor(0.65f, 0.65f, 0.65f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects - if (m_pLightingShader) { + if(m_pLightingShader) + { m_pLightingShader->use(); m_pLightingShader->setVec3("light.position", m_lightPos); m_pLightingShader->setVec3("viewPos", m_camera->getPosition()); // light properties - m_pLightingShader->setVec3("light.ambient", 0.5f, 0.5f, 0.5f); - m_pLightingShader->setVec3("light.diffuse", 1.f, 1.f, 1.f); + m_pLightingShader->setVec3("light.ambient", 0.2f, 0.2f, 0.2f); + m_pLightingShader->setVec3("light.diffuse", 0.5f, 0.5f, 0.5f); m_pLightingShader->setVec3("light.specular", 1.0f, 1.0f, 1.0f); // material properties - m_pLightingShader->setFloat("material.shininess", 64.f * 64.f); + m_pLightingShader->setFloat("material.shininess", 64.0f); // view/projection transformations glm::mat4 projection = m_camera->getProjectionMatrix(); @@ -163,7 +156,8 @@ void BasicLightingScene::draw() { glDrawArrays(GL_TRIANGLES, 0, 36); // also draw the lamp object - if (m_pLightCubeShader) { + if(m_pLightCubeShader) + { m_pLightCubeShader->use(); m_pLightCubeShader->setMat4("projection", projection); m_pLightCubeShader->setMat4("view", view); @@ -176,26 +170,27 @@ void BasicLightingScene::draw() { glDrawArrays(GL_TRIANGLES, 0, 36); } } - check_gl_error(); } void BasicLightingScene::destroy() { glDeleteVertexArrays(1, &m_cubeVAO); glDeleteVertexArrays(1, &m_lightCubeVAO); glDeleteBuffers(1, &m_VBO); - if (m_pLightingShader) { + if (m_pLightingShader) + { delete m_pLightingShader; m_pLightingShader = nullptr; } - if (m_pLightCubeShader) { + if (m_pLightCubeShader) + { delete m_pLightCubeShader; m_pLightCubeShader = nullptr; } delete m_camera; - check_gl_error(); } BasicLightingScene::~BasicLightingScene() { + } std::map BasicLightingScene::propertyEvent(std::map &map) { @@ -210,12 +205,12 @@ std::map BasicLightingScene::propertyEvent(std::map &event) { - auto *targetCamera = dynamic_cast(m_camera); + auto* targetCamera = dynamic_cast(m_camera); if (!targetCamera) return; if (auto it = event.find("single_touching"); it != event.end()) { if (it->second.type() == typeid(std::vector)) { - const auto &val = std::any_cast &>(it->second); + const auto& val = std::any_cast&>(it->second); if (val.size() >= 4) { targetCamera->onSingleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3])); } @@ -224,10 +219,10 @@ void BasicLightingScene::parseTargetCameraEvent(std::map if (auto it = event.find("double_touching"); it != event.end()) { if (it->second.type() == typeid(std::vector)) { - const auto &val = std::any_cast &>(it->second); + const auto& val = std::any_cast&>(it->second); if (val.size() >= 8) { targetCamera->onDoubleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3]), - glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); + glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); } } }