Created
January 31, 2016 12:10
-
-
Save iondune/83381a2fd1f30a47f2b3 to your computer and use it in GitHub Desktop.
Slightly more advanced ionGraphics demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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{ | |
| // Position Color | |
| 0.5f, 0.5f, 1, 0, 0, | |
| 0.5f, -0.5f, 0, 1, 0, | |
| -0.5f, -0.5f, 0, 0, 1, | |
| -0.5f, 0.5f, 0, 1, 1, | |
| }; | |
| vector<u32> const Indices{ | |
| 0, 1, 2, | |
| 0, 2, 3 | |
| }; | |
| 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; | |
| in vec3 vColor; | |
| out vec3 fColor; | |
| void main() | |
| { | |
| gl_Position = vec4(vPosition, 0.0, 1.0); | |
| fColor = vColor; | |
| } | |
| )SHADER"; | |
| string const FragmentShaderSource = R"SHADER( | |
| #version 130 | |
| in vec3 fColor; | |
| out vec4 outColor; | |
| void main() | |
| { | |
| outColor = vec4(fColor, 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 }, | |
| { "vColor", 3, 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->Run()) | |
| { | |
| 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