32 #include <glad/glad.h> 36 #define MAX_CIRCLE_SIZE (5.0f) 37 #define MAX_DISPLACEMENT (1.0f) 38 #define DISPLACEMENT_SIGN_LIMIT (0.3f) 39 #define MAX_ITER (200) 40 #define NUM_ITER_AT_A_TIME (1) 43 #define MAP_SIZE (10.0f) 44 #define MAP_NUM_VERTICES (80) 45 #define MAP_NUM_TOTAL_VERTICES (MAP_NUM_VERTICES*MAP_NUM_VERTICES) 46 #define MAP_NUM_LINES (3* (MAP_NUM_VERTICES - 1) * (MAP_NUM_VERTICES - 1) + \ 47 2 * (MAP_NUM_VERTICES - 1)) 56 "uniform mat4 project;\n" 57 "uniform mat4 modelview;\n" 64 " gl_Position = project * modelview * vec4(x, y, z, 1.0);\n" 72 " color = vec4(0.2, 1.0, 0.2, 1.0); \n" 87 1.0f, 0.0f, 0.0f, 0.0f,
88 0.0f, 1.0f, 0.0f, 0.0f,
89 0.0f, 0.0f, 1.0f, 0.0f,
90 0.0f, 0.0f, 0.0f, 1.0f
95 1.0f, 0.0f, 0.0f, 0.0f,
96 0.0f, 1.0f, 0.0f, 0.0f,
97 0.0f, 0.0f, 1.0f, 0.0f,
98 0.0f, 0.0f, 0.0f, 1.0f
137 fprintf(stderr,
"ERROR: Failed to compile %s shader\n", (type ==
GL_FRAGMENT_SHADER) ?
"fragment" :
"vertex" );
139 fprintf(stderr,
"ERROR: \n%s\n\n", info_log);
153 GLuint vertex_shader = 0u;
154 GLuint fragment_shader = 0u;
159 if (vertex_shader != 0u)
162 if (fragment_shader != 0u)
176 fprintf(stderr,
"ERROR, failed to link shader program\n");
178 fprintf(stderr,
"ERROR: \n%s\n\n", info_log);
188 fprintf(stderr,
"ERROR: Unable to load fragment shader\n");
194 fprintf(stderr,
"ERROR: Unable to load vertex shader\n");
231 printf (
"Vertice %d (%f, %f, %f)\n",
247 for (i = 0 ; i < MAP_NUM_VERTICES -1 ; ++
i)
253 for (i = 0 ; i < MAP_NUM_VERTICES -1 ; ++
i)
259 for (i = 0 ; i < (MAP_NUM_VERTICES - 1) ; ++
i)
261 for (j = 0 ; j < (MAP_NUM_VERTICES - 1) ; ++
j)
265 map_line_indices[k++] = ref + 1;
267 map_line_indices[k++] =
ref;
270 map_line_indices[k++] =
ref;
271 map_line_indices[k++] = ref + MAP_NUM_VERTICES + 1;
281 printf (
"Line %d: %d -> %d (%f, %f, %f) -> (%f, %f, %f)\n",
290 float*
size,
float* displacement)
295 *center_y = (
MAP_SIZE * rand()) / (1.0
f * RAND_MAX);
297 sign = (1.0f * rand()) / (1.0
f * RAND_MAX);
321 GLfloat dz = center_z - map_vertices[2][
ii];
322 GLfloat pd = (2.0f * (float) sqrt((dx * dx) + (dz * dz))) / circle_size;
323 if (fabs(pd) <= 1.0f)
326 GLfloat new_height = disp + (float) (cos(pd*3.14
f)*disp);
327 map_vertices[1][
ii] += new_height;
396 fprintf(stderr,
"Error: %s\n", description);
399 int main(
int argc,
char** argv)
404 double last_update_time;
407 GLint uloc_modelview;
440 if (shader_program == 0u)
494 if ((dt - last_update_time) > 0.2)
503 last_update_time =
dt;
#define glGetShaderInfoLog
static GLfloat map_vertices[3][MAP_NUM_TOTAL_VERTICES]
static const char * vertex_shader_text
static void update_mesh(void)
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
int main(int argc, char **argv)
#define glGetAttribLocation
void *(* GLADloadproc)(const char *name)
static GLFWwindow * window
#define GLFW_OPENGL_FORWARD_COMPAT
OpenGL forward-compatibility hint and attribute.
#define MAP_NUM_TOTAL_VERTICES
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
static GLfloat modelview_matrix[16]
static void make_mesh(GLuint program)
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
#define glUniformMatrix4fv
static GLuint make_shader(GLenum type, const char *text)
#define GLFW_OPENGL_PROFILE
OpenGL profile hint and attribute.
static void init_map(void)
static GLuint mesh_vbo[4]
static void generate_heightmap__circle(float *center_x, float *center_y, float *size, float *displacement)
static void error_callback(int error, const char *description)
static GLfloat view_angle
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
static GLfloat projection_matrix[16]
static const char * fragment_shader_text
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
#define glGenVertexArrays
#define glVertexAttribPointer
#define glEnableVertexAttribArray
#define GL_COLOR_BUFFER_BIT
#define glBindVertexArray
GLbitfield GLuint program
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
static GLuint map_line_indices[2 *MAP_NUM_LINES]
#define glGetUniformLocation
static void update_map(int num_iter)
#define GL_FRAGMENT_SHADER
#define GLFW_RESIZABLE
Window resize-ability window hint and attribute.
GLAPI int gladLoadGLLoader(GLADloadproc)
static const textual_icon exit
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
#define GLFW_OPENGL_CORE_PROFILE
#define NUM_ITER_AT_A_TIME
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
#define GL_COMPILE_STATUS
#define DISPLACEMENT_SIGN_LIMIT
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
#define glGetProgramInfoLog
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
#define GL_ELEMENT_ARRAY_BUFFER
static GLuint make_shader_program(const char *vs_text, const char *fs_text)
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
static GLfloat aspect_ratio
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.