bus error with opengl code

Nibbie
Posts: 4
Joined: 2009.09
Post: #1
hi,

I am putting together a barebones piece of code to load a png file.
When I add the following line to my code,

glGenTextures(1, &texture[0]);


and executed the code,
the computer reported bus error.

Any ideas come to mind? Here is the whole program source. It's small.

Code:
#include <stdlib.h>
#include <GLUT/glut.h>          
#include <png.h>

#include "png_tools.h"


#define kWindowWidth    640
#define kWindowHeight   480


int main(int argc, char** argv)
{
        char **image_data;
        int height, width;

        int texture[1];

        /* read in the png image file */
        if (read_png_file("testfile.png") != 0)
        {
                image_data = get_png_image_data();
                width = get_png_width();
                height = get_png_height();
        } else {
                return -1;
        }

        /* adding this line caused the bus error */
        glGenTextures(1, &texture[0]);

        printf("Done\n");
        return 0;
}


Here is a sample make and build,

Michael-Nortons-Computer:~/Game_Dev/png_lib] mnorton% make
g++ -framework GLUT -framework OpenGL -Wall -o pngExample -L"/System/Library/Frameworks/OpenGL.framework/Libraries" pngTest.o png_tools.o -lGL -lGLU -lm -lobjc -lstdc++ -L/sw/lib -lpng -lz
[2] - Done xemacs makefile
[Michael-Nortons-Computer:~/Game_Dev/png_lib] mnorton% ls
NeHe.png makefile~ pngExample.c pngTest.o
png_tools.o
libpng.notes notes pngExample.o png_tools.c
makefile pngExample pngTest.c png_tools.h
[Michael-Nortons-Computer:~/Game_Dev/png_lib] mnorton% ./pngExample
width: 256 height: 256 color type: 2 bit_depth: 8
Bus error
[Michael-Nortons-Computer:~/Game_Dev/png_lib] mnorton%


For posterity, what caused the bus error? Whatt bare essentials do I need to add to
my code to make the error go away?

thanks,

Mike
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #2
You are submitting a GL command without any GL context. Ka-boom.

If you are using GLUT, you must:
* init glut
* init the display mode to create a context
* create a window to attach the context to a drawable

Only after doing that can you expect GL commands to work. See GLUT examples for working source code.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
You need to have a valid GL context before calling any OpenGL functions. Otherwise you'll crash.

[Edit: Whoops, Arekkusu beat me to it. Oh well.]

- Alex Diener
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Strange OpenGl or NSBitmapImageRep error magnusrw 4 3,225 Apr 24, 2009 11:13 AM
Last Post: magnusrw
  Error trying to link an OpenGL app memon 4 3,653 Mar 19, 2007 12:33 PM
Last Post: memon
  NEED HELP! OpenGL code for my exam doesn't work mr02077 2 2,777 Feb 9, 2007 05:44 PM
Last Post: stevejohnson
  SDL/OpenGL Initialization Code Nick 15 7,093 Sep 7, 2005 07:28 AM
Last Post: Nick
  OpenGL code optimization unknown 38 13,471 Jul 28, 2005 10:22 PM
Last Post: unknown