Compare commits

...

3 Commits

27 changed files with 419 additions and 179 deletions

View File

@ -14,6 +14,8 @@
"move.h": "c", "move.h": "c",
"hash.h": "c", "hash.h": "c",
"proj.h": "c", "proj.h": "c",
"entities.h": "c" "entities.h": "c",
"menus.h": "c",
"structure.h": "c"
} }
} }

View File

@ -1,6 +1,6 @@
CC = gcc CC = gcc
FLAGS = -O2 -Wall -Wextra -g FLAGS = -O2 -Wall -Wextra -g
LFLAGS = -lm -lncurses src/glad.c -ldl -lglfw -lcglm LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm
all: bin/back all: bin/back

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>
@ -231,6 +230,20 @@ teleporter* create_teleporter(
return tp ; return tp ;
} }
void copy_cube(cube_0* src, cube_0* dest) {
dest->red = src->red;
dest->green = src->green;
dest->blue = src->blue;
dest->hz_angle = src->hz_angle;
dest->vt_angle = src->vt_angle;
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
dest->w = src->w;
dest->h = src->h;
dest->d = src->d;
}
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) { double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) {
@ -313,54 +326,31 @@ void project_to_camera(double x0, double y0, double z0, double* rx, double* ry,
double z = z0 - camz ; double z = z0 - camz ;
// rotate (y) // rotate (y)
double xry = x*cos(rot_hz) + z*sin(rot_hz) ; double xry = x*cos(rot_hz) - z*sin(rot_hz) ;
double yry = y ; double yry = y ;
double zry = z*cos(rot_hz) - x*sin(rot_hz) ; double zry = z*cos(rot_hz) + x*sin(rot_hz) ;
// rotate (x) // rotate (x)
if(rx != NULL) {*rx = xry ;} if(rx != NULL) {*rx = xry ;}
if(ry != NULL) {*ry = yry*cos(rot_vt) - zry*sin(rot_vt) ;} if(ry != NULL) {*ry = yry*cos(rot_vt) + zry*sin(rot_vt) ;}
if(rz != NULL) {*rz = zry*cos(rot_vt) + yry*sin(rot_vt) ;} if(rz != NULL) {*rz = zry*cos(rot_vt) - yry*sin(rot_vt) ;}
} }
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c) { void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c) {
// align pt to (0, 0, 0) // align pt to (0, 0, 0)
double x = x0 - (c.x + c.w/2.0) ; double x = x0 - (c->x + c->w/2.0) ;
double y = y0 - (c.y + c.h/2.0) ; double y = y0 - (c->y + c->h/2.0) ;
double z = z0 - (c.z + c.d/2.0) ; double z = z0 - (c->z + c->d/2.0) ;
// rotate (y) // rotate (y)
double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ; double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle) ;
double yry = y ; double yry = y ;
double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ; double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle) ;
// rotate (x) // rotate (x)
if(rx != NULL) {*rx = xry ;} if(rx != NULL) {*rx = c->x + c->w/2.0 + xry ;}
if(ry != NULL) {*ry = yry*cos(c.vt_angle) - zry*sin(c.vt_angle);} if(ry != NULL) {*ry = c->y + c->h/2.0 + yry*cos(c->vt_angle) - zry*sin(c->vt_angle);}
if(rz != NULL) {*rz = zry*cos(c.vt_angle) + yry*sin(c.vt_angle);} if(rz != NULL) {*rz = c->z + c->d/2.0 + zry*cos(c->vt_angle) + yry*sin(c->vt_angle);}
}
double dist_to_cam_cube(double x0, double y0, double z0, cube_0 c) {
double px0, py0, pz0;
project_to_cube(x0, y0, z0, &px0, &py0, &pz0, c);
return distance_pt_pt_3d(px0 + c.x + c.w/2.0, py0 + c.y + c.h/2.0, pz0 + c.z + c.d/2.0, camx, camy, camz);
}
bool pt_equal_3D(pt_2d p1, pt_2d p2) {
return (p1.x == p2.x && p1.y == p2.y && p1.z == p2.z);
}
bool pt_equal_2D(pt_2d p1, pt_2d p2) {
return (p1.x == p2.x && p1.y == p2.y);
}
bool pt_equal_3D_eps(pt_2d p1, pt_2d p2, double epsilon) {
return (absf(p1.x - p2.x) <= epsilon && absf(p1.y - p2.y) <= epsilon && absf(p1.z - p2.z) <= epsilon);
}
bool pt_equal_2D_eps(pt_2d p1, pt_2d p2, double epsilon, bool debug0) {
if(debug0) {printf("(%lf, %lf)\n", absf(p1.x - p2.x), absf(p1.y - p2.y));}
return (absf(p1.x - p2.x) <= epsilon && absf(p1.y - p2.y) <= epsilon);
} }
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //

