terrain code works do run

This commit is contained in:
MallocNull 2020-12-21 19:52:31 -06:00
parent 8d3c9d4dd8
commit be4691bf54
23 changed files with 503 additions and 143 deletions

View file

@ -10,6 +10,20 @@ v 0.500000 -0.500000 0.500000
v 0.500000 0.500000 0.500000
v 0.500000 -0.500000 -0.500000
v 0.500000 0.500000 -0.500000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.375000 0.000000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.125000 0.750000
vt 0.125000 0.500000
vt 0.875000 0.500000
vt 0.625000 1.000000
vt 0.875000 0.750000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
@ -18,9 +32,15 @@ vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
usemtl None
s off
f 1//1 2//1 4//1 3//1
f 3//2 4//2 8//2 7//2
f 7//3 8//3 6//3 5//3
f 5//4 6//4 2//4 1//4
f 3//5 7//5 5//5 1//5
f 8//6 4//6 2//6 6//6
f 2/1/1 3/2/1 1/3/1
f 4/4/2 7/5/2 3/2/2
f 8/6/3 5/7/3 7/5/3
f 6/8/4 1/9/4 5/7/4
f 7/5/5 1/10/5 3/11/5
f 4/12/6 6/8/6 8/6/6
f 2/1/1 4/4/1 3/2/1
f 4/4/2 8/6/2 7/5/2
f 8/6/3 6/8/3 5/7/3
f 6/8/4 2/13/4 1/9/4
f 7/5/5 5/7/5 1/10/5
f 4/12/6 2/14/6 6/8/6

Binary file not shown.

51
src/data/cube2.obj Normal file
View file

@ -0,0 +1,51 @@
# Blender v2.79 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube2.mtl
o Cube_Cube.001
v -0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v -0.500000 -0.500000 -0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 -0.500000 0.500000
v 0.500000 0.500000 0.500000
v 0.500000 -0.500000 -0.500000
v 0.500000 0.500000 -0.500000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.375000 0.000000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.125000 0.750000
vt 0.125000 0.500000
vt 0.875000 0.500000
vt 0.625000 1.000000
vt 0.875000 0.750000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
usemtl None
s off
f 2/1/1 3/2/1 1/3/1
f 2/1/1 4/4/1 3/2/1
f 4/4/2 7/5/2 3/2/2
f 4/4/2 8/6/2 7/5/2
f 8/6/3 5/7/3 7/5/3
f 8/6/3 6/8/3 5/7/3
f 6/8/4 1/9/4 5/7/4
f 6/8/4 2/13/4 1/9/4
f 7/5/5 1/10/5 3/11/5
f 7/5/5 5/7/5 1/10/5
f 4/12/6 6/8/6 8/6/6
f 4/12/6 2/14/6 6/8/6

BIN
src/data/cube2.rbm Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 39 KiB

BIN
src/data/map-reserve.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

1
src/data/rbm.sh Executable file
View file

@ -0,0 +1 @@
../../cmake-build-debug/rbm_gen $1

View file

