To render a geometry in just three steps with GitHub - floooh/sokol: minimal cross-platform standalone C headers
Step 1: Set Up the Pipeline
#pragma once
#include "sokol_gfx.h"
struct SimplePipeline {
sg_pass pass;
sg_pipeline pipeline;
sg_pipeline pipeline_use_index;
SimplePipeline(sg_primitive_type primitive_type = _SG_PRIMITIVETYPE_DEFAULT);
};
We specify vertex and fragment shaders here. The vertex shader sets up the vertex positions and colors, while the fragment shader processes the colors:
const char simple_quad_vs[] = R"(#version 300 es
layout(location = 0) in vec4 in_position;
layout(location = 1) in vec4 in_color;
out vec4 color;
void main() {
gl_Position = in_position;
color = in_color;
})";
const char simple_quad_fs[] = R"(#version 300 es
precision mediump float;
in vec4 color;
out vec4 FragColor;
void main() {
FragColor = color;
})";
In SimplePipeline, we configure the layout of vertex attributes and then compile the shader:
SimplePipeline::SimplePipeline(sg_primitive_type primitive_type) {
pipeline = sg_make_pipeline((sg_pipeline_desc) {
.layout = {
.attrs = {
[0] = { .format = SG_VERTEXFORMAT_FLOAT3, .offset = 0 },
[1] = { .format = SG_VERTEXFORMAT_FLOAT4, .offset = sizeof(float) * 3 }
},
},
.primitive_type = primitive_type,
.shader = sg_make_shader((sg_shader_desc) {
.vs = { .source = simple_quad_vs },
.fs = { .source = simple_quad_fs }
}),
.label = "simple-quad-pipeline",
});
}
Step 2: Configure Buffer Bindings
We define the buffers required to hold the vertex and color data as well as the indices for drawing:
class SimpleQuad {
public:
static SimpleQuad& Instance();
const sg_bindings& GetBindings() const { return bindings; }
const sg_bindings& GetBindings_use_index() const { return bindings_use_index; }
int GetNumElements() const { return 3; }
private:
SimpleQuad();
sg_bindings bindings;
sg_buffer vertex_buffer;
sg_buffer index_buffer;
sg_bindings bindings_use_index;
};
SimpleQuad::SimpleQuad() {
float vertices[] = {
0.0f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f
};
vertex_buffer = sg_make_buffer({ .data = SG_RANGE(vertices), .label = "quad-vertices" });
bindings = (sg_bindings){ .vertex_buffers[0] = vertex_buffer };
uint16_t indices[] = { 0, 1, 2 };
index_buffer = sg_make_buffer({ .type = SG_BUFFERTYPE_INDEXBUFFER, .data = SG_RANGE(indices) });
bindings_use_index = (sg_bindings){ .vertex_buffers[0] = vertex_buffer, .index_buffer = index_buffer };
}
Step 3: Draw the Shape
With the pipeline and buffers set up, all that’s left is to render the triangle. In the render loop, we bind the pipeline and then draw the shape:
{
sg_apply_pipeline(_quard_pipeline->pipeline);
sg_apply_bindings(SimpleQuad::Instance().GetBindings());
sg_draw(0, SimpleQuad::Instance().GetNumElements(), 1);
}
The final result
(GitHub - iomeone/jucesokoltest at step11_triangle_2)
Need help:
this->openGLContext.setComponentPaintingEnabled(false);
addAndMakeVisible(button);
button.setButtonText("Click Me");
If I want to show the button, then I must setComponentPaintingEnabled to true, but when I set it to true, then the 3D scene would not show.
How to get around this issue?
I want to use gui to configure lots of 3d parameters in the future!




