From e9a3efba18a85bbcd464a78c6f81ea74b274f175 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 27 Aug 2012 12:25:30 +0200 Subject: [PATCH] change the position of the shader : now in file, and create she shader and program loader --- Sources/libewol/ewol/Resource.h | 7 +- Sources/libewol/ewol/ResourceManager.cpp | 31 ++--- Sources/libewol/ewol/ResourceManager.h | 2 +- Sources/libewol/ewol/font/FontManager.cpp | 115 ---------------- Sources/libewol/ewol/font/FontManager.h | 7 - Sources/libewol/ewol/font/TexturedFont.cpp | 24 +++- Sources/libewol/ewol/font/TexturedFont.h | 3 +- .../libewol/ewol/oObject/2DTextColored.cpp | 5 +- Sources/libewol/ewol/openGL/Program.cpp | 96 ++++++++++++- Sources/libewol/ewol/openGL/Program.h | 6 +- Sources/libewol/ewol/openGL/Shader.cpp | 92 ++++++++++++- Sources/libewol/ewol/openGL/Shader.h | 2 + Sources/libewol/ewol/openGL/openGL.cpp | 129 ++++-------------- share/basicShader.frag | 6 + share/basicShader.prog | 4 + share/basicShader.vert | 7 + 16 files changed, 281 insertions(+), 255 deletions(-) create mode 100644 share/basicShader.frag create mode 100644 share/basicShader.prog create mode 100644 share/basicShader.vert diff --git a/Sources/libewol/ewol/Resource.h b/Sources/libewol/ewol/Resource.h index f6ea0312..11af891f 100644 --- a/Sources/libewol/ewol/Resource.h +++ b/Sources/libewol/ewol/Resource.h @@ -33,16 +33,17 @@ namespace ewol { // class resources is pure virtual class Resource { + protected: etk::UString m_name; uint32_t m_counter; public: Resource(etk::UString& filename) : m_name(filename), m_counter(1) { }; virtual ~Resource(void) { }; - bool HasName(etk::UString& fileName) { return fileName==m_name; }; - etk::UString GetName(void) { return m_name; }; + virtual bool HasName(etk::UString& fileName) { return fileName==m_name; }; + virtual etk::UString GetName(void) { return m_name; }; void Increment(void) { m_counter++; }; bool Decrement(void) { m_counter--; return (m_counter==0)?true:false; }; - virtual const char* GetType(void)=0; + virtual const char* GetType(void) { return "unknow"; }; }; }; diff --git a/Sources/libewol/ewol/ResourceManager.cpp b/Sources/libewol/ewol/ResourceManager.cpp index 1a103dbd..245d5113 100644 --- a/Sources/libewol/ewol/ResourceManager.cpp +++ b/Sources/libewol/ewol/ResourceManager.cpp @@ -54,11 +54,12 @@ void ewol::resource::UnInit(void) } // internal generic keeper ... -static ewol::Resource* LocalKeep(etk::UString& name) +static ewol::Resource* LocalKeep(etk::UString& filename) { + EWOL_DEBUG("KEEP : DEFAULT : file : \"" << filename << "\""); for (int32_t iii=l_resourceList.Size()-1; iii>=0; iii--) { if (l_resourceList[iii] != NULL) { - if(l_resourceList[iii]->HasName(name)) { + if(l_resourceList[iii]->HasName(filename)) { l_resourceList[iii]->Increment(); return l_resourceList[iii]; } @@ -70,26 +71,15 @@ static ewol::Resource* LocalKeep(etk::UString& name) // return the type of the resource ... -bool ewol::resource::Keep(etk::UString& filename, ewol::TexturedFont*& object, int32_t size) +bool ewol::resource::Keep(etk::UString& filename, ewol::TexturedFont*& object) { - object = NULL; - for (int32_t iii=l_resourceList.Size()-1; iii>=0; iii--) { - if (l_resourceList[iii] != NULL) { - if(l_resourceList[iii]->HasName(filename)) { - ewol::TexturedFont* tmpObject = static_cast(l_resourceList[iii]); - if (NULL!=tmpObject) { - if (tmpObject->getFontSize() == size) { - l_resourceList[iii]->Increment(); - object = static_cast(l_resourceList[iii]); - return false; - } - } - // good name but not good Size - } - } + EWOL_DEBUG("KEEP : TexturedFont : file : \"" << filename << "\""); + object = static_cast(LocalKeep(filename)); + if (NULL != object) { + return true; } // need to crate a new one ... - object = new ewol::TexturedFont(filename, size); + object = new ewol::TexturedFont(filename); if (NULL == object) { EWOL_ERROR("allocation error of a resource : " << filename); return false; @@ -101,6 +91,7 @@ bool ewol::resource::Keep(etk::UString& filename, ewol::TexturedFont*& object, i bool ewol::resource::Keep(etk::UString& filename, ewol::Font*& object) { + EWOL_DEBUG("KEEP : Font : file : \"" << filename << "\""); object = static_cast(LocalKeep(filename)); if (NULL != object) { return true; @@ -118,6 +109,7 @@ bool ewol::resource::Keep(etk::UString& filename, ewol::Font*& object) bool ewol::resource::Keep(etk::UString& filename, ewol::Program*& object) { + EWOL_DEBUG("KEEP : Program : file : \"" << filename << "\""); object = static_cast(LocalKeep(filename)); if (NULL != object) { return true; @@ -135,6 +127,7 @@ bool ewol::resource::Keep(etk::UString& filename, ewol::Program*& object) bool ewol::resource::Keep(etk::UString& filename, ewol::Shader*& object) { + EWOL_DEBUG("KEEP : Shader : file : \"" << filename << "\""); object = static_cast(LocalKeep(filename)); if (NULL != object) { return true; diff --git a/Sources/libewol/ewol/ResourceManager.h b/Sources/libewol/ewol/ResourceManager.h index b8be1445..8bc04271 100644 --- a/Sources/libewol/ewol/ResourceManager.h +++ b/Sources/libewol/ewol/ResourceManager.h @@ -40,7 +40,7 @@ namespace ewol void UnInit(void); // return the type of the resource ... - bool Keep(etk::UString& filename, ewol::TexturedFont*& object, int32_t size); + bool Keep(etk::UString& filename, ewol::TexturedFont*& object); bool Keep(etk::UString& filename, ewol::Font*& object); bool Keep(etk::UString& filename, ewol::Program*& object); bool Keep(etk::UString& filename, ewol::Shader*& object); diff --git a/Sources/libewol/ewol/font/FontManager.cpp b/Sources/libewol/ewol/font/FontManager.cpp index e5c1a7f6..0df1cf1a 100644 --- a/Sources/libewol/ewol/font/FontManager.cpp +++ b/Sources/libewol/ewol/font/FontManager.cpp @@ -29,19 +29,11 @@ #include #include -static etk::Vector l_fontList; -static etk::Vector l_fontTexturedList; -static etk::UString l_folderName = ""; static etk::UString l_defaultFontName = ""; static int32_t l_defaultFontSize = 10; void ewol::font::Init(void) { - // nothing to do in théory then, we clean the buffers : - // NOTE : If we do domething here, then the system does not work corectly - l_fontList.Clear(); - l_fontTexturedList.Clear(); - l_folderName = ""; l_defaultFontName = ""; l_defaultFontSize = 10; // Specific for free Font @@ -50,37 +42,10 @@ void ewol::font::Init(void) void ewol::font::UnInit(void) { - // remove textured font ... - for (int32_t iii=0; iii=0; iii--) { - if (l_fontList[iii] != NULL) { - if(l_fontList[iii]->HasName(fontName)) { - l_fontList[iii]->Increment(); - return l_fontList[iii]; - } - } - } - ewol::FontFreeType* tmpResources = new ewol::FontFreeType(l_folderName, fontName); - if (NULL == tmpResources) { - EWOL_ERROR("allocation error of a resource Font : " << l_folderName << "/" << fontName); - return NULL; - } - l_fontList.PushBack(tmpResources); - return tmpResources; -} - -void ewol::font::Release(ewol::Font*& object) -{ - for (int32_t iii=l_fontList.Size()-1; iii>=0; iii--) { - if (l_fontList[iii] != NULL) { - if(l_fontList[iii] == object) { - if (true == l_fontList[iii]->Decrement()) { - // delete element - delete(l_fontList[iii]); - // remove element from the list : - l_fontList.Erase(iii); - } - // insidiously remove the pointer for the caller ... - object = NULL; - return; - } - } - } -} - - -ewol::TexturedFont* ewol::font::TexturedKeep(etk::UString fontName, int32_t size) -{ - EWOL_VERBOSE("Textured font : try to find ... \"" << fontName << "\""); - for (int32_t iii=l_fontTexturedList.Size()-1; iii>=0; iii--) { - if (l_fontTexturedList[iii] != NULL) { - if(l_fontTexturedList[iii]->HasName(fontName, size)) { - EWOL_VERBOSE(" ==> Find prellocated"); - l_fontTexturedList[iii]->Increment(); - return l_fontTexturedList[iii]; - } - } - } - EWOL_VERBOSE(" ==> Create new One"); - ewol::TexturedFont* tmpResources = new ewol::TexturedFont(fontName, size); - if (NULL == tmpResources) { - EWOL_ERROR("allocation error of a resource textured Font : " << fontName); - return NULL; - } - l_fontTexturedList.PushBack(tmpResources); - return tmpResources; -} - -void ewol::font::TexturedRelease(ewol::TexturedFont*& object) -{ - for (int32_t iii=l_fontTexturedList.Size()-1; iii>=0; iii--) { - if (l_fontTexturedList[iii] != NULL) { - if(l_fontTexturedList[iii] == object) { - if (true == l_fontTexturedList[iii]->Decrement()) { - // delete element - delete(l_fontTexturedList[iii]); - // remove element from the list : - l_fontTexturedList.Erase(iii); - } - // insidiously remove the pointer for the caller ... - object = NULL; - return; - } - } - } -} - diff --git a/Sources/libewol/ewol/font/FontManager.h b/Sources/libewol/ewol/font/FontManager.h index 3df5523f..a661ce04 100644 --- a/Sources/libewol/ewol/font/FontManager.h +++ b/Sources/libewol/ewol/font/FontManager.h @@ -34,17 +34,10 @@ namespace ewol namespace font { void Init(void); void UnInit(void); - void SetFontFolder(etk::UString folderName); void SetDefaultFont(etk::UString fontName); etk::UString& GetDefaultFont(void); void SetDefaultSize(int32_t newSize); int32_t GetDefaultSize(void); - - ewol::Font* Keep(etk::UString fontName); - void Release(ewol::Font*& object); - - ewol::TexturedFont* TexturedKeep(etk::UString fontName, int32_t size); - void TexturedRelease(ewol::TexturedFont*& object); }; }; diff --git a/Sources/libewol/ewol/font/TexturedFont.cpp b/Sources/libewol/ewol/font/TexturedFont.cpp index 999fb66b..960ccb91 100644 --- a/Sources/libewol/ewol/font/TexturedFont.cpp +++ b/Sources/libewol/ewol/font/TexturedFont.cpp @@ -56,13 +56,22 @@ static int32_t simpleSQRT(int32_t value) } -ewol::TexturedFont::TexturedFont(etk::UString fontName, int32_t size) : +ewol::TexturedFont::TexturedFont(etk::UString fontName) : ewol::Resource(fontName), - m_size(size), m_font(NULL), m_lastGlyphPos(0,0), m_lastRawHeigh(0) { + char tmpName[1024] = ""; + int32_t tmpSize = 0; + // extarct name and size : + if (sscanf(fontName.c_str(), "%s:%d", tmpName, &tmpSize)!=2) { + m_size = 1; + EWOL_CRITICAL("Can not parse the font name : \"" << fontName << "\""); + return; + } + m_size = tmpSize; + m_name = tmpName; ewol::resource::Keep(fontName, m_font); if (NULL == m_font) { return; @@ -189,6 +198,17 @@ ewol::TexturedFont::~TexturedFont(void) } } + +bool ewol::TexturedFont::HasName(etk::UString& fileName) +{ + etk::UString tmpName = m_name; + tmpName += ":"; + tmpName += m_size; + return fileName==tmpName; +} + + + int32_t ewol::TexturedFont::Draw(Vector2D textPos, const etk::UString& unicodeString, etk::Vector > & coord, diff --git a/Sources/libewol/ewol/font/TexturedFont.h b/Sources/libewol/ewol/font/TexturedFont.h index 087ffc96..93139bf2 100644 --- a/Sources/libewol/ewol/font/TexturedFont.h +++ b/Sources/libewol/ewol/font/TexturedFont.h @@ -50,8 +50,9 @@ namespace ewol Vector2D m_lastGlyphPos; int32_t m_lastRawHeigh; public: - TexturedFont(etk::UString fontName, int32_t size); + TexturedFont(etk::UString fontName); ~TexturedFont(void); + virtual bool HasName(etk::UString& fileName); const char* GetType(void) { return "ewol::TexturedFont"; }; int32_t getFontSize(void) { return m_size; }; int32_t Draw(Vector2D textPos, diff --git a/Sources/libewol/ewol/oObject/2DTextColored.cpp b/Sources/libewol/ewol/oObject/2DTextColored.cpp index b3bf8868..0ea15b2a 100644 --- a/Sources/libewol/ewol/oObject/2DTextColored.cpp +++ b/Sources/libewol/ewol/oObject/2DTextColored.cpp @@ -38,8 +38,11 @@ void ewol::OObject2DTextColored::SetFontProperty(etk::UString fontName, int32_t ewol::resource::Release(m_font); m_font = NULL; } + etk::UString tmpName = fontName; + tmpName += ":"; + tmpName += fontSize; // link to new One - if (false == ewol::resource::Keep(fontName, m_font, fontSize)) { + if (false == ewol::resource::Keep(tmpName, m_font)) { } } diff --git a/Sources/libewol/ewol/openGL/Program.cpp b/Sources/libewol/ewol/openGL/Program.cpp index 5123fb1e..d375a012 100644 --- a/Sources/libewol/ewol/openGL/Program.cpp +++ b/Sources/libewol/ewol/openGL/Program.cpp @@ -25,18 +25,110 @@ #include #include #include +#include ewol::Program::Program(etk::UString& filename) : ewol::Resource(filename), - m_program(0), - m_needToReleaseShader(false) + m_program(0) { + // load data from file "all the time ..." + + etk::File file(m_name, etk::FILE_TYPE_DATA); + if (false == file.Exist()) { + EWOL_ERROR("File does not Exist : \"" << file << "\""); + return; + } + + etk::UString fileExtention = file.GetExtention(); + if (fileExtention != "prog") { + EWOL_ERROR("File does not have extention \".prog\" for program but : \"" << fileExtention << "\""); + return; + } + if (false == file.fOpenRead()) { + EWOL_ERROR("Can not open the file : " << file); + return; + } + #define MAX_LINE_SIZE (2048) + char tmpData[MAX_LINE_SIZE]; + while (file.fGets(tmpData, MAX_LINE_SIZE) != NULL) { + int32_t len = strlen(tmpData); + if( tmpData[len-1] == '\n' + || tmpData[len-1] == '\r') { + tmpData[len-1] = '\0'; + len--; + } + if (len == 0) { + continue; + } + etk::UString tmpFilename = tmpData; + ewol::Shader* tmpShader = NULL; + if (false == ewol::resource::Keep(tmpFilename, tmpShader)) { + EWOL_CRITICAL("Error while getting a specific shader filename : " << tmpFilename); + } else { + EWOL_DEBUG("Add shader on program : "<< tmpFilename); + m_shaderList.PushBack(tmpShader); + } + + } + // close the file: + file.fClose(); + + CreateAndLink(); + } ewol::Program::~Program(void) { + for (int32_t iii=0; iiiGetGL_ID()); + checkGlError("glAttachShader"); + } + } + glLinkProgram(m_program); + GLint linkStatus = GL_FALSE; + glGetProgramiv(m_program, GL_LINK_STATUS, &linkStatus); + if (linkStatus != GL_TRUE) { + GLint bufLength = 0; + l_bufferDisplayError[0] = '\0'; + glGetProgramInfoLog(m_program, LOG_OGL_INTERNAL_BUFFER_LEN, &bufLength, l_bufferDisplayError); + EWOL_ERROR("Could not compile \"PROGRAM\": " << l_bufferDisplayError); + glDeleteProgram(m_program); + m_program = 0; + return false; + } + return true; } diff --git a/Sources/libewol/ewol/openGL/Program.h b/Sources/libewol/ewol/openGL/Program.h index 801dcc4a..6f5e0b25 100644 --- a/Sources/libewol/ewol/openGL/Program.h +++ b/Sources/libewol/ewol/openGL/Program.h @@ -29,19 +29,21 @@ #include #include #include +#include namespace ewol { class Program : public ewol::Resource { private : - GLuint m_program; - bool m_needToReleaseShader; + GLuint m_program; + etk::Vector m_shaderList; public: Program(etk::UString& filename); virtual ~Program(void); const char* GetType(void) { return "ewol::Program"; }; GLuint GetGL_ID(void) { return m_program; }; + bool CreateAndLink(void); }; }; diff --git a/Sources/libewol/ewol/openGL/Shader.cpp b/Sources/libewol/ewol/openGL/Shader.cpp index 4c62cdb9..598d2b3c 100644 --- a/Sources/libewol/ewol/openGL/Shader.cpp +++ b/Sources/libewol/ewol/openGL/Shader.cpp @@ -23,6 +23,7 @@ */ #include +#include #include #include @@ -30,15 +31,104 @@ ewol::Shader::Shader(etk::UString& filename): ewol::Resource(filename), + m_fileData(NULL), m_shader(0), m_type(0) { + // load data from file "all the time ..." + + etk::File file(m_name, etk::FILE_TYPE_DATA); + if (false == file.Exist()) { + EWOL_ERROR("File does not Exist : \"" << file << "\""); + return; + } + + etk::UString fileExtention = file.GetExtention(); + if (fileExtention == "frag") { + m_type = GL_FRAGMENT_SHADER; + } else if (fileExtention == "vert") { + m_type = GL_VERTEX_SHADER; + } else { + EWOL_ERROR("File does not have extention \".vert\" for Vertex Shader or \".frag\" for Fragment Shader. but : \"" << fileExtention << "\""); + return; + } + + int32_t fileSize = file.Size(); + if (0==fileSize) { + EWOL_ERROR("This file is empty : " << file); + return; + } + if (false == file.fOpenRead()) { + EWOL_ERROR("Can not open the file : " << file); + return; + } + // allocate data + m_fileData = new char[fileSize+5]; + if (NULL == m_fileData) { + EWOL_ERROR("Error Memory allocation size=" << fileSize); + return; + } + memset(m_fileData, 0, (fileSize+5)*sizeof(char)); + // load data from the file : + file.fRead(m_fileData, 1, fileSize); + // close the file: + file.fClose(); + + CompileAndSendShader(); } + ewol::Shader::~Shader(void) { - + if (NULL != m_fileData) { + delete [] m_fileData; + m_fileData = NULL; + } + if (0!=m_shader) { + glDeleteShader(m_shader); + m_shader = 0; + } } +static void checkGlError(const char* op) +{ + for (GLint error = glGetError(); error; error = glGetError()) { + EWOL_INFO("after " << op << "() glError (" << error << ")"); + } +} +#define LOG_OGL_INTERNAL_BUFFER_LEN (8192) +static char l_bufferDisplayError[LOG_OGL_INTERNAL_BUFFER_LEN] = ""; + +bool ewol::Shader::CompileAndSendShader(void) +{ + if (NULL == m_fileData) { + m_shader = 0; + return false; + } + m_shader = glCreateShader(m_type); + if (!m_shader) { + EWOL_ERROR("glCreateShader return error ..."); + checkGlError("glCreateShader"); + return false; + } else { + glShaderSource(m_shader, 1, (const char**)&m_fileData, NULL); + glCompileShader(m_shader); + GLint compiled = 0; + glGetShaderiv(m_shader, GL_COMPILE_STATUS, &compiled); + if (!compiled) { + GLint infoLen = 0; + l_bufferDisplayError[0] = '\0'; + glGetShaderInfoLog(m_shader, LOG_OGL_INTERNAL_BUFFER_LEN, &infoLen, l_bufferDisplayError); + const char * tmpShaderType = "GL_FRAGMENT_SHADER"; + if (m_type == GL_VERTEX_SHADER){ + tmpShaderType = "GL_VERTEX_SHADER"; + } + EWOL_ERROR("Could not compile \"" << tmpShaderType << "\": " << l_bufferDisplayError); + return false; + } + } + return true; +} + diff --git a/Sources/libewol/ewol/openGL/Shader.h b/Sources/libewol/ewol/openGL/Shader.h index 96442372..3b7c76a0 100644 --- a/Sources/libewol/ewol/openGL/Shader.h +++ b/Sources/libewol/ewol/openGL/Shader.h @@ -35,6 +35,7 @@ namespace ewol class Shader : public ewol::Resource { private : + char* m_fileData; GLuint m_shader; GLenum m_type; public: @@ -43,6 +44,7 @@ namespace ewol const char* GetType(void) { return "ewol::Shader"; }; GLuint GetGL_ID(void) { return m_shader; }; GLenum GetShaderType(void) { return m_type; }; + bool CompileAndSendShader(void); }; }; diff --git a/Sources/libewol/ewol/openGL/openGL.cpp b/Sources/libewol/ewol/openGL/openGL.cpp index e542392a..4ef411c8 100644 --- a/Sources/libewol/ewol/openGL/openGL.cpp +++ b/Sources/libewol/ewol/openGL/openGL.cpp @@ -71,87 +71,10 @@ static void checkGlError(const char* op) } } -static const char gVertexShader[] = - "attribute vec4 vPosition;\n" - "void main() {\n" - " gl_Position = vPosition;\n" - "}\n"; +#include +#include -static const char gFragmentShader[] = - /*"precision mediump float;\n"*/ - "void main() {\n" - " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n" - "}\n"; - -#define LOG_OGL_INTERNAL_BUFFER_LEN (8192) -static char l_bufferDisplayError[LOG_OGL_INTERNAL_BUFFER_LEN] = ""; - -GLuint loadShader(GLenum shaderType, const char* pSource) -{ - GLuint shader = glCreateShader(shaderType); - if (!shader) { - EWOL_ERROR("glCreateShader return error ..."); - checkGlError("glCreateShader"); - } else { - glShaderSource(shader, 1, &pSource, NULL); - glCompileShader(shader); - GLint compiled = 0; - glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); - if (!compiled) { - GLint infoLen = 0; - l_bufferDisplayError[0] = '\0'; - glGetShaderInfoLog(shader, LOG_OGL_INTERNAL_BUFFER_LEN, &infoLen, l_bufferDisplayError); - const char * tmpShaderType = "GL_FRAGMENT_SHADER"; - if (shaderType == GL_VERTEX_SHADER){ - tmpShaderType = "GL_VERTEX_SHADER"; - } - EWOL_ERROR("Could not compile \"" << tmpShaderType << "\": " << l_bufferDisplayError); - } - } - return shader; -} - -GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) -{ - EWOL_INFO("Create the VERTEX shader ..."); - GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); - if (!vertexShader) { - EWOL_ERROR("VERTEX shader return error ..."); - return 0; - } - EWOL_INFO("Create the FRAGMENT shader ..."); - GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); - if (!pixelShader) { - EWOL_ERROR("FRAGMENT shader return error ..."); - return 0; - } - - EWOL_INFO("Create the Program ..."); - GLuint program = glCreateProgram(); - if (!program) { - EWOL_ERROR("program creation return error ..."); - - } else { - glAttachShader(program, vertexShader); - checkGlError("glAttachShader"); - glAttachShader(program, pixelShader); - checkGlError("glAttachShader"); - glLinkProgram(program); - GLint linkStatus = GL_FALSE; - glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); - if (linkStatus != GL_TRUE) { - GLint bufLength = 0; - l_bufferDisplayError[0] = '\0'; - glGetProgramInfoLog(program, LOG_OGL_INTERNAL_BUFFER_LEN, &bufLength, l_bufferDisplayError); - EWOL_ERROR("Could not compile \"PROGRAM\": " << l_bufferDisplayError); - glDeleteProgram(program); - program = 0; - } - } - return program; -} - -GLuint gProgram; +ewol::Program* l_program = NULL; GLuint gvPositionHandle; bool TESTsetupGraphics(int w, int h) @@ -162,42 +85,46 @@ bool TESTsetupGraphics(int w, int h) printGLString("Extensions", GL_EXTENSIONS); EWOL_INFO("setupGraphics("<< w << "," << h); - gProgram = createProgram(gVertexShader, gFragmentShader); - if (!gProgram) { - EWOL_ERROR("Could not create program."); - return false; - } - gvPositionHandle = glGetAttribLocation(gProgram, "vPosition"); - checkGlError("glGetAttribLocation"); - EWOL_INFO("glGetAttribLocation(\"vPosition\") = " << gvPositionHandle); - glViewport(0, 0, w, h); checkGlError("glViewport"); return true; } + +bool isLoaded = false; +void basicLoad(void) +{ + if (isLoaded==false) { + etk::UString tmpString("basicShader.prog"); + if (true == ewol::resource::Keep(tmpString, l_program) ) { + gvPositionHandle = glGetAttribLocation(l_program->GetGL_ID(), "vPosition"); + checkGlError("glGetAttribLocation"); + EWOL_INFO("glGetAttribLocation(\"vPosition\") = " << gvPositionHandle); + } + isLoaded = true; + } +} + + const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f }; //const GLfloat gTriangleVertices[] = { 0.0f, 0.0f, 200.0f, 0.0f, 0.0f, 200.0f }; -void TEST_renderFrame(void) { +void TEST_renderFrame(void) +{ static float grey = 0.5; - + basicLoad(); grey += 0.01f; if (grey > 1.0f) { grey = 0.0f; } glClearColor(grey, grey, grey, 1.0f); - checkGlError("glClearColor"); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - checkGlError("glClear"); - - glUseProgram(gProgram); - checkGlError("glUseProgram"); - - glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices); - checkGlError("glVertexAttribPointer"); + if (NULL != l_program) { + glUseProgram(l_program->GetGL_ID()); + checkGlError("glUseProgram"); + } + glVertexAttribPointer( gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices); glEnableVertexAttribArray(gvPositionHandle); - checkGlError("glEnableVertexAttribArray"); glDrawArrays(GL_TRIANGLES, 0, 3); - checkGlError("glDrawArrays"); + } \ No newline at end of file diff --git a/share/basicShader.frag b/share/basicShader.frag new file mode 100644 index 00000000..4b00534b --- /dev/null +++ b/share/basicShader.frag @@ -0,0 +1,6 @@ + + +void main() { + gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); +} + diff --git a/share/basicShader.prog b/share/basicShader.prog new file mode 100644 index 00000000..40e03d34 --- /dev/null +++ b/share/basicShader.prog @@ -0,0 +1,4 @@ +basicShader.vert +basicShader.frag + + diff --git a/share/basicShader.vert b/share/basicShader.vert new file mode 100644 index 00000000..c545f3a9 --- /dev/null +++ b/share/basicShader.vert @@ -0,0 +1,7 @@ + +attribute vec4 vPosition; + +void main() { + gl_Position = vPosition; +} +