Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include "GL/glut.h"
- #include "GL/gl.h"
- #include <stdio.h>
- GLuint texture[2];
- GLint slices=16;
- GLint stacks=16;
- struct Image {
- unsigned long sizeX;
- unsigned long sizeY;
- char *data;
- };
- typedef struct Image Image;
- int textureToDisplay = 0;
- float fogDens = 0.55f;
- // texture brick
- GLubyte const c=255;
- GLubyte const b=51;
- GLubyte const w=150;
- GLubyte tex_brick[8][8][3] = {
- { {c,b,b},{c,b,b},{c,b,b},{w,w,w},{c,b,b},{c,b,b},{c,b,b},{c,b,b} },
- { {c,b,b},{c,b,b},{c,b,b},{w,w,w},{c,b,b},{c,b,b},{c,b,b},{c,b,b} },
- { {c,b,b},{c,b,b},{c,b,b},{w,w,w},{c,b,b},{c,b,b},{c,b,b},{c,b,b} },
- { {w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w} },
- { {c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{w,w,w} },
- { {c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{w,w,w} },
- { {c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{c,b,b},{w,w,w} },
- { {w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w},{w,w,w} }
- };
- GLuint filter; // Which filter
- GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // 3 types of fog
- GLuint fogfilter= 0; // Which filter use
- GLfloat fogColor[4]= {0.5, 0.5, 0.5, 1.0}; // Color of gof
- int ImageLoad(char *filename, Image *image) {
- FILE *file;
- unsigned long size; // size of the image in bytes.
- unsigned long i; // standard counter.
- char temp; // temporary color storage for
- // make sure the file is there.
- if ((file = fopen(filename, "rb"))==NULL)
- {
- printf("File Not Found : %s\n",filename);
- return 0;
- }
- // seek through the bmp header, up to the width/height:
- fseek(file, 18, SEEK_CUR);
- // read the width and height
- if ((fread(&image->sizeX, 4, 1, file) != 1)
- || (fread(&image->sizeY, 4, 1, file) != 1)) {
- printf("Error reading width or height from %s.\n", filename);
- return 0;
- }
- // calculate the size (assuming 24 bits or 3 bytes per pixel).
- size = image->sizeX * image->sizeY * 3;
- // seek past the rest of the bitmap header.
- fseek(file, 24, SEEK_CUR);
- // read the data.
- image->data = (char *) malloc(size);
- if (image->data == NULL) {
- printf("Error in allocating memory");
- 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;
- }
- fclose(file);
- return 1;
- }
- Image * loadTexture(char *filename)
- {
- Image *image1;
- // allocate space for texture
- image1 = (Image *) malloc(sizeof(Image));
- if (image1 == NULL) {
- printf("Error allocating space for image");
- exit(0);
- }
- //pic.bmp is a 64x64 picture
- if (!ImageLoad(filename, image1)) {
- exit(1);
- }
- return image1;
- }
- void init_texture(void)
- {
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- Image *image1 = loadTexture("gold.bmp");
- Image *image2 = loadTexture("satin.bmp");
- if (image2 == NULL)
- {
- printf("Image was not returned from loadTexture\n");
- exit(0);
- }
- if(image1 == NULL)
- {
- printf("Image was not returned from loadTexture\n");
- exit(0);
- }
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- // Create Texture
- glGenTextures(2, texture);
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- //TO DO Task 3
- //add texture from code stored in tex_brick
- glTexImage2D(GL_TEXTURE_2D, 0, 3, 8, 8, 0,
- GL_RGB, GL_UNSIGNED_BYTE, tex_brick);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- //TO DO Task 3
- //add second texture into texture[1] read from file
- glBindTexture(GL_TEXTURE_2D, texture[1]);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- 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);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- free(image1);
- glEnable(GL_TEXTURE_2D);
- glShadeModel(GL_FLAT);
- }
- void init_fog(void)
- {
- glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog mode
- glFogfv(GL_FOG_COLOR, fogColor); // Set fog color
- glFogf(GL_FOG_DENSITY, fogDens); // Density of fog
- glHint(GL_FOG_HINT, GL_DONT_CARE); // kind of creation - pixel or vertex
- glFogf(GL_FOG_START, 1.0f); // length from begin
- glFogf(GL_FOG_END, 5.0f); // length to end
- glEnable(GL_FOG); // Enable GL_FOG
- }
- void myinit(void)
- {
- glClearColor (0.5, 0.5, 0.5, 1.0);
- //TO DO Task 2
- init_texture();
- }
- void display_textures(void)
- {
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glPushMatrix ();
- //TO DO Task 3
- glTranslatef (2.5, 0, 0);
- glutSolidTeapot(1.0);
- glPopMatrix ();
- //TO DO Task 3
- //add second object
- glBindTexture(GL_TEXTURE_2D, texture[1]);
- glPushMatrix();
- //TO DO Task 3
- glutSolidTeapot(1.0);
- glPopMatrix();
- }
- void display_cylinder(void)
- {
- GLUquadricObj *cylinder = gluNewQuadric();
- glBindTexture(GL_TEXTURE_2D, texture[1]);
- gluQuadricTexture(cylinder, GL_TRUE);
- gluQuadricDrawStyle(cylinder, GLU_FILL);
- glPolygonMode(GL_FRONT, GL_FILL);
- gluQuadricNormals(cylinder, GLU_SMOOTH);
- gluCylinder(cylinder, 1.0, 0.0, 1.0, 20, 100);
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //TO DO Task 1
- glutWireTeapot(1.0);
- //TO DO Task 2
- display_textures();
- //TO DO Task 5
- //display_cylinder();
- glutSwapBuffers();
- }
- void myReshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.0, 0.0, -3.6);
- }
- void keyboard (unsigned char key, int x, int y)
- {
- //TO DO Task 1
- //rotate of object
- if (key == 'w') {
- glRotatef(10.0, 1.0, 0, 0);
- }
- if (key == 's') {
- glRotatef(10.0, -1.0, 0, 0);
- }
- if (key == 'd') {
- glRotatef(10.0, 0, 1.0, 0);
- }
- if (key == 'a') {
- glRotatef(10.0, 0, -1.0, 0);
- }
- if (key == 'q') {
- glRotatef(10.0, 0, 0, -1.0);
- }
- if (key == 'e') {
- glRotatef(10.0, 0, 0, 1.0);
- }
- //TO DO Task 4
- //init fog
- if (key == 'f') {
- init_fog();
- }
- if (key == ' ') {
- fogfilter = fogfilter++ % 3;
- init_fog();
- }
- if (key == '+') {
- fogDens += 0.05;
- init_fog();
- }
- if (key == '-') {
- fogDens -= 0.05;
- init_fog();
- }
- glutPostRedisplay();
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutCreateWindow("Shaders");
- myinit(); //initialization of objects
- glutReshapeFunc (myReshape); //processing during reshape
- glutDisplayFunc(display); //processing when displaying objects
- glutKeyboardFunc(keyboard); //keyboard subroutine
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement