change the position of the shader : now in file, and create she shader and program loader
This commit is contained in:
parent
1d5be4e953
commit
e9a3efba18
@ -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"; };
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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<ewol::TexturedFont*>(l_resourceList[iii]);
|
||||
if (NULL!=tmpObject) {
|
||||
if (tmpObject->getFontSize() == size) {
|
||||
l_resourceList[iii]->Increment();
|
||||
object = static_cast<ewol::TexturedFont*>(l_resourceList[iii]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// good name but not good Size
|
||||
}
|
||||
}
|
||||
EWOL_DEBUG("KEEP : TexturedFont : file : \"" << filename << "\"");
|
||||
object = static_cast<ewol::TexturedFont*>(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<ewol::Font*>(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<ewol::Program*>(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<ewol::Shader*>(LocalKeep(filename));
|
||||
if (NULL != object) {
|
||||
return true;
|
||||
|
@ -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);
|
||||
|
@ -29,19 +29,11 @@
|
||||
#include <ewol/font/FontFreeType.h>
|
||||
#include <ewol/font/TexturedFont.h>
|
||||
|
||||
static etk::Vector<ewol::Font*> l_fontList;
|
||||
static etk::Vector<ewol::TexturedFont*> 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<l_fontTexturedList.Size(); iii++) {
|
||||
if (l_fontTexturedList[iii] != NULL) {
|
||||
delete(l_fontTexturedList[iii]);
|
||||
l_fontTexturedList[iii] = NULL;
|
||||
}
|
||||
}
|
||||
l_fontTexturedList.Clear();
|
||||
// remove fonts :
|
||||
for (int32_t iii=0; iii<l_fontList.Size(); iii++) {
|
||||
if (l_fontList[iii] != NULL) {
|
||||
delete(l_fontList[iii]);
|
||||
l_fontList[iii] = NULL;
|
||||
}
|
||||
}
|
||||
l_fontList.Clear();
|
||||
// Specific for free Font
|
||||
ewol::FreeTypeUnInit();
|
||||
}
|
||||
|
||||
void ewol::font::SetFontFolder(etk::UString folderName)
|
||||
{
|
||||
if (l_folderName != "") {
|
||||
EWOL_WARNING("Change the FontFolder, old=\"" << l_folderName << "\"");
|
||||
}
|
||||
EWOL_TODO("Check if folder exist");
|
||||
l_folderName = folderName;
|
||||
EWOL_INFO("New default font folder name=\"" << l_folderName << "\"");
|
||||
}
|
||||
|
||||
|
||||
void ewol::font::SetDefaultFont(etk::UString fontName)
|
||||
{
|
||||
l_defaultFontName = fontName;
|
||||
@ -102,83 +67,3 @@ int32_t ewol::font::GetDefaultSize(void)
|
||||
}
|
||||
|
||||
|
||||
ewol::Font* ewol::font::Keep(etk::UString fontName)
|
||||
{
|
||||
for (int32_t iii=l_fontList.Size()-1; 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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<float> textPos,
|
||||
const etk::UString& unicodeString,
|
||||
etk::Vector<Vector2D<float> > & coord,
|
||||
|
@ -50,8 +50,9 @@ namespace ewol
|
||||
Vector2D<int32_t> 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<float> textPos,
|
||||
|
@ -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)) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -25,18 +25,110 @@
|
||||
#include <etk/Types.h>
|
||||
#include <ewol/Debug.h>
|
||||
#include <ewol/openGL/Program.h>
|
||||
#include <ewol/ResourceManager.h>
|
||||
|
||||
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; iii<m_shaderList.Size(); iii++) {
|
||||
ewol::resource::Release(m_shaderList[iii]);
|
||||
m_shaderList[iii] = 0;
|
||||
}
|
||||
m_shaderList.Clear();
|
||||
if (0!=m_program) {
|
||||
glDeleteProgram(m_program);
|
||||
m_program = 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::Program::CreateAndLink(void)
|
||||
{
|
||||
EWOL_INFO("Create the Program ...");
|
||||
m_program = glCreateProgram();
|
||||
if (0 == m_program) {
|
||||
EWOL_ERROR("program creation return error ...");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int32_t iii=0; iii<m_shaderList.Size(); iii++) {
|
||||
if (NULL != m_shaderList[iii]) {
|
||||
glAttachShader(m_program, m_shaderList[iii]->GetGL_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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -29,19 +29,21 @@
|
||||
#include <ewol/Debug.h>
|
||||
#include <ewol/Resource.h>
|
||||
#include <ewol/openGL/openGL.h>
|
||||
#include <ewol/openGL/Shader.h>
|
||||
|
||||
namespace ewol
|
||||
{
|
||||
class Program : public ewol::Resource
|
||||
{
|
||||
private :
|
||||
GLuint m_program;
|
||||
bool m_needToReleaseShader;
|
||||
GLuint m_program;
|
||||
etk::Vector<ewol::Shader*> 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);
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
*/
|
||||
|
||||
#include <etk/Types.h>
|
||||
#include <etk/File.h>
|
||||
#include <ewol/Debug.h>
|
||||
#include <ewol/openGL/Shader.h>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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 <ewol/openGL/Program.h>
|
||||
#include <ewol/ResourceManager.h>
|
||||
|
||||
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");
|
||||
|
||||
}
|
6
share/basicShader.frag
Normal file
6
share/basicShader.frag
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
void main() {
|
||||
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
|
||||
}
|
||||
|
4
share/basicShader.prog
Normal file
4
share/basicShader.prog
Normal file
@ -0,0 +1,4 @@
|
||||
basicShader.vert
|
||||
basicShader.frag
|
||||
|
||||
|
7
share/basicShader.vert
Normal file
7
share/basicShader.vert
Normal file
@ -0,0 +1,7 @@
|
||||
|
||||
attribute vec4 vPosition;
|
||||
|
||||
void main() {
|
||||
gl_Position = vPosition;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user