broken terrain code do not run
This commit is contained in:
parent
041f291963
commit
8d3c9d4dd8
17 changed files with 2514 additions and 41 deletions
26
src/data/cube.obj
Normal file
26
src/data/cube.obj
Normal file
|
@ -0,0 +1,26 @@
|
|||
# Blender v2.79 (sub 0) OBJ File: ''
|
||||
# www.blender.org
|
||||
mtllib cube.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
|
||||
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 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
|
BIN
src/data/cube.rbm
Normal file
BIN
src/data/cube.rbm
Normal file
Binary file not shown.
BIN
src/data/map-colors.bmp
Normal file
BIN
src/data/map-colors.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
BIN
src/data/map-heights.bmp
Normal file
BIN
src/data/map-heights.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
BIN
src/data/map.xcf
Normal file
BIN
src/data/map.xcf
Normal file
Binary file not shown.
BIN
src/data/monkey.rbm
Normal file
BIN
src/data/monkey.rbm
Normal file
Binary file not shown.
2258
src/data/player.obj
Normal file
2258
src/data/player.obj
Normal file
File diff suppressed because it is too large
Load diff
BIN
src/data/player.rbm
Normal file
BIN
src/data/player.rbm
Normal file
Binary file not shown.
|
@ -6,6 +6,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define __MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||
#define __MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||
|
||||
void mfree(int n, ...);
|
||||
uint8_t* ltoh(uint8_t*, int);
|
||||
uint8_t* btoh(uint8_t*, int);
|
||||
|
|
|
@ -61,12 +61,6 @@ char* file_read(const char* file) {
|
|||
return content;
|
||||
}
|
||||
|
||||
int _bmp_load_metadata(FILE* fp, bmp_meta_t* out) {
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bmp_load_metadata(const char* file, bmp_meta_t* out) {
|
||||
FILE* fp = fopen(file, "rb");
|
||||
if(fp == NULL)
|
||||
|
@ -146,11 +140,8 @@ int bmp_reload_chunk(bmp_t* bmp, int x, int y, int width, int height) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if(bmp->pixels != NULL) {
|
||||
for(int i = 0; i < bmp->height; ++i)
|
||||
free(bmp->pixels[i]);
|
||||
free(bmp->pixels);
|
||||
}
|
||||
if(bmp->pixels != NULL)
|
||||
bmp_discard_pixels(bmp);
|
||||
|
||||
width = (width <= 0 || (x + width > data->width))
|
||||
? data->width - x
|
||||
|
@ -188,7 +179,18 @@ int bmp_reload_chunk(bmp_t* bmp, int x, int y, int width, int height) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
void bmp_discard_pixels(bmp_t* bmp) {
|
||||
if(bmp->pixels == NULL)
|
||||
return;
|
||||
|
||||
for(int i = 0; i < bmp->height; ++i)
|
||||
free(bmp->pixels[i]);
|
||||
free(bmp->pixels);
|
||||
bmp->pixels = NULL;
|
||||
}
|
||||
|
||||
void bmp_unload(bmp_t* bmp) {
|
||||
bmp_discard_pixels(bmp);
|
||||
free(bmp->pixels);
|
||||
free(bmp);
|
||||
}
|
|
@ -42,6 +42,7 @@ bmp_t* bmp_load(const char*);
|
|||
bmp_t* bmp_load_chunk(const char*, int, int, int, int);
|
||||
int bmp_reload_chunk(bmp_t*, int, int, int, int);
|
||||
int bmp_load_metadata(const char*, bmp_meta_t*);
|
||||
void bmp_discard_pixels(bmp_t*);
|
||||
void bmp_unload(bmp_t*);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,23 +6,23 @@ void get_pixel(SDL_Color* out, SDL_Surface* img, int x, int y) {
|
|||
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;
|
||||
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);
|
||||
|
|
|
@ -41,7 +41,7 @@ int main(int argc, char* argv[]) {
|
|||
if(init() < 0)
|
||||
return -1;
|
||||
|
||||
_g.monkey = mesh_load("data/player.rbm");
|
||||
_g.monkey = mesh_load("data/cube.rbm");
|
||||
|
||||
_s_def.shader = shader_create("default");
|
||||
shader_source(_s_def.shader, 2,
|
||||
|
@ -91,7 +91,7 @@ void run() {
|
|||
glClearColor(0.f, 0.f, 0.5f, 0.f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
float radius = 2.f + 2.f * (rot_up / 90.f);
|
||||
float radius = 4.f + 4.f * (rot_up / 90.f);
|
||||
glm_lookat(
|
||||
(vec3){
|
||||
cos(glm_rad(rot_up)) * radius * cos(glm_rad(rot_around)),
|
||||
|
|
|
@ -1,8 +1,178 @@
|
|||
#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)
|
||||
|
||||
terrain_t* terrain_load
|
||||
(const char* heights, const char* colors,
|
||||
int initial_x, int initial_y)
|
||||
int center_x, int center_y)
|
||||
{
|
||||
int top_x = __MAX(0, center_x - CHUNK_SIZE / 2),
|
||||
top_y = __MAX(0, center_y - CHUNK_SIZE / 2);
|
||||
|
||||
bmp_t* heights_bmp =
|
||||
bmp_load_chunk(heights, top_x, top_y, CHUNK_SIZE, CHUNK_SIZE);
|
||||
if(heights_bmp == NULL)
|
||||
return NULL;
|
||||
|
||||
bmp_t* colors_bmp =
|
||||
bmp_load_chunk(colors, top_x, top_y, CHUNK_SIZE, CHUNK_SIZE);
|
||||
if(colors_bmp == NULL) {
|
||||
bmp_unload(heights_bmp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
terrain_t* terrain = malloc(sizeof(terrain_t));
|
||||
terrain->bmps[0] = heights_bmp;
|
||||
terrain->bmps[1] = colors_bmp;
|
||||
terrain->center_x = terrain->center_y = -1;
|
||||
|
||||
terrain_move(terrain, center_x, center_y);
|
||||
return terrain;
|
||||
}
|
||||
|
||||
void terrain_move(terrain_t* terrain, int center_x, int center_y) {
|
||||
int new_terrain = terrain->center_x == -1 || terrain->center_y == -1;
|
||||
|
||||
if(new_terrain) {
|
||||
glGenVertexArrays(1, &terrain->vao);
|
||||
glGenBuffers(TERR_BUFFS, terrain->buffers);
|
||||
} else {
|
||||
int top_x = __MAX(0, center_x - CHUNK_SIZE / 2),
|
||||
top_y = __MAX(0, center_y - CHUNK_SIZE / 2);
|
||||
|
||||
bmp_reload_chunk
|
||||
(terrain->bmps[0], top_x, top_y, CHUNK_SIZE, CHUNK_SIZE);
|
||||
bmp_reload_chunk
|
||||
(terrain->bmps[1], top_x, top_y, CHUNK_SIZE, CHUNK_SIZE);
|
||||
}
|
||||
|
||||
terrain->center_x = center_x;
|
||||
terrain->center_y = center_y;
|
||||
|
||||
const int data_length = 6 * 3 * (CHUNK_SIZE - 1) * (CHUNK_SIZE - 1);
|
||||
float data[data_length];
|
||||
vec3 dir1, dir2, norm;
|
||||
|
||||
for(int i = 0; i < TERR_BUFFS; ++i) {
|
||||
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);
|
||||
|
||||
switch(i) {
|
||||
case 0:
|
||||
data[at] = x;
|
||||
data[at + 1] = _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 + 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 + 12] = x + 1;
|
||||
data[at + 13] = _HGT_AT(terrain, x + 1, y);
|
||||
data[at + 14] = y;
|
||||
|
||||
data[at + 15] = x;
|
||||
data[at + 16] = _HGT_AT(terrain, x, y + 1);
|
||||
data[at + 17] = y + 1;
|
||||
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));
|
||||
|
||||
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));
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, terrain->buffers[i]);
|
||||
if(new_terrain) {
|
||||
glBufferData(
|
||||
GL_ARRAY_BUFFER,
|
||||
data_length * sizeof(float),
|
||||
data,
|
||||
GL_DYNAMIC_DRAW
|
||||
);
|
||||
|
||||
glBindVertexArray(terrain->vao); {
|
||||
glEnableVertexAttribArray(i);
|
||||
glVertexAttribPointer(i, 3, GL_FLOAT,
|
||||
i == 1 ? GL_TRUE : GL_FALSE, 0, (void*)0);
|
||||
} glBindVertexArray(0);
|
||||
} else {
|
||||
glBufferSubData(
|
||||
GL_ARRAY_BUFFER, 0,
|
||||
data_length * sizeof(float),
|
||||
data
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void terrain_unload(terrain_t* terrain) {
|
||||
bmp_unload(terrain->bmps[0]);
|
||||
bmp_unload(terrain->bmps[1]);
|
||||
free(terrain);
|
||||
}
|
|
@ -8,22 +8,24 @@
|
|||
#include <cglm/cglm.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "koa/file.h"
|
||||
|
||||
#define CHUNK_SIZE 20
|
||||
#define CHUNK_SIZE 32
|
||||
#define TERR_BUFFS 3
|
||||
|
||||
typedef struct {
|
||||
bmp_meta_t meta[2];
|
||||
GLuint buffers[3], vao;
|
||||
int offset_x, offset_y;
|
||||
uint8_t heights[CHUNK_SIZE][CHUNK_SIZE];
|
||||
bmp_t* bmps[2];
|
||||
int center_x, center_y;
|
||||
float heights[CHUNK_SIZE][CHUNK_SIZE];
|
||||
GLuint buffers[TERR_BUFFS], vao;
|
||||
} terrain_t;
|
||||
|
||||
terrain_t* terrain_load(const char*, const char*, int, int);
|
||||
|
||||
void terrain_move(terrain_t*, int, int);
|
||||
void terrain_unload(terrain_t*);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,12 +1,20 @@
|
|||
#version 100
|
||||
precision mediump float;
|
||||
varying vec3 f_normal;
|
||||
|
||||
//varying vec3 coord;
|
||||
const vec3 light_color = vec3(0.8, 0.8, 0.8);
|
||||
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;
|
||||
gl_FragColor = vec4(xval, yval, 1.0, 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);
|
||||
|
||||
gl_FragColor = vec4(color, 1.0);
|
||||
}
|
|
@ -9,7 +9,10 @@ uniform mat4 model;
|
|||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
varying vec3 f_normal;
|
||||
|
||||
void main() {
|
||||
f_normal = normal;
|
||||
gl_Position =
|
||||
projection * view * model *
|
||||
vec4(vertex, 1.0);
|
||||
|
|
Loading…
Reference in a new issue