Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <math.h>
- #define ESCAPE_KEY 27
- #define WIREFRAME_KEY 119
- #define INC_KEY 43
- #define DEC_KEY 45
- #define DEFAULT_TIMER 50
- int window;
- float xrot, yrot, zrot;
- float points[45][45][3];
- int wiggle_count = 0;
- unsigned char has_wireframe = 0;
- unsigned int single_shot = DEFAULT_TIMER;
- int texture[1];
- struct Image {
- unsigned long sizeX;
- unsigned long sizeY;
- char *data;
- };
- typedef struct Image Image;
- int ImageLoad(char *filename, Image *image) {
- FILE *file;
- unsigned long size; // size of the image in bytes.
- unsigned long i; // standard counter.
- unsigned short int planes; // number of planes in image (must be 1)
- unsigned short int bpp; // number of bits per pixel (must be 24)
- char temp; // temporary color storage for bgr-rgb conversion.
- if ((file = fopen(filename, "rb"))==NULL)
- {
- printf("File Not Found : %s\n",filename);
- return 0;
- }
- fseek(file, 18, SEEK_CUR);
- if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
- printf("Error reading width from %s.\n", filename);
- return 0;
- }
- if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
- printf("Error reading height from %s.\n", filename);
- return 0;
- }
- size = image->sizeX * image->sizeY * 3;
- if ((fread(&planes, 2, 1, file)) != 1) {
- printf("Error reading planes from %s.\n", filename);
- return 0;
- }
- if (planes != 1) {
- printf("Planes from %s is not 1: %u\n", filename, planes);
- return 0;
- }
- if ((i = fread(&bpp, 2, 1, file)) != 1) {
- printf("Error reading bpp from %s.\n", filename);
- return 0;
- }
- if (bpp != 24) {
- printf("Bpp from %s is not 24: %u\n", filename, bpp);
- return 0;
- }
- fseek(file, 24, SEEK_CUR);
- // read the data.
- image->data = (char *) malloc(size);
- if (image->data == NULL) {
- printf("Error allocating memory for color-corrected image data");
- return 0;
- }
- if ((i = fread(image->data, size, 1, file)) != 1) {
- printf("Error reading image data from %s.\n", filename);
- return 0;
- }
- for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
- temp = image->data[i];
- image->data[i] = image->data[i+2];
- image->data[i+2] = temp;
- }
- // we're done.
- return 1;
- }
- void LoadGLTextures() {
- Image *image1;
- image1 = (Image *) malloc(sizeof(Image));
- if (image1 == NULL) {
- printf("Error allocating space for image");
- exit(0);
- }
- if (!ImageLoad("Data/lesson11/tim.bmp", image1)) {
- exit(1);
- }
- glGenTextures(1, &texture[0]);
- glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
- glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
- };
- void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
- {
- float float_x, float_y; // loop counters.
- LoadGLTextures(); // Load The Texture(s)
- glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Clear The Background Color To Blue
- glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
- glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
- glEnable(GL_DEPTH_TEST); // Enables Depth Testing
- glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity(); // Reset The Projection Matrix
- gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
- glMatrixMode(GL_MODELVIEW);
- for(float_x = 0.0f; float_x < 9.0f; float_x += 0.2f ) {
- for(float_y = 0.0f; float_y < 9.0f; float_y += 0.2f) {
- points[ (int) (float_x*5) ][ (int) (float_y*5) ][0] = float_x - 4.4f;
- points[ (int) (float_x*5) ][ (int) (float_y*5) ][1] = float_y - 4.4f;
- points[ (int) (float_x*5) ][ (int) (float_y*5) ][2] = (float) (sin( ( (float_x*5*8)/360 ) * 3.14159 * 2 ));
- }
- }
- }
- void ReSizeGLScene(int Width, int Height)
- {
- if (Height==0) // Prevent A Divide By Zero If The Window Is Too Small
- Height=1;
- glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
- glMatrixMode(GL_MODELVIEW);
- }
- void timer_func(int which_timer)
- {
- glutTimerFunc(single_shot, timer_func, 0); // re-set timer function
- glutPostRedisplay(); // redraw our scene
- }
- void DrawGLScene()
- {
- int x, y;
- float float_x, float_y, float_xb, float_yb;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
- glLoadIdentity(); // Reset The View
- glTranslatef(0.0f,0.0f,-12.0f); // move 12 units into the screen.
- glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use.
- if (has_wireframe) {
- glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
- } else {
- glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
- }
- glBegin(GL_QUADS);
- for (x=0; x<44; x++) {
- for (y=0; y<44; y++) {
- float_x = (float) (x)/44;
- float_y = (float) (y)/44;
- float_xb = (float) (x+1)/44;
- float_yb = (float) (y+1)/44;
- glTexCoord2f( float_x, float_y);
- glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] );
- glTexCoord2f( float_x, float_yb );
- glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] );
- glTexCoord2f( float_xb, float_yb );
- glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] );
- glTexCoord2f( float_xb, float_y );
- glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] );
- }
- }
- glEnd();
- if (wiggle_count == 2) { // cycle the sine values
- for (y = 0; y <45; y++) {
- points[44][y][2] = points[0][y][2];
- }
- for( x = 0; x < 44; x++ ) {
- for( y = 0; y < 45; y++) {
- points[x][y][2] = points[x+1][y][2];
- }
- }
- wiggle_count = 0;
- }
- wiggle_count++;
- xrot +=0.3f;
- yrot +=0.2f;
- zrot +=0.7f;
- glutSwapBuffers();
- }
- void keyPressed(unsigned char key, int x, int y)
- {
- usleep(100);
- switch (key) {
- case ESCAPE_KEY:
- glutDestroyWindow(window);
- exit(0);
- case WIREFRAME_KEY:
- has_wireframe = ~has_wireframe;
- break;
- case INC_KEY:
- single_shot += 10;
- break;
- case DEC_KEY:
- single_shot -= 10;
- break;
- default:
- break;
- }
- printf("%d\n", single_shot);
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
- glutInitWindowSize(640, 480);
- glutInitWindowPosition(0, 0);
- window = glutCreateWindow("CG");
- glutDisplayFunc(&DrawGLScene);
- glutTimerFunc(single_shot, timer_func, 0);
- glutReshapeFunc(&ReSizeGLScene);
- glutKeyboardFunc(&keyPressed);
- InitGL(640, 480);
- glutMainLoop();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement