]> git.sesse.net Git - movit/blob - util.h
Revert "Support pad/crop from bottom, not just from the top."
[movit] / util.h
1 #ifndef _MOVIT_UTIL_H
2 #define _MOVIT_UTIL_H 1
3
4 // Various utilities.
5
6 #include <GL/glew.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <Eigen/Core>
10 #include <string>
11
12 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
13
14 namespace movit {
15
16 // Converts a HSV color to RGB. Assumes h in [0, 2pi> or [-pi, pi>
17 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b);
18
19 // Converts a HSV color to RGB, but keeps luminance constant
20 // (ie. color luminance is as if S=0).
21 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b);
22
23 // Read a file from disk and return its contents.
24 // Dies if the file does not exist.
25 std::string read_file(const std::string &filename);
26
27 // Compile the given GLSL shader (typically a vertex or fragment shader)
28 // and return the object number.
29 GLuint compile_shader(const std::string &shader_src, GLenum type);
30
31 // Print a 3x3 matrix to standard output. Useful for debugging.
32 void print_3x3_matrix(const Eigen::Matrix3d &m);
33
34 // Output a GLSL 3x3 matrix declaration.
35 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m);
36
37 // Calculate a / b, rounding up. Does not handle overflow correctly.
38 unsigned div_round_up(unsigned a, unsigned b);
39
40 // Calculate where to sample, and with what weight, if one wants to use
41 // the GPU's bilinear hardware to sample w1 * x[0] + w2 * x[1].
42 //
43 // Note that since the GPU might have limited precision in its linear
44 // interpolation, the effective weights might be different from the ones you
45 // asked for. sum_sq_error, if not NULL, will contain the sum of the
46 // (estimated) squared errors of the two weights.
47 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error);
48
49 // Create a VBO with the given data, and bind it to the vertex attribute
50 // with name <attribute_name>. Returns the VBO number.
51 GLuint fill_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data);
52
53 // Clean up after fill_vertex_attribute().
54 void cleanup_vertex_attribute(GLuint glsl_program_num, const std::string &attribute_name, GLuint vbo);
55
56 }  // namespace movit
57
58 #ifdef NDEBUG
59 #define check_error()
60 #else
61 #define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at %s:%d\n", err, __FILE__, __LINE__); abort(); } }
62 #endif
63
64 // CHECK() is like assert(), but retains any side effects no matter the compilation mode.
65 #ifdef NDEBUG
66 #define CHECK(x) (void)(x)
67 #else
68 #define CHECK(x) do { bool ok = x; if (!ok) { fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #x); abort(); } } while (false)
69 #endif
70
71 #endif // !defined(_MOVIT_UTIL_H)