commit eaf6435af0a6cde7568b1198eb36ff3a6a76df72
parent dea92e01fd55c7964cecf51269b9a53e0d852c17
Author: kyle <kyle@getaddrinfo.net>
Date: Wed, 28 Oct 2015 20:29:38 -0600
viewer: split up classes into source files
Diffstat:
7 files changed, 244 insertions(+), 227 deletions(-)
diff --git a/viewer/Makefile b/viewer/Makefile
@@ -7,7 +7,7 @@ LDLIBS += -lfreetype -lz -lglut
CXX = eg++ -std=c++1y
BIN = viewer
-SRCS = viewer.cpp shader_utils.cpp
+SRCS = viewer.cpp shader_utils.cpp text.cpp
OBJS = $(SRCS:cpp=o)
$(BIN): $(OBJS)
diff --git a/viewer/draw.h b/viewer/draw.h
@@ -0,0 +1,23 @@
+#ifndef DRAW_H
+#define DRAW_H
+
+#include <GL/glew.h>
+
+struct point {
+ GLfloat x;
+ GLfloat y;
+ GLfloat s;
+ GLfloat t;
+};
+
+class drawable {
+public:
+ virtual void draw() = 0;
+};
+
+class idleable {
+public:
+ virtual void idle() = 0;
+};
+
+#endif
diff --git a/viewer/shader_utils.cpp b/viewer/shader_utils.cpp
@@ -8,6 +8,41 @@
#include <stdlib.h>
#include <GL/glew.h>
+#include "shader_utils.h"
+
+char* file_read(const char* filename);
+void print_log(GLuint object);
+GLuint create_shader(const char* filename, GLenum type);
+GLuint create_program(const char* vertexfile, const char *fragmentfile);
+GLuint create_gs_program(const char* vertexfile, const char *geometryfile, const char *fragmentfile, GLint input, GLint output, GLint vertices);
+GLint get_attrib(GLuint program, const char *name);
+GLint get_uniform(GLuint program, const char *name);
+
+shader::shader()
+{
+ program = create_program("text.v.glsl", "text.f.glsl");
+ if(program == 0)
+ exit(1);
+
+ attribute_coord = get_attrib(program, "coord");
+ uniform_tex = get_uniform(program, "tex");
+ uniform_color = get_uniform(program, "color");
+
+ if(attribute_coord == -1 || uniform_tex == -1 || uniform_color == -1)
+ exit(1);
+}
+
+void
+shader::use()
+{
+ glUseProgram(program);
+}
+
+shader::~shader()
+{
+ glDeleteProgram(program);
+}
+
/**
* Store all the file's contents in memory, useful to pass shaders
* source code to OpenGL
diff --git a/viewer/shader_utils.h b/viewer/shader_utils.h
@@ -1,16 +1,18 @@
-/**
- * From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/OpenGL_Programming
- * This file is in the public domain.
- * Contributors: Sylvain Beucler
- */
#ifndef _CREATE_SHADER_H
#define _CREATE_SHADER_H
#include <GL/glew.h>
-char* file_read(const char* filename);
-void print_log(GLuint object);
-GLuint create_shader(const char* filename, GLenum type);
-GLuint create_program(const char* vertexfile, const char *fragmentfile);
-GLuint create_gs_program(const char* vertexfile, const char *geometryfile, const char *fragmentfile, GLint input, GLint output, GLint vertices);
-GLint get_attrib(GLuint program, const char *name);
-GLint get_uniform(GLuint program, const char *name);
+
+class shader {
+public:
+ shader();
+ void use();
+ ~shader();
+
+ GLint attribute_coord;
+ GLint uniform_tex;
+ GLint uniform_color;
+private:
+ GLuint program;
+};
+
#endif
diff --git a/viewer/text.cpp b/viewer/text.cpp
@@ -0,0 +1,136 @@
+#include <iostream>
+#include <vector>
+
+#include "text.h"
+
+text::text()
+{
+ font_file_name = "DejaVuSansMono.ttf";
+
+ /* Initialize the FreeType2 library */
+ if (FT_Init_FreeType(&ft)) {
+ std::cerr << "Could not init freetype library" << std::endl;
+ exit(1);
+ }
+
+ /* Load a font */
+ if (FT_New_Face(ft, font_file_name.c_str(), 0, &face)) {
+ std::cerr << "Could not open font " << font_file_name << std::endl;
+ exit(1);
+ }
+
+ g = face->glyph;
+ glGenBuffers(1, &vbo);
+}
+
+/**
+ * Render text using the currently loaded font and currently set font size.
+ * Rendering starts at coordinates (x, y), z is always 0.
+ * The pixel coordinates that the FreeType2 library uses are scaled by (sx, sy).
+ */
+void
+text::render_text(const char *text, float x, float y, float sx, float sy)
+{
+ const char *p;
+
+ /* Create a texture that will be used to hold one "glyph" */
+ GLuint tex;
+
+ glActiveTexture(GL_TEXTURE0);
+ glGenTextures(1, &tex);
+ glBindTexture(GL_TEXTURE_2D, tex);
+ glUniform1i(text_shader.uniform_tex, 0);
+
+ /* We require 1 byte alignment when uploading texture data */
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+
+ /* Clamping to edges is important to prevent artifacts when scaling */
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+ /* Linear filtering usually looks best for text */
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ /* Set up the VBO for our vertex data */
+ glEnableVertexAttribArray(text_shader.attribute_coord);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glVertexAttribPointer(text_shader.attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0);
+
+ /* Loop through all characters */
+ for (p = text; *p; p++) {
+ /* Try to load and render the character */
+ if (FT_Load_Char(face, *p, FT_LOAD_RENDER))
+ continue;
+
+ /* Upload the "bitmap", which contains an 8-bit grayscale image, as an alpha texture */
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
+
+ /* Calculate the vertex and texture coordinates */
+ float x2 = x + g->bitmap_left * sx;
+ float y2 = -y - g->bitmap_top * sy;
+ float w = g->bitmap.width * sx;
+ float h = g->bitmap.rows * sy;
+
+ point box[4] = {
+ {x2, -y2, 0, 0},
+ {x2 + w, -y2, 1, 0},
+ {x2, -y2 - h, 0, 1},
+ {x2 + w, -y2 - h, 1, 1},
+ };
+
+ /* Draw the character on the screen */
+ glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+ /* Advance the cursor to the start of the next character */
+ x += (g->advance.x >> 6) * sx;
+ y += (g->advance.y >> 6) * sy;
+ }
+
+ glDisableVertexAttribArray(text_shader.attribute_coord);
+ glDeleteTextures(1, &tex);
+}
+
+void
+text::draw()
+{
+ float sx = 2.0 / glutGet(GLUT_WINDOW_WIDTH);
+ float sy = 2.0 / glutGet(GLUT_WINDOW_HEIGHT);
+
+ // glUseProgram(program);
+ text_shader.use();
+
+ GLfloat black[4] = { 0, 0, 0, 1 };
+ GLfloat red[4] = { 1, 0, 0, 1 };
+ GLfloat transparent_green[4] = { 0, 1, 0, 0.5 };
+
+ /* Set font size to 48 pixels, color to black */
+ FT_Set_Pixel_Sizes(face, 0, 48);
+ glUniform4fv(text_shader.uniform_color, 1, black);
+
+ /* Effects of alignment */
+ render_text("The Quick Brown Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy);
+ render_text("The Misaligned Fox Jumps Over The Lazy Dog", -1 + 8.5 * sx, 1 - 100.5 * sy, sx, sy);
+
+ /* Scaling the texture versus changing the font size */
+ render_text("The Small Texture Scaled Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 175 * sy, sx * 0.5, sy * 0.5);
+ FT_Set_Pixel_Sizes(face, 0, 24);
+ render_text("The Small Font Sized Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 200 * sy, sx, sy);
+ FT_Set_Pixel_Sizes(face, 0, 48);
+ render_text("The Tiny Texture Scaled Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 235 * sy, sx * 0.25, sy * 0.25);
+ FT_Set_Pixel_Sizes(face, 0, 12);
+ render_text("The Tiny Font Sized Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 250 * sy, sx, sy);
+ FT_Set_Pixel_Sizes(face, 0, 48);
+
+ /* Colors and transparency */
+ render_text("The Solid Black Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 430 * sy, sx, sy);
+
+ glUniform4fv(text_shader.uniform_color, 1, red);
+ render_text("The Solid Red Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 330 * sy, sx, sy);
+ render_text("The Solid Red Fox Jumps Over The Lazy Dog", -1 + 28 * sx, 1 - 450 * sy, sx, sy);
+
+ glUniform4fv(text_shader.uniform_color, 1, transparent_green);
+ render_text("The Transparent Green Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 380 * sy, sx, sy);
+ render_text("The Transparent Green Fox Jumps Over The Lazy Dog", -1 + 18 * sx, 1 - 440 * sy, sx, sy);
+}
diff --git a/viewer/text.h b/viewer/text.h
@@ -0,0 +1,28 @@
+#ifndef TEXT_H
+#define TEXT_H
+
+#include <GL/glew.h>
+#include <GL/freeglut.h>
+
+#include <ft2build.h>
+#include FT_FREETYPE_H
+
+#include "shader_utils.h"
+#include "draw.h"
+
+class text : public drawable {
+public:
+ text();
+ void draw();
+private:
+ std::string font_file_name;
+ FT_Library ft;
+ FT_Face face;
+ FT_GlyphSlot g;
+ GLuint vbo;
+ shader text_shader;
+
+ void render_text(const char *, float x, float y, float sx, float sy);
+};
+
+#endif
diff --git a/viewer/viewer.cpp b/viewer/viewer.cpp
@@ -1,6 +1,5 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <math.h>
+#include <iostream>
+#include <vector>
#include <GL/glew.h>
#include <GL/freeglut.h>
@@ -10,216 +9,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
-#include <ft2build.h>
-#include FT_FREETYPE_H
-
-#include <iostream>
-#include <vector>
-
-#include "shader_utils.h"
-
-struct point {
- GLfloat x;
- GLfloat y;
- GLfloat s;
- GLfloat t;
-};
-
-class drawable {
-public:
- virtual void draw() = 0;
-};
-
-class idleable {
-public:
- virtual void idle() = 0;
-};
-
-class shader {
-public:
- shader();
- void use();
- ~shader();
-
- GLint attribute_coord;
- GLint uniform_tex;
- GLint uniform_color;
-private:
- GLuint program;
-};
-
-shader::shader()
-{
- program = create_program("text.v.glsl", "text.f.glsl");
- if(program == 0)
- exit(1);
-
- attribute_coord = get_attrib(program, "coord");
- uniform_tex = get_uniform(program, "tex");
- uniform_color = get_uniform(program, "color");
-
- if(attribute_coord == -1 || uniform_tex == -1 || uniform_color == -1)
- exit(1);
-}
-
-void
-shader::use()
-{
- glUseProgram(program);
-}
-
-shader::~shader()
-{
- glDeleteProgram(program);
-}
-
-
-class text : public drawable {
-public:
- text();
- void draw();
-private:
- std::string font_file_name;
- FT_Library ft;
- FT_Face face;
- FT_GlyphSlot g;
- GLuint vbo;
- shader text_shader;
-
- void render_text(const char *, float x, float y, float sx, float sy);
-};
-
-text::text()
-{
- font_file_name = "DejaVuSansMono.ttf";
-
- /* Initialize the FreeType2 library */
- if (FT_Init_FreeType(&ft)) {
- std::cerr << "Could not init freetype library" << std::endl;
- exit(1);
- }
-
- /* Load a font */
- if (FT_New_Face(ft, font_file_name.c_str(), 0, &face)) {
- std::cerr << "Could not open font " << font_file_name << std::endl;
- exit(1);
- }
-
- g = face->glyph;
- glGenBuffers(1, &vbo);
-}
-
-/**
- * Render text using the currently loaded font and currently set font size.
- * Rendering starts at coordinates (x, y), z is always 0.
- * The pixel coordinates that the FreeType2 library uses are scaled by (sx, sy).
- */
-void
-text::render_text(const char *text, float x, float y, float sx, float sy)
-{
- const char *p;
-
- /* Create a texture that will be used to hold one "glyph" */
- GLuint tex;
-
- glActiveTexture(GL_TEXTURE0);
- glGenTextures(1, &tex);
- glBindTexture(GL_TEXTURE_2D, tex);
- glUniform1i(text_shader.uniform_tex, 0);
-
- /* We require 1 byte alignment when uploading texture data */
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
- /* Clamping to edges is important to prevent artifacts when scaling */
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
- /* Linear filtering usually looks best for text */
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
- /* Set up the VBO for our vertex data */
- glEnableVertexAttribArray(text_shader.attribute_coord);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glVertexAttribPointer(text_shader.attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0);
-
- /* Loop through all characters */
- for (p = text; *p; p++) {
- /* Try to load and render the character */
- if (FT_Load_Char(face, *p, FT_LOAD_RENDER))
- continue;
-
- /* Upload the "bitmap", which contains an 8-bit grayscale image, as an alpha texture */
- glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
-
- /* Calculate the vertex and texture coordinates */
- float x2 = x + g->bitmap_left * sx;
- float y2 = -y - g->bitmap_top * sy;
- float w = g->bitmap.width * sx;
- float h = g->bitmap.rows * sy;
-
- point box[4] = {
- {x2, -y2, 0, 0},
- {x2 + w, -y2, 1, 0},
- {x2, -y2 - h, 0, 1},
- {x2 + w, -y2 - h, 1, 1},
- };
-
- /* Draw the character on the screen */
- glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
-
- /* Advance the cursor to the start of the next character */
- x += (g->advance.x >> 6) * sx;
- y += (g->advance.y >> 6) * sy;
- }
-
- glDisableVertexAttribArray(text_shader.attribute_coord);
- glDeleteTextures(1, &tex);
-}
-
-void
-text::draw()
-{
- float sx = 2.0 / glutGet(GLUT_WINDOW_WIDTH);
- float sy = 2.0 / glutGet(GLUT_WINDOW_HEIGHT);
-
- // glUseProgram(program);
- text_shader.use();
-
- GLfloat black[4] = { 0, 0, 0, 1 };
- GLfloat red[4] = { 1, 0, 0, 1 };
- GLfloat transparent_green[4] = { 0, 1, 0, 0.5 };
-
- /* Set font size to 48 pixels, color to black */
- FT_Set_Pixel_Sizes(face, 0, 48);
- glUniform4fv(text_shader.uniform_color, 1, black);
-
- /* Effects of alignment */
- render_text("The Quick Brown Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy);
- render_text("The Misaligned Fox Jumps Over The Lazy Dog", -1 + 8.5 * sx, 1 - 100.5 * sy, sx, sy);
-
- /* Scaling the texture versus changing the font size */
- render_text("The Small Texture Scaled Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 175 * sy, sx * 0.5, sy * 0.5);
- FT_Set_Pixel_Sizes(face, 0, 24);
- render_text("The Small Font Sized Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 200 * sy, sx, sy);
- FT_Set_Pixel_Sizes(face, 0, 48);
- render_text("The Tiny Texture Scaled Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 235 * sy, sx * 0.25, sy * 0.25);
- FT_Set_Pixel_Sizes(face, 0, 12);
- render_text("The Tiny Font Sized Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 250 * sy, sx, sy);
- FT_Set_Pixel_Sizes(face, 0, 48);
-
- /* Colors and transparency */
- render_text("The Solid Black Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 430 * sy, sx, sy);
-
- glUniform4fv(text_shader.uniform_color, 1, red);
- render_text("The Solid Red Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 330 * sy, sx, sy);
- render_text("The Solid Red Fox Jumps Over The Lazy Dog", -1 + 28 * sx, 1 - 450 * sy, sx, sy);
-
- glUniform4fv(text_shader.uniform_color, 1, transparent_green);
- render_text("The Transparent Green Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 380 * sy, sx, sy);
- render_text("The Transparent Green Fox Jumps Over The Lazy Dog", -1 + 18 * sx, 1 - 440 * sy, sx, sy);
-}
+#include "text.h"
class window {
public:
@@ -285,7 +75,7 @@ window::display(void)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- for(auto &d : drawables)
+ for(auto &d : drawables)
d->draw();
glutSwapBuffers();
@@ -295,6 +85,9 @@ void
window::idle(void)
{
// printf("idling!\n");
+
+ for(auto &i : idleables)
+ i->idle();
}
int main(int argc, char *argv[]) {