Results 1 to 2 of 2

Thread: second time not the charm texture going away

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2002
    Location
    Little Rock, AR,
    Posts
    7

    second time not the charm texture going away

    im making a very simp game engine as i learn OpenGl and i am trying to make a textured object but when i render it the second time it goes away. If I write the programme to render once it stays on the object here is the loader and render. Do i have to store the bitmap in some OpenGL function?if you know plz they are if you need all the code just ask. thx for any help in this problem.

    #include "main.h"
    BITMAPFILEHEADER bitmapFileHeader;
    unsigned char *bitmapImage;
    unsigned int imageIdx=0;
    unsigned char tempRGB;
    unsigned char* bitmapData;
    unsigned int texture;
    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
    {
    FILE *filePtr;

    filePtr = fopen(filename, "rb");

    if (filePtr == NULL)
    return NULL;

    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);


    //if (bitmapFileHeader.bfType == BITMAP_ID)
    // {
    // MessageBox(NULL, "image for you maybe", NULL, MB_OK);
    //
    // }


    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1,filePtr);
    fseek(filePtr, bitmapFileHeader.bfOffBits,SEEK_SET);
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

    if (!bitmapImage)
    {
    free(bitmapImage);
    fclose(filePtr);
    return NULL;
    }

    fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage,filePtr);

    if (bitmapImage == NULL)
    {
    fclose(filePtr);
    return NULL;
    }


    for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
    {
    tempRGB = bitmapImage[imageIdx];
    bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
    bitmapImage[imageIdx + 2] = tempRGB;
    }
    fclose(filePtr);

    return bitmapImage;
    }

    void setuptexture()
    {
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    bitmapData = LoadBitmapFile("checker.bmp",&bitmapInfoHeader);
    if (!bitmapData)
    MessageBox(NULL, "No image for you", NULL, MB_OK);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB,GL_UNSIGNED_BYTE, bitmapData);

    }

    #include "main.h"
    #include "texturemap.h"
    void drawQuads()
    {
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f); glVertex3f(0.5f,-0.5f,0.5f);
    glTexCoord2f(1.0f,0.0f); glVertex3f(0.5f,0.5f,0.5f);
    glTexCoord2f(1.0f,1.0f); glVertex3f(-0.5f,0.5f,0.5f);
    glTexCoord2f(0.0f,1.0f); glVertex3f(-0.5f,-0.5f,0.5f);
    glEnd();

    }

    int render(HWND hwnd)
    {

    glClearColor(0.0f,0.0f,0.0f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.5f,100.0f);

    gluLookAt(myCam.posx,myCam.posy,myCam.posz,
    myCam.eyex,myCam.eyey,myCam.eyez,
    myCam.upx,myCam.upy,myCam.upz);


    drawQuads();
    glBegin(GL_TRIANGLES);
    glTexCoord2f(0.5f,1.0f); glVertex3f(0.0f,10.0f,-10.0f);
    glTexCoord2f(0.0f,0.0f); glVertex3f(-10.f,0.0f,-10.0f);
    glTexCoord2f(1.0f,0.0f); glVertex3f(10.0f,0.0f,-10.0f);
    glEnd();
    glBegin(GL_TRIANGLES);
    glTexCoord2f(0.5f,1.0f); glVertex3f(10.0f,0.0f,-10.0f);
    glTexCoord2f(0.0f,0.0f); glVertex3f(-10.0f,0.0f,-10.0f);
    glTexCoord2f(1.0f,0.0f); glVertex3f(0.0f,10.0f, -10.0f);
    glEnd();

    glColor3f(0.0f, 0.5f, 0.05f);


    for(float i = -50; i <= 50; i += 1)
    {

    glBegin(GL_LINES);


    glVertex3f(-50, -10, i);
    glVertex3f(50, -10, i);


    glVertex3f(i, -10, -50);
    glVertex3f(i, -10, 50);


    glEnd();
    }
    //glEnable(GL_LIGHTING);
    glColor3f(0.0f,0.0f,0.0f);
    //auxSolidSphere(1.0f);

    //glEnable(GL_LIGHTING);

    SwapBuffers(g_HDC);


    //fps(hwnd);



    return 0;
    }

  2. #2

    Re: second time not the charm texture going away

    I usually place the set up texture(s) routine to run when the rest of the GL stuff is initialised in say an InitGL routine:

    void InitGL()
    {
    SetUpMainSceneGLSettings();
    SetUpTexture();
    }

    Then in the Render routine bind the texture ID before calling the begin/end drawing segment. The texture variable you use to bind will need to be global for it to be useable in both the setup and render routines. Note that when you set up the texture in your routine the first 4 lines could be placed in the InitGL routine as they affect the scene as well as the texture.

    GLuint texture[1];

    void RenderScene()
    {
    glBindTexture(GL_TEXTURE_2D,texture);
    DrawSquare();
    DrawCube();
    DrawTriangle();
    etc.. // each object drawn will use the texture in each scene.
    }

    A look at Nehe's tutorials will see this in practice.

    Hope that helps you in some way.

    Tina http://programming.swangen.co.uk/opengl/
    Learning OpenGL while working on sourceforge projects:
    https://sourceforge.net/projects/simulant/ and
    https://sourceforge.net/projects/projectnova/

Similar Threads

  1. Real-time texture modification
    By etienne.thomas31 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-28-2016, 03:59 AM
  2. Uploading texture, one channel at a time
    By andras in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 09-21-2005, 11:39 AM
  3. First time texture: doesn't work ( .raw ).
    By DeathZan in forum OpenGL: Basic Coding
    Replies: 20
    Last Post: 07-01-2005, 06:16 AM
  4. AGP texture transfer time
    By lazrhog in forum OpenGL: Advanced Coding
    Replies: 14
    Last Post: 07-15-2003, 12:29 AM
  5. Texture swapping time hit
    By SuperFly in forum OpenGL: Advanced Coding
    Replies: 10
    Last Post: 11-14-2000, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean