PrevUpHomeNext

Chapter 1. Introduction

Table of Contents

Hello world

OGLplus is a header-only library which implements a thin object-oriented facade over the modern OpenGL® (version 3 and higher) C-language API. It provides wrappers which automate resource and object management and make the use of OpenGL in C++ safer, easier and more convenient.

The following code shows a basic, working example of usage of the OGLplus library, using the GLUT and GLEW libraries.

#include <cassert>
#include <iostream>

#include <GL/glew.h> 1
#include <GL/glut.h>

#include <oglplus/all.hpp> 2

1

Before including OGLplus a header declaring the GL API must be included.

2

Includes most of OGLplus excluding geometry and image data generators and optional utilities.

class Example
{
private:
	oglplus::Context gl; 1

	oglplus::VertexShader vs; 2

	oglplus::FragmentShader fs; 3

	oglplus::Program prog; 4

	oglplus::Buffer positions; 5

	oglplus::VertexArray triangle; 6
public:
	Example(void)
	{
		using namespace oglplus;

		vs.Source(
			"#version 330\n"
			"in vec3 Position;"
			"void main(void)"
			"{"
			"	gl_Position = vec4(Position, 1.0);"
			"}"
		);
		vs.Compile();

		fs.Source(
			"#version 330\n"
			"out vec4 fragColor;"
			"void main(void)"
			"{"
			"	fragColor = vec4(1.0, 0.0, 0.0, 1.0);"
			"}"
		);
		fs.Compile();

		prog.AttachShader(vs);
		prog.AttachShader(fs);

		prog.Link();
		prog.Use();

		triangle.Bind();

		GLfloat triangle_verts[9] = {
			0.0f, 0.0f, 0.0f,
			1.0f, 0.0f, 0.0f,
			0.0f, 1.0f, 0.0f
		};

		positions.Bind(BufferTarget::Array);
		Buffer::Data(
			BufferTarget::Array,
			9,
			triangle_verts
		);

		VertexArrayAttrib vert_attr(prog, "Position");
		vert_attr.Setup<GLfloat>(3);
		vert_attr.Enable();

		gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.ClearDepth(1.0f);
	}

	void Display(void)
	{
		using namespace oglplus;

		gl.Clear().ColorBuffer().DepthBuffer();

		gl.DrawArrays(PrimitiveType::Triangles, 0, 3);
	}
};

1

Stateless wrapper for GL context functions.

2

Vertex shader object.

3

Fragment shader object.

4

GPU rendering program object,

5

(Vertex) buffer object.

6

Vertex array object.

class SingleExample
{
private:
	static Example*& SingleInstance(void)
	{
		static Example* test = nullptr;
		return test;
	}

	SingleExample(const SingleExample&)/* = delete */;
public:

	SingleExample(void)
	{
		assert(!SingleInstance());
		SingleInstance() = new Example();
	}

	~SingleExample(void)
	{
		assert(SingleInstance());
		delete SingleInstance();
		SingleInstance() = nullptr;
	}

	static void Display(void)
	{
		assert(SingleInstance());
		SingleInstance()->Display();
		glutSwapBuffers();
	}
};
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(100,100);
	glutCreateWindow("OGLplus+GLUT+GLEW");

	if(glewInit() == GLEW_OK) try
	{
		glGetError();
		SingleExample example;
		glutDisplayFunc(&SingleExample::Display);
		glutMainLoop();
		return 0;
	}
	catch(oglplus::ProgramBuildError& pbe)
	{
		std::cerr
			<< "Program build error (in "
			<< pbe.GLFunc()
			<< "'): "
			<< pbe.what()
			<< " ["
			<< pbe.SourceFile()
			<< ":"
			<< pbe.SourceLine()
			<< "] "
			<< std::endl
			<< pbe.Log()
			<< std::endl;
	}
	catch(oglplus::Error& err)
	{
		std::cerr
			<< "Error (in "
			<< err.GLFunc()
			<< "'): "
			<< err.what()
			<< " ["
			<< err.SourceFile()
			<< ":"
			<< err.SourceLine()
			<< "] "
			<< std::endl;
	}
	return 1;
}

PrevUpHomeNext