View File

@ -37,6 +37,7 @@ teleporter* create_teleporter(
double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b, double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b,
int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest
); );
void copy_cube(cube_0* src, cube_0* dest);
double convex_pt(double a, double b, double theta); double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1); double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);
@ -58,7 +59,6 @@ void remove_entity(entity** arr, int* memlen, int* len, int index);
void add_entity(entity** arr, int* memlen, int* len, entity ent); void add_entity(entity** arr, int* memlen, int* len, entity ent);
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz); void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz);
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c); void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c);
double dist_to_cam_cube(double x0, double y0, double z0, cube_0 c);
#endif #endif

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>
@ -16,6 +15,7 @@
#include "structure.h" #include "structure.h"
#include "base.h" #include "base.h"
#include "move.h" #include "move.h"
#include "menus.h"
#include "proj.h" #include "proj.h"
#include "entities.h" #include "entities.h"
#include "generation.h" #include "generation.h"
@ -136,21 +136,40 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glUniform4f(glGetUniformLocation(fragmentShader, "u_color"), c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f); glUniform4f(glGetUniformLocation(fragmentShader, "u_color"), c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
triCount += 12;
}
double px, py, pz, pz2;
bool is_visible(cube_0* cb, double offx, double offy, double offz) {
for(int d = 0; d < 8; d++) {
project_to_cube(cb->x+cb->w*(d%2==0)+offx, cb->y+cb->h*((d/2)%2==0)+offy, cb->z+cb->d*((d/4)%2==0)+offz, &px, &py, &pz, cb);
project_to_camera(px, py, pz, NULL, NULL, &pz2);
if(pz2 > near) {
return true;
}
}
return false;
} }
void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, room* rtd, double offx, double offy, double offz) { void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, room* rtd, double offx, double offy, double offz) {
if(rtd != NULL) { if(rtd != NULL) {
for(int k = 0; k < rtd->map_size; k++) { for(int k = 0; k < rtd->map_size; k++) {
if(is_visible(rtd->map[k], offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz); gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz);
} }
}
for(int k = 0; k < rtd->tps_size; k++) { for(int k = 0; k < rtd->tps_size; k++) {
if(is_visible(rtd->tps[k]->hitbox, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->tps[k]->hitbox, offx, offy, offz); gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->tps[k]->hitbox, offx, offy, offz);
} }
}
for(int k = 0; k < rtd->ent_len; k++) { for(int k = 0; k < rtd->ent_len; k++) {
if(is_visible(rtd->ents[k].pos, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz); gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz);
} }
} }
} }
}
void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, int render_distance) { void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, int render_distance) {
for(int w = -render_distance; w <= render_distance; w++) { for(int w = -render_distance; w <= render_distance; w++) {
@ -174,12 +193,12 @@ void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glm_lookat((vec3){(float)camx, (float)camy, (float)camz}, direction, (vec3){0.0f, 1.0f, 0.0f}, view); glm_lookat((vec3){(float)camx, (float)camy, (float)camz}, direction, (vec3){0.0f, 1.0f, 0.0f}, view);
glm_perspective(glm_rad((float)fov), 1500.0f / 1000.0f, 0.1f, 100.0f, projection); glm_perspective(glm_rad((float)fov), 1500.0f / 1000.0f, 0.1f, 100.0f, projection);
glUseProgram(shaderProgram);
//glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
} }
void gl_drawTriangle() { void gl_drawData(unsigned int shaderProg) {
gl_drawInteger(shaderProg, (int)camx, 0.95f, 0.9f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, (int)camy, 0.95f, 0.75f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, (int)camz, 0.95f, 0.6f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, player_hp, -0.95f, 0.9f, 0.05f, 255-player_hp/4, player_hp/4, 0, 0.005f, 1);
} }

View File

@ -8,4 +8,6 @@ void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShad
void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO); void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO);
void init_vertices(); void init_vertices();
void gl_drawData(unsigned int shaderProg);
#endif #endif

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>
@ -424,6 +423,35 @@ void parse_rooms(int n_rooms) {
free(name); free(name);
} }
int divider = 8;
void generate_terrain(room* r) {
int rsize = 4*(room_width/divider)*(room_width/divider)*(room_depth/divider)*(room_depth/divider); // floor size (with 1x1 cubes)
cube** newMap = malloc(sizeof(cube_0*)*(rsize+r->map_size));
for(int k = 0; k < rsize+r->map_size; k++) {
newMap[k] = malloc(sizeof(cube_0));
}
for(int k = 0; k < r->map_size; k++) {
copy_cube(r->map[k], newMap[k]);
free(r->map[k]);
}
free(r->map);
// use whatever noise funtcion here
int i = r->map_size;
for(int l = -room_width; l < room_width; l+=divider) {
for(int d = -room_depth; d < room_depth; d+=divider) {
newMap[i] = create_cube_0((double)l, -20.0, (double)d, (double)divider, 1.0, (double)divider, 0.0, 0.0,32, 128, 32);
i+=1;
}
}
r->map = newMap;
r->map_size += rsize;
}
void generate_nearby_chunks(int render_dist) { void generate_nearby_chunks(int render_dist) {
for(int w = -render_dist; w <= render_dist; w++) { for(int w = -render_dist; w <= render_dist; w++) {
for(int h = -render_dist; h <= render_dist; h++) { for(int h = -render_dist; h <= render_dist; h++) {
@ -438,6 +466,7 @@ void generate_nearby_chunks(int render_dist) {
printf("chose %d\n", k); printf("chose %d\n", k);
room* new = malloc(sizeof(room)); room* new = malloc(sizeof(room));
copy_room(pool[k].area, new, player_chx + w, player_chy + h); copy_room(pool[k].area, new, player_chx + w, player_chy + h);
generate_terrain(new);
hashtbl_add(visited, player_chx + w, player_chy + h, new); hashtbl_add(visited, player_chx + w, player_chy + h, new);
k = pool_size+1; k = pool_size+1;
} }

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>
@ -15,12 +14,14 @@
#include "structure.h" #include "structure.h"
#include "base.h" #include "base.h"
#include "move.h" #include "move.h"
#include "menus.h"
#include "proj.h" #include "proj.h"
#include "entities.h" #include "entities.h"
#include "display.h" #include "display.h"
#include "generation.h" #include "generation.h"
double sim_time ; double sim_time ;
int triCount;
void processInput(GLFWwindow *window, float dtime) { void processInput(GLFWwindow *window, float dtime) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
@ -112,6 +113,21 @@ const char *fragmentShaderSource = "#version 330 core\n"
" FragColor = u_color;\n" " FragColor = u_color;\n"
"}\0"; "}\0";
const char *vertexShaderSourceR = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 scale;\n"
"uniform mat4 slide;\n"
"void main() {\n"
" gl_Position = slide * scale * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSourceR = "#version 330 core\n"
"uniform vec4 u_color2;\n"
"out vec4 FragColor;\n"
"void main() {\n"
" FragColor = u_color2;\n"
"}\0";
int main_alt() { int main_alt() {
// glfw: initialize and configure // glfw: initialize and configure
// ------------------------------ // ------------------------------
@ -122,7 +138,7 @@ int main_alt() {
// glfw window creation // glfw window creation
// -------------------- // --------------------
GLFWwindow* window = glfwCreateWindow(1500, 1000, "LearnOpenGL", NULL, NULL); GLFWwindow* window = glfwCreateWindow(1500, 1000, "yahoo", NULL, NULL);
if (window == NULL) if (window == NULL)
{ {
fprintf(stderr, "ERROR : cannot initialize GL window"); fprintf(stderr, "ERROR : cannot initialize GL window");
@ -144,7 +160,14 @@ int main_alt() {
glCullFace(GL_FRONT); glCullFace(GL_FRONT);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
//printf("%f\n", glDepthRange); init_csts();
init_hashtbl();
init_ent_generator(10);
init_proj();
parse_rooms(5);
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// build and compile our shader program // build and compile our shader program
// ------------------------------------ // ------------------------------------
@ -161,6 +184,7 @@ int main_alt() {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
fprintf(stderr, "ERROR : cannot initialize shader (1)"); fprintf(stderr, "ERROR : cannot initialize shader (1)");
} }
// fragment shader // fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
@ -170,31 +194,98 @@ int main_alt() {
if (!success) if (!success)
{ {
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
fprintf(stderr, "ERROR : cannot initialize shader (2)"); fprintf(stderr, "ERROR : cannot initialize shader (3)");
} }
// link shaders // link shaders
unsigned int shaderProgram = glCreateProgram(); unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader); glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram); glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
fprintf(stderr, "ERROR : cannot link shaders");
}
glDeleteShader(vertexShader); glDeleteShader(vertexShader);
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
// set up data (buffer(s)) and configure vertex attributes // main VAO/VBO
// ------------------------------------------------------------------ unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
init_csts(); init_vertices();
init_hashtbl();
init_ent_generator(10); glBindBuffer(GL_ARRAY_BUFFER, VBO);
init_proj(); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
parse_rooms(5);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// build and compile our shader program
// ------------------------------------
// vertex shader
unsigned int vertexShaderR = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderR, 1, &vertexShaderSourceR, NULL);
glCompileShader(vertexShaderR);
// check for shader compile errors
glGetShaderiv(vertexShaderR, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShaderR, 512, NULL, infoLog);
//std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShaderR = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderR, 1, &fragmentShaderSourceR, NULL);
glCompileShader(fragmentShaderR);
// check for shader compile errors
glGetShaderiv(fragmentShaderR, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShaderR, 512, NULL, infoLog);
//std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
unsigned int shaderProgramR = glCreateProgram();
glAttachShader(shaderProgramR, vertexShaderR);
glAttachShader(shaderProgramR, fragmentShaderR);
glLinkProgram(shaderProgramR);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgramR, 512, NULL, infoLog);
//std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShaderR);
glDeleteShader(fragmentShaderR);
// rectangle VAO/VBO
initMenus();
unsigned int RVBO, RVAO;
glGenVertexArrays(1, &RVAO);
glGenBuffers(1, &RVBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(RVAO);
glBindBuffer(GL_ARRAY_BUFFER, RVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(rectDefault), rectDefault, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
int fps = 60 ; int fps = 60 ;
int interval = 1000000/fps ; int interval = 1000000/fps ;
@ -202,45 +293,40 @@ int main_alt() {
clock_t finish = clock(); clock_t finish = clock();
clock_t origin = clock(); clock_t origin = clock();
unsigned int VBO, VAO; while(!glfwWindowShouldClose(window) && player_hp >= 0) {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
init_vertices();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
while(!glfwWindowShouldClose(window)) {
// input // input
// ----- // -----
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
triCount = 0;
origin = clock(); origin = clock();
generate_nearby_chunks(1); generate_nearby_chunks(1);
// draw the map
glUseProgram(shaderProgram);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
gl_initRender(shaderProgram, shaderProgram, VAO, VBO); gl_initRender(shaderProgram, shaderProgram, VAO, VBO);
//gl_renderAll(shaderProgram, shaderProgram, VAO, VBO, current_room, 0.0, 0.0, 0.0);
gl_renderNearbyChunks(shaderProgram, shaderProgram, VAO, VBO, 1); gl_renderNearbyChunks(shaderProgram, shaderProgram, VAO, VBO, 1);
gl_renderProj(shaderProgram, shaderProgram, VAO, VBO); gl_renderProj(shaderProgram, shaderProgram, VAO, VBO);
// draw data
glUseProgram(shaderProgramR);
glBindVertexArray(RVAO);
finish = clock();
gl_drawInteger(shaderProgramR, (int)(1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)), 0.9f, -0.85f, 0.05, 32, 255, 32, 0.005, -1);
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
gl_drawData(shaderProgramR);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
finish = clock();
processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC); processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC);
teleport_on_edge(); teleport_on_edge();
update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC); update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC);
updateProj(((float)finish - (float)origin)/CLOCKS_PER_SEC); updateProj(((float)finish - (float)origin)/CLOCKS_PER_SEC);
printf("%f\n", 1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)); //printf("%f\n", 1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC));
//printf("%lf, %lf, %lf\n", camx, camy, camz); //printf("%lf, %lf, %lf\n", camx, camy, camz);
usleep(interval); usleep(interval);
@ -269,5 +355,6 @@ int main_alt() {
int main(int argc, char** argv) { int main(int argc, char** argv) {
srand(time(NULL)); srand(time(NULL));
triCount = 0;
return main_alt(); return main_alt();
} }

View File

@ -3,11 +3,14 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>
#include <time.h> #include <time.h>
#include <glad/glad.h>
#include "../include/glad/glad.h"
//#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include "hash.h" #include "hash.h"
#include "structure.h" #include "structure.h"
@ -15,3 +18,117 @@
#include "entities.h" #include "entities.h"
#include "menus.h" #include "menus.h"
float rectDefault[18] ;
static mat4 scale, slide;
void initMenus() {
rectDefault[0] = -0.5f; rectDefault[1] = -0.5f; rectDefault[2] = -1.0f;
rectDefault[3] = -0.5f; rectDefault[4] = 0.5f; rectDefault[5] = -1.0f;
rectDefault[6] = 0.5f; rectDefault[7] = 0.5f; rectDefault[8] = -1.0f;
rectDefault[9] = -0.5f; rectDefault[10] = -0.5f; rectDefault[11] = -1.0f;
rectDefault[12] = 0.5f; rectDefault[13] = 0.5f; rectDefault[14] = -1.0f;
rectDefault[15] = 0.5f; rectDefault[16] = -0.5f; rectDefault[17] = -1.0f;
}
void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, int r, int g, int b) {
glm_mat4_identity(scale);
glm_mat4_identity(slide);
scale[0][0] = w;
scale[1][1] = h;
scale[2][2] = 0.5f;
glm_translate(slide, (vec3){x+w/2, y+h/2, 0.0f});
glUniformMatrix4fv(glGetUniformLocation(fragShader, "scale"), 1, GL_FALSE, (float*)scale);
glUniformMatrix4fv(glGetUniformLocation(fragShader, "slide"), 1, GL_FALSE, (float*)slide);
glUniform4f(glGetUniformLocation(fragShader, "u_color2"), r/255.0f, g/255.0f, b/255.0f, 0.5f);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void gl_drawDigit(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width) {
assert(n >= 0 && n < 10);
if(n == 0) {
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
} else if(n == 1) {
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
} else if(n == 2) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 3) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 4) {
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
} else if(n == 5) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 6) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 7) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
} else if(n == 8) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
}
}
// 7-segment display
// side can be -1 (aligned right) or 1 (aligned left)
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side) {
int left = n;
float curx = x;
if(left < 0) {
left *= (-1);
}
if(side == 1) {
curx += ((ln_baseN(abs(n), 10)-1)*(size+4*width));
}
while(left > 0) {
gl_drawDigit(fragShader, left%10, curx, y, size, r, g, b, width);
curx -= (size+4*width);
left = left/10;
}
if(n == 0) {
gl_drawDigit(fragShader, 0, curx, y, size, r, g, b, width);
curx -= (size+4*width);
}
if(n < 0) {
gl_drawRect(fragShader, curx-size/2-width, y, size+2*width, 2*width, r, g, b);
}
}
void gl_initDrawRect(unsigned int shaderProgram) {
glUseProgram(shaderProgram);
}

