354 lines
12 KiB
C
354 lines
12 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdbool.h>
|
|
#include <ncurses.h>
|
|
#include <unistd.h>
|
|
#include <termios.h>
|
|
#include <limits.h>
|
|
#include <time.h>
|
|
#include <glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include "hash.h"
|
|
#include "structure.h"
|
|
#include "base.h"
|
|
#include "move.h"
|
|
#include "menus.h"
|
|
#include "proj.h"
|
|
#include "entities.h"
|
|
#include "display.h"
|
|
#include "generation.h"
|
|
|
|
double sim_time ;
|
|
|
|
void processInput(GLFWwindow *window, float dtime) {
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
|
glfwSetWindowShouldClose(window, true);
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy += speed*sin(rot_vt)/10;
|
|
if(is_colliding(dtime)) {
|
|
camz += speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx += speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy -= speed*sin(rot_vt)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camx -= speed*cos(rot_hz)/10;
|
|
camz += speed*sin(rot_hz)/10;
|
|
if(is_colliding(dtime)) {
|
|
camx += speed*cos(rot_hz)/10;
|
|
camz -= speed*sin(rot_hz)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camz += speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx += speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy -= speed*sin(rot_vt)/10;
|
|
if(is_colliding(dtime)) {
|
|
camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy += speed*sin(rot_vt)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camx += speed*cos(rot_hz)/10;
|
|
camz -= speed*sin(rot_hz)/10;
|
|
if(is_colliding(dtime)) {
|
|
camx -= speed*cos(rot_hz)/10;
|
|
camz += speed*sin(rot_hz)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
|
stop_evetything = true ;
|
|
}
|
|
|
|
if(glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
|
|
rot_hz -= sensitivity;
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
|
rot_hz += sensitivity;
|
|
}
|
|
|
|
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
|
|
rot_vt -= sensitivity;
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
|
|
rot_vt += sensitivity;
|
|
}
|
|
}
|
|
|
|
const char *vertexShaderSource = "#version 330 core\n"
|
|
"layout (location = 0) in vec3 aPos;\n"
|
|
"uniform mat4 scale;\n"
|
|
"uniform mat4 model;\n"
|
|
"uniform mat4 view;\n"
|
|
"uniform mat4 projection;\n"
|
|
"void main() {\n"
|
|
" gl_Position = projection * view * model * scale * vec4(aPos, 1.0);\n"
|
|
"}\0";
|
|
|
|
// Fragment Shader Source
|
|
const char *fragmentShaderSource = "#version 330 core\n"
|
|
"uniform vec4 u_color;\n"
|
|
"out vec4 FragColor;\n"
|
|
"void main() {\n"
|
|
" FragColor = u_color;\n"
|
|
"}\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() {
|
|
// glfw: initialize and configure
|
|
// ------------------------------
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
// glfw window creation
|
|
// --------------------
|
|
GLFWwindow* window = glfwCreateWindow(1500, 1000, "yahoo", NULL, NULL);
|
|
if (window == NULL)
|
|
{
|
|
fprintf(stderr, "ERROR : cannot initialize GL window");
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
|
|
// glad: load all OpenGL function pointers
|
|
// ---------------------------------------
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
|
{
|
|
fprintf(stderr, "ERROR : cannot initialize GLAD loader");
|
|
return -1;
|
|
}
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
glEnable(GL_CULL_FACE);
|
|
glCullFace(GL_FRONT);
|
|
glDepthFunc(GL_LESS);
|
|
|
|
init_csts();
|
|
init_hashtbl();
|
|
init_ent_generator(10);
|
|
init_proj();
|
|
parse_rooms(5);
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
|
|
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
|
|
|
|
// build and compile our shader program
|
|
// ------------------------------------
|
|
// vertex shader
|
|
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
|
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
|
|
glCompileShader(vertexShader);
|
|
// check for shader compile errors
|
|
int success;
|
|
char infoLog[512];
|
|
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
|
if (!success)
|
|
{
|
|
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
|
fprintf(stderr, "ERROR : cannot initialize shader (1)");
|
|
}
|
|
|
|
// fragment shader
|
|
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
|
|
glCompileShader(fragmentShader);
|
|
// check for shader compile errors
|
|
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
|
if (!success)
|
|
{
|
|
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
|
fprintf(stderr, "ERROR : cannot initialize shader (3)");
|
|
}
|
|
|
|
// link shaders
|
|
unsigned int shaderProgram = glCreateProgram();
|
|
glAttachShader(shaderProgram, vertexShader);
|
|
glAttachShader(shaderProgram, fragmentShader);
|
|
glLinkProgram(shaderProgram);
|
|
|
|
glDeleteShader(vertexShader);
|
|
glDeleteShader(fragmentShader);
|
|
|
|
// 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_vertices();
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
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 interval = 1000000/fps ;
|
|
|
|
clock_t finish = clock();
|
|
clock_t origin = clock();
|
|
|
|
while(!glfwWindowShouldClose(window)) {
|
|
// input
|
|
// -----
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
origin = clock();
|
|
|
|
generate_nearby_chunks(1);
|
|
|
|
glUseProgram(shaderProgram);
|
|
glBindVertexArray(VAO);
|
|
|
|
gl_initRender(shaderProgram, shaderProgram, VAO, VBO);
|
|
gl_renderNearbyChunks(shaderProgram, shaderProgram, VAO, VBO, 1);
|
|
gl_renderProj(shaderProgram, shaderProgram, VAO, VBO);
|
|
|
|
glUseProgram(shaderProgramR);
|
|
glBindVertexArray(RVAO);
|
|
|
|
finish = clock();
|
|
gl_drawInteger(shaderProgramR, (int)(1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)), -0.5f, 0.85f, 0.05, 32, 255, 32, 0.005, 1);
|
|
|
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
|
// -------------------------------------------------------------------------------
|
|
processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC);
|
|
teleport_on_edge();
|
|
update_entities(((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("%lf, %lf, %lf\n", camx, camy, camz);
|
|
|
|
usleep(interval);
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
//printf("10\n");
|
|
//fflush(stdout);
|
|
hashtbl_free(visited);
|
|
free_proj();
|
|
free_pool();
|
|
|
|
// optional: de-allocate all resources once they've outlived their purpose:
|
|
// ------------------------------------------------------------------------
|
|
glDeleteVertexArrays(1, &VAO);
|
|
glDeleteBuffers(1, &VBO);
|
|
glDeleteProgram(shaderProgram);
|
|
|
|
// glfw: terminate, clearing all previously allocated GLFW resources.
|
|
// ------------------------------------------------------------------
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
srand(time(NULL));
|
|
return main_alt();
|
|
} |