#include "lab_m1/tema1/tema1.h"
#include <vector>
#include <cmath>
#include "lab_m1/tema1/transform2D.h"
#include "lab_m1/lab3/object2D.h"

using namespace std;
using namespace m1;

Tema1::Tema1() 
{
}

Tema1::~Tema1()
{
}

void Tema1::Init()
{
    glm::ivec2 resolution = window->GetResolution();
    auto camera = GetSceneCamera();
    camera->SetOrthographic(0, (float)resolution.x, 0, (float)resolution.y, 0.01f, 400);
    camera->SetPosition(glm::vec3(0, 0, 50));
    camera->SetRotation(glm::vec3(0, 0, 0));
    camera->Update();
    GetCameraInput()->SetActive(false);

    glm::vec3 corner = glm::vec3(0, 0, 0);
    float squareSide = 100;
    cx = corner.x + squareSide / 2;
    cy = corner.y + squareSide / 2;
    // Initialize tx and ty (the translation steps)
    translateX = 0;
    translateY = 0;

    // Initialize sx and sy (the scale factors)
    scaleX = 1;
    scaleY = 1;

    // Initialize angularStep
    angularStep = 0;

    float amplitude = 50.0f; // You can adjust this value
    float frequency = 0.1f;  // You can adjust this value

    Mesh* square1 = object2D::CreateSquare("square1", corner, squareSide, glm::vec3(1, 0, 0), true);
    AddMeshToList(square1);

    Mesh* square2 = object2D::CreateSquare("square2", corner, squareSide, glm::vec3(0, 1, 0));
    AddMeshToList(square2);

    Mesh* square3 = object2D::CreateSquare("square3", corner, squareSide, glm::vec3(0, 0, 1));
    AddMeshToList(square3);

	GenerateTerrain();
}



void Tema1::GenerateTerrain() {

    for (int i = 0; i < numSquares; i++) {
        float x = i * 10.0f; // Adjust spacing as needed
        float y = amplitude * sin(freq * x) + amplitude * sin(0.5f * freq * x);
        heightMap.push_back(glm::vec2(x, y));
    }
}

void Tema1::Update(float deltaTimeSeconds)
{
    // Clear any previous transformation
    

    // Iterate through pairs of points in the height map
    for (int i = 0; i < numSquares - 1; i++) {
        glm::vec2 A = heightMap[i];
        glm::vec2 B = heightMap[i + 1];

        // Calculate scaling factors for width and height
        //float scaleX = glm::distance(A, B);
        float scaleY = std::max(A.y, B.y);

        // Calculate the shear factor based on the slope between points A and B
        float shearFactor = (B.y - A.y) / (B.x - A.x);
		glm::mat3 modelMatrix = glm::mat3(1);
		modelMatrix *= transform2D::Translate(A.x, A.y);
		modelMatrix *= transform2D::Scale(B.x - A.x, max(B.y, A.y));
		modelMatrix *= transform2D::Sheer(shearFactor);

        // Render the transformed square for the terrain segment
        RenderMesh2D(meshes["square1"], shaders["VertexColor"], modelMatrix);
    }
}



void Tema1::FrameStart()
{
    glClearColor(0.52f, 0.81f, 0.92f, 1);  // Light blue background
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glm::ivec2 resolution = window->GetResolution();
    glViewport(0, 0, resolution.x, resolution.y);
}

void Tema1::FrameEnd()
{
}


/*
 *  These are callback functions. To find more about callbacks and
 *  how they behave, see `input_controller.h`.
 */


void Tema1::OnInputUpdate(float deltaTime, int mods)
{
}


void Tema1::OnKeyPress(int key, int mods)
{
    // Add key press event
}


void Tema1::OnKeyRelease(int key, int mods)
{
    // Add key release event
}


void Tema1::OnMouseMove(int mouseX, int mouseY, int deltaX, int deltaY)
{
    // Add mouse move event
}


void Tema1::OnMouseBtnPress(int mouseX, int mouseY, int button, int mods)
{
    // Add mouse button press event
}


void Tema1::OnMouseBtnRelease(int mouseX, int mouseY, int button, int mods)
{
    // Add mouse button release event
}


void Tema1::OnMouseScroll(int mouseX, int mouseY, int offsetX, int offsetY)
{
}


void Tema1::OnWindowResize(int width, int height)
{
}

I need to create a terrain.My code is not generating anything.Can you identify the problem and fix it.This is transform2D file #pragma once

#include "utils/glm_utils.h"


namespace transform2D
{
    // Translate matrix
    inline glm::mat3 Translate(float translateX, float translateY)
    {
        // TODO(student): Implement the translation matrix
        return glm::transpose(
            glm::mat3(1, 0, translateX,
                0, 1, translateY,
                0, 0, 1)
        );

    }

    // Scale matrix
    inline glm::mat3 Scale(float scaleX, float scaleY)
    {
        // TODO(student): Implement the scaling matrix
        return glm::transpose(
            glm::mat3(scaleX, 0, 0,
                0, scaleY, 0,
                0, 0, 1)
        );

    }

    // Rotate matrix
    inline glm::mat3 Rotate(float radians)
    {
        // TODO(student): Implement the rotation matrix
        return glm::transpose(
            glm::mat3(cos(radians), -sin(radians), 0,
                sin(radians), cos(radians), 0,
                0, 0, 1)
        );

    }

	inline glm::mat3 Sheer(float shearFactor)
	{
		return glm::transpose(
			glm::mat3(1, 0, 0,
				shearFactor, 1, 0,
				0, 0, 1)
		);

	}
}   // namespace transform2D