rendered paste body#include <stdio.h>#include <stdlib.h>#include <GL/gl.h>#include <GL/glut.h>#include "jpeg.h"#define ESCAPE 27void init(void);void display(void);void keyPressed(unsigned char key, int x, int y);void makeTextureDimensions(int width, int height, int* newWidth, int* newHeight);void makeCube();GLfloat rquad; // Angle For The Quad ( NEW )GLuint cubeDL;int window;GLuint texture[1];int main (int argc, char **argv){ FILE* infile; /* source file */ if((infile = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "can't open %s\n", argv[1]); return 1; } int width, height, components; get_jpeg_file_dimensions(infile, &width, &height, &components); printf("JPEG, w: %d, h: %d\n", width, height); makeTextureDimensions(width, height, &width, &height); printf("Texture, w: %d, h: %d\n", width, height); byte* jpeg_buffer = malloc( sizeof(byte) * width * height * components); read_rgb_jpeg_file_to_rgb(infile, jpeg_buffer, width);// int i = 0;// for(i = 0; i < 333; i+=3) {// printf("j: %d %d %d\n", jpeg_buffer[i], jpeg_buffer[i+1], jpeg_buffer[i+2]);// } glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]); 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, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, jpeg_buffer); glEnable(GL_TEXTURE_2D); free(jpeg_buffer); rquad = 0.1; glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); glutInitWindowSize(250, 250); glutInitWindowPosition(100, 100); window = glutCreateWindow("cube"); init(); glutDisplayFunc(display); //glutFullScreen(); glutIdleFunc(display); glutKeyboardFunc(keyPressed); glutMainLoop(); return 0;}void makeTextureDimensions(int width, int height, int* newWidth, int* newHeight) { *newWidth = 64; while(*newWidth < width){ *newWidth = *newWidth << 1; } *newHeight = 64; while(*newHeight < height){ *newHeight = *newHeight << 1; } if(*newHeight > *newWidth) *newWidth = *newHeight; else *newHeight = *newWidth;}void keyPressed(unsigned char key, int x, int y) { usleep(50); if (key == ESCAPE) { glutDestroyWindow(window); //exit(0); }}void init(void){ glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0); gluPerspective(70.0f,(GLfloat)250/(GLfloat)250,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); cubeDL = glGenLists(1); glNewList(cubeDL,GL_COMPILE); makeCube(); glEndList();}void makeCube() { glBindTexture(GL_TEXTURE_2D, texture[0]); glBegin(GL_QUADS); //top glColor3f(1,0,0); glVertex3f(-2.0f, 2.0f, -2.0f); glVertex3f(-2.0f, 2.0f, 2.0f); glVertex3f( 2.0f, 2.0f, 2.0f); glVertex3f( 2.0f, 2.0f, -2.0f); //bottom glVertex3f(-2.0f, -2.0f, -2.0f); glVertex3f(-2.0f, -2.0f, 2.0f); glVertex3f( 2.0f, -2.0f, 2.0f); glVertex3f( 2.0f, -2.0f, -2.0f); //back glColor3f(0,1,0); glVertex3f(-2.0f, 2.0f, -2.0f); glVertex3f( 2.0f, 2.0f, -2.0f); glVertex3f( 2.0f, -2.0f, -2.0f); glVertex3f(-2.0f, -2.0f, -2.0f); //front glNormal3f( 0.0f, 0.0f, 1.0f); //glColor3f(1,0,1); glTexCoord2f(0.0f, 1.0f); glVertex3f(-2.0f, 2.0f, 2.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 2.0f, 2.0f, 2.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 2.0f, -2.0f, 2.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.0f, -2.0f, 2.0f); //left glColor3f(0,0,1); glVertex3f(-2.0f, 2.0f, 2.0f); glVertex3f(-2.0f, 2.0f, -2.0f); glVertex3f(-2.0f, -2.0f, -2.0f); glVertex3f(-2.0f, -2.0f, 2.0f); //right glVertex3f(1.0f, 2.0f, 2.0f); glVertex3f(1.0f, 2.0f, -2.0f); glVertex3f(2.0f, -2.0f, -2.0f); glVertex3f(2.0f, -2.0f, 2.0f); glEnd();}void display(void){ glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glColor3f(1,1,0); glPushMatrix(); glTranslatef(0,0,-7); glRotatef(rquad,0.5f,1.0f,0.2f); rquad += 0.1; //glCallList(cubeDL); makeCube(); glPopMatrix(); glutSwapBuffers();}