Forums

I hope this isn't too difficult for anyone to follow . . . OpenGL

Last post 09-12-2006, 12:48 PM by 3cords_Kevinito. 2 replies.
Sort Posts: Previous Next

     09-03-2006, 10:15 AM 88

    I hope this isn't too difficult for anyone to follow . . . OpenGL

    Greetings,

    I am currently coding a screensaver for our church using MSVC++. I am loading a 256x256 bitmap graphic and using glBindTexture with glTexCoord2f to apply the texture on the cube. The screensaver compiles and executes perfectly, but without the texture applied to the cube!!! Here is the code for the OpenGL screensaver. I left out the screensaver code . . .

    Thanks,

    Kevinito


    #include <windows.h>
    #include <stdio.h>
    #include <scrnsave.h>
    #include <commctrl.h>
    #include <gl\gl.h> // Header for OpenGL32 Lib
    #include <gl\glu.h> // GLu32 Lib
    #include <gl\glaux.h> // GLaux Lib

    #pragma comment(lib, "OpenGL32.lib")
    #pragma comment(lib, "GLu32.lib")
    #pragma comment(lib, "GLaux.lib")
    #pragma comment(lib, "ScrnSave.lib")
    #pragma comment(lib, "comctl32.lib")



    GLuint texture[1];




    AUX_RGBImageRec *LoadBMP(char *Filename)
    {
    FILE *File=NULL;

    if (!Filename)
    {
    return NULL;
    }

    File=fopen(Filename,"r");

    if (File)
    {
    fclose(File);
    return auxDIBImageLoad(Filename);
    }

    return NULL;
    }

    int LoadGLTextures()
    {
    int Status=FALSE;

    AUX_RGBImageRec *TextureImage[1];

    memset(TextureImage,0,sizeof(void *)*1);
    if (TextureImage[0]=LoadBMP("Data/image1.bmp"))
    {
    Status=TRUE;

    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    }

    if (TextureImage[0])
    {
    if (TextureImage[0]->data)
    {
    free(TextureImage[0]->data);
    }

    free(TextureImage[0]);
    }

    return Status;
    }



    void SetupAnimation(int Width, int Height)
    {
    // Current viewport:x, y, width, height
    glViewport(0, 0, (GLsizei)Width, (GLsizei)Height);

    // Next we set up for a perspective view

    glMatrixMode(GL_PROJECTION); // Select the Projection Matrix
    glLoadIdentity(); // Reset(init) the Projection Matrix

    gluPerspective(45.0f, (GLfloat)Width/(GLfloat)Height, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW); // Select the modelview matrix
    glLoadIdentity(); // Reset (init) the modelview matrix
    gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);



    glEnable(GL_TEXTURE_2D);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set background to black
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glShadeModel(GL_SMOOTH); // Enable smooth shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f); // Clear depth buffer
    glEnable(GL_DEPTH_TEST); // Enables depth test
    glDepthFunc(GL_LEQUAL); // Type of depth test to perform

    // Enhances image quality
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    // glColor3f(0.5f, 0.5f, 1.0f);
    }


    // Declare rotation variables
    static GLfloat spinx, spiny, spinz;


    // OpenGL drawing and animation
    void OnTimer(HDC hDC)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Rotation variables
    spinx = 0.1f;
    spiny = -0.2f;
    spinz = 0.3f;
    // glTranslatef(-1.5f,0.0f,-6.0f); // Move Left And Into The Screen
    glRotatef(spinx, 1.0f, 0.0f, 0.0f);
    glRotatef(spiny, 0.0f, 1.0f, 0.0f);
    glRotatef(spinz, 0.0f, 0.0f, 1.0f);

    glBindTexture(GL_TEXTURE_2D, texture[0]);

    glBegin(GL_QUADS);
    // Front Face
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left
    // Back Face
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    // Top Face
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    // Bottom Face
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    // Right face
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    // Left Face
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    glEnd();

    SwapBuffers(hDC);

    }

     09-11-2006, 11:42 AM 160 in reply to 88

    Re: I hope this isn't too difficult for anyone to follow . . . OpenGL

    While I don't have any specific answers for you, I will offer some generic advice in the hopes that maybe it will cover something you haven't thought of.  I used to work in C++ doing exactly what you are now, but I haven't for awhile, and I'm certainly not familiar with the exact calls you're using.

     Just make sure you move from a known state on every area that can go wrong. 

    - Make sure you can load the graphic from your hardrive and display it to the screen using the loading calls you have (just simply display it, removing the complexity of the cube thingy)

    - Make sure you can display the cube thing with a default or example graphic that came with the api (I'm assuming there's a demo or sample code that shows you how to use it).  If not, just use a color.

    - Attack the problem from both of those known simple states, starting from the simplest possible scenarios and incrementally adding complexity until you find the error

    Basically, just isolate each of your assumptions until you find out which one is not correct.  Sorry I couldn't speak more directly to your issue.

     09-12-2006, 12:48 PM 174 in reply to 160

    Re: I hope this isn't too difficult for anyone to follow . . . OpenGL

    Hi,

    Finally, thanks for the reply.  I have researched all of the calls to no avail.  I even tried colors on the cube (which does work).  I even attempted "dumming down" my bitmap image to 8bit (256) color.  Still a good compile, but the image does not display on the cube.

    Probably time to get an OpenGL book.

    -K

View as RSS news feed in XML