gale/sample/basic.cpp

157 lines
5.4 KiB
C++
Raw Normal View History

2016-03-30 21:40:01 +02:00
/** @file
2015-07-16 21:34:35 +02:00
* @author Edouard DUPIN
* @copyright 2010, Edouard DUPIN, all right reserved
* @license GPL v3 (see license file)
*/
2016-04-08 21:04:50 +02:00
//! [gale_sample_all]
2015-07-16 21:34:35 +02:00
2016-10-02 16:10:25 +02:00
#include <etk/types.hpp>
#include <gale/gale.hpp>
#include <gale/context/commandLine.hpp>
#include <test-debug/debug.hpp>
2015-07-16 21:34:35 +02:00
2016-10-02 16:10:25 +02:00
#include <gale/Application.hpp>
#include <gale/context/Context.hpp>
#include <gale/resource/Program.hpp>
2015-07-16 21:34:35 +02:00
2016-11-04 22:54:25 +01:00
#define GALE_SAMPLE_VBO_VERTICES (0)
#define GALE_SAMPLE_VBO_COLOR (1)
2015-07-16 21:34:35 +02:00
class MainApplication : public gale::Application {
private:
2016-07-15 21:22:11 +02:00
ememory::SharedPtr<gale::resource::Program> m_GLprogram;
int32_t m_GLPosition;
int32_t m_GLMatrix;
int32_t m_GLColor;
2016-11-04 22:54:25 +01:00
float m_angle;
ememory::SharedPtr<gale::resource::VirtualBufferObject> m_verticesVBO;
2015-07-16 21:34:35 +02:00
public:
2016-09-15 22:01:35 +02:00
void onCreate(gale::Context& _context) override {
2015-07-16 21:34:35 +02:00
setSize(vec2(800, 600));
2016-11-04 22:54:25 +01:00
m_angle = 0.0f;
m_GLprogram = gale::resource::Program::create("DATA:basic.prog");
if (m_GLprogram != nullptr) {
m_GLPosition = m_GLprogram->getAttribute("EW_coord3d");
m_GLColor = m_GLprogram->getAttribute("EW_color");
m_GLMatrix = m_GLprogram->getUniform("EW_MatrixTransformation");
}
2016-11-04 22:54:25 +01:00
// this is the properties of the buffer requested : "r"/"w" + "-" + buffer type "f"=flaot "i"=integer
m_verticesVBO = gale::resource::VirtualBufferObject::create(5);
if (m_verticesVBO == nullptr) {
TEST_ERROR("can not instanciate VBO ...");
return;
}
// TO facilitate some debugs we add a name of the VBO:
m_verticesVBO->setName("[VBO] of basic SAMPLE");
m_verticesVBO->pushOnBuffer(GALE_SAMPLE_VBO_VERTICES, vec3(-0.5,-0.5,0));
m_verticesVBO->pushOnBuffer(GALE_SAMPLE_VBO_VERTICES, vec3(0,0.5,0));
m_verticesVBO->pushOnBuffer(GALE_SAMPLE_VBO_VERTICES, vec3(0.5,-0.5,0));
m_verticesVBO->pushOnBuffer(GALE_SAMPLE_VBO_COLOR, etk::Color<float>(etk::color::red));
m_verticesVBO->pushOnBuffer(GALE_SAMPLE_VBO_COLOR, etk::Color<float>(etk::color::green));
m_verticesVBO->pushOnBuffer(GALE_SAMPLE_VBO_COLOR, etk::Color<float>(etk::color::blue));
// update all the VBO elements ...
m_verticesVBO->flush();
2016-09-15 22:01:35 +02:00
TEST_INFO("==> Init APPL (END)");
2015-07-16 21:34:35 +02:00
}
2016-09-15 22:01:35 +02:00
void onDraw(gale::Context& _context) override {
2016-11-04 22:54:25 +01:00
m_angle += 0.01;
ivec2 size = getSize();
// set the basic openGL view port: (position drawed in the windows)
2015-07-20 21:07:02 +02:00
gale::openGL::setViewPort(ivec2(0,0),size);
// Clear all the stacked matrix ...
gale::openGL::setBasicMatrix(mat4());
// clear background
2017-01-16 21:25:36 +01:00
etk::Color<float,4> bgColor = etk::color::yellow;
bgColor.setA(0.75);
gale::openGL::clearColor(bgColor);
// real clear request:
gale::openGL::clear(gale::openGL::clearFlag_colorBuffer);
// create a local matrix environnement.
gale::openGL::push();
2015-07-16 21:34:35 +02:00
mat4 tmpProjection = etk::matOrtho(-1, 1,
-1, 1,
-2, 2);
// set internal matrix system:
gale::openGL::setMatrix(tmpProjection);
2016-11-04 22:54:25 +01:00
#if 0
vec3 vertices[3]={ vec3(-0.5,-0.5,0),
vec3(0,0.5,0),
vec3(0.5,-0.5,0)
};
etk::Color<float> color[3] = { etk::color::red,
etk::color::green,
etk::color::blue
};
#endif
if (m_GLprogram == nullptr) {
2016-09-15 22:01:35 +02:00
TEST_INFO("No shader ...");
return;
}
//EWOL_DEBUG(" display " << m_coord.size() << " elements" );
m_GLprogram->use();
2016-11-04 22:54:25 +01:00
// set Matrix : translation/positionMatrix
2016-11-04 22:54:25 +01:00
mat4 projMatrix = gale::openGL::getMatrix() * etk::matRotate(vec3(0,0,1),m_angle);
mat4 camMatrix = gale::openGL::getCameraMatrix();
mat4 tmpMatrix = projMatrix * camMatrix;
2016-11-04 22:54:25 +01:00
m_GLprogram->uniformMatrix(m_GLMatrix, tmpMatrix);
2017-01-11 23:00:35 +01:00
#if 1
2016-11-04 22:54:25 +01:00
// position:
m_GLprogram->sendAttributePointer(m_GLPosition, m_verticesVBO, GALE_SAMPLE_VBO_VERTICES);
// color:
m_GLprogram->sendAttributePointer(m_GLColor, m_verticesVBO, GALE_SAMPLE_VBO_COLOR);
#else
// position:
m_GLprogram->sendAttribute(m_GLPosition, 3/*x,y,z,unused*/, vertices, 4*sizeof(float));
// color:
m_GLprogram->sendAttribute(m_GLColor, 4/*r,g,b,a*/, color, 4*sizeof(float));
#endif
// Request the draw od the elements:
gale::openGL::drawArrays(gale::openGL::renderMode::triangle, 0, 3 /*number of points*/);
m_GLprogram->unUse();
// Restore context of matrix
gale::openGL::pop();
2015-07-16 21:34:35 +02:00
}
2016-09-15 22:01:35 +02:00
void onPointer(enum gale::key::type _type,
int32_t _pointerID,
const vec2& _pos,
gale::key::status _state) override {
2017-01-17 23:07:13 +01:00
/*
2016-09-15 22:01:35 +02:00
TEST_INFO("input event: type=" << _type);
TEST_INFO(" id=" << _pointerID);
TEST_INFO(" pos=" << _pos);
TEST_INFO(" state=" << _state);
2017-01-17 23:07:13 +01:00
*/
2016-09-15 22:01:35 +02:00
}
void onKeyboard(const gale::key::Special& _special,
enum gale::key::keyboard _type,
char32_t _value,
gale::key::status _state) override {
TEST_INFO("Keyboard event: special=" << _special);
TEST_INFO(" type=" << _type);
TEST_INFO(" value=" << uint32_t(_value));
TEST_INFO(" state=" << _state);
}
2015-07-16 21:34:35 +02:00
};
2016-04-08 21:04:50 +02:00
//! [gale_declare_main]
2015-07-16 21:34:35 +02:00
/**
* @brief Main of the program (This can be set in every case, but it is not used in Andoid...).
* @param std IO
* @return std IO
*/
int main(int _argc, const char *_argv[]) {
return gale::run(new MainApplication(), _argc, _argv);
}
2016-04-08 21:04:50 +02:00
//! [gale_declare_main]
2015-07-16 21:34:35 +02:00
2016-04-08 21:04:50 +02:00
//! [gale_sample_all]