9d20cffbbf2a49b1484230230c71a0bcd1c345e3
[glpng.git] / Example / Test.c
blob9d20cffbbf2a49b1484230230c71a0bcd1c345e3
1 /*
2 * Test program for glpng
3 * by Ben Wyatt ben@wyatt100.freeserve.co.uk
4 * Featuring a shameless plug for my stunt course program
5 * Available from the same site as glpng
6 * http://www.wyatt100.freeserve.co.uk/download.htm
7 */
9 #include <GL/glpng.h>
10 #include <GL/glut.h>
11 #include <stdlib.h>
13 int angle = 0;
15 /***** GLUT callback functions *****/
17 void KeyPress(unsigned char key, int x, int y) {
18 switch (key) {
19 case 27: /*ESC*/ glutDestroyWindow(glutGetWindow()); exit(0); break;
23 void Update(void) {
24 angle = (angle+1)%360;
25 glutPostRedisplay();
28 void Display(void) {
29 const float w = 2, h = 2;
31 glClear(GL_COLOR_BUFFER_BIT);
33 glLoadIdentity();
34 glTranslatef(0, 0, -10);
35 glRotatef(angle, 0, 1, 0);
37 glBegin(GL_QUADS);
38 // Front
39 glTexCoord2f(1, 1); glVertex3f( w, -h, 0);
40 glTexCoord2f(1, 0); glVertex3f( w, h, 0);
41 glTexCoord2f(0, 0); glVertex3f(-w, h, 0);
42 glTexCoord2f(0, 1); glVertex3f(-w, -h, 0);
44 // Back
45 glTexCoord2f(1, 1); glVertex3f(-w, -h, 0);
46 glTexCoord2f(1, 0); glVertex3f(-w, h, 0);
47 glTexCoord2f(0, 0); glVertex3f( w, h, 0);
48 glTexCoord2f(0, 1); glVertex3f( w, -h, 0);
49 glEnd();
51 glutSwapBuffers();
54 void Reshape(int w, int h) {
55 glViewport(0, 0, w, h);
57 glMatrixMode(GL_PROJECTION);
58 glLoadIdentity();
59 gluPerspective(30, (float) w/h, 1, 100);
61 glMatrixMode(GL_MODELVIEW);
62 Display();
65 /***** Main function *****/
67 void main() {
68 pngInfo info;
69 GLuint texture;
71 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
72 glutInitWindowSize(300, 300);
73 glutCreateWindow("glpng test");
75 #if 0 // Using pngLoad and setting texture parameters manually.
76 glGenTextures(1, &texture);
77 glBindTexture(GL_TEXTURE_2D, texture);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
84 if (!pngLoad("Stunt.png", PNG_NOMIPMAP, PNG_SOLID, &info)) {
85 puts("Can't load file");
86 exit(1);
88 #else // Using pngLoadAndBind to set texture parameters automatically.
89 texture = pngBind("Stunt.png", PNG_NOMIPMAP, PNG_ALPHA, &info, GL_CLAMP, GL_NEAREST, GL_NEAREST);
91 if (texture == 0) {
92 puts("Can't load file");
93 exit(1);
95 #endif
97 printf("Size=%i,%i Depth=%i Alpha=%i\n", info.Width, info.Height, info.Depth, info.Alpha);
99 glEnable(GL_TEXTURE_2D);
100 glBindTexture(GL_TEXTURE_2D, texture);
101 glEnable(GL_CULL_FACE);
102 glColor3f(1, 1, 1);
104 glutKeyboardFunc(KeyPress);
105 glutIdleFunc(Update);
106 glutDisplayFunc(Display);
107 glutReshapeFunc(Reshape);
109 glutMainLoop();