55 GLUniformSyncPMVMat4f& m_pmvMatUni;
56 GLUniformVec4f& m_staticColor;
61 : m_type(
type), m_st(st), m_pmvMatUni(pmvMatU), m_staticColor(
color),
64 m_array->addGLSLSubArray(
"gca_Vertex", 3, GL_ARRAY_BUFFER);
65 m_array->addGLSLSubArray(
"gca_Normal", 3, GL_ARRAY_BUFFER);
85 m_pmvMatUni.pmv().pushMv();
86 m_pmvMatUni.pmv().translateMv(m_position);
88 m_pmvMatUni.pmv().rotateMv(m_rotation);
89 m_st.send(
gl, m_pmvMatUni);
91 m_staticColor.vec4f() = m_color;
92 m_st.send(
gl, m_staticColor);
94 m_array->enableBuffer(
gl,
true);
95 ::glDrawArrays(m_type, 0, m_array->elemCount());
96 m_array->enableBuffer(
gl,
false);
97 m_pmvMatUni.pmv().popMv();
105 constexpr static float half = 0.5f;
106 constexpr static float qter = 0.25f;
107 constexpr static float zNear= 1.0f;
108 constexpr static float zFar =100.0f;
113 bool m_animating =
true;
119 Shape m_shape1, m_shape2;
126 m_initialized(false),
128 m_staticColor(
"gcu_StaticColor",
Vec4f(1, 0, 0, 1)),
129 m_light0Pos(
"gcu_Light0Pos", lightPos),
130 m_shape1(GL_TRIANGLE_STRIP, m_st, m_pmvMatUni, m_staticColor),
131 m_shape2(GL_LINE_STRIP, m_st, m_pmvMatUni, m_staticColor)
133 m_st.manage(m_pmvMatUni);
134 m_st.manage(m_staticColor);
135 m_st.manage(m_light0Pos);
142 const PMVMat4f&
pmv() const noexcept {
return m_pmvMatUni.pmv(); }
152 "demos/glsl/bin",
"SingleLight0");
154 "demos/glsl/bin",
"SingleLight0");
161 std::string custom =
"#define MAX_TEXTURE_UNITS 0\n";
169 if( !sp0->
add(
gl, vp0,
true) || !sp0->
add(
gl, fp0,
true) ) {
175 m_st.attachShaderProgram(
gl, sp0,
true);
178 m_pmvMatUni.pmv().getP().loadIdentity();
179 m_pmvMatUni.pmv().getMv().loadIdentity();
181 const Vec3f frontNormal(0, 0, 1);
182 const Vec3f backNormal(0, 0, -1);
186 v->put3f(-half, half, dz); v->put3f(frontNormal);
187 v->put3f( half, half, dz); v->put3f(frontNormal);
188 v->put3f(-half, -half, dz); v->put3f(frontNormal);
189 v->put3f( half, -half, dz); v->put3f(frontNormal);
192 v->put3f(-half, half, dz); v->put3f(backNormal);
193 v->put3f( half, half, dz); v->put3f(backNormal);
194 v->put3f(-half, -half, dz); v->put3f(backNormal);
195 v->put3f( half, -half, dz); v->put3f(backNormal);
196 m_shape1.seal(
gl,
true);
197 m_shape1.setColor(
Vec4f(0, 1, 0, 1));
198 m_shape1.position().x = 1.5f;
201 const float lineWidth = 1/10.0f;
202 const float tw = 1.0f;
203 const float th = 1.0f;
205 float lwh = lineWidth/2.0f;
210 float ctrX = 0, ctrY = 0;
215 v->put3f(ctrX-lwh, ctrY+thh, ctrZ); v->put3f(frontNormal);
216 v->put3f(ctrX-lwh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
217 v->put3f(ctrX-twh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
218 v->put3f(ctrX-twh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
219 v->put3f(ctrX-lwh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
220 v->put3f(ctrX-lwh, ctrY-thh, ctrZ); v->put3f(frontNormal);
221 v->put3f(ctrX+lwh, ctrY-thh, ctrZ); v->put3f(frontNormal);
222 v->put3f(ctrX+lwh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
223 v->put3f(ctrX+twh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
224 v->put3f(ctrX+twh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
225 v->put3f(ctrX+lwh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
226 v->put3f(ctrX+lwh, ctrY+thh, ctrZ); v->put3f(frontNormal);
227 v->put3f(ctrX-lwh, ctrY+thh, ctrZ); v->put3f(frontNormal);
228 m_shape2.seal(
gl,
true);
229 m_shape2.setColor(
Vec4f(0, 0, 1, 1));
230 m_shape2.position().x = -1.5f;
233 ::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
234 ::glEnable(GL_DEPTH_TEST);
236 m_initialized = sp0->
inUse();
237 if( !m_initialized ) {
242 return m_initialized;
248 m_initialized =
false;
256 m_pmvMatUni.pmv().getP().loadIdentity();
257 const float aspect = 1.0f;
258 const float fovy_deg=45.0f;
259 const float aspect2 = ( (float) m_viewport.width() / (
float) m_viewport.height() ) / aspect;
260 m_pmvMatUni.pmv().perspectiveP(
jau::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
261 m_st.useProgram(
gl,
true);
262 m_st.send(
gl, m_pmvMatUni);
263 m_st.useProgram(
gl,
true);
268 if( !m_initialized ) {
272 ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
274 m_st.useProgram(
gl,
true);
275 m_pmvMatUni.pmv().getMv().loadIdentity();
276 m_pmvMatUni.pmv().translateMv(0, 0, -5);
279 constexpr double angle_per_sec = 30;
280 const float rad = (float) ( (when - m_tlast).to_double() * angle_per_sec );
289 std::cerr <<
"XXX draw " << m_st <<
"\n";
292 m_st.useProgram(
gl,
false);
297 std::string
toStringImpl() const noexcept
override {
return "Primitives01"; }
302 class MyKeyListener :
public KeyListener {
308 void keyPressed(KeyEvent& e,
const KeyboardTracker& kt)
override {
310 if( e.
keySym() == VKeyCode::VK_ESCAPE ) {
315 }
else if( e.
keySym() == VKeyCode::VK_PAUSE || e.
keySym() == VKeyCode::VK_P ) {
316 m_parent.animating() = !m_parent.animating();
317 }
else if( e.
keySym() == VKeyCode::VK_W ) {
322 void keyReleased(KeyEvent& e,
const KeyboardTracker& kt)
override {
326 typedef std::shared_ptr<MyKeyListener> MyKeyListenerRef;
327 MyKeyListenerRef m_kl;
332 m_kl(
std::make_shared<MyKeyListener>(*this)) { }
347int main(
int argc,
char *argv[])
351 return launch(
"Primitives01.cpp",
353 std::make_shared<Example>(), argc, argv);
int launch(std::string_view sfile, GLLaunchProps props, const RenderListenerSRef &demo, int argc, char *argv[])
int main(int argc, char *argv[])
bool init(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
void dispose(const WindowSRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
PMVMat4f & pmv() noexcept
void reshape(const WindowSRef &win, const jau::math::Recti &viewport, const jau::fraction_timespec &when) override
Called by the drawable during the first repaint after the component has been resized.
void display(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
void dispose(const WindowSRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
std::string toStringImpl() const noexcept override
bool init(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
const PMVMat4f & pmv() const noexcept
bool animating() const noexcept
const Recti & viewport() const noexcept
bool & animating() noexcept
Recti & viewport() noexcept
void setColor(const Vec4f &c) noexcept
constexpr Vec3f & position() noexcept
constexpr const Quat4f & rotation() const noexcept
constexpr GLFloatArrayDataServerSRef & vertices() noexcept
Shape(GLenum type, ShaderState &st, GLUniformSyncPMVMat4f &pmvMatU, GLUniformVec4f &color)
constexpr const GLFloatArrayDataServerSRef & vertices() const noexcept
constexpr const Vec3f & position() const noexcept
const Vec4f & color() const noexcept
void seal(GL &gl, bool seal)
constexpr Quat4f & rotation() noexcept
Specifies a set of OpenGL capabilities.
static GLContext & downcast(RenderContext *rc)
Downcast dereferenced given RenderContext* to GLContext&, throws exception if signature doesn't match...
Specifies the OpenGL profile.
static constexpr std::string_view GLES2
The embedded OpenGL profile ES 2.x, with x >= 0.
size_t defaultShaderCustomization(const GL &gl, bool preludeVersion=true, bool addDefaultPrecision=true, bool addDefaultDefines=true)
Default customization of this shader source code.
static ShaderCodeSRef create(GLenum type, size_t count, const source_list_t &sources)
size_t insertShaderSource(size_t shaderIdx, stringview_t tag, size_t fromIndex, stringview_t data) noexcept
Adds data after the line containing tag.
static ShaderProgramSRef create() noexcept
void destroy(GL &gl) noexcept
Detaches all shader codes and deletes the program.
bool add(const ShaderCodeSRef &shaderCode) noexcept
Adds a new shader to this program.
constexpr bool inUse() const noexcept
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
constexpr RenderListener(Private) noexcept
Private ctor for shared_ptr<RenderListener> instance method w/o public ctor.
std::string toString() const noexcept
const gamp::render::RenderContext * renderContext() const noexcept
void addKeyListener(const KeyListenerSRef &l)
void dispose(const jau::fraction_timespec &when) noexcept override
size_t removeKeyListener(const KeyListenerSRef &l)
std::string toString() const noexcept
std::string toString() const noexcept
constexpr VKeyCode keySym() const noexcept
Returns the virtual key symbol reflecting the current keyboard layout.
virtual const PressedKeyCodes & pressedKeyCodes() const noexcept=0
constexpr const WindowWeakPtr & source() const noexcept
constexpr const jau::fraction_timespec & when() const noexcept
size_type count() const noexcept
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
GLArrayDataServer< float > GLFloatArrayDataServer
GLArrayDataServerSRef< float > GLFloatArrayDataServerSRef
std::shared_ptr< ShaderCode > ShaderCodeSRef
std::shared_ptr< ShaderProgram > ShaderProgramSRef
@ verbose
Verbose operations (debugging).
std::shared_ptr< Window > WindowSRef
PMVData
PMVMatrix4 derived matrices and values.
Quaternion< float > Quat4f
PMVMatrix4< float > PMVMat4f
@ inv_mv
Bit value for inverse modelview matrix (Mvi), updated via update().
@ inv_proj
Bit value for inverse projection matrix (Pi), updated via update().
@ inv_tps_mv
Bit value for inverse transposed modelview matrix (Mvit), updated via update().
ssize_t fprintf_td(const uint64_t elapsed_ms, FILE *stream, std::string_view format, const Args &...args) noexcept
Convenient secure fprintf() invocation, prepending the given elapsed_ms timestamp and using jau:forma...
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
constexpr uint64_t to_ms() const noexcept
Returns time in milliseconds.