Commit 344591d2 authored by zmoelnig's avatar zmoelnig
Browse files

[model] now supports UV-textures


git-svn-id: https://pd-gem.svn.sourceforge.net/svnroot/pd-gem/trunk/Gem@2648 49b2a0ca-c541-4d49-a7ef-d785e4371512
parent 0dc2785a
......@@ -27,14 +27,16 @@ CPPEXTERN_NEW_WITH_ONE_ARG(model, t_symbol *, A_DEFSYM)
//
/////////////////////////////////////////////////////////
model :: model(t_symbol *filename)
: m_model(0), m_dispList(0), m_rescaleModel(1), m_smooth(90), m_flags(GLM_SMOOTH | GLM_TEXTURE)
: m_model(0), m_dispList(0),
m_rescaleModel(1), m_smooth(90), m_material(0),
m_flags(GLM_SMOOTH | GLM_TEXTURE),
m_group(0),
m_rebuild(true),
m_currentH(1.f), m_currentW(1.f),
m_textype(GLM_TEX_DEFAULT)
{
// make sure that there are some characters
m_model = 0;
m_group = 0;
currentH = 1.f;
currentW = 1.f;
if (filename->s_name[0]) openMess(filename);
if (filename&&filename->s_name&&*filename->s_name) openMess(filename);
}
/////////////////////////////////////////////////////////
......@@ -84,11 +86,20 @@ void model :: materialMess(int material)
/////////////////////////////////////////////////////////
void model :: textureMess(int state)
{
if (!m_model) return;
if (state)glmSpheremapTexture(m_model,currentH,currentW);
else glmLinearTexture(m_model,currentH,currentW);
buildList();
switch(state) {
case 0:
m_textype=GLM_TEX_LINEAR;
break;
case 1:
m_textype=GLM_TEX_SPHEREMAP;
break;
case 2:
m_textype=GLM_TEX_UV;
break;
default:
m_textype=GLM_TEX_DEFAULT;
}
m_rebuild=true;
}
/////////////////////////////////////////////////////////
......@@ -161,8 +172,7 @@ void model :: openMess(t_symbol *filename)
glmFacetNormals (m_model);
glmVertexNormals(m_model, m_smooth);
glmLinearTexture(m_model,currentH,currentW);
glmTexture(m_model, m_textype, m_currentH, m_currentW);
buildList();
this->setModified();
}
......@@ -183,7 +193,7 @@ void model :: buildList()
else
{
m_dispList = glmListGroup(m_model, m_flags,m_group);
}
}
}
/////////////////////////////////////////////////////////
......@@ -192,14 +202,17 @@ void model :: buildList()
/////////////////////////////////////////////////////////
void model :: render(GemState *state)
{
if (currentH != state->texCoordX(1) || currentW != state->texCoordY(1)){
currentH = state->texCoordX(1);
currentW = state->texCoordY(1);
if(m_model)glmLinearTexture(m_model,currentH,currentW);
buildList();
}
if (state && (m_currentH != state->texCoordX(2) || m_currentW != state->texCoordY(2)))
{
m_rebuild=true;
}
if(m_rebuild) {
m_currentH = state->texCoordX(2);
m_currentW = state->texCoordY(2);
glmTexture(m_model, m_textype, m_currentH, m_currentW);
buildList();
m_rebuild=false;
}
if (!m_dispList)return;
glCallList(m_dispList);
}
......
......@@ -98,21 +98,26 @@ class GEM_EXTERN model : public GemBase
int m_flags;
int m_group;
int m_group;
float currentH, currentW;
bool m_rebuild;
float m_currentH, m_currentW;
//////////////
// how to texture
glmtexture_t m_textype;
private:
private:
//////////
// static member functions
static void openMessCallback(void *data, t_symbol *filename);
static void rescaleMessCallback(void *data, t_floatarg);
static void smoothMessCallback(void *data, t_floatarg);
//////////
// static member functions
static void openMessCallback(void *data, t_symbol *filename);
static void rescaleMessCallback(void *data, t_floatarg);
static void smoothMessCallback(void *data, t_floatarg);
static void reverseMessCallback(void *data, t_floatarg);
static void materialMessCallback(void *data, t_floatarg);
static void textureMessCallback(void *data, t_floatarg);
static void groupMessCallback(void *data, t_floatarg);
static void groupMessCallback(void *data, t_floatarg);
};
#endif // for header file
......@@ -1149,8 +1149,9 @@ glmUVTexture(GLMmodel* model, float h, float w)
GLfloat dimensions[3];
GLfloat x, y, scalefactor;
GLuint i;
if(!model)return;
if(!(model->uvtexcoords))return;
if (model->texcoords)
free(model->texcoords);
......@@ -1167,7 +1168,6 @@ glmUVTexture(GLMmodel* model, float h, float w)
/* go through and put texture coordinate indices in all the triangles */
group = model->groups;
while(group) {
printf("next group: %d triangles\n", group->numtriangles);
for(i = 0; i < group->numtriangles; i++) {
T(group->triangles[i]).tindices[0] = T(group->triangles[i]).uvtindices[0];
T(group->triangles[i]).tindices[1] = T(group->triangles[i]).uvtindices[1];
......@@ -1190,7 +1190,7 @@ glmLinearTexture(GLMmodel* model, float h, float w)
GLfloat dimensions[3];
GLfloat x, y, scalefactor;
GLuint i;
if (!(model))return;
if (model->texcoords)
......@@ -1244,7 +1244,7 @@ glmSpheremapTexture(GLMmodel* model, float h, float w)
GLMgroup* group;
GLfloat theta, phi, rho, x, y, z, r;
GLuint i;
if (!(model))return;
if (!(model->normals))return;
......@@ -1293,10 +1293,11 @@ glmSpheremapTexture(GLMmodel* model, float h, float w)
GLvoid glmTexture(GLMmodel* model, glmtexture_t typ, float h, float w)
{
if(!model)return;
switch(typ) {
GLM_UV: glmUVTexture(model, h, w); break;
GLM_LINEAR: glmLinearTexture(model, h, w); break;
GLM_SPHEREMAP: glmSpheremapTexture(model, h, w); break;
case GLM_TEX_UV: glmUVTexture(model, h, w); break;
case GLM_TEX_LINEAR: glmLinearTexture(model, h, w); break;
case GLM_TEX_SPHEREMAP: glmSpheremapTexture(model, h, w); break;
default:
if(model->numuvtexcoords)
glmUVTexture(model, h, w);
......@@ -1410,7 +1411,7 @@ glmReadOBJ(char* filename)
/* close the file */
fclose(file);
glmTexture(model, GLM_DEFAULT, 1.0, 1.0);
glmTexture(model, GLM_TEX_DEFAULT, 1.0, 1.0);
return model;
}
......
......@@ -43,12 +43,15 @@ extern "C" {
#define GLM_COLOR (1 << 3) /* render with colors */
#define GLM_MATERIAL (1 << 4) /* render with materials */
/*
* types of texturing the model
*/
typedef enum
{
GLM_DEFAULT,
GLM_UV,
GLM_LINEAR,
GLM_SPHEREMAP
GLM_TEX_DEFAULT,
GLM_TEX_UV,
GLM_TEX_LINEAR,
GLM_TEX_SPHEREMAP
} glmtexture_t;
......@@ -190,36 +193,6 @@ glmFacetNormals(GLMmodel* model);
GLvoid
glmVertexNormals(GLMmodel* model, GLfloat angle);
/* glmUVTexture: uses the UV texture-coordinates stored with the model
*
* model - pointer to initialized GLMmodel structure
*/
GLvoid
glmUVTexture(GLMmodel* model, float h=1.0, float w=1.0);
/* glmLinearTexture: Generates texture coordinates according to a
* linear projection of the texture map. It generates these by
* linearly mapping the vertices onto a square.
*
* model - pointer to initialized GLMmodel structure
*/
GLvoid
glmLinearTexture(GLMmodel* model, float h=1.0, float w=1.0);
/* glmSpheremapTexture: Generates texture coordinates according to a
* spherical projection of the texture map. Sometimes referred to as
* spheremap, or reflection map texture coordinates. It generates
* these by using the normal to calculate where that vertex would map
* onto a sphere. Since it is impossible to map something flat
* perfectly onto something spherical, there is distortion at the
* poles. This particular implementation causes the poles along the X
* axis to be distorted.
*
* model - pointer to initialized GLMmodel structure
*/
GLvoid
glmSpheremapTexture(GLMmodel* model, float h=1.0, float w=1.0);
/* glmTexture: setup texture coordinates according to the specified type
* some types (like UV) are read from files, whereas others might be generated
* a "default" type will try to use saved texcoords and fallback to generated
......@@ -227,7 +200,7 @@ GLvoid
* model - pointer to initialized GLMmodel structure
*/
GLvoid
glmTexture(GLMmodel* model, glmtexture_t type, float h=1.0, float w=1.0);
glmTexture(GLMmodel* model, glmtexture_t type=GLM_TEX_DEFAULT, float h=1.0, float w=1.0);
/* glmDelete: Deletes a GLMmodel structure.
*
......
......@@ -40,9 +40,9 @@ multimodel :: multimodel(t_symbol *filename, t_floatarg baseModel,
if (filename->s_name[0]) {
if (skipRate == 0) {
if (topModel == 0)
openMess(filename, 0, (int)baseModel, 1);
openMess(filename, 0, (int)baseModel, 1);
else
openMess(filename, (int)baseModel, (int)topModel, 1);
openMess(filename, (int)baseModel, (int)topModel, 1);
}
else openMess(filename, (int)baseModel, (int)topModel, (int)skipRate);
}
......@@ -192,7 +192,7 @@ void multimodel :: openMess(t_symbol *filename, int baseModel, int topModel, int
glmFacetNormals (m_model);
glmVertexNormals(m_model, 90); /* SMOOTH */
glmLinearTexture(m_model,1,1);
glmTexture(m_model, GLM_TEX_LINEAR, 1, 1);
newCache->realmodels[i]=m_model;
}
m_curModel = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment