Skip to content

Commit

Permalink
update examples and fix bugz
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Mar 8, 2016
1 parent 118d112 commit 8e823d1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 76 deletions.
25 changes: 11 additions & 14 deletions examples/cube.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,8 @@ ScaleCube(Cube *cube, vec3 scale) {

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;
camera.target.y = -y;
UpdateCamera(&camera);
}

Expand All @@ -247,7 +239,6 @@ main(void) {
// 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);
}

Expand All @@ -260,15 +251,23 @@ main(void) {
}

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

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

{
double x = 0;
double y = 0;
glfwGetCursorPos(window, &x, &y);
camera.target.x = x;
camera.target.y = -y;
UpdateCamera(&camera);
}

GL_RENDER({
const float time = glfwGetTime();
const float angle = time * 45.0f;
Expand All @@ -278,8 +277,6 @@ main(void) {
const float camY = -cosf(radians) * radius;
const float camZ = cosf(radians) * radius;

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

camera.aspect = width / height;
Expand Down
107 changes: 65 additions & 42 deletions examples/grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#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 {
Expand All @@ -23,7 +19,7 @@ struct Grid {

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

Expand Down Expand Up @@ -52,19 +48,43 @@ static Grid grid;
void
InitializeGrid(Grid *grid) {
int vertexLength = 101;
int facesLen = 0;
vec3 vertices[101][101];
GLuint faces[2 * 100 * 101 * 2];
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) / 50.0;
vertices[i][j].y = (i - 50) / 50.0;
vertices[i][j].x = (j - 50) / 100.0;
vertices[i][j].z = (i - 50) / 100.0;
vertices[i][j].y = 0;
}
}

grid->position = vec3(0, 0, 0);
{
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;
}
}

// 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;
}
}

facesLen = i;
}

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

glisy_vao_attribute vPosition = {
Expand All @@ -79,7 +99,6 @@ InitializeGrid(Grid *grid) {
}
};

printf("%d\n", size);
memcpy(grid->vertex.positions, vertices, size);
memcpy(grid->vertex.faces, faces, sizeof(faces));

Expand All @@ -99,19 +118,37 @@ InitializeGrid(Grid *grid) {
&grid->attributes.vPosition);

glisy_geometry_faces(&grid->geometry,
GL_UNSIGNED_INT,
grid->vertex.length,
grid->vertex.faces);
GL_UNSIGNED_SHORT,
facesLen,
faces);

glisy_geometry_update(&grid->geometry);
UpdateGrid(grid);
}

void
UpdateGrid(Grid *grid) {
mat4 model;
mat4 scale;
mat4 rotation;
mat4 translation;

mat4_identity(model);
model = mat4_scale(model, grid->scale);
model = mat4_multiply(model, grid->rotation);
model = mat4_multiply(model, grid->translation);
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_multiply(model, translation);

glisy_uniform_set(&grid->uModel, &model, sizeof(model));
glisy_uniform_bind(&grid->uModel, &program);
}
Expand All @@ -120,27 +157,10 @@ 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_draw(&grid->geometry, GL_LINES, 0, grid->vertex.length);
glisy_geometry_unbind(&grid->geometry);
}

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;
Expand All @@ -150,26 +170,29 @@ onMouseScroll(GLFWwindow* window, double xoffset, double yoffset) {
int
main(void) {
GL_CONTEXT_INIT();
glfwSetScrollCallback(window, onMouseScroll);

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

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

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

GL_RENDER({
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;
const vec3 rotation = vec3(0, 1, 0);

//camera.target.x = camX;
//camera.target.y = camY;
camera.target.z = camZ;
(void) mat4_rotate(camera.transform,
radians,
rotation);

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

return 0;
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(1, 1, 1, 1);
fragColor = vec4(0.5, 0.5, 0.5, 1);
}
16 changes: 8 additions & 8 deletions examples/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ struct Camera {
mat4 view;

vec3 position;
vec3 center;
vec3 target;
vec3 up;

Expand Down Expand Up @@ -96,11 +95,13 @@ UpdateCameraProjectionMatrix(Camera *camera) {

void
UpdateCameraLookAt(Camera *camera) {
camera->target = vec3_transform_mat4(camera->target, camera->transform);
camera->view = mat4_lookAt(camera->target,
camera->center,
vec3 target = vec3_transform_mat4(camera->target,
camera->transform);
camera->view = mat4_lookAt(target,
camera->position,
camera->up);
glisy_uniform_bind(&camera->uView, 0);
mat4_identity(camera->transform);
}

void
Expand All @@ -122,14 +123,13 @@ UpdateCamera(Camera *camera) {
void
InitializeCamera(Camera *camera, int width, int height) {
camera->position = vec3(0, 0, 0);
camera->target = vec3(0, 0, 1);
camera->center = vec3(0, 0, 0);
camera->target = vec3(1, 1, 1);
camera->up = vec3(0, 1, 0);

camera->aspect = width / height;
camera->near = 1.0f;
camera->far = 2000.0f;
camera->fov = 50.0f;
camera->far = 100.0f;
camera->fov = 45.0f;

mat4_identity(camera->projection);
mat4_identity(camera->transform);
Expand Down
3 changes: 1 addition & 2 deletions include/glisy/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ struct glisy_geometry {
glisy_vao_attribute attributes[GLISY_MAX_VAO_ATTRIBS];
GLuint attrlen;
GLuint faceslen;
GLuint indiceslen;
glisy_buffer index;
GLboolean dirty;
GLenum elementsType;
GLuint indices[0xffffff];
void *faces;
GLboolean useElements;
glisy_program *program;
};
Expand Down
1 change: 0 additions & 1 deletion include/glisy/math/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ struct vec3 { float x; float y; float z; };
// @TODO(werle) - hermite
// @TODO(werle) - bezier
// @TODO(werle) - transform
// @TODO(werle) - transformMat4
// @TODO(werle) - transformQuat
// @TODO(werle) - angle

Expand Down
19 changes: 11 additions & 8 deletions src/geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ glisy_geometry_update(glisy_geometry *geometry) {
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
geometry->indiceslen * sizeof(GLuint),
geometry->indices,
geometry->faceslen * (
GL_UNSIGNED_SHORT == geometry->elementsType ?
sizeof(GLushort) :
GL_UNSIGNED_INT ? sizeof(GLuint) : 0
),
geometry->faces,
GL_STATIC_DRAW);
} else {
glisy_vao_update(&geometry->vao, 0);
Expand All @@ -114,13 +118,13 @@ void
glisy_geometry_faces(glisy_geometry *geometry,
GLenum type,
GLuint count,
void *indices) {
void *faces) {

if (!geometry) return;
if (!indices) return;
if (!faces) return;
geometry->elementsType = type;
geometry->indiceslen = count;
memcpy(geometry->indices, indices, sizeof(GLuint) * count);
geometry->faceslen = count;
geometry->faces = faces;
geometry->useElements = GL_TRUE;
geometry->dirty = GL_TRUE;
}
Expand Down Expand Up @@ -159,8 +163,7 @@ glisy_geometry_draw(glisy_geometry *geometry,
if (geometry->vao.useElements) {
glDrawElements(mode,
stop - start,
geometry->elementsType,
0);
geometry->elementsType, 0);

} else {
glDrawArrays(mode, start, stop - start);
Expand Down

0 comments on commit 8e823d1

Please sign in to comment.