Commit 7d8cee23 authored by Sylvain Thery's avatar Sylvain Thery

upage ogl3 divers

parent d268a9bb
......@@ -41,11 +41,13 @@
#include "Algo/Parallel/parallel_foreach.h"
#include "averaging.h"
using namespace CGoGN ;
/**
* Struct that contains some informations about the types of the manipulated objects
* Mainly here to be used by the algorithms that are parameterized by it
......
......@@ -52,37 +52,38 @@ struct PFP: public PFP_STANDARD
};
const unsigned int GL_ATTRIB_POSITION=0;
const unsigned int GL_ATTRIB_NORMAL=1;
/**
* A class for a little interface and rendering
*/
class MyGlutWin: public Utils::SimpleGlutWinGL3
{
public:
void myRedraw();
void initGLAttribs();
void renderColor();
void renderPhong();
PFP::REAL gWidthObj;
PFP::VEC3 gPosObj;
void myRedraw();
void myKeyboard(unsigned char keycode, int x, int y);
void renderColor();
Algo::Render::GL3::MapRender* m_render;
void renderPhong();
Utils::GLSLShader* current_shader;
Utils::GLSLShader shaders[4];
void initPhongMaterial();
PFP::REAL gWidthObj;
PFP::VEC3 gPosObj;
Algo::Render::GL3::MapRender* m_render;
MyGlutWin(int* argc, char **argv, int winX, int winY) : SimpleGlutWinGL3(argc, argv, winX, winY)
Utils::GLSLShader* current_shader;
Utils::GLSLShader shaders[4];
int rt;
MyGlutWin(int* argc, char **argv, int winX, int winY) : SimpleGlutWinGL3(argc, argv, winX, winY),rt(0)
{
shaders[0].loadShaders("color_gl3.vert","color_gl3.frag");
shaders[1].loadShaders("phong_gl3.vert","phong_gl3.frag");
current_shader = &shaders[1];
current_shader = &shaders[0];
setCurrentShader(current_shader);
}
~MyGlutWin()
......@@ -91,30 +92,38 @@ public:
}
};
void MyGlutWin::initGLAttribs()
void MyGlutWin::initPhongMaterial()
{
shaders[0].bindAttrib(GL_ATTRIB_POSITION, "VertexPosition");
float diffuse[] = {0.0f,1.0f,0.0f,0.0f};
float specular[] = {0.3f,0.3f,1.0f,0.0f};
float ambient[] = {0.1f,0.03f,0.03f,0.0f};
float shininess = 100.0f;
float lightPos[] = {100.0,300.0,1000.0f};
shaders[1].bind();
shaders[1].setuniformf<4>("materialDiffuse",diffuse);
shaders[1].setuniformf<4>("materialSpecular",specular);
shaders[1].setuniformf<4>("materialAmbient",ambient);
shaders[1].setuniformf<1>("shininess",&shininess);
shaders[1].setuniformf<3>("LightPosition",lightPos);
shaders[1].bindAttrib(GL_ATTRIB_POSITION, "VertexPosition");
shaders[1].bindAttrib(GL_ATTRIB_NORMAL, "VertexNormal");
}
void MyGlutWin::renderColor()
{
current_shader = &shaders[0];
// setModelViewProjectionMatrix(current_shader);
setCurrentShader(current_shader);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_LIGHTING);
current_shader->bind();
float colorLine[] = {1.0f,1.0f,0.0f,0.0f};
float colorFace[] = {0.0f,1.0f,0.0f,0.0f};
// draw the lines
glLineWidth(2.0f);
float colorLine[] = {1.0f,1.0f,0.0f,0.0f};
current_shader->setuniformf<4>("Color",colorLine);
m_render->draw(Algo::Render::GL3::LINES);
......@@ -122,6 +131,7 @@ void MyGlutWin::renderColor()
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 1.0f);
float colorFace[] = {0.1f,0.1f,0.3f,0.0f};
current_shader->setuniformf<4>("Color",colorFace);
m_render->draw(Algo::Render::GL3::TRIANGLES);
......@@ -131,28 +141,12 @@ void MyGlutWin::renderColor()
void MyGlutWin::renderPhong()
{
current_shader = &shaders[1];
setCurrentShader(current_shader);
// setModelViewProjectionMatrix(current_shader);
float diffuse[] = {0.0f,1.0f,0.0f,0.0f};
float specular[] = {0.3f,0.3f,1.0f,0.0f};
float ambient[] = {0.1f,0.03f,0.03f,0.0f};
float shininess = 100.0f;
float lightPos[] = {100.0,300.0,1000.0f};
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_LIGHTING);
current_shader->bind();
current_shader->setuniformf<4>("materialDiffuse",diffuse);
current_shader->setuniformf<4>("materialSpecular",specular);
current_shader->setuniformf<4>("materialAmbient",ambient);
current_shader->setuniformf<1>("shininess",&shininess);
current_shader->setuniformf<3>("LightPosition",lightPos);
m_render->draw(Algo::Render::GL3::TRIANGLES);
}
......@@ -162,11 +156,43 @@ void MyGlutWin::myRedraw()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// oglPushModelViewMatrix();
renderPhong();
switch(rt)
{
case 0:
renderColor();
break;
case 1:
renderPhong();
break;
}
// oglPopModelViewMatrix();
}
void MyGlutWin::myKeyboard(unsigned char keycode, int x, int y)
{
switch(keycode)
{
case 'c':
rt=0;
current_shader = &shaders[0];
setCurrentShader(current_shader);
std::cout << "Color"<< std::endl;
break;
case 'p':
rt=1;
current_shader = &shaders[1];
setCurrentShader(current_shader);
std::cout << "Phong"<< std::endl;
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
......@@ -187,6 +213,8 @@ int main(int argc, char **argv)
mgw.init();
glClearColor(0.1,0.1,0.3,0.0);
// computation of the bounding box
Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
mgw.gWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
......@@ -197,16 +225,23 @@ int main(int argc, char **argv)
// instanciation of the renderer (here using VBOs)
mgw.m_render = new Algo::Render::GL3::MapRender();
// declare names attributes of shaders
mgw.m_render->useVertexAttributeName("VertexPosition",mgw.shaders[0]);
mgw.m_render->useVertexAttributeName("VertexPosition",mgw.shaders[1]);
unsigned int attNormalId = mgw.m_render->useVertexAttributeName("VertexNormal",mgw.shaders[1]);
// send data to gc
mgw.m_render->updateData("VertexPosition", position); // with name
mgw.m_render->updateData(attNormalId, normal); //with Id
// update the renderer (primitives and geometry)
SelectorTrue allDarts;
mgw.m_render->initPrimitives<PFP>(myMap, allDarts,Algo::Render::GL3::TRIANGLES);
mgw.m_render->initPrimitives<PFP>(myMap, allDarts,Algo::Render::GL3::LINES);
mgw.m_render->initPrimitives<PFP>(myMap, allDarts,Algo::Render::GL3::POINTS);
mgw.m_render->updateData(GL_ATTRIB_POSITION, position);
mgw.m_render->updateData(GL_ATTRIB_NORMAL, normal);
mgw.initGLAttribs();
mgw.initPhongMaterial();
mgw.mainLoop();
......
......@@ -35,6 +35,8 @@
#include "Container/convert.h"
#include "Geometry/vector_gen.h"
#include "Utils/GLSLShader.h"
namespace CGoGN
{
......@@ -83,6 +85,14 @@ protected:
unsigned int m_AttributesDataSize[NB_BUFFERS];
std::map<std::string,GLuint> m_attributebyName;
/**
* number of vertex attributes
*/
GLuint m_nbVertexAttrib ;
/**
* number of indices of triangles
*/
......@@ -119,27 +129,59 @@ public:
*/
~MapRender() ;
public:
/**
* update the data
* @param uptype that have to be updated: POSITIONS, NORMALS, COLORS, TEXCOORDS, ???
* @param attribId attribute where data is stored
* @param vertex_attrib vertex attrib id
* @param attrib attribute where data is stored
* @param conv Callback of attribute conversion (NULL if direct copy, default value)
*/
template <typename ATTR_HANDLER>
void updateData(unsigned int vertex_attrib, const ATTR_HANDLER& attrib, ConvertAttrib* conv = NULL) ;
/**
* update the data
* @param va_name vertex attrib name (in shader)
* @param attrib attribute where data is stored
* @param conv Callback of attribute conversion (NULL if direct copy, default value)
*/
template <typename ATTR_HANDLER>
void updateData(const std::string& name, const ATTR_HANDLER& attrib, ConvertAttrib* conv = NULL) ;
/**
* enable a vertex attribute for rendering (updateDate automatically enable attrib)
*/
void enableVertexAttrib(unsigned int index);
void enableVertexAttrib(const std::string& name);
/**
* disable a vertex attribute for rendering
*/
void disableVertexAttrib(unsigned int index);
void disableVertexAttrib(const std::string& name);
/**
* associate a name to a vertex attribute
* @param name the name in shader
* @param sh the shader
* @return the id to use with update (if not using name)
*/
unsigned int useVertexAttributeName(const std::string& name, const Utils::GLSLShader& sh);
protected:
/**
* enable a vertex attribute for rendering (updateDate automatically enable attrib)
*/
void enableVertexAttrib(unsigned int index);
/**
* disable a vertex attribute for rendering
*/
void disableVertexAttrib(unsigned int index);
/**
* fill buffer directly from attribute
*/
......
......@@ -37,6 +37,60 @@ namespace GL3
{
// inline functions:
inline void MapRender::enableVertexAttrib(unsigned int index)
{
m_usedAttributes[index] = true ;
}
inline void MapRender::disableVertexAttrib(unsigned int index)
{
m_usedAttributes[index] = false ;
}
/**
* enable a vertex attribute for rendering (updateDate automatically enable attrib)
*/
inline void MapRender::enableVertexAttrib(const std::string& name)
{
std::map<std::string,unsigned int>::iterator it = m_attributebyName.find(name);
if (it != m_attributebyName.end())
enableVertexAttrib(it->second);
else
std::cerr <<"enableVertexAttrib: unknown attribute "<< name << std::endl;
}
inline void MapRender::disableVertexAttrib(const std::string& name)
{
std::map<std::string,unsigned int>::iterator it = m_attributebyName.find(name);
if (it != m_attributebyName.end())
disableVertexAttrib(it->second);
else
std::cerr <<"disableVertexAttrib: unknown attribute "<< name << std::endl;
}
inline unsigned int MapRender::useVertexAttributeName(const std::string& name, const Utils::GLSLShader& sh)
{
unsigned int vertex_attrib =0;
std::map<std::string,unsigned int>::iterator it = m_attributebyName.find(name);
if (it == m_attributebyName.end())
{
vertex_attrib = m_nbVertexAttrib++;
m_attributebyName.insert(std::pair<std::string,unsigned int>(name,vertex_attrib));
}
else
vertex_attrib = it->second;
sh.bindAttrib(vertex_attrib,name.c_str());
return vertex_attrib;
}
template <typename ATTR_HANDLER>
void MapRender::updateData(unsigned int vertex_attrib, const ATTR_HANDLER& attrib, ConvertAttrib* conv)
......@@ -64,6 +118,30 @@ void MapRender::updateData(unsigned int vertex_attrib, const ATTR_HANDLER& attri
template <typename ATTR_HANDLER>
void MapRender::updateData(const std::string& name, const ATTR_HANDLER& attrib, ConvertAttrib* conv)
{
unsigned int vertex_attrib = 0;
std::map<std::string,unsigned int>::iterator it = m_attributebyName.find(name);
if (it == m_attributebyName.end())
{
vertex_attrib = m_nbVertexAttrib++;
m_attributebyName.insert(std::pair<std::string,unsigned int>(name,vertex_attrib));
std::cerr << "warning update data with unknown name, adding vertex attribute"<< std::endl;
}
else
{
vertex_attrib = it->second;
}
updateData<ATTR_HANDLER>(vertex_attrib,attrib,conv);
}
template <typename ATTR_HANDLER>
void MapRender::fillBufferDirect(unsigned int indexVBO, const ATTR_HANDLER& attrib)
{
......
......@@ -217,7 +217,7 @@ public:
/**
* bind vertex attribute with its name in shaders
*/
void bindAttrib(unsigned int att, const char* name);
void bindAttrib(unsigned int att, const char* name) const;
bool validateProgram();
......
......@@ -31,9 +31,16 @@
#define __X_GL_H
#define GLAPIENTRY
#include <GL/freeglut.h>
#ifndef WIN32
#include <GL/glx.h>
#ifdef MAC_OSX
#include <OpenGL/CGLCurrent.h>
#else
#include <GL/glx.h>
#endif
#endif
#include "Utils/trackball.h"
#include <IL/ilu.h>
#include <IL/ilut.h>
......@@ -114,9 +121,14 @@ protected:
HDC m_drawable;
HGLRC m_context;
#else
Display* m_dpy;
GLXDrawable m_drawable;
GLXContext m_context;
#ifdef MAC_OSX
CGLContextObj m_context;
#else
Display* m_dpy;
GLXDrawable m_drawable;
GLXContext m_context;
#endif
#endif
public:
......@@ -131,7 +143,7 @@ public:
/**
* constructor
*/
SimpleGlutWin(int* argc, char **argv, int winX, int winY, bool gl3=false);
SimpleGlutWin(int* argc, char **argv, int winX, int winY);
/**
* destructor
......
......@@ -112,6 +112,9 @@ protected:
// table of shaders
static Utils::GLSLShader* m_current_shaders;
static void recalcModelView();
// internal callbacks
static void redraw(void);
......@@ -127,14 +130,9 @@ protected:
static void keyboard(unsigned char keycode, int x, int y);
static void recalcModelView();
static SimpleGlutWinGL3* instance;
protected:
static GLfloat lightZeroPosition[4];
static GLfloat lightZeroColor[4];
virtual void myKeyboard(unsigned char, int, int) {}
......
......@@ -37,8 +37,11 @@ namespace GL3
{
MapRender::MapRender():
m_nbVertexAttrib(0),
m_nbIndicesTri(0),
m_nbIndicesLines(0)
m_nbIndicesLines(0),
m_nbIndicesPoints(0),
m_nbFlatElts(0)
{
glGenBuffersARB(4, m_VBOBuffers) ;
......@@ -56,7 +59,7 @@ MapRender::~MapRender()
glDeleteBuffersARB(4, m_VBOBuffers);
delete[] m_VBOBuffers ;
for(unsigned int i = 4; i < NB_BUFFERS; ++i)
for(unsigned int i = 4; i < 4+m_nbVertexAttrib; ++i)
{
if (m_allocatedAttributes[i])
glDeleteBuffersARB(4, &(m_VBOBuffers[i]));
......@@ -65,8 +68,11 @@ MapRender::~MapRender()
MapRender::MapRender(const MapRender& mrvbo):
m_nbVertexAttrib(mrvbo.m_nbVertexAttrib),
m_nbIndicesTri(0),
m_nbIndicesLines(0)
m_nbIndicesLines(0),
m_nbIndicesPoints(0),
m_nbFlatElts(0)
{
glGenBuffersARB(3, m_VBOBuffers) ; // gen only for indices
// get back others from mrvbo
......@@ -79,15 +85,6 @@ MapRender::MapRender(const MapRender& mrvbo):
}
void MapRender::enableVertexAttrib(unsigned int index)
{
m_usedAttributes[index] = true ;
}
void MapRender::disableVertexAttrib(unsigned int index)
{
m_usedAttributes[index] = false ;
}
void MapRender::initPrimitives(int prim, std::vector<GLuint>& tableIndices)
......@@ -122,24 +119,21 @@ void MapRender::initPrimitives(int prim, std::vector<GLuint>& tableIndices)
void MapRender::drawTriangles(bool bindColors)
{
for(unsigned int i = FIRST_ATTRIBUTE_BUFFER; i < NB_BUFFERS; ++i)
for(unsigned int j = 0; j < m_nbVertexAttrib; ++j)
{
unsigned int j = i-FIRST_ATTRIBUTE_BUFFER;
if(m_usedAttributes[j])
{
glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[i]);
glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[j+FIRST_ATTRIBUTE_BUFFER]);
glEnableVertexAttribArray(j);
glVertexAttribPointer(j, m_AttributesDataSize[j], GL_FLOAT, false, 0, 0);
}
}
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, m_VBOBuffers[TRIANGLE_INDICES]);
// glEnableClientState(GL_INDEX_ARRAY);
glDrawElements(GL_TRIANGLES, m_nbIndicesTri, GL_UNSIGNED_INT, 0);
// glDisableClientState(GL_INDEX_ARRAY);
for(unsigned int j = 0; j < NB_BUFFERS-FIRST_ATTRIBUTE_BUFFER; ++j)
for(unsigned int j = 0; j < m_nbVertexAttrib; ++j)
{
if(m_usedAttributes[j])
{
......@@ -150,12 +144,11 @@ void MapRender::drawTriangles(bool bindColors)
void MapRender::drawLines(bool bindColors)
{
for(unsigned int i = FIRST_ATTRIBUTE_BUFFER; i < NB_BUFFERS; ++i)
for(unsigned int j = 0; j < m_nbVertexAttrib; ++j)
{
unsigned int j = i-FIRST_ATTRIBUTE_BUFFER;
if(m_usedAttributes[j])
{
glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[i]);
glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[j+FIRST_ATTRIBUTE_BUFFER]);
glEnableVertexAttribArray(j);
glVertexAttribPointer(j, m_AttributesDataSize[j], GL_FLOAT, false, 0, 0);
......@@ -167,7 +160,7 @@ void MapRender::drawLines(bool bindColors)
glDrawElements(GL_LINES, m_nbIndicesLines, GL_UNSIGNED_INT, 0);
// glDisableClientState(GL_INDEX_ARRAY);
for(unsigned int j = 0; j < NB_BUFFERS-FIRST_ATTRIBUTE_BUFFER; ++j)
for(unsigned int j = 0; j < m_nbVertexAttrib; ++j)
{
if(m_usedAttributes[j])
{
......@@ -178,23 +171,21 @@ void MapRender::drawLines(bool bindColors)
void MapRender::drawPoints(bool bindColors)
{
for(unsigned int i = FIRST_ATTRIBUTE_BUFFER; i < NB_BUFFERS; ++i)
for(unsigned int j = 0; j < m_nbVertexAttrib; ++j)
{
unsigned int j = i-FIRST_ATTRIBUTE_BUFFER;
if(m_usedAttributes[j])
{
glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[i]);
glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[j+FIRST_ATTRIBUTE_BUFFER]);
glEnableVertexAttribArray(j);
glVertexAttribPointer(j, m_AttributesDataSize[j], GL_FLOAT, false, 0, 0);
}
}
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, m_VBOBuffers[POINT_INDICES]);
// glEnableClientState(GL_INDEX_ARRAY);
glDrawElements(GL_POINTS, m_nbIndicesPoints, GL_UNSIGNED_INT, 0) ;
// glDisableClientState(GL_INDEX_ARRAY);
for(unsigned int j = 0; j < NB_BUFFERS-FIRST_ATTRIBUTE_BUFFER; ++j)
for(unsigned int j = 0; j < m_nbVertexAttrib; ++j)
{
if(m_usedAttributes[j])
{
......
......@@ -645,7 +645,7 @@ bool GLSLShader::checkShader(int shaderType)
void GLSLShader::bindAttrib(unsigned int att, const char* name)
void GLSLShader::bindAttrib(unsigned int att, const char* name) const
{
glBindAttribLocation(m_program_object,att,name);
}
......
......@@ -60,7 +60,7 @@ int SimpleGlutWin::W=0;
int SimpleGlutWin::H=0;
bool SimpleGlutWin::m_noMouse=false;
SimpleGlutWin::SimpleGlutWin(int* argc, char **argv, int winX, int winY, bool gl3)
SimpleGlutWin::SimpleGlutWin(int* argc, char **argv, int winX, int winY)
{
instance = this;
spinning = 0;
......@@ -71,11 +71,6 @@ SimpleGlutWin::SimpleGlutWin(int* argc, char **argv, int winX, int winY, bool gl
trans_y=0.;
trans_z=-50.0f;
if