Sign in to follow this  
The_Nerd

OpenGL I NEED HELP BAD!!!

Recommended Posts

The_Nerd    100
Alright, I have a very weird texture problem. I build up the font characters for my GL print function. Each character is a little OpenGL list. Inside the list is the quad, and the texture bound to the quad. It's nice and compiled. Now when I print a string, everything works fine, every thing that is, except anything after the print function! For some reason, every polygon I render after the print function has the texture of the last character, no matter what I do (glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2d); glBind(MY_BLOODY_TEXTURE_THAT_IS_NOT_THE_LAST_CHARACTER);). Also, calling glDeleteTextures on my glGenTextures list causes a SEGV at the end of the program. I got allot of code, so I just want to ask if you guys have any ideas before I start posting it.

Share this post


Link to post
Share on other sites
HopeDagger    970
Quote:
Original post by The_Nerd
For some reason, every polygon I render after the print function has the texture of the last character, no matter what I do (glDisable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2d); glBind(MY_BLOODY_TEXTURE_THAT_IS_NOT_THE_LAST_CHARACTER);).


Yep, we're going to need to see some code before we can determine the problem. Could be a number of things. :)

Quote:
Also, calling glDeleteTextures on my glGenTextures list causes a SEGV at the end of the program.


I don't think glDeleteTextures is safe to call inside of a display list.

Quote:
I got allot of code, so I just want to ask if you guys have any ideas before I start posting it.


*braces self* Alright! Make sure you only post the relevant stuff, of course. :)

Share this post


Link to post
Share on other sites
The_Nerd    100
Function to build a font character:

bool make_dlist (FT_Face Face, uint16 Ch, Ner3DFont *Fnt)
{
static Ner3DImage *TempImage=VID_NewImage();
if (!TempImage) return false;
if(FT_Load_Glyph(Face, FT_Get_Char_Index(Face, Ch), FT_LOAD_DEFAULT)) return false;

//Move the face's glyph into a Glyph object.
FT_Glyph Glyph;
if(FT_Get_Glyph(Face->glyph, &Glyph)) return false;

//Convert the glyph to a bitmap.
FT_Glyph_To_Bitmap(&Glyph, FT_RENDER_MODE_NORMAL, 0, 1 );
FT_BitmapGlyph Bitmap_Glyph = (FT_BitmapGlyph)Glyph;

//This reference will make accessing the bitmap easier
FT_Bitmap& Bitmap=Bitmap_Glyph->bitmap;

//Use our helper function to get the widths of
//the bitmap data that we will need in order to create
//our texture.
int16 uiWidth = next_p2(Bitmap.width);
int16 uiHeight = next_p2(Bitmap.rows);

//Allocate memory for the texture data.
GLubyte* Expanded_Data = new GLubyte[ 2 * uiWidth * uiHeight];

//Here we fill in the data for the expanded bitmap.
//Notice that we are using two channel bitmap (one for
//luminocity and one for alpha), but we assign
//both luminocity and alpha to the value that we
//find in the FreeType bitmap.
//We use the ?: operator so that value which we use
//will be 0 if we are in the padding zone, and whatever
//is the the Freetype bitmap otherwise.
for(uint16 uiJ=0;uiJ<uiHeight;uiJ++)
for(uint16 uiJJ=0;uiJJ<uiWidth;uiJJ++)
memset(&Expanded_Data[2*(uiJJ+uiJ*uiWidth)], (uiJJ>=Bitmap.width||uiJ>=Bitmap.rows)?0:Bitmap.buffer[uiJJ+Bitmap.width*uiJ], 2);

TempImage->New(uiWidth, uiHeight, 32);
memcpy(TempImage->Buffer, Expanded_Data, uiWidth*uiHeight*4);
TempImage->uiFlags=NER3D_IMG_CLAMPX|NER3D_IMG_CLAMPY;
TempImage->Bind(GL_RGBA, GL_LUMINANCE_ALPHA);


//With the texture created, we don't need to expanded data anymore
delete [] Expanded_Data;

//So now we can create the display list
glNewList(Fnt->uiListBase+Ch,GL_COMPILE);

//glBindTexture(GL_TEXTURE_2D,Fnt->uiTextureBase[Ch]);
VID_BindGLTexture(TempImage->iGLIndex);
TempImage->Free();

//first we need to move over a little so that
//the character has the right amount of space
//between it and the one before it.
glTranslatef(Bitmap_Glyph->left,0,0);

//Now we move down a little in the case that the
//bitmap extends past the bottom of the line
//(this is only true for characters like 'g' or 'y'.
glPushMatrix();
glTranslatef(0,(0-Bitmap_Glyph->top)+DEFAULT_CHAR_WIDTH,0);
//n3dDebug("%c = %d/%d", Ch, Bitmap_Glyph->top, Bitmap.rows);

//Now we need to account for the fact that many of
//our textures are filled with empty padding space.
//We figure what portion of the texture is used by
//the actual character and store that information in
//the x and y variables, then when we draw the
//quad, we will only reference the parts of the texture
//that we contain the character itself.
float32 fX=(float32)Bitmap.width/(float32)uiWidth,
fY=(float32)Bitmap.rows/(float32)uiHeight;

//Here we draw the texturemaped quads.
//The bitmap that we got from FreeType was not
//oriented quite like we would like it to be,
//so we need to link the texture to the quad
//so that the result will be properly aligned.
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex2f(0,0);
glTexCoord2d(0,fY); glVertex2f(0,Bitmap.rows);
glTexCoord2d(fX,fY); glVertex2f(Bitmap.width,Bitmap.rows);
glTexCoord2d(fX,0); glVertex2f(Bitmap.width,0);
glEnd();
glPopMatrix();
glTranslatef(Face->glyph->advance.x >> 6 ,0,0);

//increment the raster position as if we were a bitmap font.
//(only needed if you want to calculate text length)
//glBitmap(0,0,0,0,face->glyph->advance.x >> 6,0,NULL);

//Finnish the display list
glEndList();
}
/*------------------------------------------------------------------------------------------------------------------*/



