Skip to content

Commit

Permalink
tweaks and simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Mar 8, 2016
1 parent 8e823d1 commit ee807c5
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 122 deletions.
132 changes: 51 additions & 81 deletions examples/grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,64 @@
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 640

#define MAX_FOV 179
#define MIN_FOV 1

#define GRID_DEPTH 100
#define GRID_WIDTH 100
#define GRID_HEIGHT 100
#define GRID_FACES_LENGTH 2 * (GRID_WIDTH - 1) * 2 * GRID_HEIGHT

// model
typedef struct Grid Grid;
struct Grid {
glisy_geometry geometry;
glisy_uniform uModel;

vec3 position;
vec3 scale;

mat4 translation;
mat4 transform;
mat4 rotation;

struct {
vec3 positions[101][101];
GLushort faces[2 * 100 * 101 * 2];
GLuint length;
} vertex;

struct {
glisy_vao_attribute vPosition;
glisy_vao_attribute vColor;
} attributes;
vec3 position;
};

// forward decl
static void InitializeGrid(Grid *grid);
static void UpdateGrid(Grid *grid);
static void RotateGrid(Grid *grid, float radians);

// glfw
static GLFWwindow *window;

// glisy
static glisy_program program;

// objects
static Camera camera;
static Grid grid;

