glamtk

view test.c @ 5:86ba127ac2f2

trying to put some glamour in the toolkit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 09 Mar 2011 01:13:28 +0200
parents 00a4ea4ee6dc
children cd00a5775373
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #ifndef __APPLE__
5 #include <GL/glut.h>
6 #else
7 #include <GLUT/glut.h>
8 #endif
9 #include "imtk.h"
11 void disp(void);
12 void gui(void);
13 void reshape(int x, int y);
14 void keyb(unsigned char key, int x, int y);
15 void keyb_up(unsigned char key, int x, int y);
16 void skeyb(int key, int x, int y);
17 void skeyb_up(int key, int x, int y);
18 void mouse(int bn, int state, int x, int y);
19 void motion(int x, int y);
21 int xsz, ysz;
22 float angle;
23 int objsel;
25 int main(int argc, char **argv)
26 {
27 float lpos[] = {-1, 1, 1, 0};
28 float white[] = {1, 1, 1, 1};
29 float color[] = {0.9, 0.8, 0.73, 1};
31 glutInitWindowSize(800, 600);
32 glutInit(&argc, argv);
33 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
34 glutCreateWindow("imgui test");
36 glutDisplayFunc(disp);
37 glutReshapeFunc(reshape);
38 glutKeyboardFunc(keyb);
39 glutKeyboardUpFunc(keyb_up);
40 glutSpecialFunc(skeyb);
41 glutSpecialUpFunc(skeyb_up);
42 glutMouseFunc(mouse);
43 glutMotionFunc(motion);
44 glutPassiveMotionFunc(motion);
46 glEnable(GL_DEPTH_TEST);
47 glEnable(GL_CULL_FACE);
48 glEnable(GL_LIGHTING);
49 glEnable(GL_LIGHT0);
50 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
52 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
53 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
54 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
56 glutMainLoop();
57 return 0;
58 }
60 void disp(void)
61 {
62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
64 glPushAttrib(GL_ENABLE_BIT);
65 glDisable(GL_LIGHTING);
66 glDisable(GL_DEPTH_TEST);
68 glMatrixMode(GL_MODELVIEW);
69 glLoadIdentity();
71 glMatrixMode(GL_PROJECTION);
72 glPushMatrix();
73 glLoadIdentity();
75 glBegin(GL_QUADS);
76 glColor3f(0.2, 0.3, 0.7);
77 glVertex2f(1, 1);
78 glVertex2f(-1, 1);
79 glColor3f(0.7, 0.3, 0.2);
80 glVertex2f(-1, -1);
81 glVertex2f(1, -1);
82 glEnd();
84 glPopMatrix();
85 glMatrixMode(GL_MODELVIEW);
87 glPopAttrib();
89 glTranslatef(0, 0, -8);
90 glRotatef(25, 1, 0, 0);
91 glRotatef(angle, 0, 1, 0);
93 switch(objsel) {
94 case 0:
95 glFrontFace(GL_CW);
96 glutSolidTeapot(1.0);
97 glFrontFace(GL_CCW);
98 break;
100 case 1:
101 glutSolidTorus(0.5, 1, 12, 24);
102 break;
104 case 2:
105 glutSolidSphere(1.0, 24, 12);
106 break;
108 default:
109 break;
110 }
113 gui();
115 glutSwapBuffers();
116 assert(glGetError() == GL_NO_ERROR);
117 }
119 void gui(void)
120 {
121 static int bnshow;
122 static char textbuf[256];
123 static char textbuf2[256];
124 static float val;
125 static int prev_sel;
126 char *itemlist;
128 imtk_begin();
130 if(imtk_button(IMUID, "red", 30, 50)) {
131 float color[] = {1, 0.4, 0.3, 1};
132 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
133 glutPostRedisplay();
134 }
135 if(imtk_button(IMUID, "blue", 30, 80)) {
136 float color[] = {0.3, 0.4, 1, 1};
137 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
138 glutPostRedisplay();
139 }
141 itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
142 if((objsel = imtk_listbox(IMUID, itemlist, prev_sel, 30, 120)) != prev_sel) {
143 prev_sel = objsel;
144 glutPostRedisplay();
145 }
146 imtk_free_list(itemlist);
148 imtk_textbox(IMUID, textbuf, sizeof textbuf, 30, 200);
149 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, 30, 250);
151 if((bnshow = imtk_checkbox(IMUID, "show hidden button", 30, 300, bnshow))) {
152 if(imtk_button(IMUID, "yellow", 50, 340)) {
153 float color[] = {0.8, 0.75, 0.3, 1};
154 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
155 glutPostRedisplay();
156 }
157 }
159 val = imtk_slider(IMUID, angle, 0.0, 360.0, 30, 390);
160 if(val != angle) {
161 angle = val;
162 glutPostRedisplay();
163 }
165 imtk_progress(IMUID, val / 360.0, 30, 420);
167 if(imtk_button(IMUID, "Quit", 30, 500)) {
168 exit(0);
169 }
171 imtk_end();
172 }
174 void reshape(int x, int y)
175 {
176 xsz = x;
177 ysz = y;
179 glViewport(0, 0, x, y);
180 imtk_inp_reshape(x, y);
182 glMatrixMode(GL_PROJECTION);
183 glLoadIdentity();
184 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
185 }
187 void keyb(unsigned char key, int x, int y)
188 {
189 switch(key) {
190 case 27:
191 exit(0);
193 default:
194 break;
195 }
197 imtk_inp_key(key, IMTK_DOWN);
198 }
200 void keyb_up(unsigned char key, int x, int y)
201 {
202 imtk_inp_key(key, IMTK_UP);
203 }
205 void skeyb(int key, int x, int y)
206 {
207 imtk_inp_key(key, IMTK_DOWN);
208 }
210 void skeyb_up(int key, int x, int y)
211 {
212 imtk_inp_key(key, IMTK_UP);
213 }
215 void mouse(int bn, int state, int x, int y)
216 {
217 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
218 }
220 void motion(int x, int y)
221 {
222 imtk_inp_motion(x, y);
223 }