Skip to content

Commit

Permalink
setup simple 2d rendering pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed Jul 20, 2017
1 parent 3bb2335 commit a156db6
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 423 deletions.
2 changes: 1 addition & 1 deletion examples/charts/Makefile.am
@@ -1,4 +1,4 @@
noinst_PROGRAMS = example_linechart

example_linechart_SOURCES = linechart.cc
example_linechart_LDADD = -lshowtime
example_linechart_LDADD = -lshowtime -lX11 -lGL -lglfw -lGLEW
23 changes: 19 additions & 4 deletions examples/charts/linechart.cc
Expand Up @@ -8,6 +8,7 @@
* <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <unistd.h>
#include <showtime/showtime.h>
#include "showtime/extra/chart/axisdefinition.h"
#include "showtime/extra/chart/areachart.h"
Expand All @@ -17,7 +18,11 @@
#include "showtime/extra/chart/linechart.h"
#include "showtime/extra/chart/pointchart.h"
#include "showtime/extra/chart/series.h"
#include "showtime/graphics/layer_svg.h"
#include "showtime/graphics/canvas_svg.h"
#include "showtime/platform/opengl/opengl_context.h"
#include "showtime/platform/opengl/canvas_opengl.h"
#include "showtime/platform/x11/image_display.h"


using namespace cplot;

Expand Down Expand Up @@ -51,11 +56,21 @@ int main() {
line_chart->addAxis(AxisDefinition::BOTTOM);
line_chart->addAxis(AxisDefinition::LEFT);

std::string svg;
SVGTarget target(&svg);
std::unique_ptr<showtime::opengl::GLContext> ctx;
{
auto rc = showtime::opengl::GLContext::createWindow(&ctx);
if (!rc.isSuccess()) {
std::cerr << "ERROR: " << rc.getMessage() << std::endl;
return EXIT_FAILURE;
}
}

showtime::opengl::Canvas2D target(ctx.get());
canvas.render(&target);
target.flush();

std::cout << svg << std::endl;
ctx->swap();
::usleep(5 * 1000 * 1000);

return 0;
}
12 changes: 11 additions & 1 deletion src/showtime/Makefile.am
Expand Up @@ -34,9 +34,17 @@ libshowtime_a_SOURCES = \
extra/chart/timedomain.cc \
extra/chart/timedomain.h \
extra/chart/viewport.h \
graphics/camera.h \
graphics/camera.cc \
graphics/canvas.h \
graphics/canvas.cc \
graphics/canvas_svg.h \
platform/opengl/canvas_opengl.h \
platform/opengl/canvas_opengl.cc \
platform/opengl/opengl_context.h \
platform/opengl/opengl_context.cc \
platform/opengl/opengl_shader.h \
platform/opengl/opengl_shader.cc \
platform/x11/image_display.h \
platform/x11/image_display.cc \
showtime.h \
Expand All @@ -55,4 +63,6 @@ libshowtime_a_SOURCES = \
util/stringutil.h \
util/stringutil.cc \
util/time.h \
util/time.cc
util/time.cc \
util/vmath.h \
util/vmath.cc
42 changes: 34 additions & 8 deletions core/plg/scene/Camera.cpp → src/showtime/graphics/camera.cc
Expand Up @@ -12,9 +12,38 @@
#include <string.h>
#include <cstdio>
#include <math.h>
#include <plg.h>
#include <plg/vmath.h>
#include <plg/scene/Camera.h>
#include "showtime/graphics/camera.h"

