61 PMVMat4fUniform& m_pmvMatUni;
71 : m_type(
type), m_st(st), m_pmvMatUni(pmvMatU), m_array(
GLFloatArrayDataServer::createGLSLInterleaved(2*3, false, 4, GL_STATIC_DRAW) )
73 m_array->addGLSLSubArray(
"gca_Vertex", 3, GL_ARRAY_BUFFER);
74 m_array->addGLSLSubArray(
"gca_Normal", 3, GL_ARRAY_BUFFER);
75 m_st.ownAttribute(m_array,
true);
78 m_st.ownUniform(m_uColor,
true);
90 const Vec4f&
color() const noexcept {
return m_uColor->vec4f(); }
97 m_pmvMatUni.m.pushMv();
98 m_pmvMatUni.m.translateMv(m_position);
100 m_pmvMatUni.m.rotateMv(m_rotation);
101 m_st.pushUniform(
gl, m_pmvMatUni.u);
103 m_st.pushUniform(
gl, m_uColor);
105 m_array->enableBuffer(
gl,
true);
106 ::glDrawArrays(m_type, 0, m_array->elemCount());
107 m_array->enableBuffer(
gl,
false);
108 m_pmvMatUni.m.popMv();
116 constexpr static float half = 0.5f;
117 constexpr static float qter = 0.25f;
118 constexpr static float zNear= 1.0f;
119 constexpr static float zFar =100.0f;
124 bool m_animating =
true;
128 Shape m_shape1, m_shape2;
133 m_initialized(false),
134 m_shape1(GL_TRIANGLE_STRIP, m_st, m_pmvMat), m_shape2(GL_LINE_STRIP, m_st, m_pmvMat) { }
150 "demos/glsl/bin",
"SingleLight0");
152 "demos/glsl/bin",
"SingleLight0");
159 std::string custom =
"#define MAX_TEXTURE_UNITS 0\n";
167 if( !sp0->
add(
gl, vp0,
true) || !sp0->
add(
gl, fp0,
true) ) {
173 m_st.attachShaderProgram(
gl, sp0,
true);
176 m_pmvMat.m.getP().loadIdentity();
177 m_pmvMat.m.getMv().loadIdentity();
178 m_st.ownUniform(m_pmvMat.u,
true);
181 m_st.ownUniform(lightU,
true);
183 m_st.pushAllUniforms(
gl);
185 const Vec3f frontNormal(0, 0, 1);
186 const Vec3f backNormal(0, 0, -1);
190 v->put3f(-half, half, dz); v->put3f(frontNormal);
191 v->put3f( half, half, dz); v->put3f(frontNormal);
192 v->put3f(-half, -half, dz); v->put3f(frontNormal);
193 v->put3f( half, -half, dz); v->put3f(frontNormal);
196 v->put3f(-half, half, dz); v->put3f(backNormal);
197 v->put3f( half, half, dz); v->put3f(backNormal);
198 v->put3f(-half, -half, dz); v->put3f(backNormal);
199 v->put3f( half, -half, dz); v->put3f(backNormal);
200 m_shape1.seal(
gl,
true);
201 m_shape1.setColor(
Vec4f(0, 1, 0, 1));
202 m_shape1.position().x = 1.5f;
205 const float lineWidth = 1/10.0f;
206 const float tw = 1.0f;
207 const float th = 1.0f;
209 float lwh = lineWidth/2.0f;
214 float ctrX = 0, ctrY = 0;
219 v->put3f(ctrX-lwh, ctrY+thh, ctrZ); v->put3f(frontNormal);
220 v->put3f(ctrX-lwh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
221 v->put3f(ctrX-twh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
222 v->put3f(ctrX-twh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
223 v->put3f(ctrX-lwh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
224 v->put3f(ctrX-lwh, ctrY-thh, ctrZ); v->put3f(frontNormal);
225 v->put3f(ctrX+lwh, ctrY-thh, ctrZ); v->put3f(frontNormal);
226 v->put3f(ctrX+lwh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
227 v->put3f(ctrX+twh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
228 v->put3f(ctrX+twh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
229 v->put3f(ctrX+lwh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
230 v->put3f(ctrX+lwh, ctrY+thh, ctrZ); v->put3f(frontNormal);
231 v->put3f(ctrX-lwh, ctrY+thh, ctrZ); v->put3f(frontNormal);
232 m_shape2.seal(
gl,
true);
233 m_shape2.setColor(
Vec4f(0, 0, 1, 1));
234 m_shape2.position().x = -1.5f;
237 ::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
238 ::glEnable(GL_DEPTH_TEST);
240 m_initialized = sp0->
inUse();
241 if( !m_initialized ) {
246 return m_initialized;
252 m_initialized =
false;
260 m_pmvMat.m.getP().loadIdentity();
261 const float aspect = 1.0f;
262 const float fovy_deg=45.0f;
263 const float aspect2 = ( (float) m_viewport.width() / (
float) m_viewport.height() ) / aspect;
265 m_st.useProgram(
gl,
true);
266 m_st.pushUniform(
gl, m_pmvMat.u);
267 m_st.useProgram(
gl,
true);
272 if( !m_initialized ) {
276 ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
278 m_st.useProgram(
gl,
true);
279 m_pmvMat.m.getMv().loadIdentity();
280 m_pmvMat.m.translateMv(0, 0, -5);
283 constexpr double angle_per_sec = 30;
284 const float rad = (float) ( (when - m_tlast).to_double() * angle_per_sec );
291 m_st.useProgram(
gl,
false);
296 std::string
toStringImpl() const noexcept
override {
return "Primitives01"; }
301 class MyKeyListener :
public KeyListener {
307 void keyPressed(KeyEvent& e,
const KeyboardTracker& kt)
override {
309 if( e.
keySym() == VKeyCode::VK_ESCAPE ) {
314 }
else if( e.
keySym() == VKeyCode::VK_PAUSE || e.
keySym() == VKeyCode::VK_P ) {
315 m_parent.animating() = !m_parent.animating();
316 }
else if( e.
keySym() == VKeyCode::VK_W ) {
321 void keyReleased(KeyEvent& e,
const KeyboardTracker& kt)
override {
325 typedef std::shared_ptr<MyKeyListener> MyKeyListenerRef;
326 MyKeyListenerRef m_kl;
331 m_kl(
std::make_shared<MyKeyListener>(*this)) { }
346int main(
int argc,
char *argv[])
350 return launch(
"Primitives01.cpp",
352 std::make_shared<Example>(), argc, argv);
int launch(std::string_view sfile, GLLaunchProps props, const RenderListenerRef &demo, int argc, char *argv[])
int main(int argc, char *argv[])
bool init(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
void dispose(const WindowRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
void reshape(const WindowRef &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.
PMVMat4f & pmv() noexcept
std::string toStringImpl() const noexcept override
const PMVMat4f & pmv() const noexcept
bool animating() const noexcept
void display(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
const Recti & viewport() const noexcept
bool init(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
bool & animating() noexcept
Recti & viewport() noexcept
void dispose(const WindowRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
void setColor(const Vec4f &c) noexcept
constexpr const GLFloatArrayDataServerRef & vertices() const noexcept
constexpr Vec3f & position() noexcept
constexpr const Quat4f & rotation() const noexcept
Shape(GLenum type, ShaderState &st, PMVMat4fUniform &pmvMatU)
constexpr const Vec3f & position() const noexcept
constexpr GLFloatArrayDataServerRef & vertices() noexcept
const Vec4f & color() const noexcept
void seal(GL &gl, bool seal)
constexpr Quat4f & rotation() noexcept
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 ShaderCodeRef create(GLenum type, size_t count, const source_list_t &sources) noexcept
size_t insertShaderSource(size_t shaderIdx, stringview_t tag, size_t fromIndex, stringview_t data) noexcept
Adds data after the line containing tag.
void destroy(GL &gl) noexcept
Detaches all shader codes and deletes the program.
static ShaderProgramRef create() noexcept
bool add(const ShaderCodeRef &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
size_t removeKeyListener(const KeyListenerRef &l)
void dispose(const jau::fraction_timespec &when) noexcept override
std::string toString() const noexcept
void addKeyListener(const KeyListenerRef &l)
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
GLArrayDataServerRef< float > GLFloatArrayDataServerRef
std::shared_ptr< GLUniformData > GLUniformDataRef
std::shared_ptr< GLUniformVec4f > GLUniformVec4fRef
std::shared_ptr< GLUniformVec3f > GLUniformVec3fRef
std::shared_ptr< ShaderProgram > ShaderProgramRef
std::shared_ptr< ShaderCode > ShaderCodeRef
@ verbose
Verbose operations (debugging).
std::shared_ptr< Window > WindowRef
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().
int fprintf_td(const uint64_t elapsed_ms, FILE *stream, const char *format,...) noexcept
Convenient fprintf() invocation, prepending the given elapsed_ms timestamp.
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.