Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <GLUT/glut.h>
- /* keep the picture data*/
- #define IMGFILE "cat.bmp"
- #define CHANNEL 3 // color(R, G, B)
- int width; // width of picture
- int height; // height of picture
- GLuint texture; // texture number
- GLubyte* image; // keep the BMP data
- void loadImage(const char* filepath); // read BMP
- float shiftX,shiftY;
- float mouseX,mouseY;
- float x,y;
- float a;
- float g_pressed = 0;
- void display(void)
- {
- /*diaplay the picture */
- glEnable(GL_TEXTURE_2D);
- glClear(GL_COLOR_BUFFER_BIT);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTranslated(-0.5,-0.5,0);
- glBegin(GL_QUADS);
- glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);//position
- glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
- glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
- glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
- //glClear(GL_COLOR_BUFFER_BIT);
- glDisable(GL_TEXTURE_2D);
- glEnd();
- //glColor3d(0.0, 0.0, 0.0);//color
- glTranslated(0.5,0.5,0);
- glTranslatef(shiftX,shiftY,0);
- x=0.1;
- y=0.1;
- glBegin(GL_LINE_LOOP);
- glVertex2d(-x+shiftX, -y+shiftY);//position
- glVertex2d(x+shiftX, -y+shiftY);
- glVertex2d(x+shiftX, y+shiftY);
- glVertex2d(-x+shiftX, y+shiftY);
- //glPushMatrix();
- //glTranslatef(shiftX,shiftY,0);
- // glTranslatef(0.5,0.5,0);
- //glPopMatrix();
- glEnd();
- glutSwapBuffers();
- //glFlush();
- }
- void myInit(void)
- {
- /* necessary setting for before create window*/
- glutInitWindowSize(800, 600); // setting for window size
- glutInitWindowPosition(0, 0); // window position
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); // setting display mode
- glutCreateWindow("OpenGL"); // create window
- glClearColor (0.0, 0.0, 0.0, 1.0); // window back color
- glutDisplayFunc(display);
- //create texture
- loadImage(IMGFILE);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- 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, GL_RGB,
- width, height, 0, GL_RGB,
- GL_UNSIGNED_BYTE, image);
- free(image);
- }
- void keyboard(unsigned char key, int x, int y) //keyboard setting
- {
- switch (key) {
- case 'q':
- case 'Q':
- case '\033':
- exit(0);
- default:
- break;
- }
- }
- void mouse(int btn, int state, int x, int y) //mouze setting
- {
- if(btn == GLUT_LEFT_BUTTON == state == GLUT_DOWN) //when press the left button
- {
- mouseX = x;
- mouseY = y;
- g_pressed = 1;
- }
- else if(btn == GLUT_LEFT_BUTTON == state ==GLUT_UP) //when take off the left button
- {
- g_pressed = 0;
- }
- }
- void motion(int x, int y) //controll the motion by the mouse
- {
- {
- shiftX = (float)((x - mouseX)/6000);
- shiftY = (float)(-(y - mouseY)/6000);
- glutPostRedisplay();
- }
- printf("X = %d : Y = %d\n" , x , y); //diplay the position of coordinate
- printf("ShiftX = %f : ShiftY = %f\n" , shiftX , shiftY); //display the amount of the shift
- printf("mouseX = %f : mouseY = %f \n" , mouseX , mouseY); //display the mouseX and mouseY
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- //glutInitDisplayMode(GLUT_RGBA);
- //glutCreateWindow(argv[0]);
- myInit();
- glutDisplayFunc(display); //when need to set about display function
- glutKeyboardFunc(keyboard); //when need keyboard function
- glutMouseFunc(mouse); //when need mouse function
- glutMotionFunc(motion); //when need mouse motion function
- glutMainLoop();
- return 0;
- }
- void loadImage(const char* filepath)
- {
- int i;
- int size; // size of picture (width , height, color)
- FILE* fp;
- /* read file */
- fp = fopen(filepath, "rb"); //read binary
- if (fp == NULL)
- {
- fprintf(stderr, "%sfail loading\n", filepath);
- exit(EXIT_FAILURE);
- }
- /* get width and height */
- fseek(fp, 18, SEEK_SET); //move fp to place where has been put width of picture
- fread(&width, 4, 1, fp);
- fread(&height, 4, 1, fp);
- /* read data of picture */
- fseek(fp, 54, SEEK_SET); //move fp to place where has been put dataof the picture
- size = width * height * CHANNEL;
- image = (GLubyte* ) malloc(size);
- if (image == NULL)
- {
- fprintf(stderr, "fail to keep the memory\n");
- exit(EXIT_FAILURE);
- }
- for (i = 0; i < size; ++i)
- fread(&image[i], sizeof(GLubyte), 1, fp);
- /* exit */
- fclose(fp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement