something wrong with the code in redbook that i test

Started by
4 comments, last by mrbastard 15 years, 5 months ago
the code below can be compiled ,but when i running the .exe ,it failed. i have no idea about it. #include <stdlib.h> #include<stdio.h> #include<GL/glew.h> #include <GL/glut.h> #pragma comment(lib,"glew32.lib") GLubyte *pixels; GLsizei width, height; GLubyte* readImage( const char* filename, GLsizei* width_, GLsizei *height_ ) { int n; GLubyte *pixels_; FILE* infile = fopen( filename, "rb" ); if ( !infile ) { fprintf( stderr, "Unable to open file '%s'\n", filename ); system("pause"); } fread( width_, sizeof( GLsizei ), 1, infile ); fread( height_, sizeof( GLsizei ), 1, infile ); n = 3 * (*width_) * (*height_); pixels_ = (GLubyte *) malloc( n * sizeof( GLubyte )); if ( !pixels_ ) { fprintf( stderr, "Unable to malloc() bytes for pixels\n" ); system("pause"); } fread( pixels_, sizeof( GLubyte ), n, infile ); fclose( infile ); return pixels_; } void init(void) { int i; GLubyte colorTable[256][3]; glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glClearColor(0.0, 0.0, 0.0, 0.0); /* Set up an inverse color table */ for ( i = 0; i < 256; ++i ) { colorTable[0] = 255 - i; colorTable[1] = 255 - i; colorTable[2] = 255 - i; } glColorTable(GL_COLOR_TABLE, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, colorTable); glEnable(GL_COLOR_TABLE); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glRasterPos2i( 1, 1 ); glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels ); glFlush(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho (0, w, 0, h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); } } /* Main Loop * Open window with initial window size, title bar, * RGBA display mode, and handle input events. */ int main(int argc, char** argv) { pixels = readImage("F:\\leeds.bin ", &width, &height); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(width, height); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glewInit(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }
Advertisement
What do you mean "failed"? What did you expect would happen, and what actually happened? Did the program crash? Did you get an error message? If so, what was it?

In short, we need more details to help you.
You can get all the source files from http://www.opengl-redbook.com/source/
Then,you can get three code files,colortable.c readimage.c and leeds.bin.
Building the program with them,and you will know my doubt.
Help me,thanks!
Like Gage64 said, you must specify how it "failed" (Program crash? Error messages? what happened?)
You cannot expect everybody to compile the source to see how it fails.

For example, think if somebody says:
Person A: "I compiled a new distro of linux, and it failed"
Person B: "How it failed?"
Person A: "Download it from www.somelinuxdistro.com, and compile it to see how it fails" (not to mention that the compilation takes 5 hours).
Thanks for your advice.Maybe there is something wrong with my expression. I'm so sorry.
My psychic powers tell me that the glut and/or glew dlls aren't in your path. Try putting them in the same folder as the exe.
[size="1"]

This topic is closed to new replies.

Advertisement