Implement meshes
parent
873efe17d2
commit
1d9d5af7c5
|
@ -6,7 +6,7 @@ CLIENT_LIBRARIES = -lGL -lGLEW -lglfw
|
|||
FLAGS = -g -fmax-errors=4
|
||||
|
||||
ifdef RELEASE
|
||||
FLAGS = -O3
|
||||
FLAGS = -O3 -DRELEASE
|
||||
endif
|
||||
|
||||
all: Dragonblocks DragonblocksServer
|
||||
|
|
31
src/client.c
31
src/client.c
|
@ -1,6 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
@ -11,6 +12,7 @@
|
|||
#include "client.h"
|
||||
#include "mapblock_meshgen.h"
|
||||
#include "signal.h"
|
||||
#include "shaders.h"
|
||||
#include "util.h"
|
||||
|
||||
Client client;
|
||||
|
@ -43,6 +45,12 @@ static void *reciever_thread(void *unused)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef RELEASE
|
||||
#define SHADER_PATH "shaders/"
|
||||
#else
|
||||
#define SHADER_PATH "../shaders/"
|
||||
#endif
|
||||
|
||||
static void client_loop()
|
||||
{
|
||||
if(! glfwInit()) {
|
||||
|
@ -55,7 +63,11 @@ static void client_loop()
|
|||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(1024, 768, "Dragonblocks", NULL, NULL);
|
||||
int width, height;
|
||||
width = 1024;
|
||||
height = 768;
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(width, height, "Dragonblocks", NULL, NULL);
|
||||
|
||||
if (! window) {
|
||||
printf("Failed to create window\n");
|
||||
|
@ -69,17 +81,28 @@ static void client_loop()
|
|||
return;
|
||||
}
|
||||
|
||||
int shader_program = 0;
|
||||
ShaderProgram *prog = create_shader_program(SHADER_PATH);
|
||||
|
||||
mat4x4 view, proj;
|
||||
|
||||
mat4x4_identity(view); // ToDo: camera
|
||||
mat4x4_perspective(proj, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 100.0f);
|
||||
|
||||
glUseProgram(prog->id);
|
||||
glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
|
||||
glUniformMatrix4fv(prog->loc_proj, 1, GL_FALSE, proj[0]);
|
||||
|
||||
while (! glfwWindowShouldClose(window) && client.state != CS_DISCONNECTED && ! interrupted) {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClearColor(0.52941176470588, 0.8078431372549, 0.92156862745098, 1.0);
|
||||
glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
|
||||
|
||||
scene_render(client.scene, shader_program);
|
||||
scene_render(client.scene, prog);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
delete_shader_program(prog);
|
||||
}
|
||||
|
||||
static bool client_name_prompt()
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
#include <pthread.h>
|
||||
#include "servercommands.h"
|
||||
#include "clientcommands.h"
|
||||
#include "network.h"
|
||||
#include "map.h"
|
||||
#include "network.h"
|
||||
#include "scene.h"
|
||||
|
||||
typedef struct Client
|
||||
|
|
|
@ -81,7 +81,6 @@ static void *meshgen_thread(void *unused)
|
|||
|
||||
if (vertices.siz > 0) {
|
||||
mesh = mesh_create(vertices.ptr, vertices.siz);
|
||||
free(vertices.ptr);
|
||||
scene_add_mesh(meshgen.scene, mesh);
|
||||
}
|
||||
|
||||
|
|
79
src/mesh.c
79
src/mesh.c
|
@ -1 +1,80 @@
|
|||
#include <stdlib.h>
|
||||
#include "mesh.h"
|
||||
|
||||
Mesh *mesh_create(GLfloat *vertices, GLsizei size)
|
||||
{
|
||||
Mesh *mesh = malloc(sizeof(Mesh));
|
||||
for (int i = 0; i < 3; i++) {
|
||||
mesh->pos[i] = 0;
|
||||
mesh->rot[i] = 0;
|
||||
mesh->scale[i] = 1;
|
||||
}
|
||||
mesh_transform(mesh);
|
||||
mesh->angle = 0.0f;
|
||||
mesh->VAO = 0;
|
||||
mesh->VBO = 0;
|
||||
mesh->remove = false;
|
||||
mesh->vertices = vertices;
|
||||
mesh->size = size;
|
||||
mesh->count = size / 6;
|
||||
return mesh;
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic warning "-Wpedantic"
|
||||
|
||||
void mesh_transform(Mesh *mesh)
|
||||
{
|
||||
mat4x4_translate(mesh->transform, mesh->pos[0], mesh->pos[1], mesh->pos[2]);
|
||||
mat4x4_rotate(mesh->transform, mesh->transform, mesh->rot[0], mesh->rot[1], mesh->rot[2], mesh->angle);
|
||||
mat4x4_scale_aniso(mesh->transform, mesh->transform, mesh->scale[0], mesh->scale[1], mesh->scale[2]);
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
void mesh_delete(Mesh *mesh)
|
||||
{
|
||||
if (mesh->vertices)
|
||||
free(mesh->vertices);
|
||||
if (mesh->VAO)
|
||||
glDeleteVertexArrays(1, &mesh->VAO);
|
||||
if (mesh->VBO)
|
||||
glDeleteBuffers(1, &mesh->VAO);
|
||||
free(mesh);
|
||||
}
|
||||
|
||||
static void mesh_configure(Mesh *mesh)
|
||||
{
|
||||
glGenVertexArrays(1, &mesh->VAO);
|
||||
glGenBuffers(1, &mesh->VBO);
|
||||
|
||||
glBindVertexArray(mesh->VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mesh->VBO);
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh->size, mesh->vertices, GL_STATIC_DRAW);
|
||||
|
||||
GLsizei stride = 6 * sizeof(GLfloat);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, (GLvoid *)(0 * sizeof(GLfloat)));
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, false, stride, (GLvoid *)(3 * sizeof(GLfloat)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
free(mesh->vertices);
|
||||
mesh->vertices = NULL;
|
||||
}
|
||||
|
||||
void mesh_render(Mesh *mesh, ShaderProgram *prog)
|
||||
{
|
||||
if (mesh->vertices)
|
||||
mesh_configure(mesh);
|
||||
|
||||
glUniformMatrix4fv(prog->loc_model, 1, GL_FALSE, mesh->transform[0]);
|
||||
|
||||
glBindVertexArray(mesh->VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, mesh->count);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <GL/gl.h>
|
||||
#include <linmath.h/linmath.h>
|
||||
#include <stdbool.h>
|
||||
#include "shaders.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
@ -13,10 +14,13 @@ typedef struct
|
|||
mat4x4 transform;
|
||||
GLuint VAO, VBO;
|
||||
bool remove;
|
||||
GLfloat *vertices;
|
||||
GLsizei size, count;
|
||||
} Mesh;
|
||||
|
||||
Mesh *mesh_create(const GLvoid *vertices, GLsizei size);
|
||||
Mesh *mesh_create(GLfloat *vertices, GLsizei size);
|
||||
void mesh_delete(Mesh *mesh);
|
||||
void mesh_render(Mesh *mesh, int prog);
|
||||
void mesh_transform(Mesh *mesh);
|
||||
void mesh_render(Mesh *mesh, ShaderProgram *prog);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -24,7 +24,7 @@ void scene_add_mesh(Scene *scene, Mesh *mesh)
|
|||
pthread_mutex_unlock(&scene->mtx);
|
||||
}
|
||||
|
||||
void scene_render(Scene *scene, int prog)
|
||||
void scene_render(Scene *scene, ShaderProgram *prog)
|
||||
{
|
||||
for (ListPair **pairptr = &scene->meshes.first; *pairptr != NULL; ) {
|
||||
ListPair *pair = *pairptr;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <pthread.h>
|
||||
#include "list.h"
|
||||
#include "mesh.h"
|
||||
#include "shaders.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
@ -16,6 +17,6 @@ void scene_delete(Scene *scene);
|
|||
|
||||
void scene_add_mesh(Scene *scene, Mesh *mesh);
|
||||
void scene_remove_mesh(Scene *scene, Mesh *mesh);
|
||||
void scene_render(Scene *scene, int prog);
|
||||
void scene_render(Scene *scene, ShaderProgram *prog);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef _SHADERS_H_
|
||||
#define _SHADERS_H_
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GLuint id;
|
||||
GLint loc_model;
|
||||
GLint loc_view;
|
||||
GLint loc_proj;
|
||||
} ShaderProgram;
|
||||
|
||||
ShaderProgram *create_shader_program(const char *path); // ToDo
|
||||
void delete_shader_program(ShaderProgram *);
|
||||
|
||||
#endif
|
|
@ -2,6 +2,7 @@
|
|||
#define _UTIL_H_
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <linmath.h/linmath.h>
|
||||
#include "types.h"
|
||||
|
||||
#define ever (;;)
|
||||
|
|
Loading…
Reference in New Issue