Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: Problem in Texture Render to a box

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2011
    Location
    seoul, south korea
    Posts
    14

    Exclamation Problem in Texture Render to a box

    Hello folks I need urgent help I am near to my deadline I am working on a small project similar to bloxorz. In this project the rendered box texture is not visible, I had used nehe opengl code to load bmp image and I had used same logic for render the box but I don't know where I am lacking please need a help.
    I am pasting code below of Texture load of an image and draw the box funtions and attached the output photo. Please need good suggestions. Thanks in advance.

    AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
    {
    FILE *File=NULL; // File Handle

    if (!Filename) // Make Sure A Filename Was Given
    {
    return NULL; // If Not Return NULL
    }

    File=fopen(Filename,"r"); // Check To See If The File Exists

    if (File) // Does The File Exist?
    {
    fclose(File); // Close The Handle
    return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
    }

    return NULL; // If Load Failed Return NULL
    }

    int LoadGLTextures() // Load Bitmaps And Convert To Textures
    {
    int Status=FALSE; // Status Indicator

    AUX_RGBImageRec *TextureImageBox[1]; // Create Storage Space For The Texture

    memset(TextureImageBox,0,sizeof(void *)*1); // Set The Pointer To NULL

    // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    if (TextureImageBox[0]=LoadBMP("Data/Crate.bmp"))
    {
    Status=TRUE; // Set The Status To TRUE

    glGenTextures(3, &texturebox[0]); // Create Three Textures

    // Create Nearest Filtered Texture
    glBindTexture(GL_TEXTURE_2D, texturebox[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImageBox[0]->sizeX, TextureImageBox[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImageBox[0]->data);

    // Create Linear Filtered Texture
    glBindTexture(GL_TEXTURE_2D, texturebox[1]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImageBox[0]->sizeX, TextureImageBox[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImageBox[0]->data);

    // Create MipMapped Texture
    glBindTexture(GL_TEXTURE_2D, texturebox[2]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImageBox[0]->sizeX, TextureImageBox[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImageBox[0]->data);
    }

    if (TextureImageBox[0]) // If Texture Exists
    {
    if (TextureImageBox[0]->data) // If Texture Image Exists
    {
    free(TextureImageBox[0]->data); // Free The Texture Image Memory
    }

    free(TextureImageBox[0]); // Free The Image Structure
    }

    return Status; // Return The Status
    }
    ////////////////////////////////Texture//////////////////////////////////
    int DrawGLBox(GLvoid) // Here's Where We Do All The Drawing
    {
    glPushMatrix();
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glNormal3f(0,0,1);
    //glTexCoord2f(0,1);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2,0,4);
    //glTexCoord2f(0,0);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-2,0,0);
    //glTexCoord2f(1,0);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0,0,0);
    //glTexCoord2f(1,1);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(0,0,4);

    glNormal3f(0,0,-1);
    //glTexCoord2f(1,1);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-2,2,4);
    //glTexCoord2f(0,1);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0,2,4);
    //glTexCoord2f(0,0);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0,2,0);
    //glTexCoord2f(1,0);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2,2,0);

    glNormal3f(-1,0,0);
    //glTexCoord2f(0,1);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-2,0,4);
    //glTexCoord2f(0,0);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2,2,4);
    //glTexCoord2f(1,0);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-2,2,0);
    //glTexCoord2f(1,1);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-2,0,0);

    glNormal3f(1,0,0);
    //glTexCoord2f(0,1);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0,0,4);
    //glTexCoord2f(1,1);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0,0,0);
    //glTexCoord2f(1,0);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(0,2,0);
    //glTexCoord2f(0,0);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(0,2,4);

    glNormal3f(0,1,0);
    //glTexCoord2f(0,1);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-2,2,4);
    //glTexCoord2f(0,0);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-2,0,4);
    //glTexCoord2f(1,0);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0,0,4);
    //glTexCoord2f(1,1);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(0,2,4);

    glNormal3f(0,-1,0);
    //glTexCoord2f(0,1);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2,2,0);
    //glTexCoord2f(0,0);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(0,2,0);
    //glTexCoord2f(1,0);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0,0,0);
    //glTexCoord2f(1,1);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-2,0,0);

    glEnd();
    glDisable(GL_TEXTURE_2D);
    glPopMatrix();

    return TRUE; // Keep Going
    }int DrawGLScene(GLvoid)
    {
    int i=0;

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glBindTexture(GL_TEXTURE_2D,texture[0]);
    if(Num==0)
    {
    TheApp.DrawMap1();
    }
    if(Num==1)
    {
    TheApp.DrawMap2();
    }
    TheApp.globalmoves= moves;

    glBindTexture(GL_TEXTURE_2D,texturebox[filter]);
    glTranslatef(TheApp.BoxX,TheApp.BoxY,0);


    KeyControl();
    Wining();

    if(First==TRUE)
    {
    First=FALSE;
    return TRUE;
    }
    //////////////////MovingBox////////////////////////
    if(TheApp.chain==PATH)
    {
    glTranslatef(0,0,2);
    glRotatef(-90,0,1,0);
    DrawGLBox();
    }
    else if(TheApp.chain==distance)
    {
    DrawGLBox();
    }
    else if(TheApp.chain==SPATH)
    {
    glTranslatef(0,0,2);
    glRotatef(-90,1,0,0);
    DrawGLBox();
    }
    else if(TheApp.chain==XPATH)
    {
    glRotatef(90,1,0,0);
    DrawGLBox();
    }

    return TRUE;
    }
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	blox.jpg 
Views:	86 
Size:	9.9 KB 
ID:	890  

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    One error I can see is the first quads texture coordinates are wrong - you have 2 with 1,0 coordinates. But that is not you main problem. Does "filter" have a valid value? Put a call to glError after you bind the texture to see if it is ok.

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2011
    Location
    seoul, south korea
    Posts
    14
    Thank you very much sir you are rite. I finished with texture mipmap but I had been stuck with the matter when the box is moved either left or right in the lean position the texture of top and bottom of the box texture changes to some other texture pattern but not same as start pattern I had observed that when ever the draw function called each time texture loads every time may be changes occurred due to this reason, still I am in dilemma is it correct or wrong Is there any solution for that how to maintain the const texture for respective plane. Thanks in advance.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I am not sure I understand you problem. The texture is mapped across the side of the box. This does not change as the image is rotated or translated; but the image will appear different as the box side is stretched because of the perspective view effect. Perhaps you could post 2 images showing me what is happening.

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2011
    Location
    seoul, south korea
    Posts
    14
    Click image for larger version. 

Name:	pic1.jpg 
Views:	112 
Size:	27.4 KB 
ID:	891Click image for larger version. 

Name:	pic2.jpg 
Views:	114 
Size:	28.3 KB 
ID:	892
    In the first picture when the box is rotated to its right as in the second picture the front face portion texture diagonal line strip is similar to previous stage but here the diagonal line strip should of in opposite side due to texture re draw its happening like this how to over come this sir. I appreciate your interest and Advance thanks for suggestions.

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I haven't looked over your code again but I would look at the winding of your triangles. When an image is the wrong way around it is caused by one of two things

    1) your uv coordinates at back to front
    or
    2) your triangle front face is say clockwise when you think it is counterclockwise and your are actually looking at the back of the triangle.

    You can see if you are looking at the back of a triangle by enabling cull with glEnable(GL_CULL_FACE) and the triangle will vanish.

    Does that make sense?

  7. #7
    Junior Member Newbie
    Join Date
    Aug 2011
    Location
    seoul, south korea
    Posts
    14
    Hi, I had used glEnable(GL_CULL_FACE) at the intialization can I get ur email id. I will attach the code.

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I have more a look at your code and you do not have a consistent set of triangles
    Code :
    glBegin(GL_QUADS);                              
          glNormal3f(0,0,1);                                            // this is the bottom so the normal should point down
          //glTexCoord2f(0,1);
          glTexCoord2f(0.0f, 0.0f);
          glVertex3f(-2,0,4);
          //glTexCoord2f(0,0);
          glTexCoord2f(1.0f, 0.0f);
          glVertex3f(-2,0,0);
          //glTexCoord2f(1,0);
          glTexCoord2f(1.0f, 1.0f);
          glVertex3f(0,0,0);
          //glTexCoord2f(1,1);
          glTexCoord2f(1.0f, 0.0f);
          glVertex3f(0,0,4);
     
          glNormal3f(0,0,-1);                                         // this is the top so it should wind the opposite to the bottom
          //glTexCoord2f(1,1);
          glTexCoord2f(1.0f, 0.0f);
          glVertex3f(-2,2,4);
          //glTexCoord2f(0,1);
          glTexCoord2f(1.0f, 1.0f);
          glVertex3f(0,2,4);
          //glTexCoord2f(0,0);
          glTexCoord2f(0.0f, 1.0f);
          glVertex3f(0,2,0);
          //glTexCoord2f(1,0);
          glTexCoord2f(0.0f, 0.0f);
          glVertex3f(-2,2,0);

    try just rendering these 2 quads and get them right; then take another pair and do the same thing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •