4 #include 5 #include 6 #include 7 #include 8 //variable declaration 9 GLuint texture; 10 11 12 //end variable declaration 13 //function to load raw file 14 GLuint LoadTexture(const char * filename, int width, int height) 15 { 16 GLuint texture; 17 unsigned char * data; 18 FILE * file; 19 20 //the following code will read IN our RAW file 21 file = fopen("texture.raw", "rb"); 22 if(file == NULL) return 0; 23 data = (unsigned char *)malloc(width * height * 3); 24 fread(data, width * height * 3, 1, file); 25 fclose(file); 26 27 glGenTextures(1, &texture); //generate the texture with the loaded data 28 glBindTexture(GL_TEXTURE_2D, texture); //bind the texture to its array 29 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); //set texture env parameters 30 31 //here we set what textures to use and when, the MIN filter is which quality to show 32 //when the texture is near the view, and the MAG filter is which quality to show 33 //when the texture is far from view 34 35 //the qualities are 36 //GL_NEAREST , GL_LINEAR , GL_LINEAR_MIPMAP_NEAREST , GL_LINEAR_MIPMAP_LINEAR 37 //and if you use extensions you can use anisotropic filtering 38 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 39 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 40 41 //here set the parameter to repeat the texture instead of clamping it 42 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 43 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 44 45 //generate the texture glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); 47 free(data); //free the data, and memory... 48 return texture; //return whether it was successfull or not 49 } 50 51 void FreeTexture(GLuint texture) 52 { 53 glDeleteTextures(1, &texture); 54 } 55 56 57 void square() 58 { 59 glBindTexture(GL_TEXTURE_2D, texture); //bind texture to our shape 60 glBegin(GL_QUADS); 61 glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0); 62 glTexCoord2d(1.0,0.0); glVertex2d(+1.0, -1.0); 63 glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0); 64 glTexCoord2d(0.0,1.0); glVertex2d(-1.0, +1.0); 65 glEnd(); 66 } 67 68 69 void keyboard(unsigned char key, int x, int y) 70 { 71 if(key == 27) 72 { 73 exit(0); 74 } 75 } 76 77 //our display function, sets color, clears, color buffer, load identity matrix,// , set view and finally render the o utput to the screen. 78 void display(void) 79 { 80 glClearColor(0.0,0.0,0.0,1.0); //clear the color of the window 81 glClear(GL_COLOR_BUFFER_BIT); //clear the color buffer 82 glLoadIdentity(); //load the identity matrix 83 gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); //set the view 84 square(); 85 glFlush(); //flush rendering to the screen 86 } 88 //our reshape function scales our screen, and images, to the correct size format 89 void reshape(int w, int h) 90 { 91 glViewport(0,0,(GLsizei)w,(GLsizei)h); //set viewport 92 glMatrixMode(GL_PROJECTION); //select matrix mode 93 glLoadIdentity(); //now load the matrix 94 gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,100.0); //angle, aspect of ration, min view distance, max view di stance 95 glMatrixMode(GL_MODELVIEW); 96 } 97 98 99 //Main function 100 int main(int argc, char **argv) 101 { 102 glutInit(&argc, argv); //initialize the program 103 glutInitDisplayMode(GLUT_SINGLE); //set up a basic single display buffer 104 glutInitWindowSize(500,500); //set the width and height of the window 105 glutInitWindowPosition(100,100); //set the position of the window 106 glutCreateWindow("A basic Opengl Window"); //set the caption for the window 107 glutDisplayFunc(display);//call the display function to draw our world 108 glutIdleFunc(display); 109 glutKeyboardFunc(keyboard); 110 glutReshapeFunc(reshape); 111 //load texture 112 texture = LoadTexture("texture.raw", 256,256); 113 glutMainLoop(); //initialize the Opengl loop cycle 114 FreeTexture(texture); 115 return 0; 116 }