Example #1
0
GRAPHENE_TEST_UNIT_END

GRAPHENE_TEST_UNIT_BEGIN (ray_closest_point_to_point)
{
  graphene_point3d_t tmp = GRAPHENE_POINT3D_INIT (0.f, 0.f, 50.f);
  graphene_point3d_t check = GRAPHENE_POINT3D_INIT (1.f, 1.f, 50.f);
  graphene_point3d_t res;
  graphene_ray_t r;

  graphene_ray_init (&r, &one3, graphene_vec3_z_axis ());

  if (g_test_verbose ())
    g_test_message ("Behind the ray...");
  graphene_ray_get_closest_point_to_point (&r, &zero3, &res);
  g_assert_true (graphene_point3d_near (&res, &one3, 0.00001f));

  if (g_test_verbose ())
    g_test_message ("Front of the ray...");
  graphene_ray_get_closest_point_to_point (&r, &tmp, &res);
  g_assert_true (graphene_point3d_near (&res, &check, 0.00001f));

  if (g_test_verbose ())
    g_test_message ("On the ray...");
  graphene_ray_get_closest_point_to_point (&r, &one3, &res);
  g_assert_true (graphene_point3d_near (&res, &one3, 0.00001f));
}
Example #2
0
static void
sphere_translate (mutest_spec_t *spec)
{
  graphene_sphere_t s;
  graphene_point3d_t p;

  graphene_sphere_init (&s, &GRAPHENE_POINT3D_INIT (1.f, 1.f, 1.f), 1.f);
  graphene_point3d_init (&p, -1.f, -1.f, -1.f);
  graphene_sphere_translate (&s, &p, &s);
  graphene_sphere_get_center (&s, &p);
  mutest_expect ("translating a unit sphere from (1, 1, 1) by -1 puts it at (0, 0, 0)",
                 mutest_bool_value (graphene_point3d_equal (&p, &GRAPHENE_POINT3D_INIT (0.f, 0.f, 0.f))),
                 mutest_to_be_true,
                 NULL);
}
Example #3
0
static void
sphere_distance (mutest_spec_t *spec)
{
  graphene_sphere_t s;

  graphene_sphere_init (&s, &GRAPHENE_POINT3D_INIT (1.f, 1.f, 1.f), 1.f);
  mutest_expect ("distance of unit sphere from (0, 0, 0) is 0.7320",
                 mutest_float_value (graphene_sphere_distance (&s, &GRAPHENE_POINT3D_INIT (0, 0, 0))),
                 mutest_to_be_close_to, 0.7320, 0.001,
                 NULL);
  mutest_expect ("distance of unit sphere from (1, 1, 1) is -1",
                 mutest_float_value (graphene_sphere_distance (&s, &GRAPHENE_POINT3D_INIT (1, 1, 1))),
                 mutest_to_be_close_to, -1.0, 0.001,
                 NULL);
}
static void
gst_gl_transformation_build_mvp (GstGLTransformation * transformation)
{
  graphene_point3d_t translation_vector =
      GRAPHENE_POINT3D_INIT (transformation->xtranslation * 2.0 *
      transformation->aspect,
      transformation->ytranslation * 2.0,
      transformation->ztranslation * 2.0);

  graphene_matrix_t model_matrix;
  graphene_matrix_t projection_matrix;
  graphene_matrix_t view_matrix;
  graphene_matrix_t vp_matrix;

  graphene_vec3_t eye;
  graphene_vec3_t center;
  graphene_vec3_t up;

  graphene_vec3_init (&eye, 0.f, 0.f, 1.f);
  graphene_vec3_init (&center, 0.f, 0.f, 0.f);
  graphene_vec3_init (&up, 0.f, 1.f, 0.f);

  graphene_matrix_init_scale (&model_matrix,
      transformation->xscale, transformation->yscale, 1.0f);

  graphene_matrix_rotate (&model_matrix,
      transformation->xrotation, graphene_vec3_x_axis ());
  graphene_matrix_rotate (&model_matrix,
      transformation->yrotation, graphene_vec3_y_axis ());
  graphene_matrix_rotate (&model_matrix,
      transformation->zrotation, graphene_vec3_z_axis ());

  graphene_matrix_translate (&model_matrix, &translation_vector);

  if (transformation->ortho) {
    graphene_matrix_init_ortho (&projection_matrix,
        -transformation->aspect, transformation->aspect,
        -1, 1, transformation->znear, transformation->zfar);
  } else {
    graphene_matrix_init_perspective (&projection_matrix,
        transformation->fov,
        transformation->aspect, transformation->znear, transformation->zfar);
  }

  graphene_matrix_init_look_at (&view_matrix, &eye, &center, &up);

  graphene_matrix_multiply (&view_matrix, &projection_matrix, &vp_matrix);
  graphene_matrix_multiply (&model_matrix, &vp_matrix,
      &transformation->mvp_matrix);
}
Example #5
0
static void
sphere_bounding_box (mutest_spec_t *spec)
{
  graphene_sphere_t s;
  graphene_box_t b, check;

  graphene_box_init (&check,
                     &GRAPHENE_POINT3D_INIT (0.f, 0.f, 0.f),
                     &GRAPHENE_POINT3D_INIT (2.f, 2.f, 2.f));

  graphene_sphere_init (&s, &GRAPHENE_POINT3D_INIT (1.f, 1.f, 1.f), 1.f);
  graphene_sphere_get_bounding_box (&s, &b);
  mutest_expect ("bounding box for a unit sphere centered in (1, 1, 1) is (0, 2)",
                 mutest_bool_value (graphene_box_equal (&b, &check)),
                 mutest_to_be_true,
                 NULL);

  graphene_sphere_init (&s, &GRAPHENE_POINT3D_INIT (0.f, 0.f, 0.f), 0.f);
  graphene_sphere_get_bounding_box (&s, &b);
  mutest_expect ("bounding box for degenerate sphere is degenerate box",
                 mutest_bool_value (graphene_box_equal (&b, graphene_box_zero ())),
                 mutest_to_be_true,
                 NULL);
}
Example #6
0
static void
sphere_empty (mutest_spec_t *spec)
{
  graphene_sphere_t s;

  graphene_sphere_init (&s, NULL, 0.f);
  mutest_expect ("sphere with a radius of zero is empty",
                 mutest_bool_value (graphene_sphere_is_empty (&s)),
                 mutest_to_be_true,
                 NULL);

  graphene_sphere_init (&s, &GRAPHENE_POINT3D_INIT (1.f, 1.f, 1.f), 1.f);
  mutest_expect ("unit sphere is not empty",
                 mutest_bool_value (graphene_sphere_is_empty (&s)),
                 mutest_to_be_false,
                 NULL);
}
Example #7
0
GRAPHENE_TEST_UNIT_END

