PDA

View Full Version : Problem when using multiple textures (With glTexImage2D)



ZeroSum
03-12-2015, 06:32 PM
Hello,

I am trying to render text with FreeType and also render Textured .Obj files.
I can get both of these things working in isolation, but when I try to render both text and textured .obj's, all I see is the text (correct) and an all white background, no textured object at all.

This is my text rendering code:




static void renderinfoline1(int string1, int string2, int string3 ){

glPushAttrib(GL_ALL_ATTRIB_BITS);

if(time <=0)
return;

glColor3f(1,0.75,0.75);

string textstring;
std::stringstream ss;

textstring.append("String 1: ");
ss << string1;
textstring.append(ss.str());

textstring.append(" String2: ");
ss.str("");
ss << string2;
textstring.append(ss.str());

textstring.append(" String3: ");
ss.str("");
ss.clear();
ss << string3;
textstring.append(ss.str());


float x = -1 - charwidth;
float y = 0.95; //-0.75

ss << time;
string text = textstring;//ss.str();

int currcharnum = 0;
int currrow;

for ( int n = 0; n < text.size(); n++ )
{
FT_Load_Char(face, text[n], FT_LOAD_RENDER);

currcharnum++;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);



glEnable (GL_TEXTURE_2D);

glTexImage2D( GL_TEXTURE_2D,
0,
GL_RGB,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);

x+=charwidth;

glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex2f(x, y);
glTexCoord2i(1, 1); glVertex2f(x+charwidth, y);
glTexCoord2i(1, 0); glVertex2f(x+charwidth, y+charheight);
glTexCoord2i(0, 0); glVertex2f(x, y+charheight);
glEnd();
glDisable(GL_TEXTURE_2D);

}

glPopAttrib();

}



And this is my Obj rendering code:




pMesh = &g_model.getMesh(i);
pMaterial = pMesh->pMaterial;
pVertices = g_model.getVertexBuffer();
// pMaterial = &g_model.m_materials[2];
// Render mesh.


//glGenTextures(1, &m_texname);
//glBindTexture(GL_TEXTURE_2D, m_texname);

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, pMaterial->ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pMaterial->diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pMaterial->specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, pMaterial->shininess * 128.0f);

glDisable(GL_BLEND);
glDisable(GL_LIGHTING);

bool g_enableTextures = true;
if (g_enableTextures)
{


iter = g_model.g_modelTextures.find("C:\\Torque\\PhoenixEngine(P155)\\PhoenixEngine(P15 5-MS1)\\PhoenixEngine(P155-MS1)\\Debug\\brick.bmp");


if (iter == g_model.g_modelTextures.end())
{
glDisable(GL_TEXTURE_2D);
}
else
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, iter->second);

}
}
else
{
glDisable(GL_TEXTURE_2D);
}


glEnableClientState(GL_TEXTURE_COORD_ARRAY);

if (g_model.hasPositions())
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, g_model.getVertexSize(),
g_model.getVertexBuffer()->position);
}

if (g_model.hasTextureCoords())
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, g_model.getVertexSize(),
g_model.getVertexBuffer()->texCoord);
}

if (g_model.hasNormals())
{
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, g_model.getVertexSize(),
g_model.getVertexBuffer()->normal);
}

glDrawElements(GL_TRIANGLES, (pMesh->triangleCount * 3), GL_UNSIGNED_INT, g_model.getIndexBuffer() + pMesh->startIndex);


if (g_model.hasNormals())
glDisableClientState(GL_NORMAL_ARRAY);

if (g_model.hasTextureCoords())
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

if (g_model.hasPositions())
glDisableClientState(GL_VERTEX_ARRAY);




I suspect that it is something to do with the state being changed for the text rendering, and not properly changed back?

Thanks for any advice!

GClements
03-13-2015, 03:45 AM
I don't see a glBindTexture() call in the text rendering code; are you overwriting the model texture?

ZeroSum
03-13-2015, 08:35 AM
No, I'm not, or at least, I am not trying to! That could be it!

ZeroSum
03-13-2015, 01:31 PM
Excellent, thank you GCelements, that works perfectly! However, when I call another function directly after my first render text function, which renders another line of text directly below, the second line of text is invisible. I can see everything else, and, if I remove the call to the function rendering the first line, I can see the second line correctly.
So, there must be something preventing the second line from rendering? Any ideas what this could be?

Thanks!





static void renderinfoline2(int time ){

glPushAttrib(GL_ALL_ATTRIB_BITS);

if(time <=0)
return;

printf("TIME: %d " , time);
glColor3f(1,0.75,0.75);

float x = -1 - charwidth;
float y = 0.9; //-0.75

stringstream ss;
ss << time;
string text = ss.str();

int currcharnum = 0;
int currrow;

for ( int n = 0; n < text.size(); n++ )
{
FT_Load_Char(face, text[n], FT_LOAD_RENDER);

currcharnum++;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);


glBindTexture(GL_TEXTURE_2D,0);

glTexImage2D( GL_TEXTURE_2D,
0,
GL_RGB,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);



x+=charwidth;//0.1;

glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex2f(x, y);
glTexCoord2i(1, 1); glVertex2f(x+charwidth, y);
glTexCoord2i(1, 0); glVertex2f(x+charwidth, y+charheight);
glTexCoord2i(0, 0); glVertex2f(x, y+charheight);
glEnd();

}

glPopAttrib();
}

GClements
03-13-2015, 03:23 PM
Calling glBindTexture(GL_TEXTURE_2D,0) in the middle of the function means that the glTexParameter() calls affect the texture which was bound prior to calling the function, while the glTexImage2D() call uploads data to the default 2D texture (the default texture is a legacy feature from OpenGL 1.0, and ideally should be avoided).