Copyright 2008-2014 Matus Chochlik. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <cassert>
#include <iostream>
#include <fstream>
#define EGL_CONTEXT_MAJOR_VERSION 0x3098
#define EGL_CONTEXT_MINOR_VERSION 0x30FB
#define EGL_CONTEXT_FLAGS 0x30FC
#define EGL_CONTEXT_OPENGL_PROFILE_MASK 0x30FD
#define EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY 0x31BD
#define EGL_OPENGL_ES3_BIT 0x0040
#define EGL_NO_RESET_NOTIFICATION 0x31BE
#define EGL_LOSE_CONTEXT_ON_RESET 0x31BF
#define EGL_CONTEXT_OPENGL_DEBUG_BIT 0x00000001
#define EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT 0x00000002
#define EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT 0x00000004
#define EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT 0x00000001
#define EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT 0x00000002
void render_frame(void)
{
using namespace oglplus;
vs.Source(" \
#version 120\n \
attribute vec3 Position; \
void main(void) \
{ \
gl_Position = vec4(Position, 1.0); \
} \
");
vs.Compile();
fs.Source(" \
#version 120\n \
void main(void) \
{ \
gl_FragColor = vec4(0.0, 1.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
};
verts.Bind(Buffer::Target::Array);
Buffer::Data(
Buffer::Target::Array,
9,
triangle_verts
);
VertexArrayAttrib vert_attr(prog, "Position");
vert_attr.Setup<
Vec3f>();
vert_attr.Enable();
gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.Clear().ColorBuffer();
}
void save_frame(unsigned width, unsigned height, const char* screenshot_path)
{
std::vector<char> pixels(width * height * 3);
glReadPixels(
0, 0,
width,
height,
GL_RGB,
GL_UNSIGNED_BYTE,
pixels.data()
);
std::ofstream output(screenshot_path);
output.write(pixels.data(), pixels.size());
std::cout << screenshot_path << std::endl;
}
void make_screenshot(unsigned width, unsigned height, const char* screenshot_path)
{
using namespace eglplus;
display,
.Get()
);
Config config = configs.First();
display,
config,
.Get()
);
display,
config,
.Get()
);
context.MakeCurrent(surface);
oglplus::GLAPIInitializer api_init;
render_frame();
context.WaitClient();
save_frame(width, height, screenshot_path);
}
int main(int argc, char* argv[])
{
try
{
glGetError();
make_screenshot(
800,
600,
(argc>1)?argv[1]:"screenshot.rgb"
);
return 0;
}
{
std::cerr
<< "OGLplus error (in "
<< "'): "
<< oe.what()
<< " ["
<< ":"
<< "] "
<< std::endl;
}
catch(eglplus::Error& ee)
{
std::cerr
<< "EGLplus error (in "
<< ee.EGLFunc()
<< ") "
<< ee.what()
<< " ["
<< ee.SourceFile()
<< ":"
<< ee.SourceLine()
<< "] "
<< std::endl;
}
catch(std::exception& se)
{
std::cerr << "Error: " << se.what();
std::cerr << std::endl;
}
return 1;
}