// clang++ test.cpp -framework sflm-system -framework sfml-window -framework OpenGL
#include <iostream>
#include <SFML/Window.hpp>
#include <OpenGL/gl3.h>
static GLuint depthMapFBO;
static const size_t SHADOW_MAP_SIZE = 1024;
static void SetupDepthMap() {
glGenFramebuffers(1, &depthMapFBO);
GLuint depthMap;
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE,
0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
static void RenderDepthMap() {
glViewport(0, 0, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE);
glBindFramebuffer(GL_FRAMEBUFFER, ::depthMapFBO);
glClearDepth(1.0);
glClear(GL_DEPTH_BUFFER_BIT);
// Draw geometry to depth map
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
throw std::runtime_error("Incomplete framebuffer");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
int main() {
static const int WINDOW_WIDTH = 800;
static const int WINDOW_HEIGHT = 600;
sf::Window window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "OpenGL",
sf::Style::Default, sf::ContextSettings(32, 8, 4, 4, 1));
glEnable(GL_DEPTH_TEST);
SetupDepthMap();
bool running = true;
while (running) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
running = false;
}
}
RenderDepthMap();
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
// Kernel Panic on next line from within call to glClear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render to window, passing depth map data as input
window.display();
}
return 0;
}