Ner3DImage::Bind() and Ner3DImage::New function:


GLuint uiGlobalTextureObject[IMG_MAX_TEXTURES];
static uint16 uiCurrentBoundTexture=0, uiTotalBoundTextures=0, uiBoundTextureIndex=0;
static list<Ner3DImage *> gImages;
FallBackTexture FallBackTex;
int16 iCurrentlyBoundTexture=-1;

/*------------------------------------------------------------------------------------------------------------------*/
bool Ner3DImage::Bind(GLint IFormat, GLint Format)
{
if (Buffer==NULL) return false;

if (this->iGLIndex!=-1)
{
uiCurrentBoundTexture=this->iGLIndex;
return true; //Already bound... just return
}

glBindTexture(GL_TEXTURE_2D, uiGlobalTextureObject[uiBoundTextureIndex]);
uiBoundTextureIndex++;

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (uiFlags&NER3D_IMG_CLAMPX)?GL_CLAMP:GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (uiFlags&NER3D_IMG_CLAMPY)?GL_CLAMP:GL_REPEAT);
if (uiFlags&NER3D_IMG_MIPMAPPED)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,
IFormat,
(unsigned int)uiWidth,
(unsigned int)uiHeight,
Format,
GL_UNSIGNED_BYTE,
Buffer);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,
0,
IFormat,
(unsigned int)uiWidth,
(unsigned int)uiHeight,
0,
Format,
GL_UNSIGNED_BYTE,
Buffer);
}
iGLIndex=uiGlobalTextureObject[uiBoundTextureIndex-1]-1;
uiCurrentBoundTexture=uiGlobalTextureObject[uiBoundTextureIndex-1];
uiTotalBoundTextures++;
return true;
}
/*------------------------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------------------------*/
bool Ner3DImage::New(uint16 uiWidth, uint16 uiHeight, uint16 uiBpp)
{
Free();
if (uiWidth>0&&uiHeight>0&&uiBpp>0)
{
Buffer=(RGBA_ARRAY *)realloc(Buffer, ((uiWidth*uiHeight)*uiBpp)*sizeof(RGBA_ARRAY));
if (Buffer==NULL) return false;
}
this->uiWidth=uiWidth;
this->uiHeight=uiHeight;
this->uiBpp=uiBpp;
return true;
}
/*------------------------------------------------------------------------------------------------------------------*/



Print function:

