block placement, texture loading, random generation
This commit is contained in:
parent
8ac68e31e7
commit
cd0b0ed0df
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
BIN
assets/stone.png
Normal file
BIN
assets/stone.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
@ -6,6 +6,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "noise.h"
|
#include "noise.h"
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
@ -21,6 +22,14 @@ typedef unsigned int uint;
|
|||||||
#define CHUNK_MAX_Y 16
|
#define CHUNK_MAX_Y 16
|
||||||
#define CHUNK_MAX_Z 128
|
#define CHUNK_MAX_Z 128
|
||||||
|
|
||||||
|
#define MAX_PARTICLE_GROUPS 10
|
||||||
|
#define PARTICLES_PER_GROUP 50
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
vec3f pos[PARTICLES_PER_GROUP];
|
||||||
|
unsigned int time;
|
||||||
|
} ParticleGroup;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool is_running;
|
bool is_running;
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
@ -30,14 +39,14 @@ typedef struct {
|
|||||||
ChunkManager *cm;
|
ChunkManager *cm;
|
||||||
Type_surface *surfaces;
|
Type_surface *surfaces;
|
||||||
unsigned int surface_count;
|
unsigned int surface_count;
|
||||||
|
ParticleGroup particles[MAX_PARTICLE_GROUPS];
|
||||||
|
unsigned char particle_count;
|
||||||
} App;
|
} App;
|
||||||
|
|
||||||
void app_generate_world(App *app, int a);
|
void app_generate_world(App *app, int a);
|
||||||
|
|
||||||
void app_load_textures(App *app);
|
void app_load_textures(App *app);
|
||||||
|
|
||||||
GLuint app_get_texture(App *app, Block_type type);
|
|
||||||
|
|
||||||
int check_intersection_block(App *app, vec3i *retv, int *chunk_index);
|
int check_intersection_block(App *app, vec3i *retv, int *chunk_index);
|
||||||
|
|
||||||
void app_break_block(App *app);
|
void app_break_block(App *app);
|
||||||
@ -58,4 +67,8 @@ void update_app(App* app);
|
|||||||
|
|
||||||
float calc_frame_time(struct timespec *start, struct timespec *end);
|
float calc_frame_time(struct timespec *start, struct timespec *end);
|
||||||
|
|
||||||
|
void app_draw_particles(App *app, vec3i b);
|
||||||
|
|
||||||
|
void app_update_particles(App *app);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
|
|
||||||
#define GRAVITY 5
|
#define GRAVITY 4
|
||||||
/**
|
/**
|
||||||
* Camera, as a moving point with direction
|
* Camera, as a moving point with direction
|
||||||
*/
|
*/
|
||||||
|
@ -46,8 +46,9 @@ void chunk_set_blocks_visibility(Chunk* chunk);
|
|||||||
void chunk_create_displayl(ChunkManager *cm, Chunk *chunk);
|
void chunk_create_displayl(ChunkManager *cm, Chunk *chunk);
|
||||||
void chunk_render(Chunk* chunk);
|
void chunk_render(Chunk* chunk);
|
||||||
void chunk_update(ChunkManager* cm, Chunk *chunk);
|
void chunk_update(ChunkManager* cm, Chunk *chunk);
|
||||||
void draw_cube(ChunkManager *cm, Block_type type, float x, float y, float z);
|
void draw_cube(ChunkManager *cm, Block_type type, float x, float y, float z, float s);
|
||||||
bool is_block(Block b);
|
bool is_block(Block b);
|
||||||
int chunk_get_current_chunk_index(ChunkManager *cm, vec3f pos);
|
int chunk_get_current_chunk_index(ChunkManager *cm, vec3f pos);
|
||||||
|
GLuint get_texture(ChunkManager *cm, Block_type type);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#ifndef UTILS_H
|
#ifndef UTILS_H
|
||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -59,6 +58,8 @@ void vec3f_normalize(vec3f *v);
|
|||||||
|
|
||||||
int irand_range(int min, int max);
|
int irand_range(int min, int max);
|
||||||
|
|
||||||
|
float frand_range(float min, float max);
|
||||||
|
|
||||||
bool check_index(int x, int y, int z);
|
bool check_index(int x, int y, int z);
|
||||||
|
|
||||||
void print_vec3f(vec3f v, const char *str);
|
void print_vec3f(vec3f v, const char *str);
|
||||||
|
111
src/app.c
111
src/app.c
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
void init_app(App *app, uint w, uint h)
|
void init_app(App *app, uint w, uint h)
|
||||||
{
|
{
|
||||||
|
srand(time(NULL));
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
err = SDL_Init(SDL_INIT_EVERYTHING);
|
err = SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
@ -42,6 +43,7 @@ void init_app(App *app, uint w, uint h)
|
|||||||
app_load_textures(app);
|
app_load_textures(app);
|
||||||
app->cm->textures = app->surfaces;
|
app->cm->textures = app->surfaces;
|
||||||
app->cm->texture_count = app->surface_count;
|
app->cm->texture_count = app->surface_count;
|
||||||
|
app->particle_count = 0;
|
||||||
|
|
||||||
app_generate_world(app, 10);
|
app_generate_world(app, 10);
|
||||||
app->is_running = true;
|
app->is_running = true;
|
||||||
@ -49,15 +51,13 @@ void init_app(App *app, uint w, uint h)
|
|||||||
|
|
||||||
void app_load_textures(App *app)
|
void app_load_textures(App *app)
|
||||||
{
|
{
|
||||||
app->surfaces[0].type = BLOCKTYPE_GRASS;
|
app->surfaces[0].type = BLOCKTYPE_DIRT;
|
||||||
app->surfaces[1].type = BLOCKTYPE_DIRT;
|
app->surfaces[1].type = BLOCKTYPE_STONE;
|
||||||
app->surfaces[2].type = BLOCKTYPE_STONE;
|
|
||||||
|
|
||||||
app->surfaces[0].surface = IMG_Load("assets/block.png");
|
app->surfaces[0].surface = IMG_Load("assets/dirt.png");
|
||||||
app->surfaces[1].surface = IMG_Load("assets/block.png");
|
app->surfaces[1].surface = IMG_Load("assets/stone.png");
|
||||||
app->surfaces[2].surface = IMG_Load("assets/block.png");
|
|
||||||
|
|
||||||
for (int i = 0; i < app->surface_count-1; i++) {
|
for (int i = 0; i < 1; i++) {
|
||||||
if(app->surfaces[i].surface == NULL) {
|
if(app->surfaces[i].surface == NULL) {
|
||||||
printf("error loading texture\n");
|
printf("error loading texture\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -78,13 +78,14 @@ void app_load_textures(App *app)
|
|||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
//glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid);
|
glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_app(App* app)
|
void update_app(App* app)
|
||||||
{
|
{
|
||||||
update_camera(&(app->camera), app->frame_time/1000);
|
update_camera(&(app->camera), app->frame_time/1000);
|
||||||
|
app_update_particles(app);
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_opengl()
|
void init_opengl()
|
||||||
@ -232,10 +233,14 @@ void app_generate_world(App *app, int a)
|
|||||||
app->cm->chunk_count = a*a;
|
app->cm->chunk_count = a*a;
|
||||||
app->cm->chunks = malloc(a*a*sizeof(Chunk));
|
app->cm->chunks = malloc(a*a*sizeof(Chunk));
|
||||||
memset(app->cm->chunks, 0, a*a*sizeof(Chunk));
|
memset(app->cm->chunks, 0, a*a*sizeof(Chunk));
|
||||||
|
long block_count = 0;
|
||||||
|
|
||||||
|
|
||||||
unsigned char ptable[512];
|
unsigned char ptable[512];
|
||||||
noise_init_ptable(ptable);
|
noise_init_ptable(ptable);
|
||||||
|
|
||||||
|
int r = irand_range(-10000, 10000);
|
||||||
|
|
||||||
for (int i = 0; i < a; i++) {
|
for (int i = 0; i < a; i++) {
|
||||||
for (int j = 0; j < a; j++) {
|
for (int j = 0; j < a; j++) {
|
||||||
Chunk c;
|
Chunk c;
|
||||||
@ -246,13 +251,15 @@ void app_generate_world(App *app, int a)
|
|||||||
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
||||||
//printf("x: %d y:%d noise: %f\n", x, y, round((noise_noise2(ptable,
|
//printf("x: %d y:%d noise: %f\n", x, y, round((noise_noise2(ptable,
|
||||||
// (c.start_pos.x+x)*0.03, (c.start_pos.y+y)*0.03) + 1) * 0.5 * 15);
|
// (c.start_pos.x+x)*0.03, (c.start_pos.y+y)*0.03) + 1) * 0.5 * 15);
|
||||||
int mz = round((noise_noise2(ptable, (c.start_pos.x+x)*0.03, (c.start_pos.y+y)*0.03) + 1) * 0.5 * 15);
|
int mz = round((noise_noise2(ptable, (c.start_pos.x+x+r)*0.02, (c.start_pos.y+y+r)*0.02)+1)* 0.5 * 10);
|
||||||
if (mz < 0)
|
if (mz < 0)
|
||||||
mz = 0;
|
mz = 0;
|
||||||
else if (mz > CHUNK_MAX_Z)
|
else if (mz > CHUNK_MAX_Z)
|
||||||
mz = CHUNK_MAX_Z-1;
|
mz = CHUNK_MAX_Z-1;
|
||||||
for (int z = 0; z < mz; z++)
|
for (int z = 0; z < mz; z++) {
|
||||||
c.blocks[x][y][z].type = BLOCKTYPE_DIRT;
|
c.blocks[x][y][z].type = BLOCKTYPE_DIRT;
|
||||||
|
block_count++;
|
||||||
|
}
|
||||||
for (int z = mz; z < CHUNK_MAX_Z; z++)
|
for (int z = mz; z < CHUNK_MAX_Z; z++)
|
||||||
c.blocks[x][y][z].type = BLOCKTYPE_AIR;
|
c.blocks[x][y][z].type = BLOCKTYPE_AIR;
|
||||||
}
|
}
|
||||||
@ -263,6 +270,7 @@ void app_generate_world(App *app, int a)
|
|||||||
app->cm->chunks[i+(j*a)] = c;
|
app->cm->chunks[i+(j*a)] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
printf("block count: %d\n", block_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -338,12 +346,12 @@ int check_intersection_block(App *app, vec3i *retv, int *chunk_index)
|
|||||||
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
||||||
for (int z = 0; z < CHUNK_MAX_Z; z++) {
|
for (int z = 0; z < CHUNK_MAX_Z; z++) {
|
||||||
if (app->cm->chunks[i].blocks[x][y][z].visible) {
|
if (app->cm->chunks[i].blocks[x][y][z].visible) {
|
||||||
for (float j = 0; j < 5; j+=0.2) {
|
for (float j = 0; j < 5; j+=0.1) {
|
||||||
vec3f ray = {(c_pos.x - app->cm->chunks[i].start_pos.x) + c_dir.x * j,
|
vec3f ray = {(c_pos.x - app->cm->chunks[i].start_pos.x) + c_dir.x * j,
|
||||||
(c_pos.y - app->cm->chunks[i].start_pos.y) + c_dir.y * j,
|
(c_pos.y - app->cm->chunks[i].start_pos.y) + c_dir.y * j,
|
||||||
(c_pos.z - app->cm->chunks[i].start_pos.z) + c_dir.z * j};
|
(c_pos.z - app->cm->chunks[i].start_pos.z) + c_dir.z * j};
|
||||||
printf("ray x: %f y: %f z: %f\n", ray.x, ray.y, ray.z);
|
printf("ray x: %f y: %f z: %f\n", ray.x, ray.y, ray.z);
|
||||||
vec3i rayi = {round(ray.x/1), round(ray.y/1), round(ray.z/1)};
|
vec3i rayi = {(int)ray.x, (int)ray.y, (int)ray.z};
|
||||||
|
|
||||||
if (is_block(app->cm->chunks[i].blocks[rayi.x][rayi.y][rayi.z])) {
|
if (is_block(app->cm->chunks[i].blocks[rayi.x][rayi.y][rayi.z])) {
|
||||||
retv->x = rayi.x;
|
retv->x = rayi.x;
|
||||||
@ -359,6 +367,44 @@ int check_intersection_block(App *app, vec3i *retv, int *chunk_index)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void app_draw_particles(App *app, vec3i b)
|
||||||
|
{
|
||||||
|
unsigned char index = app->particle_count;
|
||||||
|
|
||||||
|
if (index >= MAX_PARTICLE_GROUPS)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (int i = 0; i < PARTICLES_PER_GROUP; i++) {
|
||||||
|
vec3f p = {b.x+i/50+frand_range(-0.1,0.1),
|
||||||
|
b.y+i/50+frand_range(-0.1,0.1),
|
||||||
|
b.z+frand_range(-0.1, 0.1)};
|
||||||
|
app->particles[index].pos[i] = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
app->particles[index].time = SDL_GetTicks();
|
||||||
|
|
||||||
|
app->particle_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_update_particles(App *app)
|
||||||
|
{
|
||||||
|
unsigned char particles = app->particle_count;
|
||||||
|
for (int i = 0; i < particles; i++) {
|
||||||
|
unsigned int time = SDL_GetTicks();
|
||||||
|
if (time - app->particles[i].time > 1000) {
|
||||||
|
memset(&app->particles[i], 0, sizeof(ParticleGroup));
|
||||||
|
app->particle_count--;
|
||||||
|
} else {
|
||||||
|
for (int j = 0; j < PARTICLES_PER_GROUP; j++) {
|
||||||
|
app->particles[i].pos[j].z -= GRAVITY * app->frame_time/1000;
|
||||||
|
draw_cube(app->cm, 0, app->particles[i].pos[j].x,
|
||||||
|
app->particles[i].pos[j].y,
|
||||||
|
app->particles[i].pos[j].z, 0.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void app_break_block(App *app)
|
void app_break_block(App *app)
|
||||||
{
|
{
|
||||||
printf("%s\n", __func__);
|
printf("%s\n", __func__);
|
||||||
@ -376,6 +422,8 @@ void app_break_block(App *app)
|
|||||||
|
|
||||||
app->cm->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR;
|
app->cm->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR;
|
||||||
chunk_update(app->cm, &(app->cm->chunks[i]));
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
|
||||||
|
app_draw_particles(app, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_place_block(App *app)
|
void app_place_block(App *app)
|
||||||
@ -392,16 +440,48 @@ void app_place_block(App *app)
|
|||||||
vec3f t = { (float) cam_pos.x - block.x,
|
vec3f t = { (float) cam_pos.x - block.x,
|
||||||
(float) cam_pos.y - block.y,
|
(float) cam_pos.y - block.y,
|
||||||
(float) cam_pos.z - block.z };
|
(float) cam_pos.z - block.z };
|
||||||
|
printf("t x: %f\n", t.x);
|
||||||
|
printf("t y: %f\n", t.y);
|
||||||
|
//printf("t x: %f\n", t.x);
|
||||||
|
|
||||||
|
if (t.z > 0) {
|
||||||
|
app->cm->chunks[i].blocks[block.x][block.y][block.z+1].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, +z");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
app->cm->chunks[i].blocks[block.x][block.y][block.z+1].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, -z");
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (t.x > 0) {
|
if (t.x > 0) {
|
||||||
if (t.y < t.x && t.y > -t.x) {
|
if (t.z > 0) {
|
||||||
|
app->cm->chunks[i].blocks[block.x][block.y][block.z+1].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, +z");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
app->cm->chunks[i].blocks[block.x][block.y][block.z+1].type = BLOCKTYPE_STONE;
|
||||||
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, -z");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (t.y < t.x && t.y > -t.x &&
|
||||||
|
!is_block(app->cm->chunks[i].blocks[block.x+1][block.y][block.z])) {
|
||||||
|
|
||||||
app->cm->chunks[i].blocks[block.x+1][block.y][block.z].type = BLOCKTYPE_STONE;
|
app->cm->chunks[i].blocks[block.x+1][block.y][block.z].type = BLOCKTYPE_STONE;
|
||||||
chunk_update(app->cm, &(app->cm->chunks[i]));
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, +x");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (t.y < -t.x && t.y > t.x) {
|
if (t.y < -t.x && t.y > t.x &&
|
||||||
|
!is_block(app->cm->chunks[i].blocks[block.x-1][block.y][block.z])) {
|
||||||
|
|
||||||
app->cm->chunks[i].blocks[block.x-1][block.y][block.z].type = BLOCKTYPE_STONE;
|
app->cm->chunks[i].blocks[block.x-1][block.y][block.z].type = BLOCKTYPE_STONE;
|
||||||
chunk_update(app->cm, &(app->cm->chunks[i]));
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, -x");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -409,13 +489,16 @@ void app_place_block(App *app)
|
|||||||
if (t.y > 0) {
|
if (t.y > 0) {
|
||||||
app->cm->chunks[i].blocks[block.x][block.y+1][block.z].type = BLOCKTYPE_STONE;
|
app->cm->chunks[i].blocks[block.x][block.y+1][block.z].type = BLOCKTYPE_STONE;
|
||||||
chunk_update(app->cm, &(app->cm->chunks[i]));
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, +y");
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
app->cm->chunks[i].blocks[block.x][block.y-1][block.z].type = BLOCKTYPE_STONE;
|
app->cm->chunks[i].blocks[block.x][block.y-1][block.z].type = BLOCKTYPE_STONE;
|
||||||
chunk_update(app->cm, &(app->cm->chunks[i]));
|
chunk_update(app->cm, &(app->cm->chunks[i]));
|
||||||
|
puts("block blaced, -y");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//if (cam_pos.x < (float)block.x+dist/2 &&
|
//if (cam_pos.x < (float)block.x+dist/2 &&
|
||||||
// cam_pos.x > (float)block.x-dist/2) {
|
// cam_pos.x > (float)block.x-dist/2) {
|
||||||
// if (cam_pos.x > block.x)
|
// if (cam_pos.x > block.x)
|
||||||
|
23
src/camera.c
23
src/camera.c
@ -9,7 +9,7 @@ void init_camera(Camera* camera)
|
|||||||
{
|
{
|
||||||
camera->position.x = 5.0;
|
camera->position.x = 5.0;
|
||||||
camera->position.y = 5.0;
|
camera->position.y = 5.0;
|
||||||
camera->position.z = 40.0;
|
camera->position.z = 90.0;
|
||||||
camera->rotation.x = 0.0;
|
camera->rotation.x = 0.0;
|
||||||
camera->rotation.y = 0.0;
|
camera->rotation.y = 0.0;
|
||||||
camera->rotation.z = 0.0;
|
camera->rotation.z = 0.0;
|
||||||
@ -76,9 +76,9 @@ void update_camera(Camera* camera, double time)
|
|||||||
float y1 = camera->position.y + sin(side_angle) * camera->speed.x * time;
|
float y1 = camera->position.y + sin(side_angle) * camera->speed.x * time;
|
||||||
float z1 = camera->position.z + camera->speed.z * time;
|
float z1 = camera->position.z + camera->speed.z * time;
|
||||||
|
|
||||||
printf("x: %f\n",camera->position.x + camera->speed.z * time);
|
//printf("x: %f\n",camera->position.x + camera->speed.z * time);
|
||||||
printf("x: %f\n",camera->position.y + cos(angle) * camera->speed.y * time);
|
//printf("x: %f\n",camera->position.y + cos(angle) * camera->speed.y * time);
|
||||||
printf("x: %f\n",camera->position.z + sin(angle) * camera->speed.y * time);
|
//printf("x: %f\n",camera->position.z + sin(angle) * camera->speed.y * time);
|
||||||
|
|
||||||
//vec3f v = handle_collision(camera->position, camera->cm);
|
//vec3f v = handle_collision(camera->position, camera->cm);
|
||||||
|
|
||||||
@ -98,32 +98,35 @@ void update_camera(Camera* camera, double time)
|
|||||||
x -= cs.x; y -= cs.y; z -= cs.z;
|
x -= cs.x; y -= cs.y; z -= cs.z;
|
||||||
x1 -= cs.x; y1 -= cs.y; z1 -= cs.z;
|
x1 -= cs.x; y1 -= cs.y; z1 -= cs.z;
|
||||||
|
|
||||||
|
x += 0.3; y += 0.3;
|
||||||
|
x1 += 0.3; y1 += 0.3;
|
||||||
|
|
||||||
if(!is_block(camera->cm->chunks[i].blocks[(int)x][(int)y][(int)z-1])) {
|
if(!is_block(camera->cm->chunks[i].blocks[(int)x][(int)y][(int)z-1])) {
|
||||||
puts("not is block");
|
//puts("not is block");
|
||||||
camera->speed.z -= GRAVITY * time;
|
camera->speed.z -= GRAVITY * time;
|
||||||
camera->position.x += cos(angle) * camera->speed.y * time;
|
camera->position.x += cos(angle) * camera->speed.y * time;
|
||||||
camera->position.y += sin(angle) * camera->speed.y * time;
|
camera->position.y += sin(angle) * camera->speed.y * time;
|
||||||
camera->position.z += camera->speed.z * time;
|
camera->position.z += camera->speed.z * time;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
puts("is block");
|
//puts("is block");
|
||||||
camera->speed.z = 0;
|
camera->speed.z = 0;
|
||||||
float diff = ceil(camera->position.z) - camera->position.z;
|
float diff = round(camera->position.z) - camera->position.z;
|
||||||
camera->position.z += diff;
|
camera->position.z += diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!is_block(camera->cm->chunks[i].blocks[(int)x1][(int)y1][(int)z1-1])) {
|
if(!is_block(camera->cm->chunks[i].blocks[(int)x1][(int)y1][(int)z1-1])) {
|
||||||
puts("not is block");
|
//puts("not is block");
|
||||||
camera->speed.z -= GRAVITY * time;
|
camera->speed.z -= GRAVITY * time;
|
||||||
camera->position.x += cos(side_angle) * camera->speed.x * time;
|
camera->position.x += cos(side_angle) * camera->speed.x * time;
|
||||||
camera->position.y += sin(side_angle) * camera->speed.x * time;
|
camera->position.y += sin(side_angle) * camera->speed.x * time;
|
||||||
camera->position.z += camera->speed.z * time;
|
camera->position.z += camera->speed.z * time;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
puts("is block");
|
//puts("is block");
|
||||||
camera->speed.z = 0;
|
camera->speed.z = 0;
|
||||||
|
|
||||||
float diff = ceil(camera->position.z) - camera->position.z;
|
float diff = round(camera->position.z) - camera->position.z;
|
||||||
camera->position.z += diff;
|
camera->position.z += diff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
82
src/chunk.c
82
src/chunk.c
@ -45,7 +45,7 @@ void chunk_update(ChunkManager *cm, Chunk *chunk)
|
|||||||
chunk_create_displayl(cm, chunk);
|
chunk_create_displayl(cm, chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_cube(ChunkManager *cm, Block_type type, float x, float y, float z)
|
void draw_cube(ChunkManager *cm, Block_type type, float x, float y, float z, float s)
|
||||||
{
|
{
|
||||||
/*x*=2;
|
/*x*=2;
|
||||||
y*=2;
|
y*=2;
|
||||||
@ -57,57 +57,65 @@ void draw_cube(ChunkManager *cm, Block_type type, float x, float y, float z)
|
|||||||
glBindTexture(GL_TEXTURE_2D, cm->textures[i].tid);
|
glBindTexture(GL_TEXTURE_2D, cm->textures[i].tid);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
glColor3f(1, 1, 1);
|
||||||
|
|
||||||
glBegin( GL_QUADS );
|
glBegin( GL_QUADS );
|
||||||
|
|
||||||
glColor3f(1, 0, 1);
|
//glColor3f(1, 0, 1);
|
||||||
glTexCoord2f(0, 0); glVertex3f(x+1.0, y, z-1.0 );
|
glColor3f( 0.0f, 1.0f, 0.0f ); // Green
|
||||||
glTexCoord2f(0, 1); glVertex3f(x, y, z-1.0);
|
glTexCoord2f(0, 0); glVertex3f(x-0.5*s, y+0.5*s, z+0.5*s);
|
||||||
glTexCoord2f(1, 1); glVertex3f(x, y, z);
|
glTexCoord2f(0, 1); glVertex3f(x+0.5*s, y+0.5*s, z+0.5*s);
|
||||||
glTexCoord2f(1, 0); glVertex3f(x+1.0, y, z);
|
glTexCoord2f(1, 1); glVertex3f(x+0.5*s, y-0.5*s, z+0.5*s);
|
||||||
|
glTexCoord2f(1, 0); glVertex3f(x-0.5*s, y-0.5*s, z+0.5*s);
|
||||||
|
|
||||||
glColor3f(1, 0, 1);
|
//glColor3f(1, 0, 1);
|
||||||
glTexCoord2f(0, 0); glVertex3f(x+1.0, y-1.0, z-1.0);
|
glColor3f(1, 1, 1);
|
||||||
glTexCoord2f(0, 1); glVertex3f(x, y-1.0, z-1.0);
|
glTexCoord2f(0, 0); glVertex3f(x+0.5*s, y-0.5*s, z-0.5*s);
|
||||||
glTexCoord2f(1, 1); glVertex3f(x, y-1.0, z);
|
glTexCoord2f(0, 1); glVertex3f(x-0.5*s, y-0.5*s, z-0.5*s);
|
||||||
glTexCoord2f(1, 0); glVertex3f(x+1.0, y-1.0, z);
|
glTexCoord2f(1, 1); glVertex3f(x-0.5*s, y+0.5*s, z-0.5*s);
|
||||||
|
glTexCoord2f(1, 0); glVertex3f(x+0.5*s, y+0.5*s, z-0.5*s);
|
||||||
|
|
||||||
glColor3f(1, 0, 1);
|
//glColor3f(1, 0, 1);
|
||||||
glTexCoord2f(0, 0); glVertex3f(x+1.0, y, z);
|
glTexCoord2f(0, 0); glVertex3f(x+0.5*s, y+0.5*s, z+0.5*s);
|
||||||
glTexCoord2f(0, 1); glVertex3f(x, y, z);
|
glTexCoord2f(0, 1); glVertex3f(x-0.5*s, y+0.5*s, z+0.5*s);
|
||||||
glTexCoord2f(1, 1); glVertex3f(x, y-1.0, z);
|
glTexCoord2f(1, 1); glVertex3f(x-0.5*s, y+0.5*s, z-0.5*s);
|
||||||
glTexCoord2f(1, 0); glVertex3f(x+1.0, y-1.0, z);
|
glTexCoord2f(1, 0); glVertex3f(x+0.5*s, y+0.5*s, z-0.5*s);
|
||||||
|
|
||||||
glColor3f(1, 0, 1);
|
//glColor3f(1, 0, 1);
|
||||||
glTexCoord2f(0, 0); glVertex3f(x, y, z-1.0); // Top-Right of back face
|
glTexCoord2f(0, 0); glVertex3f(x+0.5*s, y-0.5*s, z+0.5*s); // Top-Right of back face
|
||||||
glTexCoord2f(0, 1); glVertex3f(x+1.0, y, z-1.0); // Top-Left of back face
|
glTexCoord2f(0, 1); glVertex3f(x-0.5*s, y-0.5*s, z+0.5*s); // Top-Left of back face
|
||||||
glTexCoord2f(1, 1); glVertex3f(x+1.0, y-1.0, z-1.0); // Bottom-Left of back face
|
glTexCoord2f(1, 1); glVertex3f(x-0.5*s, y-0.5*s, z-0.5*s); // Bottom-Left of back face
|
||||||
glTexCoord2f(1, 0); glVertex3f(x, y-1.0, z-1.0); // Bottom-Right of back face
|
glTexCoord2f(1, 0); glVertex3f(x+0.5*s, y-0.5*s, z-0.5*s); // Bottom-Right of back face
|
||||||
|
|
||||||
glColor3f(1, 0, 1);
|
//glColor3f(1, 0, 1);
|
||||||
glTexCoord2f(0, 0); glVertex3f(x, y, z); // Top-Right of left face
|
glTexCoord2f(0, 0); glVertex3f(x-0.5*s, y-0.5*s, z+0.5*s); // Top-Right of left face
|
||||||
glTexCoord2f(0, 1); glVertex3f(x, y, z-1.0); // Top-Left of left face
|
glTexCoord2f(0, 1); glVertex3f(x-0.5*s, y+0.5*s, z+0.5*s); // Top-Left of left face
|
||||||
glTexCoord2f(1, 1); glVertex3f(x, y-1.0, z-1.0); // Bottom-Left of left face
|
glTexCoord2f(1, 1); glVertex3f(x-0.5*s, y+0.5*s, z-0.5*s); // Bottom-Left of left face
|
||||||
glTexCoord2f(1, 0); glVertex3f(x, y-1.0, z); // Bottom-Right of left face
|
glTexCoord2f(1, 0); glVertex3f(x-0.5*s, y-0.5*s, z-0.5*s); // Bottom-Right of left face
|
||||||
|
|
||||||
glColor3f(1, 0, 1);
|
//glColor3f(1, 0, 1);
|
||||||
glTexCoord2f(0, 0); glVertex3f(x+1.0, y, z); // Top-Right of left face
|
glTexCoord2f(0, 0); glVertex3f(x+0.5*s, y-0.5*s, z+0.5*s); // Top-Right of right face
|
||||||
glTexCoord2f(0, 1); glVertex3f(x+1.0, y, z-1.0); // Top-Left of left face
|
glTexCoord2f(0, 1); glVertex3f(x+0.5*s, y+0.5*s, z+0.5*s); // Top-Left of right face
|
||||||
glTexCoord2f(1, 1); glVertex3f(x+1.0, y-1.0, z-1.0); // Bottom-Left of left face
|
glTexCoord2f(1, 1); glVertex3f(x+0.5*s, y+0.5*s, z-0.5*s); // Bottom-Left of right face
|
||||||
glTexCoord2f(1, 0); glVertex3f(x+1.0, y-1.0, z); // Bottom-Right of left face
|
glTexCoord2f(1, 0); glVertex3f(x+0.5*s, y-0.5*s, z-0.5*s); // Bottom-Right of right face
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void chunk_create_displayl(ChunkManager *cm, Chunk *chunk)
|
void chunk_create_displayl(ChunkManager *cm, Chunk *chunk)
|
||||||
{
|
{
|
||||||
chunk->id = glGenLists(1);
|
chunk->id = glGenLists(1);
|
||||||
//glBindTexture(GL_TEXTURE_2D, cm->textures[0].tid);
|
//glBindTexture(GL_TEXTURE_2D, cm->textures[0].tid);
|
||||||
|
glColor3f(1, 1, 1);
|
||||||
|
|
||||||
glNewList(chunk->id, GL_COMPILE);
|
glNewList(chunk->id, GL_COMPILE);
|
||||||
for (int x = 0; x < CHUNK_MAX_X; x++) {
|
for (int x = 0; x < CHUNK_MAX_X; x++) {
|
||||||
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
for (int y = 0; y < CHUNK_MAX_Y; y++) {
|
||||||
for (int z = 0; z < CHUNK_MAX_Z; z++) {
|
for (int z = 0; z < CHUNK_MAX_Z; z++) {
|
||||||
if (chunk->blocks[x][y][z].visible)
|
if (chunk->blocks[x][y][z].visible)
|
||||||
draw_cube(cm, chunk->blocks[x][y][z].type,
|
draw_cube(cm, chunk->blocks[x][y][z].type,
|
||||||
chunk->start_pos.x + x, chunk->start_pos.y + y, chunk->start_pos.z + z);
|
chunk->start_pos.x + x, chunk->start_pos.y + y, chunk->start_pos.z + z, 1.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,3 +149,13 @@ int chunk_get_current_chunk_index(ChunkManager *cm, vec3f pos)
|
|||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GLuint get_texture(ChunkManager *cm, Block_type type)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
if (cm->textures[i].type == type)
|
||||||
|
return cm->textures[i].tid;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
35
src/main.c
35
src/main.c
@ -24,22 +24,51 @@ int main()
|
|||||||
update_app(&app);
|
update_app(&app);
|
||||||
|
|
||||||
set_view(&app.camera);
|
set_view(&app.camera);
|
||||||
|
reshape(1600, 900);
|
||||||
|
|
||||||
|
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
for (int i = 0; i < app.cm->chunk_count; i++) {
|
for (int i = 0; i < app.cm->chunk_count; i++) {
|
||||||
chunk_render(&app.cm->chunks[i]);
|
chunk_render(&app.cm->chunks[i]);
|
||||||
//printf("c sp: %f\n", app.chunks[i].start_pos.x);
|
//printf("c sp: %f\n", app.chunks[i].start_pos.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
glOrtho(0, 1600, 900, 0, -1, 1);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
glColor3f(1, 0, 0);//white
|
||||||
|
glLineWidth(2.0);
|
||||||
|
glBegin(GL_LINES);
|
||||||
|
//horizontal line
|
||||||
|
glVertex2i(1600 / 2 - 7, 900 / 2);
|
||||||
|
glVertex2i(1600 / 2 + 7, 900 / 2);
|
||||||
|
glEnd();
|
||||||
|
//vertical line
|
||||||
|
glBegin(GL_LINES);
|
||||||
|
glVertex2i(1600 / 2, 900 / 2 + 7);
|
||||||
|
glVertex2i(1600 / 2, 900 / 2 - 7);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
|
||||||
vec3f pos = app.camera.position;
|
vec3f pos = app.camera.position;
|
||||||
|
vec3f dir = get_camera_dir_vec3f(&app.camera);
|
||||||
|
|
||||||
|
draw_cube(app.cm, 0, pos.x + dir.x * 2, pos.y + dir.y * 2, pos.z + dir.z * 2, 1.0);
|
||||||
|
|
||||||
SDL_GL_SwapWindow(app.window);
|
SDL_GL_SwapWindow(app.window);
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
|
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
|
||||||
app.frame_time = calc_frame_time(&start, &end);
|
app.frame_time = calc_frame_time(&start, &end);
|
||||||
|
|
||||||
printf("fps: %f\n", 1000/app.frame_time);
|
//printf("fps: %f\n", 1000/app.frame_time);
|
||||||
//printf("lkat pos x: %f y: %f z: %f\n", pos.x+5*dir.x, pos.y+5*dir.y, pos.z+5*dir.z);
|
//printf("lkat pos x: %f y: %f z: %f\n", pos.x+5*dir.x, pos.y+5*dir.y, pos.z+5*dir.z);
|
||||||
printf("camera x: %f y: %f z: %f\n", pos.x, pos.y, pos.z);
|
//printf("camera x: %f y: %f z: %f\n", pos.x, pos.y, pos.z);
|
||||||
//printf("dir x: %f y: %f z: %f\n", dir.x, dir.y, dir.z);
|
//printf("dir x: %f y: %f z: %f\n", dir.x, dir.y, dir.z);
|
||||||
printf("dird x: %f y: %f z: %f\n", app.camera.rotation.x, app.camera.rotation.y, app.camera.rotation.z);
|
//printf("dird x: %f y: %f z: %f\n", app.camera.rotation.x, app.camera.rotation.y, app.camera.rotation.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -193,3 +193,9 @@ void print_vec3f(vec3f v, const char *str)
|
|||||||
{
|
{
|
||||||
printf("%s x: %f y: %f z: %f\n", str, v.x, v.y, v.z);
|
printf("%s x: %f y: %f z: %f\n", str, v.x, v.y, v.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float frand_range(float min, float max)
|
||||||
|
{
|
||||||
|
float scale = rand() / (float) RAND_MAX;
|
||||||
|
return min + scale * ( max - min );
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user