namespace showtime {

// http://en.wikipedia.org/wiki/Orthographic_projection_(geometry)
mat4 Camera::getOrthoProjectionMatrix(
float left,
float right,
float top,
float bottom,
float near,
float far) {
mat4 mat;
auto m = (float*) &mat;
m[0x0] = 2.0f * (1.0f / (right - left));
m[0x1] = 0.0f;
m[0x2] = 0.0f;
m[0x3] = 0.0f;
m[0x4] = 0.0f;
m[0x5] = 2.0 * (1.0f / (top - bottom));
m[0x6] = 0.0f;
m[0x7] = 0.0f;
m[0x8] = 0.0f;
m[0x9] = 0.0f;
m[0xa] = -2.0f * (1.0f / (far - near));
m[0xb] = 0.0f;
m[0xc] = -(right + left) * (1.0f / (right - left));
m[0xd] = -(top + bottom) * (1.0f / (top - bottom));
m[0xe] = -(far + near) * (1.0f / (far - near));
m[0xf] = 1.0f;
return mat;
}

/**
* For extended documentation see "Camera.h"
Expand Down Expand Up @@ -47,11 +76,6 @@ Camera::Camera() {
Camera::~Camera() {
}

void Camera::activate() {
Frame* frame = PLG_CURRENT_FRAME;
frame->setCamera(this);
}

vec3* Camera::getWorldPosition() {
return &w_pos_;
}
Expand Down Expand Up @@ -152,3 +176,5 @@ void Camera::mkViewMatrix() {
view_mat_ver_++;
}

} // namespace showtime

24 changes: 14 additions & 10 deletions core/plg/scene/Camera.h → src/showtime/graphics/camera.h
Expand Up @@ -8,10 +8,10 @@
* copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <plg/vmath.h>
#pragma once
#include "showtime/util/vmath.h"

#ifndef CAMERA_H
#define CAMERA_H
namespace showtime {

/**
* The camera class. For a first person perspective the local position should
Expand All @@ -37,6 +37,16 @@ class Camera {

public:

// Returns an orthographic projection matrix
// http://en.wikipedia.org/wiki/Orthographic_projection_(geometry)
static mat4 getOrthoProjectionMatrix(
float clip_left,
float clip_right,
float clip_top,
float clip_bottom,
float near,
float far);

/**
* Initialize a new camera
*/
Expand All @@ -47,12 +57,6 @@ class Camera {
*/
~Camera();

/**
* Make this camera the active camera for the current frame. Must be called
* on every frame!
*/
void activate();

/**
* Get the world position of this camera (translation applied after rotation)
*/
Expand Down Expand Up @@ -123,5 +127,5 @@ class Camera {

};

} // namespace showtime

#endif
47 changes: 30 additions & 17 deletions src/showtime/platform/opengl/canvas_opengl.cc
Expand Up @@ -7,8 +7,11 @@
* copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "showtime/graphics/camera.h"
#include "showtime/platform/opengl/canvas_opengl.h"
#include "showtime/platform/opengl/opengl_context.h"
#include "showtime/platform/opengl/opengl_shader.h"

namespace showtime {
namespace opengl {
Expand All @@ -27,7 +30,7 @@ static const std::string kFragmentShader = R"(
#version 400
out vec4 frag_colour;
void main() {
frag_colour = vec4(1.0, 1.0, 1.0, 1.0);
frag_colour = vec4(0.0, 0.0, 0.0, 1.0);
}
)";

Expand Down Expand Up @@ -95,6 +98,8 @@ void Canvas2D::flush() {
50.0f, 200.0f, 0.0f
};

mat4 orthomatrix = showtime::Camera::getOrthoProjectionMatrix(0, 1300, 0, 600, -1, 1);

ctx_->activate();

// FIXME reuse vbo
Expand All @@ -116,30 +121,38 @@ void Canvas2D::flush() {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

// FIXME reuse shaders
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
{
auto shader_src = kVertexShader.c_str();
glShaderSource(vs, 1, &shader_src, NULL);
glCompileShader(vs);
GLShaderBuilder shader_builder;
if (!shader_builder.addVertexShader(kVertexShader)){
std::cerr << "cant' build vertex shader" << std::endl;
return;
}

GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
{
auto shader_src = kFragmentShader.c_str();
glShaderSource(fs, 1, &shader_src, NULL);
glCompileShader(fs);
if (!shader_builder.addFragmentShader(kFragmentShader)){
std::cerr << "cant' build fragment shader" << std::endl;
return;
}

GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
auto shader = shader_builder.link();
if (!shader) {
std::cerr << "cant' build fragment shader" << std::endl;
return;
}

/* draw */
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_programme);

shader.get().activate();
auto loc = shader.get().getUniformLocation("in_proj");
if (!loc) {
std::cerr << "cant find in_proj: " << loc.getMessage() << std::endl;
return;
}

shader.get().setUniform(loc.get(), orthomatrix);

glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, drawbuf_.size() / 3);
glDrawArrays(GL_TRIANGLES, 0, 3);

drawbuf_.clear();
}
Expand Down
6 changes: 3 additions & 3 deletions src/showtime/platform/opengl/opengl_context.cc
Expand Up @@ -17,7 +17,7 @@ ReturnCode GLContext::createWindow(std::unique_ptr<GLContext>* context) {
uint32_t window_height = 600;

if (!glfwInit()) {
return ReturnCode::error("EIO", "ERROR: glfw_init() failed");
return ReturnCode::error("ERROR: glfw_init() failed");
}

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Expand All @@ -29,14 +29,14 @@ ReturnCode GLContext::createWindow(std::unique_ptr<GLContext>* context) {

auto window = glfwCreateWindow(window_width, window_height, "linechart", NULL, NULL);
if (!window){
return ReturnCode::error("EIO", "ERROR: glfwCreateWindow() failed");
return ReturnCode::error("ERROR: glfwCreateWindow() failed");
}

glfwMakeContextCurrent(window);

glewExperimental = true;
if (glewInit() != GLEW_OK) {
return ReturnCode::error("EIO", "ERROR: glew_init failed() failed");
return ReturnCode::error("ERROR: glew_init failed() failed");
}

context->reset(new GLContext(window));
Expand Down

0 comments on commit a156db6

Please sign in to comment.