imtk

view test.c @ 20:c7a7ddbe7714

half-arsed automatic layout
author John Tsiombikas <nuclear@siggraph.org>
date Sat, 30 Apr 2011 05:23:59 +0300
parents 737e9047d9c9
children f416d8def7ef
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 glMatrixMode(GL_MODELVIEW);
65 glLoadIdentity();
66 glMatrixMode(GL_PROJECTION);
67 glPushMatrix();
68 glLoadIdentity();
70 glPushAttrib(GL_ENABLE_BIT);
71 glDisable(GL_LIGHTING);
72 glDisable(GL_DEPTH_TEST);
74 glBegin(GL_QUADS);
75 glColor3f(0.3, 0.4, 0.8);
76 glVertex2f(-1, -1);
77 glVertex2f(1, -1);
78 glColor3f(0.7, 0.3, 0.2);
79 glVertex2f(1, 1);
80 glVertex2f(-1, 1);
81 glEnd();
83 glPopAttrib();
84 glPopMatrix();
86 glMatrixMode(GL_MODELVIEW);
87 glLoadIdentity();
88 glTranslatef(0, 0, -4);
89 glRotatef(25, 1, 0, 0);
90 glRotatef(angle, 0, 1, 0);
92 switch(objsel) {
93 case 0:
94 glFrontFace(GL_CW);
95 glutSolidTeapot(1.0);
96 glFrontFace(GL_CCW);
97 break;
99 case 1:
100 glutSolidTorus(0.5, 1, 12, 24);
101 break;
103 case 2:
104 glutSolidSphere(1.0, 24, 12);
105 break;
107 default:
108 break;
109 }
112 gui();
114 glutSwapBuffers();
115 assert(glGetError() == GL_NO_ERROR);
116 }
118 void gui(void)
119 {
120 static int bnshow;
121 static char textbuf[256];
122 static char textbuf2[256];
123 static float val;
124 static int prev_sel = 0;
125 char *itemlist;
127 imtk_begin();
128 imtk_layout_start(30, 50, 10, IMTK_VERTICAL);
130 /*glBegin(GL_QUADS);
131 glColor3f(0.6, 0.6, 0.6);
132 glVertex2f(0, 0);
133 glVertex2f(200, 0);
134 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
135 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
136 glEnd();*/
138 if(imtk_button(IMUID, "red", IMTK_AUTO, IMTK_AUTO)) {
139 float color[] = {1, 0.4, 0.3, 1};
140 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
141 glutPostRedisplay();
142 }
143 if(imtk_button(IMUID, "blue", IMTK_AUTO, IMTK_AUTO)) {
144 float color[] = {0.3, 0.4, 1, 1};
145 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
146 glutPostRedisplay();
147 }
149 itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
150 if((objsel = imtk_radiogroup(IMUID, itemlist, prev_sel, IMTK_AUTO, IMTK_AUTO)) != prev_sel) {
151 prev_sel = objsel;
152 glutPostRedisplay();
153 }
154 imtk_free_list(itemlist);
156 imtk_textbox(IMUID, textbuf, sizeof textbuf, IMTK_AUTO, IMTK_AUTO);
157 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, IMTK_AUTO, IMTK_AUTO);
159 if((bnshow = imtk_checkbox(IMUID, "show hidden button", IMTK_AUTO, IMTK_AUTO, bnshow))) {
160 if(imtk_button(IMUID, "yellow", IMTK_AUTO, IMTK_AUTO)) {
161 float color[] = {0.8, 0.75, 0.3, 1};
162 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
163 glutPostRedisplay();
164 }
165 }
167 val = imtk_slider(IMUID, angle, 0.0, 360.0, IMTK_AUTO, IMTK_AUTO);
168 if(val != angle) {
169 angle = val;
170 glutPostRedisplay();
171 }
173 imtk_progress(IMUID, val / 360.0, IMTK_AUTO, IMTK_AUTO);
175 imtk_layout_dir(IMTK_HORIZONTAL);
176 imtk_label("alpha:", IMTK_AUTO, IMTK_AUTO);
177 imtk_set_alpha(imtk_slider(IMUID, imtk_get_alpha(), 0.0, 1.0, IMTK_AUTO, IMTK_AUTO));
178 imtk_layout_dir(IMTK_VERTICAL);
180 if(imtk_button(IMUID, "Quit", IMTK_AUTO, IMTK_AUTO)) {
181 exit(0);
182 }
184 imtk_end();
185 }
187 void reshape(int x, int y)
188 {
189 xsz = x;
190 ysz = y;
192 glViewport(0, 0, x, y);
193 imtk_set_viewport(x, y);
195 glMatrixMode(GL_PROJECTION);
196 glLoadIdentity();
197 gluPerspective(45.0, (float)xsz / (float)ysz, 1.0, 1000.0);
198 }
200 void keyb(unsigned char key, int x, int y)
201 {
202 switch(key) {
203 case 27:
204 exit(0);
206 default:
207 break;
208 }
210 imtk_inp_key(key, IMTK_DOWN);
211 }
213 void keyb_up(unsigned char key, int x, int y)
214 {
215 imtk_inp_key(key, IMTK_UP);
216 }
218 void skeyb(int key, int x, int y)
219 {
220 imtk_inp_key(key | 0x100, IMTK_DOWN);
221 }
223 void skeyb_up(int key, int x, int y)
224 {
225 imtk_inp_key(key | 0x100, IMTK_UP);
226 }
228 void mouse(int bn, int state, int x, int y)
229 {
230 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
231 }
233 void motion(int x, int y)
234 {
235 imtk_inp_motion(x, y);
236 }