GRAPHENE_TEST_UNIT_BEGIN (ray_get_distance_to_point)
{
  graphene_ray_t r;
  graphene_point3d_t tmp = GRAPHENE_POINT3D_INIT (0, 0, 50);

  graphene_ray_init (&r, &one3, graphene_vec3_z_axis ());

  if (g_test_verbose ())
    g_test_message ("On the ray's origin...");
  graphene_assert_fuzzy_equals (graphene_ray_get_distance_to_point (&r, &one3), 0.f, 0.00001);

  if (g_test_verbose ())
    g_test_message ("Behind the ray...");
  graphene_assert_fuzzy_equals (graphene_ray_get_distance_to_point (&r, &zero3), sqrtf (3.f), 0.00001);

  if (g_test_verbose ())
    g_test_message ("On the ray...");
  graphene_assert_fuzzy_equals (graphene_ray_get_distance_to_point (&r, &tmp), sqrtf (2.f), 0.00001);
}
Example #8
0
void
gtk_snapshot_push_transform (GtkSnapshot             *snapshot,
                             const graphene_matrix_t *transform,
                             const char              *name,
                             ...)
{
  graphene_matrix_t offset;
  graphene_matrix_t* real_transform;

  graphene_matrix_init_translate (&offset,
                                  &GRAPHENE_POINT3D_INIT(
                                      snapshot->state->translate_x,
                                      snapshot->state->translate_y,
                                      0
                                  ));

  real_transform = g_slice_new (graphene_matrix_t);
  graphene_matrix_multiply (transform, &offset, real_transform);

  char *str;

  if (name)
    {
      va_list args;

      va_start (args, name);
      str = g_strdup_vprintf (name, args);
      va_end (args);
    }
  else
    str = NULL;

  snapshot->state = gtk_snapshot_state_new (snapshot->state,
                                            str,
                                            NULL,
                                            0, 0,
                                            gtk_snapshot_collect_transform,
                                            real_transform);
}
Example #9
0
static void
sphere_contains_point (mutest_spec_t *spec)
{
  graphene_point3d_t check, zero = GRAPHENE_POINT3D_INIT_ZERO;
  graphene_sphere_t *s;

  s = graphene_sphere_init (graphene_sphere_alloc (), &zero, 1.f);

  mutest_expect ("unit sphere to contain origin",
                 mutest_bool_value (graphene_sphere_contains_point (s, &zero)),
                 mutest_to_be_true,
                 NULL);

  graphene_point3d_init (&check, 1.f, 0.f, 0.f);
  mutest_expect ("unit sphere to contain x=1",
                 mutest_bool_value (graphene_sphere_contains_point (s, &check)),
                 mutest_to_be_true,
                 NULL);

  graphene_point3d_init (&check, 0.f, -1.f, 0.f);
  mutest_expect ("unit sphere to contain y=-1",
                 mutest_bool_value (graphene_sphere_contains_point (s, &check)),
                 mutest_to_be_true,
                 NULL);

  graphene_point3d_init (&check, 0.f, 0.f, 1.f);
  mutest_expect ("unit sphere to contain z=1",
                 mutest_bool_value (graphene_sphere_contains_point (s, &check)),
                 mutest_to_be_true,
                 NULL);

  mutest_expect ("unit sphere to not contain (1, 1, 1)",
                 mutest_bool_value (graphene_sphere_contains_point (s, &GRAPHENE_POINT3D_INIT (1, 1, 1))),
                 mutest_not, mutest_to_be_true,
                 NULL);

  graphene_sphere_free (s);
}
Example #10
0
static void
sphere_init (mutest_spec_t *spec)
{
  graphene_point3d_t check;
  graphene_point3d_t zero = GRAPHENE_POINT3D_INIT_ZERO;
  graphene_point3d_t one = GRAPHENE_POINT3D_INIT (1, 1, 1);
  graphene_sphere_t *s;

  s = graphene_sphere_init (graphene_sphere_alloc (), &one, 1.f);
  mutest_expect ("alloc to not return null",
                 mutest_pointer (s),
                 mutest_not, mutest_to_be_null,
                 NULL);
  mutest_expect ("unit sphere not to be empty",
                 mutest_bool_value (graphene_sphere_is_empty (s)),
                 mutest_not, mutest_to_be_true,
                 NULL);

  graphene_sphere_get_center (s, &check);
  mutest_expect ("init to set the center",
                 mutest_bool_value (graphene_point3d_equal (&one, &check)),
                 mutest_to_be_true,
                 NULL);
  mutest_expect ("init to set the radius",
                 mutest_float_value (graphene_sphere_get_radius (s)),
                 mutest_to_be_close_to, 1.0, 0.0001,
                 NULL);

  graphene_sphere_init (s, NULL, 1.f);
  graphene_sphere_get_center (s, &check);
  mutest_expect ("null center to be the origin",
                 mutest_bool_value (graphene_point3d_equal (&zero, &check)),
                 mutest_to_be_true,
                 NULL);

  graphene_sphere_free (s);
}
Example #11
0
// The MAIN function, from here we start the application and run the game loop
int main()
{
    // Init GLFW
    glfwInit();
    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
    glfwMakeContextCurrent(window);

    // Set the required callback functions
    glfwSetKeyCallback(window, key_callback);

    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers
    glewInit();

    // Define the viewport dimensions
    glViewport(0, 0, WIDTH, HEIGHT);

    // Setup OpenGL options
    glEnable(GL_DEPTH_TEST);


    // Build and compile our shader program
    shader our_shader = shader_new("shader.vs", "shader.frag");


    // Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
         0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };
    // World space positions of our cubes
    graphene_point3d_t cubePositions[] = {
        GRAPHENE_POINT3D_INIT(0.0f,  0.0f,  0.0f),
        GRAPHENE_POINT3D_INIT( 2.0f,  5.0f, -15.0f),
        GRAPHENE_POINT3D_INIT(-1.5f, -2.2f, -2.5f),
        GRAPHENE_POINT3D_INIT(-3.8f, -2.0f, -12.3f),
        GRAPHENE_POINT3D_INIT( 2.4f, -0.4f, -3.5f),
        GRAPHENE_POINT3D_INIT(-1.7f,  3.0f, -7.5f),
        GRAPHENE_POINT3D_INIT( 1.3f, -2.0f, -2.5f),
        GRAPHENE_POINT3D_INIT( 1.5f,  2.0f, -2.5f),
        GRAPHENE_POINT3D_INIT( 1.5f,  0.2f, -1.5f),
        GRAPHENE_POINT3D_INIT(-1.3f,  1.0f, -1.5f)
    };

    GLuint VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);
    // TexCoord attribute
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(2);

    glBindVertexArray(0); // Unbind VAO


    // Load and create a texture
    GLuint texture1;
    GLuint texture2;
    // ====================
    // Texture 1
    // ====================
    glGenTextures(1, &texture1);
    glBindTexture(GL_TEXTURE_2D, texture1); // All upcoming GL_TEXTURE_2D operations now have effect on our texture object
    // Set our texture parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// Set texture wrapping to GL_REPEAT
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // Set texture filtering
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // Load, create texture and generate mipmaps
    int width, height;
    unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    SOIL_free_image_data(image);
    glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, so we won't accidentily mess up our texture.
    // ===================
    // Texture 2
    // ===================
    glGenTextures(1, &texture2);
    glBindTexture(GL_TEXTURE_2D, texture2);
    // Set our texture parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // Set texture filtering
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // Load, create texture and generate mipmaps
    image = SOIL_load_image("awesomeface.png", &width, &height, 0, SOIL_LOAD_RGB);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    SOIL_free_image_data(image);
    glBindTexture(GL_TEXTURE_2D, 0);


    // Game loop
    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();

        // Render
        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        // Bind Textures using texture units
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture1);
        glUniform1i(glGetUniformLocation(our_shader.program, "ourTexture1"), 0);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, texture2);
        glUniform1i(glGetUniformLocation(our_shader.program, "ourTexture2"), 1);

        // Activate shader
        shader_use(&our_shader);

        // Create transformations
        graphene_matrix_t view;
        graphene_point3d_t view_point;
        graphene_matrix_init_translate(&view, graphene_point3d_init(&view_point, 0.0f, 0.0f, -3.0f));

        graphene_matrix_t projection;
        graphene_matrix_init_perspective(&projection, 45.0f, (GLfloat) WIDTH / (GLfloat) HEIGHT, 0.1f, 100.0f);

        // Get their uniform location
        GLint modelLoc = glGetUniformLocation(our_shader.program, "model");
        GLint viewLoc = glGetUniformLocation(our_shader.program, "view");
        GLint projLoc = glGetUniformLocation(our_shader.program, "projection");

        // Pass the matrices to the shader
        GLfloat view_f[16] = {};
        graphene_matrix_to_float(&view, view_f);
        GLfloat proj_f[16] = {};
        graphene_matrix_to_float(&projection, proj_f);

        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view_f);
        // Note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
        glUniformMatrix4fv(projLoc, 1, GL_FALSE, proj_f);

        glBindVertexArray(VAO);
        for (GLuint i = 0; i < 10; i++)
        {
            // Calculate the model matrix for each object and pass it to shader before drawing
            graphene_matrix_t model;
            graphene_matrix_init_translate(&model, &cubePositions[i]);
            GLfloat angle = 5.0f * i;

            graphene_vec3_t axis;
            graphene_vec3_init(&axis, -1.3f,  1.0f, -1.5f);
            graphene_matrix_rotate(&model, angle, &axis);

            GLfloat model_f[16] = {};
            graphene_matrix_to_float(&model, model_f);

            glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model_f);

            glDrawArrays(GL_TRIANGLES, 0, 36);
        }
        glBindVertexArray(0);

        // Swap the screen buffers
        glfwSwapBuffers(window);
    }
    // Properly de-allocate all resources once they've outlived their purpose
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    // Terminate GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
}