-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38cb8b8
commit c14d473
Showing
76 changed files
with
6,774 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* HelloTriangle.c | ||
* | ||
* Copyright (c) 2015 Lukas Hermanns. All rights reserved. | ||
* Licensed under the terms of the BSD 3-Clause license (see LICENSE.txt). | ||
*/ | ||
|
||
#include <LLGL-C/LLGL.h> | ||
#include <stdio.h> | ||
|
||
|
||
#define ENABLE_MULTISAMPLING 1 | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
// Load render system module | ||
const char* rendererModule = "OpenGL"; | ||
if (llglLoadRenderSystem(rendererModule) == 0) | ||
{ | ||
fprintf(stderr, "Failed to load render system: %s\n", rendererModule); | ||
return 1; | ||
} | ||
|
||
// Create swap-chain | ||
LLGLSwapChainDescriptor swapChainDesc = | ||
{ | ||
.resolution = { 800, 600 }, | ||
.colorBits = 32, | ||
.depthBits = 0, // We don't need a depth buffer for this example | ||
.stencilBits = 0, // We don't need a stencil buffer for this example | ||
#if ENABLE_MULTISAMPLING | ||
.samples = 8, // check if LLGL adapts sample count that is too high | ||
#endif | ||
}; | ||
LLGLSwapChain swapChain = llglCreateSwapChain(&swapChainDesc); | ||
|
||
// Enable V-sync | ||
llglSetVsyncInterval(swapChain, 1); | ||
|
||
// Set window title and show window | ||
LLGLSurface surface = llglGetSurface(swapChain); | ||
LLGLWindow window = LLGL_GET_AS(LLGLWindow, surface); | ||
|
||
llglSetWindowTitle(window, L"LLGL C99 Example: Hello Triangle"); | ||
llglShowWindow(window, true); | ||
|
||
// Vertex data structure | ||
typedef struct Vertex | ||
{ | ||
float position[2]; | ||
uint8_t color[4]; | ||
} | ||
Vertex; | ||
|
||
// Vertex data (3 vertices for our triangle) | ||
const float s = 0.5f; | ||
|
||
Vertex vertices[] = | ||
{ | ||
{ { 0, s }, { 255, 0, 0, 255 } }, // 1st vertex: center-top, red | ||
{ { s, -s }, { 0, 255, 0, 255 } }, // 2nd vertex: right-bottom, green | ||
{ { -s, -s }, { 0, 0, 255, 255 } }, // 3rd vertex: left-bottom, blue | ||
}; | ||
|
||
// Vertex format with 2D floating-point vector for position and 4D byte vector for color | ||
LLGLVertexAttribute vertexAttributes[2] = | ||
{ | ||
{ .name = "position", .format = LLGLFormatRG32Float, .location = 0, .offset = 0, .stride = sizeof(Vertex) }, | ||
{ .name = "color", .format = LLGLFormatRGBA8UNorm, .location = 1, .offset = offsetof(Vertex, color), .stride = sizeof(Vertex) }, | ||
}; | ||
|
||
// Create vertex buffer | ||
LLGLBufferDescriptor vertexBufferDesc = | ||
{ | ||
.size = sizeof(vertices), // Size (in bytes) of the vertex buffer | ||
.bindFlags = LLGLBindVertexBuffer, // Enables the buffer to be bound to a vertex buffer slot | ||
.numVertexAttribs = 2, | ||
.vertexAttribs = vertexAttributes, // Vertex format layout | ||
}; | ||
LLGLBuffer vertexBuffer = llglCreateBuffer(&vertexBufferDesc, vertices); | ||
|
||
// Create shaders | ||
LLGLShaderDescriptor vertShaderDesc = { .type = LLGLShaderTypeVertex, .source = "HelloTriangle.vert", .sourceType = LLGLShaderSourceTypeCodeFile, }; | ||
LLGLShaderDescriptor fragShaderDesc = { .type = LLGLShaderTypeFragment, .source = "HelloTriangle.frag", .sourceType = LLGLShaderSourceTypeCodeFile, }; | ||
|
||
// Specify vertex attributes for vertex shader | ||
vertShaderDesc.vertex.numInputAttribs = 2; | ||
vertShaderDesc.vertex.inputAttribs = vertexAttributes; | ||
|
||
LLGLShader shaders[2] = | ||
{ | ||
llglCreateShader(&vertShaderDesc), | ||
llglCreateShader(&fragShaderDesc), | ||
}; | ||
|
||
for (int i = 0; i < 2; ++i) | ||
{ | ||
LLGLReport shaderReport = llglGetShaderReport(shaders[i]); | ||
if (llglHasReportErrors(shaderReport)) | ||
{ | ||
fprintf(stderr, "%s\n", llglGetReportText(shaderReport)); | ||
return 1; | ||
} | ||
} | ||
|
||
// Create graphics pipeline | ||
LLGLGraphicsPipelineDescriptor pipelineDesc = | ||
{ | ||
.vertexShader = shaders[0], | ||
.fragmentShader = shaders[1], | ||
.renderPass = llglGetRenderTargetRenderPass(LLGL_GET_AS(LLGLRenderTarget, swapChain)), | ||
.primitiveTopology = LLGLPrimitiveTopologyTriangleList, | ||
#if ENABLE_MULTISAMPLING | ||
.rasterizer = { .multiSampleEnabled = (swapChainDesc.samples > 1) }, | ||
#endif | ||
.blend.targets[0].colorMask = LLGLColorMaskAll, | ||
}; | ||
|
||
// Create graphics PSO | ||
LLGLPipelineState pipeline = llglCreateGraphicsPipelineState(&pipelineDesc); | ||
|
||
// Link shader program and check for errors | ||
LLGLReport pipelineReport = llglGetPipelineStateReport(pipeline); | ||
if (llglHasReportErrors(pipelineReport)) | ||
{ | ||
fprintf(stderr, "%s\n", llglGetReportText(pipelineReport)); | ||
return 1; | ||
} | ||
|
||
// Create command buffer to submit subsequent graphics commands to the GPU | ||
LLGLCommandBufferDescriptor cmdBufferDesc = | ||
{ | ||
.flags = LLGLCommandBufferImmediateSubmit, | ||
.numNativeBuffers = 2, | ||
}; | ||
LLGLCommandBuffer cmdBuffer = llglCreateCommandBuffer(&cmdBufferDesc); | ||
|
||
// Initialize frame constants | ||
const LLGLViewport viewport = | ||
{ | ||
.x = 0.0f, | ||
.y = 0.0f, | ||
.width = (float)swapChainDesc.resolution.width, | ||
.height = (float)swapChainDesc.resolution.height, | ||
.minDepth = 0.0f, | ||
.maxDepth = 1.0f | ||
}; | ||
|
||
const LLGLClearValue clearColor = | ||
{ | ||
.color = { 0.1f, 0.1f, 0.2f, 1.0f }, | ||
}; | ||
|
||
// Enter main loop | ||
while (llglProcessSurfaceEvents(surface)) | ||
{ | ||
// Begin recording commands | ||
llglBegin(cmdBuffer); | ||
{ | ||
// Set viewport and scissor rectangle | ||
llglSetViewport(&viewport); | ||
|
||
// Set vertex buffer | ||
llglSetVertexBuffer(vertexBuffer); | ||
|
||
// Set the swap-chain as the initial render target | ||
llglBeginRenderPass(LLGL_GET_AS(LLGLRenderTarget, swapChain)); | ||
{ | ||
// Clear color buffer | ||
llglClear(LLGLClearColor, &clearColor); | ||
|
||
// Set graphics pipeline | ||
llglSetPipelineState(pipeline); | ||
|
||
// Draw triangle with 3 vertices | ||
llglDraw(3, 0); | ||
} | ||
llglEndRenderPass(); | ||
} | ||
llglEnd(); | ||
|
||
// Present the result on the screen | ||
llglPresent(swapChain); | ||
} | ||
|
||
// Clean up | ||
llglUnloadRenderSystem(); | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// GLSL shader version 1.30 (for OpenGL 3.1) | ||
#version 130 | ||
|
||
in vec4 vColor; | ||
|
||
out vec4 outColor; | ||
|
||
void main() | ||
{ | ||
outColor = vColor; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// GLSL shader version 1.30 (for OpenGL 3.1) | ||
#version 130 | ||
|
||
in vec2 position; | ||
in vec4 color; | ||
|
||
out vec4 vColor; | ||
|
||
void main() | ||
{ | ||
gl_Position = vec4(position, 0, 1); | ||
vColor = color; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# C99 Examples | ||
|
||
This folder contains all LLGL examples written in the C programming language (ISO-C99). | ||
Note that you need to set the **working directory** to `<Your-LLGL-Repository>/examples/C99/<Example>` in order to get your examples running. | ||
For the first example, this could be `/Users/JohnDoe/LLGL/examples/C99/HelloTriangle` or `C:\Users\JohnDoe\LLGL\examples\C99\HelloTriangle` for instance. | ||
|
||
## Examples | ||
|
||
### [Hello Triangle](HelloTriangle) | ||
|
||
This example illustrates how to interact with LLGL in the C programing language. | ||
|
||
<p align="center"><img src="HelloTriangle/HelloTriangle.png" width="400" height="300"/></p> | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Buffer.h | ||
* | ||
* Copyright (c) 2015 Lukas Hermanns. All rights reserved. | ||
* Licensed under the terms of the BSD 3-Clause license (see LICENSE.txt). | ||
*/ | ||
|
||
#ifndef LLGL_C99_BUFFER_H | ||
#define LLGL_C99_BUFFER_H | ||
|
||
|
||
#include <LLGL-C/Export.h> | ||
#include <LLGL-C/Types.h> | ||
#include <LLGL-C/BufferFlags.h> | ||
|
||
|
||
LLGL_C_EXPORT long llglGetBufferBindFlags(LLGLBuffer buffer); | ||
LLGL_C_EXPORT void llglGetBufferDesc(LLGLBuffer buffer, LLGLBufferDescriptor* outDesc); | ||
|
||
|
||
#endif | ||
|
||
|
||
|
||
// ================================================================================ |
Oops, something went wrong.