/*------------------------------------------------------------------------------------------------------------------*/
void VID_FontPrint(Ner3DFontInfo *Info, uchar8 *ucpStr)
{
if (Info->uiFont>=uiGFontCount) return;
Ner3DFont *CurrentFont=&tpFonts[Info->uiFont];
uint16 uiI, uiII, uiTab=0, uiJ, uiLayerCount;
float32 fCharWidth=(float32)DEFAULT_CHAR_SPACING*Info->Size.fX;
uchar8 *ucpPt, ucpTag[128]={0}, ucpValue[128]={0};
list<Ner3DMaterialLayer *>::iterator TempItr=NULL;
Ner3DImage *Char;

glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
//Info->Position.fX-=fCharWidth;

glPushMatrix();
glLoadIdentity();
glTranslatef(Info->Position.fX, Info->Position.fY, 0);

for (;ucpStr[0]!=0;uiTab++, ucpStr++)
{
if ((Info->uiFlags&FONT_FLAG_IGNORETAGS)==0)
{
if (ucpStr[0]=='[')
{
//ucpStr++;
if (ucpStr[1]!='['&&ucpStr[1]!=0)
{
for (ucpPt=&ucpStr[1], uiII=0;ucpPt[0]!=(uchar8)' '&&ucpPt[0]!=0;uiII++, ++ucpPt)
{
ucpTag[uiII]=ucpPt[0];
ucpTag[uiII+1]=0;
}
for (uiII=0;ucpPt[0]!=']'&&ucpPt[0]!=0;uiII++, ++ucpPt)
{
ucpValue[uiII]=ucpPt[0];
ucpValue[uiII+1]=0;
}
//n3dMessage("Tag %s value %s", ucpTag, ucpValue);
if (ucpPt[0]==']')
{
STR_SWITCH((char *)ucpTag)
{
STR_CASE("color")
{
sscanf((char *)ucpValue, "%f, %f, %f, %f", &Info->Color.fR, &Info->Color.fG, &Info->Color.fB, &Info->Color.fA);
break;
}
STR_CASE("tabwidth")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->uiTabWidth=atoi((char *)ucpValue);
break;
}
STR_CASE("i")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->uiFlags=(atoi((char *)ucpValue))?FONT_FLAG_ITALIC:0;
break;
}
STR_CASE("size")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->Size.fX=atof((char *)ucpValue);
Info->Size.fY=Info->Size.fX;
fCharWidth=DEFAULT_CHAR_SPACING*Info->Size.fX;
glScalef(Info->Size.fX, Info->Size.fY, 1);
break;
}
STR_CASE("sizex")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->Size.fX=atof((char *)ucpValue);
fCharWidth=DEFAULT_CHAR_SPACING*Info->Size.fX;
glScalef(Info->Size.fX, Info->Size.fY, 1);
break;
}
STR_CASE("sizey")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->Size.fY=atof((char *)ucpValue);
glScalef(Info->Size.fX, Info->Size.fY, 1);
break;
}
STR_CASE("xpos")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->Position.fX=(float32)atoi((char *)ucpValue);
glTranslatef(Info->Position.fX, 0, 0);
break;
}
STR_CASE("ypos")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->Position.fY=(float32)atoi((char *)ucpValue);
glTranslatef(0, Info->Position.fY, 0);
break;
}
STR_CASE("rotate")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->Rotation.fAngleZ=atof((char *)ucpValue);
break;
}
STR_CASE("rot")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->CRotation.fAngleZ=atof((char *)ucpValue);
break;
}
STR_CASE("font")
{
//sscanf((char *)ucpValue, "%d, %d, %d", &ucRed, &ucGreen, &ucBlue);
Info->uiFont=atoi((char *)ucpValue);
if (Info->uiFont>=uiGFontCount) Info->uiFont=uiGFontCount-1;
CurrentFont=&tpFonts[Info->uiFont];
break;
}
break;
}
ucpStr=ucpPt;
continue;
}
}
}
}

glColor4f(Info->Color.fR, Info->Color.fG, Info->Color.fB, Info->Color.fA);
glCallList(CurrentFont->uiListBase+ucpStr[0]);
}
glPopMatrix();
glColor3ub(255, 255, 255);
}
/*------------------------------------------------------------------------------------------------------------------*/



Misc classes:


class Ner3DFont
{
public:
uint16 uiListBase;
};

class Ner3DFontInfo
{
public:
Ner3DPoint2D Position;
Ner3DPoint2D Size;
Ner3DNormal Rotation;
Ner3DNormal CRotation;
Ner3DColor Color;
uint16 uiFlags, uiFont, uiTabWidth;

Ner3DFontInfo() { Reset(); }
void Reset(){
Position.fX=0.0;
Position.fY=0.0;
Size.fX=1.0;
Size.fY=1.0;
Rotation.fAngleZ=0.0;
CRotation.fAngleZ=0.0;
Color.fR=1.0;
Color.fG=1.0;
Color.fB=1.0;
Color.fA=1.0;
uiFlags=0;
uiFont=0;
uiTabWidth=4;
}
};




Anything else??? Don't say I didn't warn you! :)

[Edited by - The_Nerd on November 11, 2004 12:57:25 PM]

Share this post


Link to post
Share on other sites
zedzeek    529
>>For some reason, every polygon I render after the print function has the texture of the last character, no matter what I do (glDisable(GL_TEXTURE_2D);<<

did u disable ALL the texture units

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now