View File

@ -1,5 +1,11 @@
#ifndef MENUS_H #ifndef MENUS_H
#define MENUS_H #define MENUS_H
void initMenus();
void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, int r, int g, int b);
void gl_initDrawRect(unsigned int shaderProgram);
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side);
#endif #endif

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <limits.h> #include <limits.h>
@ -62,20 +61,6 @@ void appendProj(double x, double y, double z, double w, double h, double d, doub
} }
} }
void copy_cube(cube_0* src, cube_0* dest) {
dest->red = src->red;
dest->green = src->green;
dest->blue = src->blue;
dest->hz_angle = src->hz_angle;
dest->vt_angle = src->vt_angle;
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
dest->w = src->w;
dest->h = src->h;
dest->d = src->d;
}
void removeProj(int k) { void removeProj(int k) {
if(k >= 0 && k < bullets_id) { if(k >= 0 && k < bullets_id) {
copy_cube(bullets[bullets_id-1].pos, bullets[k].pos); copy_cube(bullets[bullets_id-1].pos, bullets[k].pos);
@ -113,6 +98,10 @@ void updateProj(float dtime) {
if(*(bullets[k].ttl) <= 0.0 || is_colliding_with_map(bullets[k].pos) || is_colliding_with_tp(bullets[k].pos)) { if(*(bullets[k].ttl) <= 0.0 || is_colliding_with_map(bullets[k].pos) || is_colliding_with_tp(bullets[k].pos)) {
removeProj(k); removeProj(k);
k -= 1; k -= 1;
} else if(distance_pt_cube_0_3d(camx, camy, camz, bullets[k].pos) <= 0.1) {
player_hp -= bullets[k].damage;
removeProj(k);
k -= 1;
} else { } else {
bullets[k].vel->x += ((double)dtime)*bullets[k].acc->x; bullets[k].vel->x += ((double)dtime)*bullets[k].acc->x;
bullets[k].vel->y += ((double)dtime)*bullets[k].acc->y; bullets[k].vel->y += ((double)dtime)*bullets[k].acc->y;

View File

@ -32,14 +32,14 @@ typedef struct teleporter {
typedef struct entity { typedef struct entity {
cube_0* pos; cube_0* pos;
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) ;
// act as velocity function // act as velocity function
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret);
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret) ;
// triggers when object is hit // triggers when object is hit
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret);
// triggers when <hitpoints> goes negative (death)
void (*onDeath)(float dtime); void (*onDeath)(float dtime);
// triggers when <hitpoints> goes negative
int damage; int damage;
int* hitpoints; int* hitpoints;
@ -122,5 +122,8 @@ extern double min_dist ;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
extern float vertices[108]; extern float vertices[108];
extern float rectDefault[18];
extern int triCount ;
#endif #endif