Skip to content

Commit

Permalink
renderer: rendering quad with opengl 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJJ committed Jul 31, 2015
1 parent bf6dfcc commit 9f214c7
Show file tree
Hide file tree
Showing 26 changed files with 752 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Technology | Component
**Python** | Scripting, media conversion, in-game console, code generation
**Cython** | Glue code
**CMake** | Build system
**OpenGL2.1** | Rendering, shaders
**OpenGL3.3** | Rendering, shaders
**SDL2** | Cross-platform Audio/Input/Window handling
**Opus** | Audio codec
**Humans** | Mixing together all of the above
Expand Down
13 changes: 7 additions & 6 deletions libopenage/renderer/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "../config.h"
#include "../log/log.h"
#include "../util/error.h"
#include "../error/error.h"

#if WITH_OPENGL
#include "opengl/context.h"
Expand Down Expand Up @@ -33,13 +33,13 @@ std::unique_ptr<Context> Context::generate(context_type t) {
ctx_requested = context_type::opengl;
}
else {
throw util::Error(MSG(err) << "No render context available!");
throw Error{MSG(err) << "No render context available!"};
}
}

if (ctx_requested == context_type::opengl) {
if (not WITH_OPENGL) {
throw util::Error(MSG(err) << "OpenGL support not enabled!");
throw Error{MSG(err) << "OpenGL support not enabled!"};
}
#if WITH_OPENGL
log::log(MSG(dbg) << "Using OpenGL context...");
Expand All @@ -48,18 +48,19 @@ std::unique_ptr<Context> Context::generate(context_type t) {
}
else if (ctx_requested == context_type::vulkan) {
if (not WITH_VULKAN) {
throw util::Error(MSG(err) << "Vulkan support not enabled!");
throw Error{MSG(err) << "Vulkan support not enabled!"};
}
#if WITH_VULKAN
log::log(MSG(dbg) << "Using Vulkan context...");
return std::make_unique<vulkan::Context>();
#endif
}
else {
throw util::Error(MSG(err) << "Unknown context type requested!");
throw Error{MSG(err) << "Unknown context type requested!"};
}

throw util::Error(MSG(err) << "Context creation dead code reached! Veeery bad.");
throw Error{MSG(err) << "Context creation dead code reached! Veeery bad."};
return nullptr;
}

}} // namespace openage::renderer
16 changes: 16 additions & 0 deletions libopenage/renderer/material.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015-2015 the openage authors. See copying.md for legal info.

#ifndef OPENAGE_RENDERER_MATERIAL_H_
#define OPENAGE_RENDERER_MATERIAL_H_

namespace openage {
namespace renderer {


class material {
Shader code;
};

}} // namespace openage::renderer

#endif
24 changes: 24 additions & 0 deletions libopenage/renderer/object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2015-2015 the openage authors. See copying.md for legal info.

#ifndef OPENAGE_RENDERER_OBJECT_H_
#define OPENAGE_RENDERER_OBJECT_H_


namespace openage {
namespace renderer {


class Object {
public:
Object();
~Object();

private:
std::string name;

position -> Texture
};
}
}

#endif
2 changes: 2 additions & 0 deletions libopenage/renderer/opengl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
add_sources(libopenage
context.cpp
program.cpp
shader.cpp
)
20 changes: 11 additions & 9 deletions libopenage/renderer/opengl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
#include <SDL2/SDL.h>

#include "../../log/log.h"
#include "../../util/error.h"
#include "../../error/error.h"

