Skip to content

Commit

Permalink
hi dom
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Mar 7, 2016
1 parent 61b362b commit 118d112
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 74 deletions.
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cube
grid
97 changes: 66 additions & 31 deletions examples/cube.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,29 @@ static GLFWwindow *window;
static glisy_program program;

// objects
#define numberOfCubes 10
static int cubeID = 0;
static Camera camera;
static Cube cubes[5];
static Cube cubes[numberOfCubes];

// colors
static glisy_color blue;
static glisy_color green;
static glisy_color cubeColors[numberOfCubes] = {
{"blue"},
{"cyan"},
{"green"},
{"purple"},
{"yellow"},
{"salmon"},
{"silver"},
{"palegreen"},
{"mintcream"},
{"papayawhip"},
};

// cube constructor
void
InitializeCube(Cube *cube) {
const int id = cubeID++;
const vec3 vertices[] = {
vec3(-0.5, -0.5, +0.5),
vec3(+0.5, -0.5, +0.5),
Expand All @@ -68,15 +81,15 @@ InitializeCube(Cube *cube) {
};

const vec3 colors[] = {
vec3(blue.r, blue.g, blue.a),
vec3(blue.r, blue.g, blue.a),
vec3(blue.r, blue.g, blue.a),
vec3(blue.r, blue.g, blue.a),

vec3(green.r, green.g, green.a),
vec3(green.r, green.g, green.a),
vec3(green.r, green.g, green.a),
vec3(green.r, green.g, green.a),
vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),
vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),
vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),
vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),

vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),
vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),
vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),
vec3(cubeColors[id].r, cubeColors[id].g, cubeColors[id].a),
};

