forked from Totktonada/WaveSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.c
153 lines (120 loc) · 3.7 KB
/
scene.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include "scene.h"
#include "camera.h"
#include "shaders_errors.h"
/* ==== Defines ==== */
#define CONTEXT_WIDTH_INITIAL 640
#define CONTEXT_HEIGHT_INITIAL 480
/* ==== Functions ==== */
void setupOpenGLContext(ContextSize * context)
{
if (glfwInit() == GL_FALSE)
{
fprintf(stderr, "glfwInit failed.\n");
exit(EXIT_FAILURE);
}
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (glfwOpenWindow(
context->w, context->h,
0, 0, 0, /* red, green, blue bits -> default */
0, 0, 0, /* alpha, depth, stencil bits -> default */
GLFW_WINDOW) == GL_FALSE)
{
fprintf(stderr, "glfwOpenWindow failed.\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetWindowTitle("Wave Simulation");
CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
void checkOpenGLVersion()
{
GLenum glewInitValue;
/* For compatinility with OpenGL 3.2+ core context. */
glewExperimental = GL_TRUE;
glewInitValue = glewInit();
/* Avoid GLEW errors, see LINKS: [10]. */
glGetError();
if (glewInitValue != GLEW_OK)
{
fprintf(stderr, "glewInit failed: %s\n.",
glewGetErrorString(glewInitValue));
exit(EXIT_FAILURE);
}
if (! glewIsSupported("GL_VERSION_3_3"))
{
fprintf(stderr, "No OpenGL 3.3 support. I'm sorry.\n");
fprintf(stderr, "Your OpenGL version: %s.\n",
glGetString(GL_VERSION));
exit(EXIT_FAILURE);
}
CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
void setupOpenGLState(ContextSize * context, GLboolean vsync)
{
if (vsync)
{
glfwSwapInterval(1);
}
else
{
glfwSwapInterval(0);
}
glViewport(0, 0, context->w, context->h);
glClearColor(0.0f, 0.20f, 0.40f, 1.0f);
#if 0
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
#endif
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0f, 1.0f);
/* No interpolate 'flat' varying variables, i.e. normals. */
glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
/* Perform some rendering with near and far planes clipping off. */
/* I use small znear value instead, that work better. */
/* glEnable(GL_DEPTH_CLAMP); */
/* For correct bilinear interpolation at rib of skybox. */
/* TODO: glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);*/
#if 0
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glLineWidth(1.0f);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
#endif
CHECK_OPENGL_ERRORS(__FILE__, __LINE__);
}
Scene * newScene(GLboolean vsync)
{
Scene * scene = (Scene *) malloc(sizeof(Scene));
scene->context = (ContextSize *) malloc(sizeof(ContextSize));
scene->context->w = CONTEXT_WIDTH_INITIAL;
scene->context->h = CONTEXT_HEIGHT_INITIAL;
setupOpenGLContext(scene->context);
checkOpenGLVersion();
setupOpenGLState(scene->context, vsync);
scene->camera = newCamera(scene->context->w, scene->context->h);
scene->world = getWorld("world.txt");
scene->water = getWater();
setupWater(scene->water->drawSP, scene->world);
setupWorldUniforms(scene->world->sp, scene->water);
return scene;
}
void freeScene(Scene * scene)
{
freeCamera(scene->camera);
freeWorld(scene->world);
freeWater(scene->water);
free(scene->context);
free(scene);
glfwTerminate();
}