Skip to content

Commit

Permalink
Add compatibility with Mesa-driven GPUs (ebruneton#14)
Browse files Browse the repository at this point in the history
* Don't require GL_EXT_geometry_shader4

OpenGL 3.3 alone is perfectly capable of what we need.

* Unuse glBegin/glEnd and family in class Demo to make it possible to enable Core Profile

* Unuse glBegin/glEnd and family in class Model to make it possible to enable Core Profile

* Unuse glBegin/glEnd and family in model_test to make it possible to enable Core Profile

* Implement text rendering compatible with OpenGL 3.3 Core Profile

* Use GL_RGBA32F/GL_RGBA16F for color buffers unconditionally

GL_RGB32F/GL_RGB16F are not required color buffer formats, and some
OpenGL implementations like Mesa don't support them, leading to
Framebuffer Incomplete Attachment errors.

* Request OpenGL 3.3 Core Profile from GLUT

* Switch to GLAD

GLEW can't handle OpenGL Core profile without hacks: you need
glewExperimental option, and then you have to loop until glGetError
returns GL_NO_ERROR to make the demo work. This sounds unclean.

As a bonus, we get the ability to restrict the headers to only the
relevant API functions and constants, i.e. OpenGL 3.3 Core Profile,
without any others, which lets us check at compile time that we
aren't using anything higher than requested (except possibly in the
shaders, which the C++ compiler can't know about).

* Fix output path for glad.o

* Move glad-related files to external/

* Rename attribIndex and coordsPerVertex to match Google coding style

* Rename TextRenderer.{cc,h} to text_renderer.{cc,h}

* Move private members of class TextRenderer after public members

* Add trailing underscore to data members of class TextRenderer

* Rename {vertex,fragment}ShaderSrc to k{Vertex,Fragment}ShaderSrc variables

* Reduce indentation in implementation of TextRenderer from 4 spaces per tab to 2

* Rename {vao_,vbo_} to full_screen_quad_{vao_,vbo_} in demo.{cc,h}

* Rename {vao_,vbo_} to full_screen_quad_{vao_,vbo_} in model.{cc,h}

* Rename {vao_,vbo_} to full_screen_quad_{vao_,vbo_} in model_test.cc

* Add white space around operators in text_renderer.cc

* Captialize first letters of methods of TextRenderer

* Add missing space

* Revert "Use GL_RGBA32F/GL_RGBA16F for color buffers unconditionally"

* Use RGBA32F format for 2D textures unconditionally

RGB32F is not guaranteed to be supported as a framebuffer color
attachment, while RGBA32F is. This matters on such OpenGL
implementations as Mesa, which drives opensource-powered Linux GPU
drivers.

* Fallback to RGBA32F for 3D textures if RGB32F is not supported as FBO color attachment
  • Loading branch information
10110111 authored and ebruneton committed Nov 6, 2017
1 parent b4513bc commit abf6c79
Show file tree
Hide file tree
Showing 13 changed files with 3,823 additions and 54 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
GPP := g++
GPP_FLAGS := -Wall -Wmain -pedantic -pedantic-errors -std=c++11
INCLUDE_FLAGS := \
-I. -Iexternal -Iexternal/dimensional_types -Iexternal/progress_bar
-I. -Iexternal/glad/include -Iexternal -Iexternal/dimensional_types -Iexternal/progress_bar
DEBUG_FLAGS := -g
RELEASE_FLAGS := -DNDEBUG -O3 -fexpensive-optimizations

DIRS := atmosphere tools
DIRS := atmosphere tools text external/glad/src
HEADERS := $(shell find $(DIRS) -name "*.h")
SOURCES := $(shell find $(DIRS) -name "*.cc")
GLSL_SOURCES := $(shell find $(DIRS) -name "*.glsl")
Expand All @@ -49,6 +49,7 @@ all: lint doc test integration_test demo
# <regex>.
lint: $(HEADERS) $(SOURCES)
cpplint --exclude=tools/docgen_main.cc \
--exclude=external/glad/src/glad.cc \
--exclude=atmosphere/reference/functions.h \
--exclude=atmosphere/reference/model_test.cc --root=$(PWD) $^
cpplint --filter=-runtime/references --root=$(PWD) \
Expand Down Expand Up @@ -86,19 +87,22 @@ output/Debug/atmosphere_test: \
$(GPP) $^ -o $@

output/Release/atmosphere_integration_test: \
output/Release/external/glad/src/glad.o \
output/Release/atmosphere/model.o \
output/Release/atmosphere/reference/functions.o \
output/Release/atmosphere/reference/model.o \
output/Release/atmosphere/reference/model_test.o \
output/Release/external/dimensional_types/test/test_main.o \
output/Release/external/progress_bar/util/progress_bar.o
$(GPP) $^ -pthread -lGLEW -lglut -lGL -o $@
$(GPP) $^ -pthread -ldl -lglut -lGL -o $@

output/Debug/atmosphere_demo: \
output/Debug/external/glad/src/glad.o \
output/Debug/text/text_renderer.o \
output/Debug/atmosphere/demo/demo.o \
output/Debug/atmosphere/demo/demo_main.o \
output/Debug/atmosphere/model.o
$(GPP) $^ -pthread -lGLEW -lglut -lGL -o $@
$(GPP) $^ -pthread -ldl -lglut -lGL -o $@

output/Debug/%.o: %.cc
mkdir -p $(@D)
Expand Down
48 changes: 33 additions & 15 deletions atmosphere/demo/demo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ independent of our atmosphere model. The only part which is related to it is the

#include "atmosphere/demo/demo.h"

#include <GL/glew.h>
#include <glad/glad.h>
#include <GL/freeglut.h>

#include <algorithm>
Expand All @@ -48,6 +48,7 @@ independent of our atmosphere model. The only part which is related to it is the
#include <sstream>
#include <string>
#include <vector>
#include <stdexcept>

namespace atmosphere {
namespace demo {
Expand Down Expand Up @@ -115,7 +116,10 @@ Demo::Demo(int viewport_width, int viewport_height) :
glutInitWindowSize(viewport_width, viewport_height);
window_id_ = glutCreateWindow("Atmosphere Demo");
INSTANCES[window_id_] = this;
glewInit();
if (!gladLoadGL())
throw std::runtime_error("GLAD initialization failed");
if (!GLAD_GL_VERSION_3_3)
throw std::runtime_error("OpenGL 3.3 or higher is required");

glutDisplayFunc([]() {
INSTANCES[glutGetWindow()]->HandleRedisplayEvent();
Expand All @@ -136,7 +140,26 @@ Demo::Demo(int viewport_width, int viewport_height) :
INSTANCES[glutGetWindow()]->HandleMouseWheelEvent(dir);
});

glGenVertexArrays(1, &full_screen_quad_vao_);
glBindVertexArray(full_screen_quad_vao_);
glGenBuffers(1, &full_screen_quad_vbo_);
glBindBuffer(GL_ARRAY_BUFFER, full_screen_quad_vbo_);
const GLfloat vertices[] = {
-1.0, -1.0, 0.0, 1.0,
+1.0, -1.0, 0.0, 1.0,
-1.0, +1.0, 0.0, 1.0,
+1.0, +1.0, 0.0, 1.0,
};
constexpr int kCoordsPerVertex = 4;
glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
constexpr GLuint kAttribIndex = 0;
glVertexAttribPointer(kAttribIndex, kCoordsPerVertex, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(kAttribIndex);
glBindVertexArray(0);

InitModel();

text_renderer_.reset(new TextRenderer);
}

/*
Expand All @@ -145,6 +168,8 @@ Demo::Demo(int viewport_width, int viewport_height) :

Demo::~Demo() {
glDeleteProgram(program_);
glDeleteBuffers(1, &full_screen_quad_vbo_);
glDeleteVertexArrays(1, &full_screen_quad_vao_);
INSTANCES.erase(window_id_);
}

Expand Down Expand Up @@ -357,16 +382,13 @@ void Demo::HandleRedisplayEvent() const {
sin(sun_azimuth_angle_radians_) * sin(sun_zenith_angle_radians_),
cos(sun_zenith_angle_radians_));

glBegin(GL_TRIANGLE_STRIP);
glVertex4f(-1.0, -1.0, 0.0, 1.0);
glVertex4f(+1.0, -1.0, 0.0, 1.0);
glVertex4f(-1.0, +1.0, 0.0, 1.0);
glVertex4f(+1.0, +1.0, 0.0, 1.0);
glEnd();
glBindVertexArray(full_screen_quad_vao_);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);

if (show_help_) {
std::stringstream help;
help << "\nMouse:\n"
help << "Mouse:\n"
<< " drag, CTRL+drag, wheel: view and sun directions\n"
<< "Keys:\n"
<< " h: help\n"
Expand All @@ -384,12 +406,8 @@ void Demo::HandleRedisplayEvent() const {
<< (do_white_balance_ ? "on" : "off") << ")\n"
<< " +/-: increase/decrease exposure (" << exposure_ << ")\n"
<< " 1-9: predefined views\n";
glUseProgram(0);
glColor3f(1.0, 0.0, 0.0);
glRasterPos2f(-0.99, 1.0);
glutBitmapString(GLUT_BITMAP_9_BY_15,
(const unsigned char*) help.str().c_str());
glUseProgram(program_);
text_renderer_->SetColor(1.0, 0.0, 0.0);
text_renderer_->DrawText(help.str().c_str(), 5, 4);
}

glutSwapBuffers();
Expand Down
7 changes: 7 additions & 0 deletions atmosphere/demo/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ atmosphere model and to the GLSL program used to render the scene:
#ifndef ATMOSPHERE_DEMO_DEMO_H_
#define ATMOSPHERE_DEMO_DEMO_H_

#include <glad/glad.h>

#include <memory>

#include "atmosphere/model.h"
#include "text/text_renderer.h"

namespace atmosphere {
namespace demo {
Expand Down Expand Up @@ -87,6 +90,8 @@ class Demo {
bool show_help_;

std::unique_ptr<Model> model_;
GLuint full_screen_quad_vao_;
GLuint full_screen_quad_vbo_;
unsigned int program_;
int window_id_;

Expand All @@ -100,6 +105,8 @@ class Demo {
int previous_mouse_x_;
int previous_mouse_y_;
bool is_ctrl_key_pressed_;

std::unique_ptr<TextRenderer> text_renderer_;
};

} // namespace demo
Expand Down
4 changes: 3 additions & 1 deletion atmosphere/demo/demo_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ application.

#include "atmosphere/demo/demo.h"

#include <GL/glew.h>
#include <glad/glad.h>
#include <GL/freeglut.h>

#include <memory>

using atmosphere::demo::Demo;

int main(int argc, char** argv) {
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
Expand Down
Loading

0 comments on commit abf6c79

Please sign in to comment.