@ -1,5 +1,17 @@
#include "etc.h"
int color_eq(color_t a, color_t b) {
return color_eqp(&a, &b);
}
int color_eqp(const color_t* a, const color_t* b) {
return
(a->r == b->r) &&
(a->g == b->g) &&
(a->b == b->b) &&
(a->a == b->a);
}
void mfree(int n, ...) {
va_list args;
va_start(args, n);

View file

@ -9,6 +9,13 @@
#define __MIN(A, B) ((A) < (B) ? (A) : (B))
#define __MAX(A, B) ((A) > (B) ? (A) : (B))
typedef struct {
uint8_t r, g, b, a;
} color_t;
int color_eq(color_t, color_t);
int color_eqp(const color_t*, const color_t*);
void mfree(int n, ...);
uint8_t* ltoh(uint8_t*, int);
uint8_t* btoh(uint8_t*, int);

View file

@ -137,7 +137,7 @@ int bmp_reload_chunk(bmp_t* bmp, int x, int y, int width, int height) {
if(x > data->width || y > data->height) {
fclose(fp);
return NULL;
return 0;
}
if(bmp->pixels != NULL)
@ -152,9 +152,9 @@ int bmp_reload_chunk(bmp_t* bmp, int x, int y, int width, int height) {
bmp->width = width;
bmp->height = height;
bmp->pixels = malloc(sizeof(pixel_t*) * height);
bmp->pixels = malloc(sizeof(color_t*) * height);
for(int i = 0; i < height; ++i)
bmp->pixels[i] = malloc(sizeof(pixel_t) * width);
bmp->pixels[i] = malloc(sizeof(color_t) * width);
uint8_t buffer[4];
for(int ay = 0; ay < height; ++ay) {
@ -168,10 +168,11 @@ int bmp_reload_chunk(bmp_t* bmp, int x, int y, int width, int height) {
for(int ax = 0; ax < width; ++ax) {
fread(buffer, 1, data->bytepp, fp);
pixel_t* px = &bmp->pixels[ay][ax];
color_t* px = &bmp->pixels[ay][ax];
px->b = buffer[0];
px->g = buffer[1];
px->r = buffer[2];
px->a = 0xFF;
}
}

View file

@ -20,10 +20,6 @@ char* file_read(const char*);
// BEGIN BMP LOADING FUNCTIONS
typedef struct {
uint8_t r, g, b;
} pixel_t;
typedef struct {
char file[4096];
uint32_t width, height, size,
@ -35,7 +31,7 @@ typedef struct {
bmp_meta_t data;
uint32_t width, height;
pixel_t** pixels;
color_t** pixels;
} bmp_t;
bmp_t* bmp_load(const char*);

View file

@ -1,29 +0,0 @@
#include "sdl.h"
void get_pixel(SDL_Color* out, SDL_Surface* img, int x, int y) {
int bpp = img->format->BytesPerPixel;
uint8_t* ptr = (uint8_t*)img->pixels + y * img->pitch + x * bpp;
Uint32 pixel;
switch(bpp) {
case 1:
pixel = *ptr;
break;
case 2:
pixel = *(Uint16*)ptr;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
pixel = ptr[0] << 16 | ptr[1] << 8 | ptr[2];
else
pixel = ptr[0] | ptr[1] << 8 | ptr[2] << 16;
break;
case 4:
pixel = *(Uint32*)ptr;
break;
default:
pixel = 0;
}
SDL_GetRGBA(pixel, img->format, &out->r, &out->g, &out->b, &out->a);
}

View file

@ -1,13 +0,0 @@
#ifndef KOA_SDL_H
#define KOA_SDL_H
#include <SDL.h>
#include <stdlib.h>
typedef struct {
uint8_t r, g, b, a;
} pixel_t;
void get_pixel(SDL_Color*, SDL_Surface*, int x, int y);
#endif

View file

@ -10,6 +10,7 @@
#include "koa/file.h"
#include "okuu/mesh.h"
#include "okuu/shader.h"
#include "okuu/terrain.h"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
@ -21,6 +22,7 @@ struct {
int mode, running;
mesh_t* monkey;
terrain_t* map;
} _g;
struct {
@ -41,9 +43,18 @@ int main(int argc, char* argv[]) {
if(init() < 0)
return -1;
_g.monkey = mesh_load("data/cube.rbm");
_g.monkey = mesh_load("data/player.rbm");
_g.map = terrain_load(
"data/map-heights.bmp",
"data/map-colors.bmp",
10, 10
);
_s_def.shader = shader_create("default");
shader_layout(_s_def.shader, 4,
"vertex", "texuv", "normal", "color"
);
shader_source(_s_def.shader, 2,
"shaders/test.vert", GL_VERTEX_SHADER,
"shaders/test.frag", GL_FRAGMENT_SHADER
@ -62,19 +73,22 @@ int main(int argc, char* argv[]) {
}
void run() {
static mat4 model, view, projection;
static mat4 model, map, view, projection;
static float rot_up = 45, rot_around = 45;
static float x = 0, y = 0;
static int init = 1;
if(init) {
glm_rotate_make(model, glm_rad(90), (vec3){ 0.f, -1.f, 0.f });
//glm_rotate_make(model, glm_rad(90), (vec3){ 0.f, -1.f, 0.f });
glm_mat4_identity(model);
glm_mat4_identity(map);
glm_mat4_identity(view);
glm_perspective(
glm_rad(45),
glm_rad(55),
(float)WINDOW_WIDTH/(float)WINDOW_HEIGHT,
0.1f, 10.f,
0.1f, 100.f,
projection
);
@ -106,9 +120,16 @@ void run() {
shader_start(_s_def.shader); {
glUniformMatrix4fv(_ATTR(DEF_VIEW), 1, GL_FALSE, (float*)view);
mesh_bind(_g.monkey);
/*mesh_bind(_g.monkey);
mesh_render(_g.monkey);
mesh_unbind();
mesh_unbind();*/
glm_translate_make(model, (vec3){-x, -(_g.map->heights[(int)y][(int)x] + 2.f), -y});
glm_mat4_mul(model, map, model);
glUniformMatrix4fv(_ATTR(DEF_MODEL), 1, GL_FALSE, (float*)model);
glBindVertexArray(_g.map->vao);
glDrawArrays(GL_TRIANGLES, 0, _g.map->tri_cnt * 3);
glBindVertexArray(0);
} shader_stop();
SDL_GL_SwapWindow(_g.window);
@ -137,6 +158,21 @@ void run() {
else if(_g.keys[SDL_SCANCODE_LEFT])
rot_around += 2.f;
if(_g.keys[SDL_SCANCODE_W])
x += .25;
else if(_g.keys[SDL_SCANCODE_S])
x -= .25;
if(_g.keys[SDL_SCANCODE_D])
y += .25;
else if(_g.keys[SDL_SCANCODE_A])
y -= .25;
if(_g.keys[SDL_SCANCODE_SPACE]) {
terrain_move(_g.map, x, y);
glm_translate_make(map, (vec3){__MAX(0, x - CHUNK_SIZE / 2), 0, __MAX(0, y - CHUNK_SIZE / 2)});
}
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
if(ev.type == SDL_QUIT)

View file

@ -35,8 +35,10 @@ int _populate_faces
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
if((flags & (1 << j)) == 0)
if((flags & (1 << j)) == 0) {
data[at].data[i][j] = 0;
continue;
}
chk = fread(buffer, 1, length, fp);
if(chk != length)
@ -127,7 +129,7 @@ mesh_t* mesh_load(const char* file) {
}
memcpy(
&verts_asm[i*3*3 + 3*j],
&verts_asm[i * 3 *3 + 3 * j],
verts[faces[i].data[j][0] - 1].data,
3 * sizeof(float)
);

View file

@ -5,6 +5,8 @@ shader_t* _active = NULL;
shader_t* shader_create(const char* name) {
shader_t* shader = malloc(sizeof(shader_t));
shader->name = name;
shader->layout = NULL;
shader->layout_cnt = 0;
shader->attribs = NULL;
shader->program = glCreateProgram();
shader->loaded = 0;
@ -12,6 +14,23 @@ shader_t* shader_create(const char* name) {
return shader;
}
int shader_layout(shader_t* shader, int count, ...) {
if(shader->loaded)
return -1;
if(shader->layout != NULL)
free(shader->layout);
shader->layout = malloc(sizeof(layout_name_t) * count);
shader->layout_cnt = count;
va_list args;
va_start(args, count);
for(int i = 0; i < count; ++i)
strcpy(shader->layout[i], va_arg(args, const char*));
va_end(args);
return 0;
}
int shader_source(shader_t* shader, int files, ...) {
if(shader->loaded)
return -1;
@ -66,6 +85,12 @@ int shader_source(shader_t* shader, int files, ...) {
if(failed)
return -1;
if(shader->layout != NULL) {
for(int i = 0; i < shader->layout_cnt; ++i)
glBindAttribLocation(shader->program, i, shader->layout[i]);
free(shader->layout);
}
glLinkProgram(shader->program);
glGetProgramiv(shader->program, GL_LINK_STATUS, &err);
if(err == GL_FALSE) {

View file

@ -12,8 +12,13 @@
#define _SHADER shader_active()
#define _ATTR(I) shader_attr(_SHADER, I)
typedef char layout_name_t[256];
typedef struct {
const char* name;
int layout_cnt;
layout_name_t* layout;
GLuint program;
GLint* attribs;
int loaded;
@ -21,6 +26,7 @@ typedef struct {
shader_t* shader_create(const char*);
int shader_layout(shader_t*, int, ...);
int shader_source(shader_t*, int, ...);
int shader_attribs(shader_t*, int, ...);
GLint shader_attr(const shader_t*, int);

View file

@ -1,7 +1,18 @@
#include "terrain.h"
#define _HGT_AT(A, X, Y) (A->bmps[0]->pixels[Y][X].r / 10.f)
#define _COL_AT(A, X, Y, C) (A->bmps[1]->pixels[Y][X].C / 255.f)
#define _RWCOL_AT(A, X, Y) (A->bmps[1]->pixels[Y][X])
#define _RCOL_AT(A, X, Y, C) (_RWCOL_AT(A, X, Y).C)
#define _COL_AT(A, X, Y, C) (_RCOL_AT(A, X, Y, C) / 255.f)
#define _COL_EQ(A, X1, Y1, X2, Y2) \
(color_eqp(&A->bmps[1]->pixels[Y1][X1], &A->bmps[1]->pixels[Y2][X2]))
float _avg_hgt(terrain_t* map, int x, int y) {
return
(_HGT_AT(map, x, y) + _HGT_AT(map, x + 1, y) +
_HGT_AT(map, x, y + 1) + _HGT_AT(map, x + 1, y + 1))
/ 4.f;
}
terrain_t* terrain_load
(const char* heights, const char* colors,
@ -50,96 +61,289 @@ void terrain_move(terrain_t* terrain, int center_x, int center_y) {
terrain->center_x = center_x;
terrain->center_y = center_y;
const int data_length = 6 * 3 * (CHUNK_SIZE - 1) * (CHUNK_SIZE - 1);
const int data_length = 12 * 3 * (CHUNK_SIZE - 1) * (CHUNK_SIZE - 1);
terrain->tri_cnt = data_length / 3;
float data[data_length];
vec3 dir1, dir2, norm;
vec3 dir1, dir2, norm1, norm2, norm3;
for(int i = 0; i < TERR_BUFFS; ++i) {
int cnt = i == 1 ? 2 : 3;
for(int y = 0; y < CHUNK_SIZE - 1; ++y) {
for(int x = 0; x < CHUNK_SIZE - 1; ++x) {
int at = (y * CHUNK_SIZE * 6 * 3) + (x * 6 * 3);
int at = (y * (CHUNK_SIZE - 1) * 12 * cnt) + (x * 12 * cnt);
switch(i) {
case 0:
data[at] = x;
data[at + 1] = _HGT_AT(terrain, x, y);
data[at + 1] = terrain->heights[y][x] =
_HGT_AT(terrain, x, y);
data[at + 2] = y;
data[at + 3] = x;
data[at + 4] = _HGT_AT(terrain, x, y + 1);
data[at + 5] = y + 1;
data[at + 3] = x + 0.5f;
data[at + 4] = _avg_hgt(terrain, x, y);
data[at + 5] = y + 0.5f;
data[at + 6] = x + 1;
data[at + 7] = _HGT_AT(terrain, x + 1, y);
data[at + 8] = y;
data[at + 9] = x + 1;
data[at + 10] = _HGT_AT(terrain, x + 1, y + 1);
data[at + 11] = y + 1;
data[at + 10] = _HGT_AT(terrain, x + 1, y);
data[at + 11] = y;
data[at + 12] = x + 1;
data[at + 13] = _HGT_AT(terrain, x + 1, y);
data[at + 14] = y;
data[at + 12] = x + 0.5f;
data[at + 13] = _avg_hgt(terrain, x, y);
data[at + 14] = y + 0.5f;
data[at + 15] = x;
data[at + 16] = _HGT_AT(terrain, x, y + 1);
data[at + 15] = x + 1;
data[at + 16] = _HGT_AT(terrain, x + 1, y + 1);
data[at + 17] = y + 1;
data[at + 18] = x;
data[at + 19] = _HGT_AT(terrain, x, y + 1);
data[at + 20] = y + 1;
data[at + 21] = x + 1;
data[at + 22] = _HGT_AT(terrain, x + 1, y + 1);
data[at + 23] = y + 1;
data[at + 24] = x + 0.5f;
data[at + 25] = _avg_hgt(terrain, x, y);
data[at + 26] = y + 0.5f;
data[at + 27] = x;
data[at + 28] = _HGT_AT(terrain, x, y);
data[at + 29] = y;
data[at + 30] = x;
data[at + 31] = _HGT_AT(terrain, x, y + 1);
data[at + 32] = y + 1;
data[at + 33] = x + 0.5f;
data[at + 34] = _avg_hgt(terrain, x, y);
data[at + 35] = y + 0.5f;
break;
case 1:
glm_vec3_sub(
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y), y },
(vec3){ x, _HGT_AT(terrain, x, y), y },
dir1
);
glm_vec3_sub(
(vec3){ x, _HGT_AT(terrain, x, y + 1), y + 1 },
(vec3){ x, _HGT_AT(terrain, x, y), y },
dir2
);
glm_vec3_cross(dir1, dir2, norm);
memcpy(&data[at], norm, 3 * sizeof(float));
memcpy(&data[at + 3], norm, 3 * sizeof(float));
memcpy(&data[at + 6], norm, 3 * sizeof(float));
data[at] = 0.f;
data[at + 1] = 0.f;
glm_vec3_sub(
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y + 1), y + 1 },
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y), y },
dir1
);
glm_vec3_sub(
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y + 1), y + 1 },
(vec3){ x, _HGT_AT(terrain, x, y + 1), y + 1 },
dir2
);
glm_vec3_cross(dir1, dir2, norm);
memcpy(&data[at + 9], norm, 3 * sizeof(float));
memcpy(&data[at + 12], norm, 3 * sizeof(float));
memcpy(&data[at + 15], norm, 3 * sizeof(float));
data[at + 2] = .5f;
data[at + 3] = .5f;
data[at + 4] = 1.f;
data[at + 5] = 0.f;
data[at + 6] = 1.f;
data[at + 7] = 0.f;
data[at + 8] = .5f;
data[at + 9] = .5f;
data[at + 10] = 1.f;
data[at + 11] = 1.f;
data[at + 12] = 0.f;
data[at + 13] = 1.f;
data[at + 14] = 1.f;
data[at + 15] = 1.f;
data[at + 16] = .5f;
data[at + 17] = .5f;
data[at + 18] = 0.f;
data[at + 19] = 0.f;
data[at + 20] = 0.f;
data[at + 21] = 1.f;
data[at + 22] = .5f;
data[at + 23] = .5f;
break;
case 2:
data[at] = _COL_AT(terrain, x, y, r);
data[at + 1] = _COL_AT(terrain, x, y, g);
data[at + 2] = _COL_AT(terrain, x, y, b);
glm_vec3_sub(
(vec3){ x, _HGT_AT(terrain, x, y), y },
(vec3){ x + 0.5f, _avg_hgt(terrain, x, y), y + 0.5f },
dir1
);
glm_vec3_sub(
(vec3){ x, _HGT_AT(terrain, x, y), y },
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y), y },
dir2
);
glm_vec3_cross(dir1, dir2, norm1);
glm_vec3_normalize(norm1);
memcpy(&data[at], norm1, 3 * sizeof(float));
memcpy(&data[at + 3], norm1, 3 * sizeof(float));
memcpy(&data[at + 6], norm1, 3 * sizeof(float));
data[at + 3] = _COL_AT(terrain, x + 1, y, r);
data[at + 4] = _COL_AT(terrain, x + 1, y, g);
data[at + 5] = _COL_AT(terrain, x + 1, y, b);
glm_vec3_sub(
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y), y },
(vec3){ x + 0.5f, _avg_hgt(terrain, x, y), y + 0.5f },
dir1
);
glm_vec3_sub(
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y), y },
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y + 1), y + 1 },
dir2
);
glm_vec3_cross(dir1, dir2, norm1);
glm_vec3_normalize(norm1);
memcpy(&data[at + 9], norm1, 3 * sizeof(float));
memcpy(&data[at + 12], norm1, 3 * sizeof(float));
memcpy(&data[at + 15], norm1, 3 * sizeof(float));
data[at + 6] = _COL_AT(terrain, x, y + 1, r);
data[at + 7] = _COL_AT(terrain, x, y + 1, g);
data[at + 8] = _COL_AT(terrain, x, y + 1, b);
glm_vec3_sub(
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y + 1), y + 1 },
(vec3){ x + 0.5f, _avg_hgt(terrain, x, y), y + 0.5f },
dir1
);
glm_vec3_sub(
(vec3){ x + 1, _HGT_AT(terrain, x + 1, y + 1), y + 1 },
(vec3){ x, _HGT_AT(terrain, x, y + 1), y + 1 },
dir2
);
glm_vec3_cross(dir1, dir2, norm1);
glm_vec3_normalize(norm1);
memcpy(&data[at + 18], norm1, 3 * sizeof(float));
memcpy(&data[at + 21], norm1, 3 * sizeof(float));
memcpy(&data[at + 24], norm1, 3 * sizeof(float));
data[at + 9] = _COL_AT(terrain, x + 1, y + 1, r);
data[at + 10] = _COL_AT(terrain, x + 1, y + 1, g);
data[at + 11] = _COL_AT(terrain, x + 1, y + 1, b);
glm_vec3_sub(
(vec3){ x, _HGT_AT(terrain, x, y), y },
(vec3){ x + 0.5f, _avg_hgt(terrain, x, y), y + 0.5f },
dir1
);
glm_vec3_sub(
(vec3){ x, _HGT_AT(terrain, x, y), y },
(vec3){ x, _HGT_AT(terrain, x, y + 1), y + 1 },
dir2
);
glm_vec3_cross(dir2, dir1, norm1);
glm_vec3_normalize(norm1);
memcpy(&data[at + 27], norm1, 3 * sizeof(float));
memcpy(&data[at + 30], norm1, 3 * sizeof(float));
memcpy(&data[at + 33], norm1, 3 * sizeof(float));
data[at + 12] = _COL_AT(terrain, x + 1, y, r);
data[at + 13] = _COL_AT(terrain, x + 1, y, g);
data[at + 14] = _COL_AT(terrain, x + 1, y, b);
glm_vec3_add(
data + at + 3,
data + at + 12,
data + at + 3
);
glm_vec3_add(
data + at + 3,
data + at + 33,
data + at + 3
);
glm_vec3_add(
data + at + 3,
data + at + 24,
data + at + 3
);
glm_vec3_normalize(data + at + 3);
memcpy(data + at + 12, data + at + 3, 3 * sizeof(float));
memcpy(data + at + 24, data + at + 3, 3 * sizeof(float));
memcpy(data + at + 33, data + at + 3, 3 * sizeof(float));
//_norm_smooth(data, x, y);
break;
case 3:
for(int j = 0; j < 12; ++j) {
data[at + j * 3] = _COL_AT(terrain, x, y, r);
data[at + j * 3 + 1] = _COL_AT(terrain, x, y, g);
data[at + j * 3 + 2] = _COL_AT(terrain, x, y, b);
}
const color_t path_colors[] = {
{0x60, 0x60, 0x60, 0xFF},
{0x9C, 0x00, 0x0E, 0xFF}
};
int found = 0;
for(int j = 0; j < sizeof(path_colors) / sizeof(color_t); ++j) {
if(color_eqp(&path_colors[j], &_RWCOL_AT(terrain, x, y))) {
found = 1;
break;
}
}
if(!found)
continue;
if(x == 0 || y == 0)
continue;
int neighbors =
_COL_EQ(terrain, x, y, x, y - 1) +
_COL_EQ(terrain, x, y, x + 1, y) +
_COL_EQ(terrain, x, y, x, y + 1) +
_COL_EQ(terrain, x, y, x - 1, y);
if(neighbors > 2)
continue;
const int coords[][2] = {
{0, -1}, {1, 0},
{0, 1}, {-1, 0}
};
for(int j = 0; j < 4; ++j) {
if(!_COL_EQ(terrain,
x, y,
x + coords[j][0],
y + coords[j][1]))
{
data[at + j * 9] = _COL_AT(terrain,
x + coords[j][0],
y + coords[j][1], r
);
data[at + j * 9 + 1] = _COL_AT(terrain,
x + coords[j][0],
y + coords[j][1], g
);
data[at + j * 9 + 2] = _COL_AT(terrain,
x + coords[j][0],
y + coords[j][1], b
);
memcpy(data + at + j * 9 + 3, data + at + j * 9, 3 * sizeof(float));
memcpy(data + at + j * 9 + 6, data + at + j * 9, 3 * sizeof(float));
}
}
/*
for(int j = 0; j < 4; ++j) {
if(_COL_EQ(terrain,
x + coords[j][0],
y + coords[j][1],
x + coords[j][2],
y + coords[j][3]))
{
for(int k = 0; k < 6; ++k) {
data[at + ((k * 3 + j * 9) % 36)] =
_COL_AT(terrain,
x + coords[j][0],
y + coords[j][1], r);
data[at + ((k * 3 + j * 9 + 1) % 36)] =
_COL_AT(terrain,
x + coords[j][0],
y + coords[j][1], g);
data[at + ((k * 3 + j * 9 + 2) % 36)] =
_COL_AT(terrain,
x + coords[j][0],
y + coords[j][1], b);
}
}
}*/
data[at + 15] = _COL_AT(terrain, x, y + 1, r);
data[at + 16] = _COL_AT(terrain, x, y + 1, g);
data[at + 17] = _COL_AT(terrain, x, y + 1, b);
break;
}
}
@ -156,8 +360,8 @@ void terrain_move(terrain_t* terrain, int center_x, int center_y) {
glBindVertexArray(terrain->vao); {
glEnableVertexAttribArray(i);
glVertexAttribPointer(i, 3, GL_FLOAT,
i == 1 ? GL_TRUE : GL_FALSE, 0, (void*)0);
glVertexAttribPointer(i, cnt, GL_FLOAT,
i == 2 ? GL_TRUE : GL_FALSE, 0, (void*)0);
} glBindVertexArray(0);
} else {
glBufferSubData(

View file

@ -14,12 +14,12 @@
#include "koa/file.h"
#define CHUNK_SIZE 32
#define TERR_BUFFS 3
#define CHUNK_SIZE 100
#define TERR_BUFFS 4
typedef struct {
bmp_t* bmps[2];
int center_x, center_y;
int center_x, center_y, tri_cnt;
float heights[CHUNK_SIZE][CHUNK_SIZE];
GLuint buffers[TERR_BUFFS], vao;
} terrain_t;

View file

@ -1,20 +1,48 @@
#version 100
precision mediump float;
varying vec3 f_normal;
precision lowp float;
const vec3 light_color = vec3(0.8, 0.8, 0.8);
const vec3 light = normalize(vec3(-1.0, -1.0, 1.0));
varying vec3 f_normal;
varying vec3 f_color;
const vec3 light_color = vec3(1.0, 1.0, 1.0);
const vec3 light_dir = normalize(vec3(-1.0, 2.0, -1.0));
const float ambient_strength = 0.1;
void main() {
vec3 ambient = light_color * ambient_strength;
vec3 norm = normalize(f_normal);
float diff = max(0.0, dot(norm, light_dir));
vec3 diffuse = diff * light_color;
vec3 color = (ambient + diffuse) * f_color;
gl_FragColor = vec4(color, 1.0);
//gl_FragColor = vec4(f_normal, 1.0);
}
/*varying vec3 f_normal;
//varying vec3 f_pos;
const vec3 light_dir = normalize(vec3(1, 1, -1));
const vec3 object_color = vec3(.4157, .051, .6784);
const vec3 light_pos = vec3(3, 3, 3);
//const vec3 light = normalize(vec3(1.0, 1.0, 1.0));
void main() {
//gl_FragColor = vec4(coord.z, coord.z, coord.z, 1.0);
//float xval = gl_FragCoord.x / 640.0;
//float yval = gl_FragCoord.y / 480.0;
vec3 ambient_color = vec3(0.2, 0.1, 0.1);
float light_intensity = dot(light, normalize(f_normal));
//vec3 color = f_normal;
vec3 color = ambient_color + light_color * light_intensity;
//vec3 color = vec3(light_intensity, light_intensity, light_intensity);
vec3 ambient = light_color * ambient_strength;
vec3 norm = normalize(f_normal);
//vec3 light_dir = normalize(light_pos - f_pos);
float diff = max(0.0, dot(norm, light_dir));
vec3 diffuse = diff * light_color;
//vec3 color = (f_normal + vec3(1.0)) / vec3(2.0);
//vec3 color = f_normal;
//vec3 color = vec3(f_normal, f_normal, f_normal);
vec3 color = (ambient + diffuse) * object_color;
gl_FragColor = vec4(color, 1.0);
}
}*/

View file

@ -4,15 +4,28 @@ precision highp float;
attribute vec3 vertex;
attribute vec2 texuv;
attribute vec3 normal;
attribute vec3 color;
/*layout(location = 0) in vec3 vertex;
layout(location = 1) in vec2 texuv;
layout(location = 2) in vec3 normal;*/
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
varying vec3 f_normal;
varying vec3 f_color;
void main() {
f_normal = normal;
//f_normal = normal;
//f_pos = vertex;
/*vec3 norm = normalize(normal);
float intensity = max(dot(norm, light_dir), 0.0);
f_color = intensity * vec3(.4157, .051, .6784);*/
f_normal = normalize(normal);
f_color = color;
gl_Position =
projection * view * model *
vec4(vertex, 1.0);