iDevGames Forums
Texture/Image Binding crash. - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Texture/Image Binding crash. (/thread-5673.html)

Pages: 1 2


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 01:23 PM

I'm a bit of a newbie where it comes ot OpenGL and such, but I'm trying my best to get into it head first. I'm taking a "Computer Graphics" course here at college and it's all OpenGL in C/C++(Yay!) thing is we do everything on Windows. so far (with all the 2D stuff) I've had no problem making our windows programs as OS X programs. some of the 3D ones run fine as well. Problem I'm hitting is Image Binding (I believe it's the same as texture binding as most people seem to be reffering to it.) Everything works fine and dandy on the peecees but when I bring it over to my G5 and make the needed alterations it does something wierd. It compiles fine, but when I tell it to build & run, it gives a Warning -
passing arg 2 of `glGenTextures' from incompatible pointer type
which reffers to a line: glGenTextures(1, &texture[0]);
Then it runs and crashes and asks if I want to submit a report. The debug window gives the following line -
OpenGLTest has exited due to signal 10 (SIGBUS).
Here's my code (I apologize for the length, but I tend to make stupid mistakes at the most inconvienient spots, so it may not be where I'm looking.) I know the problem probably lies in th efact that this is code for a windows machine... but it technically compiled. Is there just something I need to change? or is the code for doing this in OS X different in it's entirety?

Code:
#import <Cocoa/Cocoa.h>
#include<GLUT/glut.h>
#include<stdio.h>        //for I/O
#include<stdlib.h>        //for exit()

unsigned int texture[1];

struct Image{
    unsigned long sizeX;        //the longs hold the info for
    unsigned long sizeY;        //the height and width
    char * data;                //pointer to the image
};

typedef struct Image Image;

int loadImage(char * filename, Image * image)
{
    FILE * file;                //Pointer to the open file
    unsigned long size;            //size of the image in bytes
    unsigned long i;            //a counter variable
    unsigned short int planes;    //number of planes (Always 1)
    unsigned short int bpp;        //number of bits per pixel (24);
    char temp;

    file = fopen(filename, "rb");    //open the file mode (read binary)

    fseek(file, 18, SEEK_CUR);    //more into the header 18 bytes to ...
    fread(&image->sizeX, 4, 1, file);//The next 4 bytes of the header
    fread(&image->sizeY, 4, 1, file);//The next 4 bytes of the header

    size = image->sizeX * image->sizeY * 3;//determine size

    fread(&planes, 2, 1, file);    //read the plane should be 1 fo
    fread(&bpp, 2, 1, file);    //read the bytes per pixel

    fseek(file, 24, SEEK_CUR);    //move past the rest of the header

    image->data = (char *) malloc(size);

    fread(image->data, size, 1, file);
    //bmp's are stores bqr, reverse for RGB
    for(i = 0; i < size; i += 3)
    {
        //switch the b and the r.  the G stays in the middle
        temp = image->data[i];
        image->data[i] = image->data[i+2];
        image->data[i+2] = temp;
    }

    return 1;
}
//Load an image and bind it as a texture
void loadTextures()
{
    Image * image1;

    image1 = (Image *) malloc(sizeof(Image));

    loadImage("Earth.bmp", image1);
    glGenTextures(1, &texture[0]);

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

void init(int w, int h)
{
    loadTextures();
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
                //FOV            Ratio            Near  Far
    gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void reshape(int w, int h)
{
    if(h==0) h = 1;

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
    static int angle = 0.0;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    
    glTranslatef(0.0, 0.0, -5.0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
        //Front Face
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0, 1.0);
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,-1.0, 1.0);
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
    glEnd();
    angle++;
    glutSwapBuffers();
}

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
    glutInitWindowSize(400,400);
    glutCreateWindow("Binding an image to a quad");

    init(400, 400);

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();
    return(0);
}

Again, I'm sorry if this has already been done, but I did a search and could not find my answer.Mad
-=J=-


Texture/Image Binding crash. - OneSadCookie - Apr 5, 2005 01:51 PM

you can't use "unsigned int" to refer to texture ids, you must use GLuint. That's what the warning's about.

what's the backtrace you get for the crash? hard to know what's going wrong without that kind of information...


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 03:09 PM

OneSadCookie Wrote:you can't use "unsigned int" to refer to texture ids, you must use GLuint. That's what the warning's about.

what's the backtrace you get for the crash? hard to know what's going wrong without that kind of information...
Backtrace?

I'm using XCode soo... where would I find out about that?


Texture/Image Binding crash. - OneSadCookie - Apr 5, 2005 03:23 PM

run it in the debugger, or don't run it in the debugger and get it from the "do you want to submit this crash report to apple" dialog.


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 03:28 PM

ok, I fixed the GLint thing and now no warning Grin
I copied the pictures into a few of the folders to make sure they were accessible (prolly didn't need ot do that.
The last time I added pictures to an XCode project (in the view-thing on the side) it wouldn't compile, but this time I added them to "Targets -> App -> Bundle Resources -> InfoPlist.strings" so Earth.bmp is in there.
so now it compiles and runs, but I get a window with a background color of "GL_COLOR_BUFFER_BIT" and a White box where it SHOULD be displaying Earth.bmp.
I checked attributes. Earth.bmp is 256X256. we learned that if the picture wasn't a multiple of a binary number it wouldn't disply. we used bitmaps that were 256X256 and this is the picture I used for that exercise. so now I can't figure out what's wrong with the dispaying...

And I suddenly just noticed another thing.
I used ot be able to throw a *.cpp file into the source for Targets for building and it would work fine. now it'll only take a *.m file. I would assume that has something to do with the fact that my project is technically a "Cocoa Application" (that's what I chose when I made the new project.)?? how can I make it run off of cpp's again?


Texture/Image Binding crash. - OneSadCookie - Apr 5, 2005 05:10 PM

post error messages and backtraces.
http://www.idevgames.com/forum/showthread.php?t=8857


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 05:33 PM

Point taken. I've checked Google. I didn't find anything I could understand as helpful (meaning I what I read didn't seem like it had any relevance.) I can't seem to find many sites that deal; with what I'm trying to do. I've found a lot of coding using some way that I don't quite get. example:
Code:
- (void)resetItem:(int)i AtX:(int)x Y:(int)y;

looks like C but I haven't seen syntax like that (mostly because a) I'm kinda new to programming - only been in it for about 4 years - b) being in college makes be program mailny on PC's ).
anyway, enough personal history. It's not crashing like it was before, so as far as I can tell no backtraces.. I tweaked it and got it to actually run, but the image isn't showing up in the quad I'm drawing. I went through my code and my picture to make sure it's set up they way we were taught. It is.
here's my newer code (not too different tho). I've added a few "printf" lines just to see if the program is actually geting to certain points... it is:
Code:
#import <Cocoa/Cocoa.h>
#include<GLUT/glut.h>
#include<stdio.h>        //for I/O
#include<stdlib.h>        //for exit()

//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

GLuint texture[1];

struct Image{
    unsigned long sizeX;        //the longs hold the info for
    unsigned long sizeY;        //the height and width
    char * data;                //pointer to the image
};

typedef struct Image Image;

int loadImage(char * filename, Image * image)
{
printf("got this far 2");
    FILE * file;                //Pointer to the open file
    unsigned long size;            //size of the image in bytes
    unsigned long i;            //a counter variable
    unsigned short int planes;    //number of planes (Always 1)
    unsigned short int bpp;        //number of bits per pixel (24);
    char temp;

    file = fopen(filename, "rb");    //open the file mode (read binary)

    fseek(file, 18, SEEK_CUR);    //more into the header 18 bytes to ...
    fread(&image->sizeX, 4, 1, file);//The next 4 bytes of the header
    fread(&image->sizeY, 4, 1, file);//The next 4 bytes of the header

    size = image->sizeX * image->sizeY * 3;//determine size

    fread(&planes, 2, 1, file);    //read the plane should be 1 fo
    fread(&bpp, 2, 1, file);    //read the bytes per pixel

    fseek(file, 24, SEEK_CUR);    //move past the rest of the header

    image->data = (char *) malloc(size);

    fread(image->data, size, 1, file);
    //bmp's are stores bqr, reverse for RGB
    for(i = 0; i < size; i += 3)
    {
        //switch the b and the r.  the G stays in the middle
        temp = image->data[i];
        image->data[i] = image->data[i+2];
        image->data[i+2] = temp;
    }

    return 1;
}
//Load an image and bind it as a texture
void loadTextures()
{
    Image * image1;

    image1 = (Image *) malloc(sizeof(Image));

    loadImage("Earth.bmp", image1);
    glGenTextures(1, &texture[0]);
    
    printf("got this far 3");

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

void init(int w, int h)
{
    loadTextures();
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
                //FOV            Ratio            Near  Far
    gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void reshape(int w, int h)
{
    if(h==0) h = 1;

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    
    glTranslatef(0.0, 0.0, -5.0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
        //Front Face
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0, 1.0);
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,-1.0, 1.0);
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);
    glEnd();
    glutSwapBuffers();
}

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    printf("got this far 1");
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
    glutInitWindowSize(400,400);
    glutCreateWindow("Binding an image to a quad");

    init(400, 400);

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();
    return(0);
}

The debug window IS giving me an error if that helps at all. It is as follows:
2005-04-05 21:36:28.255 OpenGLTest[1210] GLUT Warning: GL error: invalid value
got this far 1got this far 2got this far 3
OpenGLTest has exited with status 0.


so I see there's an error code 1210. no idea what that means. it tells me there is an invalid value, but for what? I found if I make the file nonexistant and the program can't find it, then it crashes like before, so I know for a fact that It IS finding the .bmp file. the program [technically] runs fine, because it IS running, with no picture showing, and exiting with a status of 0. Only conclusion I can draw is that the error code 1210 is the reason my bitmap wont show up. unfortunately, I can't find the problem.
Thnk you very much for the help you have given me so far by the way. at least I can get the window to show up rather than dying on me like it was.


Texture/Image Binding crash. - OneSadCookie - Apr 5, 2005 05:56 PM

the code at the top is Objective C.

I think 1210 is your process ID...
you can run your program in the opengl profiler with the "break on opengl error" option turned on. that will show you which GL call is causing the invalid value error.


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 07:48 PM

oh. THAT'S objective C. which is technically what I should be programming in considering when I made my XCode project I chose "Cocoa Application" which is describes as "This project builds a Cocoa-based application written in Objective-C." is there any way I can use XCode and build my programs in regular C or C++?
while we're on the subject, I ran OpenGL Profiler, but I'm not quite sure what to do.
I selected the built app. hit "New" and opened the "Breakpoints" window. I checked "Break on error" and used Break after all, but that just makes it stop after each function call, or so it seems. I've never used the app before, is there something where I can learn what I should be doing with it? haven't seen an FAQ on it.


Texture/Image Binding crash. - OneSadCookie - Apr 5, 2005 08:08 PM

Carbon Application is a better starting point for C/C++ applications. See http://onesadcookie.com/~keith/XcodeGLUT/ for a tutorial.

I think you don't want "break after all", just "break on error" should be fine.


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 08:42 PM

aha, here's what I get outta that
glTexImage2D()
Error: GL_INVALID_VALUE
Context: <not available>
Function call stack:
0: glTexImage2D <libGL.dylib>
1: loadTextures <ImageBinding.ob> ImageBinding.m: 77
2: init <ImageBinding.ob> ImageBinding.m: 82
3: main <ImageBinding.ob> ImageBinding.m: 134
4: start <OpenGLTest>
5: start <OpenGLTest>

So am I right in assuming the problem is in
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);

?
therein would lie the problem that we were tahgt to use this, but not taught what each individual part means. so here I am still stuck Blush Huh
I'm gonna try it out as a carbon app like you suggested and I'll let you know if that helps at all
Thanks! Grin

Edit: Well, Carbon works well to, but I get the same exact problem - GLUT Warning: GL error: Invalid value.

so yeah, I'm looking into what needs ot be fixed in there


Texture/Image Binding crash. - OneSadCookie - Apr 5, 2005 09:25 PM

from man glTexImage2D
Code:
void glTexImage2D( GLenum target,
                          GLint level,
                          GLint internalformat,
                          GLsizei width,
                          GLsizei height,
                          GLint border,
                          GLenum format,
                          GLenum type,
                          const GLvoid *pixels )
...
Code:
GL_INVALID_VALUE is generated if level is less than 0.

       GL_INVALID_VALUE  may  be generated if level is greater than log2(max),
       where max is the returned value of GL_MAX_TEXTURE_SIZE.

       GL_INVALID_VALUE is generated if internalformat is not 1, 2, 3,  4,  or
       one of the accepted resolution and  symbolic constants.

       GL_INVALID_VALUE  is  generated  if  width  or height is less than 0 or
       greater than 2 + GL_MAX_TEXTURE_SIZE, or if  either  cannot  be  repre-
       sented as 2^k +2 (border) for some integer value of k.

       GL_INVALID_VALUE is generated if border is not 0 or 1.

my guess is that sizeX and sizeY are not powers of two.


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 09:32 PM

OneSadCookie Wrote:from man glTexImage2D
Code:
void glTexImage2D( GLenum target,
                          GLint level,
                          GLint internalformat,
                          GLsizei width,
                          GLsizei height,
                          GLint border,
                          GLenum format,
                          GLenum type,
                          const GLvoid *pixels )
...
Code:
GL_INVALID_VALUE is generated if level is less than 0.

       GL_INVALID_VALUE  may  be generated if level is greater than log2(max),
       where max is the returned value of GL_MAX_TEXTURE_SIZE.

       GL_INVALID_VALUE is generated if internalformat is not 1, 2, 3,  4,  or
       one of the accepted resolution and  symbolic constants.

       GL_INVALID_VALUE  is  generated  if  width  or height is less than 0 or
       greater than 2 + GL_MAX_TEXTURE_SIZE, or if  either  cannot  be  repre-
       sented as 2^k +2 (border) for some integer value of k.

       GL_INVALID_VALUE is generated if border is not 0 or 1.

my guess is that sizeX and sizeY are not powers of two.

That's a very good point. I'l check that out. as for the first part, I found that and was loking into what might be off. all values are correct (tho I'm not sure about the powers of 2 thing. That's actually quite helpful!

Thanks! I'll get back when I find out if that works out or not.


Texture/Image Binding crash. - Aftershock6783 - Apr 5, 2005 09:52 PM

Nope. sizeX & sizeY are both 2^16 (65536) so they are both 256X256 bitmap files...
Mad
now I'm really starting to get annoyed with this project.
the only thing left is image->data. and even if that is the problem, I don't get it. I thought that value didn't matter as far as the number goes. does that also have to be a power of 2? is there any way to check/fix that if it is the problem.
I really have to thank you for not only the help you've provided, but the patience you've had with a "noob" like myself. I think most people would have told me either give up or go away by now.
any way, I'm gonna continue to look into the data property and see if that's it if I had my server still up I'd give you and FTP link and maybe if you tried running it you'd see the problem. either way, if you happen to think of anything else that might help me out, please let me know. I'm also gonna ask my professor tomorrow.
Thanks Again!
-=J=-


Texture/Image Binding crash. - OneSadCookie - Apr 5, 2005 09:56 PM

um, if the image is 256x256, you should be passing 256, 256 to glTexImage2D..... where's 65536 come from?!