const GLushort faces[] = {
Expand Down Expand Up @@ -122,8 +135,8 @@ InitializeCube(Cube *cube) {
memcpy(cube->vertex.colors, colors, size);
memcpy(cube->vertex.faces, faces, sizeof(faces));

memcpy(&cube->attributes.vPosition.buffer.data, vertices, size);
memcpy(&cube->attributes.vColor.buffer.data, colors, size);
cube->attributes.vPosition.buffer.data = (void *) vertices;
cube->attributes.vColor.buffer.data = (void *) colors;

mat4_identity(cube->translation);
mat4_identity(cube->transform);
Expand Down Expand Up @@ -188,7 +201,7 @@ RotateCube(Cube *cube, float radians) {
}

void
MoveCube(Cube *cube, vec3 translation) {
TranslateCube(Cube *cube, vec3 translation) {
cube->position = vec3_add(cube->position, translation);
cube->translation = mat4_translate(cube->translation, translation);
UpdateCube(cube);
Expand All @@ -202,10 +215,17 @@ ScaleCube(Cube *cube, vec3 scale) {

static void
onMouseMove(GLFWwindow* window, double x, double y) {
const float angle = 20.0f;
const float angle = 45.0f;
const float radians = dtor(angle);
RotateCartesianCube(&cubes[0], x, y);
RotateCartesianCube(&cubes[1], -x, -y);
const float radius = 10.0f;
const float camX = sinf(radians) * radius;
const float camY = -cosf(radians) * radius;
const float camZ = cosf(radians) * radius;

camera.target.x = x;
camera.target.y = y;
camera.target.z = camZ;
UpdateCamera(&camera);
}

static void
Expand All @@ -222,19 +242,33 @@ main(void) {

program = CreateProgram("cube.v.glsl", "cube.f.glsl");

glisy_color_init(&blue, "blue", 0);
glisy_color_init(&green, "green", 0);

InitializeCamera(&camera, WINDOW_WIDTH, WINDOW_HEIGHT);
InitializeCube(&cubes[0]);
InitializeCube(&cubes[1]);

MoveCube(&cubes[0], vec3(1, 1, 1));
MoveCube(&cubes[1], vec3(-1, -4, 1));
ScaleCube(&cubes[0], vec3(4, 4, 4));
ScaleCube(&cubes[1], vec3(2, 2, 2));
// init colors and cubes
for (int i = 0; i < numberOfCubes; ++i) {
glisy_color *color = &cubeColors[i];
printf("Initializing color %s\n", color->name);
glisy_color_init(color, strdup(color->name), 0);
}

for (int i = 0; i < numberOfCubes; ++i) {
Cube *cube = &cubes[i];
vec3 scale = {i + 1, i + 1, i + 1};
vec3 translation = {1 - i, i, i + 1};
if (i > 0) {
translation.y = -sinf(i) * 50;
}

if (i % 2) {
//translation = vec3_negate(translation);
}
printf("%s\n", vec3_string(translation));

InitializeCube(cube);
ScaleCube(cube, scale);
TranslateCube(cube, translation);
}

int n = 0;
GL_RENDER({
const float time = glfwGetTime();
const float angle = time * 45.0f;
Expand All @@ -244,15 +278,16 @@ main(void) {
const float camY = -cosf(radians) * radius;
const float camZ = cosf(radians) * radius;

camera.target.x = camX;
camera.target.y = camY;
//camera.target.x = camX;
//camera.target.y = camY;
camera.target.z = camZ;

camera.aspect = width / height;
UpdateCamera(&camera);

for (int i = 0; i < 2; ++i) {
for (int i = 0; i < numberOfCubes; ++i) {
RotateCube(&cubes[i], radians);
RotateCartesianCube(&cubes[i], camX, camY);
DrawCube(&cubes[i]);
}

Expand Down
174 changes: 143 additions & 31 deletions examples/grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,172 @@
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 640

#define DEPTH 32
#define NUM_VERTICES (DEPTH+1)*(DEPTH+1)
#define NUM_INDICES 2*3*DEPTH*DEPTH

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

vec3 position;
vec3 scale;

mat4 translation;
mat4 transform;
mat4 rotation;

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

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

// 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;
static glisy_uniform uProjection;
static glisy_uniform uModel;
static glisy_uniform uView;

// objects
static Camera camera;
static Grid grid;

int
main(void) {
GL_CONTEXT_INIT();
program = CreateProgram("grid.v.glsl", "grid.f.glsl");
// grid constructor
void
InitializeGrid(Grid *grid) {
int vertexLength = 101;
vec3 vertices[101][101];
GLuint 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) / 50.0;
vertices[i][j].y = (i - 50) / 50.0;
}
}

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

glisy_vao_attribute vPosition = {
.buffer = {
.data = (void *) vertices,
.type = GL_FLOAT,
.size = size,
.usage = GL_STATIC_DRAW,
.offset = 0,
.stride = 0,
.dimension = 3,
}
};

printf("%d\n", size);
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);

camera.position = vec3(0, 0, -1);
camera.target = vec3(0, 0, 0);
camera.center = vec3(0, 0, 0);
camera.up = vec3(0, 1, 0);
glisy_uniform_init(&grid->uModel,
"uModel",
GLISY_UNIFORM_MATRIX, 4);

camera.aspect = WINDOW_WIDTH / WINDOW_HEIGHT;
camera.near = 1.0f;
camera.far = 1000.0f;
camera.fov = 45.0f;
glisy_geometry_init(&grid->geometry);
glisy_geometry_attr(&grid->geometry,
"vPosition",
&grid->attributes.vPosition);

UpdateCameraProjectionMatrix(&camera);
UpdateCameraLookAt(&camera);
glisy_geometry_faces(&grid->geometry,
GL_UNSIGNED_INT,
grid->vertex.length,
grid->vertex.faces);
UpdateGrid(grid);
}

glisy_uniform_init(&uProjection, "uProjection", GLISY_UNIFORM_MATRIX, 4);
glisy_uniform_init(&uModel, "uModel", GLISY_UNIFORM_MATRIX, 4);
glisy_uniform_init(&uView, "uView", GLISY_UNIFORM_MATRIX, 4);
void
UpdateGrid(Grid *grid) {
mat4 model;
mat4_identity(model);
model = mat4_scale(model, grid->scale);
model = mat4_multiply(model, grid->rotation);
model = mat4_multiply(model, grid->translation);
glisy_uniform_set(&grid->uModel, &model, sizeof(model));
glisy_uniform_bind(&grid->uModel, &program);
}

void
DrawGrid(Grid *grid) {
UpdateGrid(grid);
glisy_geometry_bind(&grid->geometry, &program);
for (int i = 0; i < 101; i++) {
glisy_geometry_draw(&grid->geometry, GL_LINE_STRIP, 101 * i, grid->vertex.length);
}
glisy_geometry_unbind(&grid->geometry);
}

mat4_identity(grid.model);
static void
onMouseMove(GLFWwindow* window, double x, double y) {
const float angle = 45.0f;
const float radians = dtor(angle);
const float radius = 10.0f;
const float camX = sinf(radians) * radius;
const float camY = -cosf(radians) * radius;
const float camZ = cosf(radians) * radius;

camera.target.x = x;
camera.target.y = y;
camera.target.z = camZ;
UpdateCamera(&camera);
}

static void
onMouseScroll(GLFWwindow* window, double xoffset, double yoffset) {
camera.fov += yoffset;
UpdateCamera(&camera);
}

int
main(void) {
GL_CONTEXT_INIT();
program = CreateProgram("grid.v.glsl", "grid.f.glsl");

InitializeCamera(&camera, WINDOW_WIDTH, WINDOW_HEIGHT);
InitializeGrid(&grid);

GL_RENDER({
camera.aspect = width / height;
UpdateCameraProjectionMatrix(&camera);
UpdateCameraLookAt(&camera);
const float time = glfwGetTime();
const float angle = time * 45.0f;
const float radians = dtor(angle);
const float radius = 10.0f;
const float camX = sinf(radians) * radius;
const float camY = -cosf(radians) * radius;
const float camZ = cosf(radians) * radius;

glisy_uniform_set(&uProjection, &camera.projection, sizeof(camera.projection));
glisy_uniform_set(&uView, &camera.view, sizeof(camera.view));
glisy_uniform_set(&uModel, &grid.model, sizeof(grid.model));
//camera.target.x = camX;
//camera.target.y = camY;
camera.target.z = camZ;

glisy_uniform_bind(&uProjection, &program);
glisy_uniform_bind(&uModel, &program);
glisy_uniform_bind(&uView, &program);
camera.aspect = width / height;
UpdateCamera(&camera);
});

return 0;
Expand Down
4 changes: 1 addition & 3 deletions examples/grid.f.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#version 400

precision mediump float;

in vec3 mColor;
out vec4 fragColor;

void
main(void) {
fragColor = vec4(mColor, 1);
fragColor = vec4(1, 1, 1, 1);
}
3 changes: 0 additions & 3 deletions examples/grid.v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ uniform mat4 uModel;
uniform mat4 uView;

in vec3 vPosition;
in vec3 vColor;
out vec3 mColor;

void
main(void) {
mColor = vColor;
gl_Position = uProjection * uModel * uView * vec4(vPosition, 1.0);
}
Loading

0 comments on commit 118d112

Please sign in to comment.