Skip to content

Instantly share code, notes, and snippets.

@iondune
Last active January 31, 2016 12:10
Show Gist options
  • Select an option

  • Save iondune/afef85631d896b9a3107 to your computer and use it in GitHub Desktop.

Select an option

Save iondune/afef85631d896b9a3107 to your computer and use it in GitHub Desktop.
Minimal ionGraphics demo using OpenGL
#include <ionWindow.h>
#include <ionGraphics.h>
#include <ionGraphicsGL.h>
using namespace ion::Graphics;
int main()
{
Log::AddDefaultOutputs();
SingletonPointer<CWindowManager> WindowManager;
WindowManager->Init();
CWindow * Window = WindowManager->CreateWindow(vec2i(640, 480), "TestGL", EWindowType::Windowed);
//////////////////
// Buffer Setup //
//////////////////
vector<f32> const Vertices{
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
};
vector<u32> const Indices{
0, 1, 2
};
IGraphicsAPI * GraphicsAPI = new COpenGLAPI();
IVertexBuffer * VertexBuffer = GraphicsAPI->CreateVertexBuffer(Vertices.data(), Vertices.size());
IIndexBuffer * IndexBuffer = GraphicsAPI->CreateIndexBuffer(Indices.data(), Indices.size(), EValueType::UnsignedInt32);
//////////////////
// Shader Setup //
//////////////////
string const VertexShaderSource = R"SHADER(
#version 130
in vec2 vPosition;
void main()
{
gl_Position = vec4(vPosition, 0.0, 1.0);
}
)SHADER";
string const FragmentShaderSource = R"SHADER(
#version 130
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
)SHADER";
IVertexShader * VertexShader = GraphicsAPI->CreateVertexShaderFromSource(VertexShaderSource);
IPixelShader * PixelShader = GraphicsAPI->CreatePixelShaderFromSource(FragmentShaderSource);
if (! VertexShader)
std::cerr << "Failed to compile vertex shader!" << std::endl;
if (! PixelShader)
std::cerr << "Failed to compile pixel shader!" << std::endl;
IShaderProgram * ShaderProgram = GraphicsAPI->CreateShaderProgram();
ShaderProgram->SetVertexStage(VertexShader);
ShaderProgram->SetPixelStage(PixelShader);
SInputLayoutElement InputLayout[] =
{
{ "vPosition", 2, EValueType::Float },
};
ShaderProgram->SetInputLayout(InputLayout, ION_ARRAYSIZE(InputLayout));
///////////////
// Draw Loop //
///////////////
IPipelineState * PipelineState = GraphicsAPI->CreatePipelineState();
PipelineState->SetIndexBuffer(IndexBuffer);
PipelineState->SetVertexBuffer(VertexBuffer);
PipelineState->SetProgram(ShaderProgram);
IRenderTarget * RenderTarget = GraphicsAPI->GetWindowBackBuffer(Window);
while (! WindowManager->ShouldClose())
{
WindowManager->PollEvents();
RenderTarget->ClearColor();
RenderTarget->ClearDepth();
GraphicsAPI->Draw(PipelineState);
Window->SwapBuffers();
}
/////////////
// Cleanup //
/////////////
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment