Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Question About U/V Coordinates For Textures...

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
Hey all. I've fixed my clipping/crashing problem and the engine is now running nicely, but without textures. I went to OpenGL.org's forums, and posted my question there, but apparently the boards are un-moderated and bump/mip-mapping are considered "newbie" questions and should be posted on their beginner's board. Post there and they say it's over their head. Needless to say I got no help at OpenGL.org for my OpenGL app! I'll just use PH from now on where I know that even if I can't get an answer, at least there aren't any (*)(&)&(*&(*%#'s like on those forums!
Now, I have heard that you can somehow have OpenGL auto-generate coordinates for U and V (S and T). How much extra processing power would it take to allow OpenGL to handle that kind of data? Also, since bitmap reading isn't finished yet, I manually create a black and white, 32x32 bitmap in the LoadBMP() function to use. It's basically "GLubyte image[32][32][4];" and I use a loop to color the upper-left and lower-right quarters black, while leaving the rest white. The thing is, I still cannot get the image to show up on any polygon! I [b]AM[/b] using triangles instead of quads since they draw faster, if that makes any difference. Heck, as far as I know, quads are broken down into triangles before they're drawn anyway!
Anyways, could somebody show me a basic WORKING example of how to display an image on a triangle or two in OpenGL? Keep in mind that I do NOT use GLUT. Thanks everbody, I appreciate the help.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • gautamgautam Member Posts: 642
    Hey,

    Well hmm the webmaster won't like any !@#$% such posts and I think we let it stay that way and keep this place very friendly

    Anyway wasn't I suppose to write a bitmap loading routine for you ? Here it is - it will put the whole thing into a unsigned char *buffer - then you can map the buffer to whatever you want.

    [code=grey]

    #define BITMAP_ID 0x4d42
    typedef struct /**** Colormap entry structure ****/
    {
    unsigned char rgbBlue; /* Blue value */
    unsigned char rgbGreen; /* Green value */
    unsigned char rgbRed; /* Red value */
    unsigned char rgbReserved; /* Reserved */
    } RGBQUAD;

    typedef struct {
    BITMAPINFOHEADER bmiHeader; /* Image header */
    RGBQUAD bmiColors[256]; /* Image colormap */
    } BITMAPINFO;

    GLubyte * /* O - Bitmap data */
    LoadDIBitmap(const char *filename, /* I - File to load */
    BITMAPINFO **info) /* O - Bitmap information */
    {
    FILE *fp; /* Open file pointer */
    GLubyte *bits; /* Bitmap pixel bits */
    int bitsize; /* Size of bitmap */
    int infosize; /* Size of header information */
    BITMAPFILEHEADER header; /* File header */


    /* Try opening the file; use "rb" mode to read this *binary* file. */
    if ((fp = fopen(filename, "rb")) == NULL)
    return (NULL);

    /* Read the file header and any following bitmap information... */
    if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
    {
    /* Couldn't read the file header - return NULL... */
    fclose(fp);
    return (NULL);
    }

    if (header.bfType != 'MB') /* Check for BM reversed... */
    {
    /* Not a bitmap file - return NULL... */
    fclose(fp);
    return (NULL);
    }

    infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
    if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
    {
    /* Couldn't allocate memory for bitmap info - return NULL... */
    fclose(fp);
    return (NULL);
    }

    if (fread(*info, 1, infosize, fp) < infosize)
    {
    /* Couldn't read the bitmap header - return NULL... */
    free(*info);
    fclose(fp);
    return (NULL);
    }

    /* Now that we have all the header info read in, allocate memory for *
    * the bitmap and read *it* in... */
    if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
    bitsize = ((*info)->bmiHeader.biWidth *
    (*info)->bmiHeader.biBitCount + 7) / 8 *
    abs((*info)->bmiHeader.biHeight);

    if ((bits = malloc(bitsize)) == NULL)
    {
    /* Couldn't allocate memory - return NULL! */
    free(*info);
    fclose(fp);
    return (NULL);
    }

    if (fread(bits, 1, bitsize, fp) < bitsize)
    {
    /* Couldn't read bitmap - free memory and return NULL! */
    free(*info);
    free(bits);
    fclose(fp);
    return (NULL);
    }

    /* OK, everything went fine - return the allocated bitmap... */
    fclose(fp);
    return (bits);
    }
    [/code]

    Well I am not sure if this will work - but in 2-3 days I will have a bitmap engine working so I'll let you see a working code by then. If you don't understand anything feel free to mail me or post here.


    : Hey all. I've fixed my clipping/crashing problem and the engine is now running nicely, but without textures. I went to OpenGL.org's forums, and posted my question there, but apparently the boards are un-moderated and bump/mip-mapping are considered "newbie" questions and should be posted on their beginner's board. Post there and they say it's over their head. Needless to say I got no help at OpenGL.org for my OpenGL app! I'll just use PH from now on where I know that even if I can't get an answer, at least there aren't any (*)(&)&(*&(*%#'s like on those forums!


    : Now, I have heard that you can somehow have OpenGL auto-generate coordinates for U and V (S and T). How much extra processing power would it take to allow OpenGL to handle that kind of data? Also, since bitmap reading isn't finished yet, I manually create a black and white, 32x32 bitmap in the LoadBMP() function to use. It's basically "GLubyte image[32][32][4];" and I use a loop to color the upper-left and lower-right quarters black, while leaving the rest white. The thing is, I still cannot get the image to show up on any polygon! I [b]AM[/b] using triangles instead of quads since they draw faster, if that makes any difference. Heck, as far as I know, quads are broken down into triangles before they're drawn anyway!

    [blue]
    Yes, triangles are faster cos many graphics cards are hardware accelerated for drawing triangles but rectangles are easier to figure out and do calculations/collisions with.
    [/blue]


  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    Thanks, I appreciate the info. Right now I have the text coordinates figured out using the formula I found online for the values 0.0 to 1.0 to stretch the image or whatever. Even so, the image won't display on the triangles in OpenGL. I can't figure it out and I may have discovered a bug in OpenGL, since the guys at the OpenGL forums are now stumped. All OpenGL requires from a bitmap is the height and width (which is contained in a bitmapinfo structure if I am not mistaken) and the actual RGBA data form the 24bit bitmap. The height and width doesn't bug me, since I can just tell the game (for now) to use 64x64 or 32x32 for testing purposes. All I need is to return the RGBA data, and I can do that with what you posted before this one. Now, any clue as to why OpenGL won't texture no matter what? (=

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • gautamgautam Member Posts: 642
    : Thanks, I appreciate the info. Right now I have the text coordinates figured out using the formula I found online for the values 0.0 to 1.0 to stretch the image or whatever.

    [code=grey]
    Hi,
    With what I posted you can stretch or resize the image by using glScalef
    [/code]

    Even so, the image won't display on the triangles in OpenGL. I can't figure it out and I may have discovered a bug in OpenGL,
    since the guys at the OpenGL forums are now stumped.

    [code=grey]
    I doubt if this is a bug. I have been to opengl forums and rarely have my questions been answered. Just because some guys couldn't answer your questions doesn't imply that its a bug :-)
    [/code]

    All OpenGL requires from a bitmap is the height and width (which is contained in a bitmapinfo structure if I am not mistaken) and the actual RGBA data form the 24bit bitmap. The height and width doesn't bug me, since I can just tell the game (for now) to use 64x64 or 32x32 for testing purposes. All I need is to return the RGBA data, and I can do that with what you posted before this one. Now, any clue as to why OpenGL won't texture no matter what? (=

    [code=grey]
    Hmm, is it that with my code you can map the image into a triangle or map it only a rectangle.

    If you are not able to view it on the screen with triangle mapping then I assume the triangle is small and the bitmap is going out of the triangle.

    If you post that part of the code then maybe I can help you out. The code will give me a better idea.
    [/code]

    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    :

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    [code]
    //Initialize OpenGL!
    BOOL InitGL(GLvoid)
    {
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);
    glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    return TRUE;
    }

    //This Creates A 32x32 Bitmap Data Structure (Solide Yellow Cube)
    void LoadBMP()
    {
    GLubyte image[32][32][4];

    for(short int loop = 0; loop < 32; loop++)
    {
    for(short int h_loop = 0; h_loop < 32; h_loop++)
    {
    image[loop][h_loop][0] = 255;
    image[loop][h_loop][1] = 0;
    image[loop][h_loop][2] = 255;
    image[loop][h_loop][3] = 255;
    }
    }

    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

    return;
    }

    //Finally, We Draw The Room (512w x 512l x 128h)
    BOOL DrawGLScene(GLvoid)
    {
    player.lx = player.xpos + cos(player.heading * (pi / 180)) * 2;
    player.ly = player.ypos + sin(player.lookup * (pi / 180)) * 2;
    player.lz = player.zpos + sin(player.heading * (pi / 180)) * 2;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(player.xpos, player.ypos, player.zpos, player.lx, player.ly, player.lz, 0.0, 1.0, 0.0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);

    for(unsigned long o_loop = 0; o_loop < area.numObjects; o_loop++)
    {
    for(int t_loop = 0; t_loop < area.object[o_loop].numTriangles; t_loop++)
    {
    glBegin(GL_TRIANGLES);
    glNormal3f(0.0f, 0.0f, 1.0f);

    glVertex3f(area.object[o_loop].triangle[t_loop].vertex[0].x, area.object[o_loop].triangle[t_loop].vertex[0].y, area.object[o_loop].triangle[t_loop].vertex[0].z);
    glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[0].u, area.object[o_loop].triangle[t_loop].vertex[0].v);

    glVertex3f(area.object[o_loop].triangle[t_loop].vertex[1].x, area.object[o_loop].triangle[t_loop].vertex[1].y, area.object[o_loop].triangle[t_loop].vertex[1].z);
    glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[1].u, area.object[o_loop].triangle[t_loop].vertex[1].v);

    glVertex3f(area.object[o_loop].triangle[t_loop].vertex[2].x, area.object[o_loop].triangle[t_loop].vertex[2].y, area.object[o_loop].triangle[t_loop].vertex[2].z);
    glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[2].u, area.object[o_loop].triangle[t_loop].vertex[2].v);

    glEnd();
    }
    }

    return TRUE;
    }
    [/code]
    "texture[x]" is a GLuint that is globaly declared. I have commented out that fancy code which draws my level that is stored in "Maps/World.TXT" and manually drawn a triangle and a quad and the texture will NOT map to either one. Any ideas?

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Sign In or Register to comment.