imtk
view test.c @ 27:48e708baa7be
added predicate imtk_layout_contains(x, y)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 12 Dec 2020 17:20:00 +0200 |
parents | c7a7ddbe7714 |
children |
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);
129 imtk_layout_spacing(10);
130 imtk_layout_dir(IMTK_VERTICAL);
132 /*glBegin(GL_QUADS);
133 glColor3f(0.6, 0.6, 0.6);
134 glVertex2f(0, 0);
135 glVertex2f(200, 0);
136 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
137 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
138 glEnd();*/
140 if(imtk_button(IMUID, "red", IMTK_AUTO, IMTK_AUTO)) {
141 float color[] = {1, 0.4, 0.3, 1};
142 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
143 glutPostRedisplay();
144 }
145 if(imtk_button(IMUID, "blue", IMTK_AUTO, IMTK_AUTO)) {
146 float color[] = {0.3, 0.4, 1, 1};
147 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
148 glutPostRedisplay();
149 }
151 itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
152 if((objsel = imtk_radiogroup(IMUID, itemlist, prev_sel, IMTK_AUTO, IMTK_AUTO)) != prev_sel) {
153 prev_sel = objsel;
154 glutPostRedisplay();
155 }
156 imtk_free_list(itemlist);
158 imtk_textbox(IMUID, textbuf, sizeof textbuf, IMTK_AUTO, IMTK_AUTO);
159 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, IMTK_AUTO, IMTK_AUTO);
161 if((bnshow = imtk_checkbox(IMUID, "show hidden button", IMTK_AUTO, IMTK_AUTO, bnshow))) {
162 if(imtk_button(IMUID, "yellow", IMTK_AUTO, IMTK_AUTO)) {
163 float color[] = {0.8, 0.75, 0.3, 1};
164 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
165 glutPostRedisplay();
166 }
167 }
169 val = imtk_slider(IMUID, angle, 0.0, 360.0, IMTK_AUTO, IMTK_AUTO);
170 if(val != angle) {
171 angle = val;
172 glutPostRedisplay();
173 }
175 imtk_progress(IMUID, val / 360.0, IMTK_AUTO, IMTK_AUTO);
177 imtk_layout_dir(IMTK_HORIZONTAL);
178 imtk_label("alpha:", IMTK_AUTO, IMTK_AUTO);
179 imtk_set_alpha(imtk_slider(IMUID, imtk_get_alpha(), 0.0, 1.0, IMTK_AUTO, IMTK_AUTO));
180 imtk_layout_dir(IMTK_VERTICAL);
182 if(imtk_button(IMUID, "Quit", IMTK_AUTO, IMTK_AUTO)) {
183 exit(0);
184 }
186 imtk_end();
187 }
189 void reshape(int x, int y)
190 {
191 xsz = x;
192 ysz = y;
194 glViewport(0, 0, x, y);
195 imtk_set_viewport(x, y);
197 glMatrixMode(GL_PROJECTION);
198 glLoadIdentity();
199 gluPerspective(45.0, (float)xsz / (float)ysz, 1.0, 1000.0);
200 }
202 void keyb(unsigned char key, int x, int y)
203 {
204 switch(key) {
205 case 27:
206 exit(0);
208 default:
209 break;
210 }
212 imtk_inp_key(key, IMTK_DOWN);
213 }
215 void keyb_up(unsigned char key, int x, int y)
216 {
217 imtk_inp_key(key, IMTK_UP);
218 }
220 void skeyb(int key, int x, int y)
221 {
222 imtk_inp_key(key | 0x100, IMTK_DOWN);
223 }
225 void skeyb_up(int key, int x, int y)
226 {
227 imtk_inp_key(key | 0x100, IMTK_UP);
228 }
230 void mouse(int bn, int state, int x, int y)
231 {
232 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
233 }
235 void motion(int x, int y)
236 {
237 imtk_inp_motion(x, y);
238 }