6#include <glm/gtc/type_ptr.hpp>
7#include <glm/gtc/matrix_transform.hpp>
18 const unsigned char pixels[2 * 2 * 4] = {
19 56, 56, 56, 255, 46, 46, 46, 255,
20 46, 46, 46, 255, 56, 56, 56, 255,
24 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 2, 2, 0,
25 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
26 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
27 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
28 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
29 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
30 glBindTexture(GL_TEXTURE_2D, 0);
48 glDisable(GL_LIGHTING);
52 glEnable(GL_LIGHTING);
58 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb);
87 glLightfv(GL_LIGHT0, GL_POSITION, pos);
88 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
89 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
90 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
95 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDir);
96 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, L.
spotCutoff);
101 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f);
105 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb);
112 glMatrixMode(GL_PROJECTION);
115 glOrtho(0, w, 0, h, -1, 1);
117 glMatrixMode(GL_MODELVIEW);
121 glDisable(GL_DEPTH_TEST);
122 glDisable(GL_LIGHTING);
123 glEnable(GL_TEXTURE_2D);
126 glColor3f(1.0f, 1.0f, 1.0f);
128 const float tileSize = 16.0f;
129 float u =
static_cast<float>(w) / (tileSize * 2.0f);
130 float v =
static_cast<float>(h) / (tileSize * 2.0f);
133 glTexCoord2f(0, 0); glVertex2f(0, 0);
134 glTexCoord2f(u, 0); glVertex2f(
static_cast<float>(w), 0);
135 glTexCoord2f(u, v); glVertex2f(
static_cast<float>(w),
static_cast<float>(h));
136 glTexCoord2f(0, v); glVertex2f(0,
static_cast<float>(h));
139 glBindTexture(GL_TEXTURE_2D, 0);
140 glDisable(GL_TEXTURE_2D);
141 glEnable(GL_DEPTH_TEST);
143 glMatrixMode(GL_PROJECTION);
145 glMatrixMode(GL_MODELVIEW);
152 const glm::vec3& top,
const glm::vec3& bottom)
154 glMatrixMode(GL_PROJECTION);
157 glOrtho(0, w, 0, h, -1, 1);
159 glMatrixMode(GL_MODELVIEW);
163 glDisable(GL_DEPTH_TEST);
164 glDisable(GL_LIGHTING);
165 glDisable(GL_TEXTURE_2D);
168 glColor3f(bottom.x, bottom.y, bottom.z);
170 glVertex2f(
static_cast<float>(w), 0);
171 glColor3f(top.x, top.y, top.z);
172 glVertex2f(
static_cast<float>(w),
static_cast<float>(h));
173 glVertex2f(0,
static_cast<float>(h));
176 glEnable(GL_DEPTH_TEST);
178 glMatrixMode(GL_PROJECTION);
180 glMatrixMode(GL_MODELVIEW);
188 glDisable(GL_TEXTURE_2D);
189 glDisable(GL_LIGHTING);
192 glColor4f(0.55f, 0.55f, 0.55f, 0.6f);
194 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
197 for (
float i = -gridSize; i <= gridSize; i += step)
199 if (i == 0.0f)
continue;
200 glVertex3f(-gridSize, i, 0.0f);
201 glVertex3f( gridSize, i, 0.0f);
202 glVertex3f(i, -gridSize, 0.0f);
203 glVertex3f(i, gridSize, 0.0f);
208 glColor3f(0.2f, 0.5f, 1.0f);
210 glVertex3f(-gridSize, 0.0f, 0.0f);
211 glVertex3f( gridSize, 0.0f, 0.0f);
212 glVertex3f(0.0f, -gridSize, 0.0f);
213 glVertex3f(0.0f, gridSize, 0.0f);
218 glEnable(GL_LIGHTING);
219 glEnable(GL_TEXTURE_2D);
225 float nearPlane,
float farPlane)
227 glMatrixMode(GL_PROJECTION);
229 glm::mat4 proj = glm::perspective(
230 fov,
static_cast<float>(w) /
static_cast<float>(h),
231 nearPlane, farPlane);
232 glMultMatrixf(glm::value_ptr(proj));
237 glMatrixMode(GL_MODELVIEW);
239 glMultMatrixf(glm::value_ptr(viewMatrix));
245 const glm::vec3& clearColor)
249 glViewport(0, 0, w, h);
250 glClearColor(clearColor.x, clearColor.y, clearColor.z, 1.0f);
251 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
282 const glm::vec3& clearColor,
bool drawGridFlag,
283 const std::function<
void()>& drawObjects)
285 if (w <= 0 || h <= 0)
291 glClear(GL_DEPTH_BUFFER_BIT);
303 glEnable(GL_NORMALIZE);
305 glDisable(GL_NORMALIZE);
Orbit camera that revolves around a target point.
glm::mat4 getViewMatrix() const
Compute the current view matrix from yaw, pitch, and radius.
void renderScene(ViewportFBO &fbo, int w, int h, const OrbitCamera &camera, float fov, const glm::vec3 &clearColor, bool drawGrid, const std::function< void()> &drawObjects)
void applyProjection(int w, int h, float fov, float nearPlane=0.1f, float farPlane=6400.0f)
Load a perspective projection matrix.
void beginPass(ViewportFBO &fbo, int w, int h, const glm::vec3 &clearColor)
Begin an off-screen FBO render pass (resize, bind, clear).
void init()
Create GPU resources. Call once after the OpenGL context is current.
void applyLighting()
Configure OpenGL fixed-function lighting from the current state.
void drawGrid(float size=40.0f, float step=1.0f)
Draw a ground grid with highlighted centre axes.
void drawBackground(int w, int h)
Draw the background (checker or gradient) based on current state.
void drawCheckerboard(int w, int h)
Draw a tiled checkerboard pattern in screen space.
void shutdown()
Release GPU resources.
void applyView(const glm::mat4 &viewMatrix)
Load a view (camera) matrix.
void endPass(ViewportFBO &fbo)
End the current FBO render pass (unbind).
void drawGradient(int w, int h, const glm::vec3 &top, const glm::vec3 &bottom)
Draw a vertical gradient quad in screen space.
Plain-data description of a single light source.
Simple OpenGL framebuffer object wrapper for off-screen rendering.
void bind() const
Bind this FBO as the current render target.
void resize(int w, int h)
Resize the FBO; destroys and recreates if dimensions changed.
void unbind() const
Unbind (revert to the default framebuffer).