// grid constructor
void
InitializeGrid(Grid *grid) {
int vertexLength = 101;
int facesLen = 0;
vec3 vertices[101][101];
GLushort faces[2 * 100 * 101 * 2];

for (int i = 0; i < 101; i++) {
for (int j = 0; j < 101; j++) {
vertices[i][j].x = (j - 50) / 100.0;
vertices[i][j].z = (i - 50) / 100.0;
vertices[i][j].y = 0;
vec3 vertices[GRID_HEIGHT][GRID_WIDTH];
GLushort faces[GRID_FACES_LENGTH];
GLuint size = sizeof(vertices);

for (int i = 0; i < GRID_HEIGHT; ++i) {
for (int j = 0; j < GRID_WIDTH; ++j) {
vec3 *vertex = &vertices[i][j];
vertex->x = (float) (j - 50) / GRID_DEPTH;
vertex->y = 0;
vertex->z = (float) (i - 50) / GRID_DEPTH;
}
}

{
int i = 0;

// Horizontal grid lines
for (int y = 0; y < 101; y++) {
for (int x = 0; x < 100; x++) {
faces[i++] = y * 101 + x;
faces[i++] = y * 101 + x + 1;
for (int y = 0; y < GRID_WIDTH; ++y) {
for (int x = 0; x < GRID_WIDTH - 1; ++x) {
faces[i++] = y * GRID_WIDTH + x;
faces[i++] = y * GRID_WIDTH + x + 1;
}
}

// Vertical grid lines
for (int x = 0; x < 101; x++) {
for (int y = 0; y < 100; y++) {
faces[i++] = y * 101 + x;
faces[i++] = (y + 1) * 101 + x;
for (int x = 0; x < GRID_WIDTH; ++x) {
for (int y = 0; y < GRID_WIDTH - 1; ++y) {
faces[i++] = y * GRID_WIDTH + x;
faces[i++] = (y + 1) * GRID_WIDTH + x;
}
}

facesLen = i;
}

grid->position = vec3(0, 0, 1);
grid->scale = vec3(1, 1, 1);
grid->vertex.length = 2 * 100 * 101 * 2;
GLuint size = sizeof(vertices);
grid->position = vec3(0, 0, 0);

glisy_vao_attribute vPosition = {
.buffer = {
Expand All @@ -99,14 +75,7 @@ InitializeGrid(Grid *grid) {
}
};

memcpy(grid->vertex.positions, vertices, size);
memcpy(grid->vertex.faces, faces, sizeof(faces));

grid->attributes.vPosition = vPosition;

mat4_identity(grid->translation);
mat4_identity(grid->transform);
mat4_identity(grid->rotation);

glisy_uniform_init(&grid->uModel,
"uModel",
Expand All @@ -115,11 +84,11 @@ InitializeGrid(Grid *grid) {
glisy_geometry_init(&grid->geometry);
glisy_geometry_attr(&grid->geometry,
"vPosition",
&grid->attributes.vPosition);
&vPosition);

glisy_geometry_faces(&grid->geometry,
GL_UNSIGNED_SHORT,
facesLen,
GRID_FACES_LENGTH,
faces);

glisy_geometry_update(&grid->geometry);
Expand All @@ -129,46 +98,47 @@ InitializeGrid(Grid *grid) {
void
UpdateGrid(Grid *grid) {
mat4 model;
mat4 scale;
mat4 rotation;
mat4 translation;

mat4_identity(model);
mat4_identity(scale);
mat4_identity(rotation);
mat4_identity(translation);

mat4_scale(scale, grid->scale);

mat4_copy(translation, grid->translation);
mat4_translate(translation, grid->position);

mat4_copy(rotation, grid->rotation);

model = mat4_multiply(model, scale);
model = mat4_multiply(model, rotation);
model = mat4_translate(translation, grid->position);
model = mat4_multiply(model, translation);

glisy_uniform_set(&grid->uModel, &model, sizeof(model));
glisy_uniform_bind(&grid->uModel, &program);
glisy_uniform_bind(&grid->uModel, 0);
}

void
DrawGrid(Grid *grid) {
UpdateGrid(grid);
glisy_geometry_bind(&grid->geometry, &program);
glisy_geometry_draw(&grid->geometry, GL_LINES, 0, grid->vertex.length);
glisy_geometry_bind(&grid->geometry, 0);
glisy_geometry_draw(&grid->geometry,
GL_LINES, 0, GRID_FACES_LENGTH);
glisy_geometry_unbind(&grid->geometry);
}

static void
onMouseScroll(GLFWwindow* window, double xoffset, double yoffset) {
camera.fov += yoffset;
UpdateCamera(&camera);
Camera *camera = (Camera *) glfwGetWindowUserPointer(window);
camera->fov += yoffset;
camera->fov = min(MAX_FOV, max(camera->fov, MIN_FOV));
UpdateCamera(camera);
}

int
main(void) {
// glfw
GLFWwindow *window;

// glisy
glisy_program program;

// objects
Camera camera;
Grid grid;

GL_CONTEXT_INIT();
glfwSetScrollCallback(window, onMouseScroll);

Expand All @@ -177,9 +147,9 @@ main(void) {
InitializeCamera(&camera, WINDOW_WIDTH, WINDOW_HEIGHT);
InitializeGrid(&grid);

(void) mat4_rotate(camera.transform, dtor(90), vec3(0, 1, 0));
grid.position.y = 1;
glfwSetWindowUserPointer(window, &camera);

glisy_program_bind(&program);
GL_RENDER({
const float time = glfwGetTime();
const float angle = time * 45.0f;
Expand Down
2 changes: 1 addition & 1 deletion examples/grid.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ out vec4 fragColor;

void
main(void) {
fragColor = vec4(0.5, 0.5, 0.5, 1);
fragColor = vec4(0.4, 0.5, 0.6, 1);
}
4 changes: 2 additions & 2 deletions examples/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <stdio.h>

#define dtor(d) d * (M_PI / 180)
#define min(a, b) a < b ? a : b < a ? b : a
#define max(a, b) a > b ? a : b > a ? b : a
#define min(a, b) (a < b ? a : b < a ? b : a)
#define max(a, b) (a > b ? a : b > a ? b : a)

#define GL_CONTEXT_INIT() \
if (!glfwInit()) return 1; \
Expand Down
3 changes: 1 addition & 2 deletions include/glisy/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
typedef struct glisy_geometry glisy_geometry;
struct glisy_geometry {
glisy_vao vao;
glisy_vao_attribute attributes[GLISY_MAX_VAO_ATTRIBS];
GLuint attrlen;
GLuint faceslen;
glisy_buffer index;
GLboolean dirty;
GLenum elementsType;
GLenum usage;
void *faces;
GLboolean useElements;
glisy_program *program;
Expand Down
Loading

0 comments on commit ee807c5

Please sign in to comment.