Strange Textures and Primitives Problem [Solved]

Started by
1 comment, last by Hacktank 14 years, 3 months ago
Hello, i am having trouble with rendering textures and primitives, they both just dont show up. I have my texture system set up where i can define texture sections and render them explicitly. And this actually works. The rendering function for texture sections is almost exactly the same, yet sections work and full textures dont. I am fairly sure the reason for the primitives not drawing is the same as why full textures wont draw. Here is my initialization of OpenGL:

bool Graphics::Init(int width,int height) {

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0,(GLfloat)width,(GLfloat)height,0,0,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glShadeModel(GL_SMOOTH);
    glClearColor(0.3f,0.3f,0.3f,0.0f);
    glClearDepth(0.0f);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);
    glDepthFunc(GL_ALWAYS);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

    glDisable(GL_LIGHTING);

    glEnable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);

    if(loaded) texturemanager->ReloadTextures();

    loaded = true;

    return true;

}
Here is the working texturesection function (im currently using it for bitmap fonts):

void TextureManager::DrawTextureSection(unsigned int id,GLfloat x,GLfloat y,bool onscreen,GLfloat xscale,GLfloat yscale,GLfloat rotation,GLfloat red,GLfloat green,GLfloat blue,GLfloat alpha) {

    if(graphics->currenttexture != texturesections[id].id) {

	   glBindTexture(GL_TEXTURE_2D,textures[texturesections[id].id].texture);
	   graphics->currenttexture = texturesections[id].id;
	   
    }

    if(!onscreen) {

	   x -= camera->GetXPosition();
	   y -= camera->GetYPosition();

    }

    GLfloat width = texturesections[id].width * xscale;
    GLfloat height = texturesections[id].height * yscale;

    glLoadIdentity();
    glTranslatef(x,y,0.0);
    glScaled(xscale,yscale,0);
    glRotatef(rotation,0.0f,0.0f,1.0f);
    
    glBegin(GL_QUADS);
	   glColor4f(red,green,blue,alpha);

	   glTexCoord2f(texturesections[id].section.x1,texturesections[id].section.y1);
	   glVertex2f(0,0);

	   glTexCoord2f(texturesections[id].section.x1,texturesections[id].section.y2);
	   glVertex2f(0,height);

	   glTexCoord2f(texturesections[id].section.x2,texturesections[id].section.y2);
	   glVertex2f(width,height);

	   glTexCoord2f(texturesections[id].section.x2,texturesections[id].section.y1);
	   glVertex2f(width,0);
    glEnd();

}
Here is the nonworking full texture drawing function:

void TextureManager::DrawTexture(unsigned int id,GLfloat x,GLfloat y,bool onscreen,GLfloat xscale,GLfloat yscale,GLfloat rotation,GLfloat red,GLfloat green,GLfloat blue,GLfloat alpha) {

    if(graphics->currenttexture != id) {

	   glBindTexture(GL_TEXTURE_2D,textures[id].texture);
	   graphics->currenttexture = id;
	   
    }

    if(!onscreen) {

	   x -= camera->GetXPosition();
	   y -= camera->GetYPosition();

    }

    GLfloat width = textures[id].width * xscale;
    GLfloat height = textures[id].height * yscale;

    glLoadIdentity();

    glTranslatef(x,y,1.0f);
    glScaled(xscale,yscale,0);
    glRotatef(rotation,0.0f,0.0f,1.0f);

    glBegin(GL_QUADS);
	   glColor4f(red,green,blue,alpha);

	   glTexCoord2f(0,0);
	   glVertex2f(0,0);

	   glTexCoord2f(0,1);
	   glVertex2f(0,height);

	   glTexCoord2f(1,1);
	   glVertex2f(width,height);

	   glTexCoord2f(1,0);
	   glVertex2f(width,0);
    glEnd();

}
And here is the nonworking primitive drawing code (box, but i have similar ones for other primitives):

void PrimitiveManager::DrawBox(GLfloat x,GLfloat y,GLfloat width,GLfloat height,GLfloat rotation,bool onscreen,GLfloat red,GLfloat green,GLfloat blue,GLfloat alpha) {


    if(!onscreen) {

	   x -= camera->GetXPosition();
	   y -= camera->GetYPosition();

    }

    glLoadIdentity();
    glTranslatef(x,y,0.0);
    glRotatef(rotation,0.0f,0.0f,1.0f);

    glBegin(GL_QUADS);
   
	   glColor4f(red,green,blue,alpha);

	   glVertex2f(0,0);
	   glVertex2f(0,height);
	   glVertex2f(width,height);
	   glVertex2f(width,0);

    glEnd();

}
Here are my calls to the functions:

// the if statement ensures that they are loaded before they attempt to draw, they are loaded
	  if(testtex>0) texturemanager->DrawTexture(testtex,200,200,false,1.0f,1.0f,rot,1,1,1,1); //not working

	  if(testfont!="") fontmanager->DrawText(testfont,"FPS="+IntToString(fps),0,50,true,1.0f,1.0f,0,1.0f,1.0f,1.0f,1.0f); // works like a charm
// drawtext loops through all the characters and calls TextureManager::DrawTextureSection appropriately

	  primitivemanager->DrawBox(50,200,20,20,0,true,1,0,0,1); // not working
Thank you for your time. [Edited by - Hacktank on December 29, 2009 4:29:45 AM]

Advertisement
I'm not sure, but maybe backface culling is enabled by default in openGL.

In this case, all your quads will be culled, since the culled orientation by default is the clockwise order: The order of the drawn vertices in the primitives.

Maybe in the text drawing, you use negative height value, so the order will be okay, that's why it is drawn.

And an other thing, in most cases, scaling should be called just before drawing, so after other transformations. You didn't notice the effect of the bad placement, because you scale by 1,1,0.
Thank you, I fixed the order of transformations, and it turns out that I typoed on translatef() to move to z-1.0 but my max is 0. Its all fixed now.

This topic is closed to new replies.

Advertisement