aboutsummaryrefslogtreecommitdiff
path: root/09-september/tomcat/terrain.c
blob: f23d057b527f9321ee0a2c2d48b876d704721422 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "terrain.h"
#include "math/math_util.h"
#include "math/vector.h"
#include "util/util.h"
#include "renderer/renderer.h"

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <math.h>

#define PLANE_SIZE 128
#define PLANE_MAX_HEIGHT 10

#define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a)

static float GetHeight(int x, int y, SDL_Surface* surface)
{
    if(x < 0 || x >= surface->w || y < 0 || y >= surface->h)
        return 0.0f;

    Uint32 pixel = ( (Uint32*)surface->pixels )[y * surface->w + x];
    Uint8 r, g, b;
    SDL_GetRGB(pixel, surface->format, &r, &g, &b);

    float height = (float)r / 255.0f;

    return height * 40.0f;
}

static Vec3 GenerateNomal(int x, int y, SDL_Surface* surface)
{
    float hLeft  = GetHeight(x-1, y, surface);
    float hRight = GetHeight(x+1, y, surface);
    float hUp    = GetHeight(x, y+1, surface);
    float hDown  = GetHeight(x, y-1, surface);

    Vec3 normal = { hLeft - hRight, 2.0f, hDown - hUp};
    return vec3_normalize(&normal);
}

GLfloat Terrain_GetHeightOfTerrain(Terrain *terrain, GLfloat x, GLfloat z)
{
    GLfloat terrainX = x - terrain->position.x;
    GLfloat terrainZ = z - terrain->position.z;

    GLfloat gridSquareSize = (float)terrain->l / ( (float)PLANE_SIZE - 1 );

    GLint gridX = (GLint) floor(terrainX / gridSquareSize);
    GLint gridZ = (GLint) floor(terrainZ / gridSquareSize);

    if(gridX >= PLANE_SIZE - 1 || gridX < 0 || gridZ >= PLANE_SIZE - 1 || gridZ < 0)
    {
        printf("called\n");
        return 0;
    }

    GLfloat xCoord = fmod(terrainX, gridSquareSize) / gridSquareSize;
    GLfloat zCoord = fmod(terrainZ, gridSquareSize) / gridSquareSize;
    GLfloat answer;

    /* Determine in which triangle of the square are we. "Bary Centric Interpolation"*/
    if(xCoord <= (1 - zCoord)){
        /* 0, heights[gridX][gridZ], 0) */
        Vec3 p1 = { 0, terrain->height[ gridX * PLANE_SIZE + gridZ ], 0 };
        /* 1, heights[gridX + 1][gridZ], 0) */
        Vec3 p2 = { 1, terrain->height[ (gridX + 1) * PLANE_SIZE + gridZ ], 0};
        /* 0, heights[gridX][gridZ + 1], 1) */
        Vec3 p3 = { 0, terrain->height[ gridX * PLANE_SIZE + (gridZ + 1) ], 1};

        Vec2 pos = {xCoord, zCoord};

        answer = baryCentric(&p1, &p2, &p3, &pos);
    } else {
        /* (1, heights[gridX + 1][gridZ], 0) */
        Vec3 p1 = { 1, terrain->height[ (gridX + 1) * PLANE_SIZE + gridZ ], 0 };
        /* (1, heights[gridX + 1][gridZ + 1], 1) */
        Vec3 p2 = { 1, terrain->height[ (gridX + 1) * PLANE_SIZE + (gridZ + 1) ], 1};
        /* (0, heights[gridX][gridZ + 1], 1) */
        Vec3 p3 = { 0, terrain->height[ gridX * PLANE_SIZE + (gridZ + 1) ], 1};
        Vec2 pos = {xCoord, zCoord};

        answer = baryCentric(&p1, &p2, &p3, &pos);
    }

    return answer;
}

Terrain *Terrain_Create( int w, int l, const char* heightmap_path, Texture *blendmap, TerrainTexturePack *textures )
{
    Terrain *terrain = malloc( sizeof(Terrain) );
    terrain->height = (GLfloat*) malloc( sizeof(GLfloat) * PLANE_SIZE * PLANE_SIZE);

    terrain->blendmap = blendmap;
    terrain->w = w; terrain->l = l;
    terrain->textures = *textures;

    SDL_Surface* surface = IMG_Load(heightmap_path);
    if(surface == NULL)
        Util_FatalError("Heightmap file could not be loaded\n");

    vertex_t data[PLANE_SIZE * PLANE_SIZE];
    int x, y;
    for(x = 0; x < PLANE_SIZE; x++)
    {
        for(y = 0; y < PLANE_SIZE; y++)
        {
            vertex_t* v = &data[y + x * PLANE_SIZE];
            v->position = (Vec3){ (float)x / (float)PLANE_SIZE, 0.0f, (float)y / (float)PLANE_SIZE };
            /* Heightmap cordinates */
            int image_x = v->position.x * surface->w, image_y = v->position.z * surface->h;

            v->texCoord = (Vec2){ v->position.x, v->position.z };
            GLfloat height = GetHeight(image_x, image_y, surface);
            terrain->height[y + x * PLANE_SIZE] = height;
            v->position.y = height;
            v->position.x *= w;
            v->position.z *= l;

            v->normal = GenerateNomal(image_x, image_y, surface);
        }
    }

    int runner = 0;
    GLushort indices[ (PLANE_SIZE-1) * (PLANE_SIZE-1) * 6 ];
    for(x = 0; x < PLANE_SIZE-1; x++)
    {
        for(y = 0; y < PLANE_SIZE-1; y++)
        {
            indices[runner++] = PLANE_SIZE * x + y;
            indices[runner++] = PLANE_SIZE * x + y + 1;
            indices[runner++] = PLANE_SIZE * x + y + PLANE_SIZE;

            indices[runner++] = PLANE_SIZE * x + y + 1;
            indices[runner++] = PLANE_SIZE * x + y + PLANE_SIZE + 1;
            indices[runner++] = PLANE_SIZE * x + y + PLANE_SIZE;
        }
    }

    GLsizeiptr vertexBufferSize = NUM_ARRAY_ELEMENTS(data) * sizeof(vertex_t);
    GLsizeiptr indexBufferSize = NUM_ARRAY_ELEMENTS(indices) * sizeof(GLushort);

    SDL_FreeSurface(surface);

    terrain->mesh = mesh_new(data, vertexBufferSize, indices, indexBufferSize);
    return terrain;
}

void Terrain_Destroy( Terrain *terrain )
{
    if(terrain->height)
        free(terrain->height);

    free(terrain);
}