namespace openage {
namespace renderer {
namespace opengl {

// TODO: get max available gl version
constexpr int opengl_version_major = 2;
constexpr int opengl_version_minor = 1;
constexpr int opengl_version_major = 3;
constexpr int opengl_version_minor = 3;

Context::Context() {}
Context::~Context() {}
Expand All @@ -39,16 +39,18 @@ void Context::create(SDL_Window *window) {
this->glcontext = SDL_GL_CreateContext(window);

if (this->glcontext == nullptr) {
throw util::Error(MSG(err) << "Failed creating OpenGL context: " << SDL_GetError());
throw Error(MSG(err) << "Failed creating OpenGL context: " << SDL_GetError());
}

// check the OpenGL version, for shaders n stuff
int epoxy_glv = opengl_version_major * 10 + opengl_version_minor;
if (not epoxy_is_desktop_gl() or epoxy_gl_version() < epoxy_glv) {
throw util::Error(MSG(err) << "OpenGL "
<< opengl_version_major << "." << opengl_version_minor
<< " not available");
throw Error(MSG(err) << "OpenGL "
<< opengl_version_major << "." << opengl_version_minor
<< " not available");
}

log::log(MSG(info) << "Using OpenGL " << opengl_version_major << "." << opengl_version_minor);
}

void Context::setup() {
Expand All @@ -59,14 +61,14 @@ void Context::setup() {
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
log::log(MSG(dbg) << "Maximum supported texture size: " << max_texture_size);
if (max_texture_size < 1024) {
throw util::Error(MSG(err) << "Maximum supported texture size too small: " << max_texture_size);
throw Error(MSG(err) << "Maximum supported texture size too small: " << max_texture_size);
}

int max_texture_units;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_texture_units);
log::log(MSG(dbg) << "Maximum supported texture units: " << max_texture_units);
if (max_texture_units < 2) {
throw util::Error(MSG(err) << "Your GPU has too less texture units: " << max_texture_units);
throw Error(MSG(err) << "Your GPU has too less texture units: " << max_texture_units);
}

// vsync on
Expand Down
177 changes: 177 additions & 0 deletions libopenage/renderer/opengl/program.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright 2013-2015 the openage authors. See copying.md for legal info.

#include "program.h"

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

#include "../../log/log.h"
#include "../../error/error.h"
#include "../../util/compiler.h"
#include "../../util/file.h"
#include "../../util/strings.h"

namespace openage {
namespace renderer {
namespace opengl {

Program::Program()
:
is_linked{false} {

this->id = glCreateProgram();
}

Program::~Program() {
glDeleteProgram(this->id);
}

void Program::attach_shader(const Shader &shader) {
if (unlikely(this->is_linked)) {
throw Error{MSG(err) << "can't attach shader to linked program", true};
}

this->shader_ids.push_back(shader.id);
glAttachShader(this->id, shader.id);
}

void Program::link() {
if (unlikely(this->is_linked)) {
throw Error{MSG(err) << "can't relink a program", true};
}

// link shaders to a program
glLinkProgram(this->id);
this->check(GL_LINK_STATUS);

// check if program is usable
glValidateProgram(this->id);
this->check(GL_VALIDATE_STATUS);

this->is_linked = true;
this->post_link_hook();

for (auto &id : this->shader_ids) {
glDetachShader(this->id, id);
}
}

void Program::check(GLenum what_to_check) {
GLint status;
glGetProgramiv(this->id, what_to_check, &status);

if (status != GL_TRUE) {
GLint loglen;
glGetProgramiv(this->id, GL_INFO_LOG_LENGTH, &loglen);

auto infolog = std::make_unique<char[]>(loglen);
glGetProgramInfoLog(this->id, loglen, NULL, infolog.get());

const char *what_str;
switch(what_to_check) {
case GL_LINK_STATUS:
what_str = "linking";
break;
case GL_VALIDATE_STATUS:
what_str = "validation";
break;
case GL_COMPILE_STATUS:
what_str = "compiliation";
break;
default:
what_str = "<unknown task>";
break;
}

throw Error{MSG(err) << "Program " << what_str << " failed:\n" << infolog, true};
}
}

void Program::use() {
if (unlikely(not this->is_linked)) {
throw Error{MSG(err) << "using none-linked program!", true};
}
glUseProgram(this->id);
}

void Program::stopusing() {
glUseProgram((GLuint) 0);
}

void Program::check_is_linked(const char *info) {
// just throw up when we're not linked yet.

if (unlikely(not this->is_linked)) {
throw Error{MSG(err) << info << " before program was linked!", true};
}
}

GLint Program::get_uniform_id(const char *name) {
this->check_is_linked("Uniform id requested");
return glGetUniformLocation(this->id, name);
}

GLint Program::get_uniformbuffer_id(const char *name) {
this->check_is_linked("Uniform buffer requested");
return glGetUniformBlockIndex(this->id, name);
}

GLint Program::get_attribute_id(const char *name) {
this->check_is_linked("Vertex attribute requested");

GLint aid = glGetAttribLocation(this->id, name);

if (unlikely(aid == -1)) {
this->dump_active_attributes();
throw Error{MSG(err) << "Attribute " << name
<< " queried but not found or active"
<< " (optimized out by the compiler?).", true};
}

return aid;
}

void Program::set_attribute_id(const char *name, GLuint id) {
if (unlikely(this->is_linked)) {
// TODO: maybe enable overwriting, but after that relink the program
throw Error{MSG(err)
<< "assigned attribute " << name << " = "
<< id << " after program was linked!", true};
}
else {
glBindAttribLocation(this->id, id, name);
}
}

void Program::dump_active_attributes() {
auto msg = MSG(info);
msg << "Dumping shader program " << this->id << " active attribute list:";

GLint num_attribs;
glGetProgramiv(this->id, GL_ACTIVE_ATTRIBUTES, &num_attribs);

GLint attrib_max_length;
glGetProgramiv(this->id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &attrib_max_length);

for (int i = 0; i < num_attribs; i++) {
GLsizei attrib_length;
GLint attrib_size;
GLenum attrib_type;
auto attrib_name = std::make_unique<char[]>(attrib_max_length);

glGetActiveAttrib(this->id, i, attrib_max_length, &attrib_length, &attrib_size, &attrib_type, attrib_name.get());

msg << "\n -> attribute " << attrib_name
<< ": type=" << attrib_type << ", size=" << attrib_size
<< ", id=" << this->get_attribute_id(attrib_name.get());
}

log::log(msg);
}


void Program::post_link_hook() {}

}}} // openage::renderer::opengl
Loading

0 comments on commit 9f214c7

Please sign in to comment.