imtk

annotate test.c @ 23:4c2b3e281409

added a half-assed automatic layout thing
author John Tsiombikas <nuclear@siggraph.org>
date Sat, 28 May 2011 22:31:51 +0300
parents 737e9047d9c9
children f416d8def7ef
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@2 3 #include <assert.h>
nuclear@0 4 #ifndef __APPLE__
nuclear@0 5 #include <GL/glut.h>
nuclear@0 6 #else
nuclear@0 7 #include <GLUT/glut.h>
nuclear@0 8 #endif
nuclear@0 9 #include "imtk.h"
nuclear@0 10
nuclear@0 11 void disp(void);
nuclear@1 12 void gui(void);
nuclear@0 13 void reshape(int x, int y);
nuclear@0 14 void keyb(unsigned char key, int x, int y);
nuclear@0 15 void keyb_up(unsigned char key, int x, int y);
nuclear@0 16 void skeyb(int key, int x, int y);
nuclear@0 17 void skeyb_up(int key, int x, int y);
nuclear@0 18 void mouse(int bn, int state, int x, int y);
nuclear@0 19 void motion(int x, int y);
nuclear@0 20
nuclear@2 21 int xsz, ysz;
nuclear@3 22 float angle;
nuclear@4 23 int objsel;
nuclear@0 24
nuclear@0 25 int main(int argc, char **argv)
nuclear@0 26 {
nuclear@2 27 float lpos[] = {-1, 1, 1, 0};
nuclear@2 28 float white[] = {1, 1, 1, 1};
nuclear@2 29 float color[] = {0.9, 0.8, 0.73, 1};
nuclear@2 30
nuclear@0 31 glutInitWindowSize(800, 600);
nuclear@0 32 glutInit(&argc, argv);
nuclear@0 33 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@0 34 glutCreateWindow("imgui test");
nuclear@0 35
nuclear@0 36 glutDisplayFunc(disp);
nuclear@0 37 glutReshapeFunc(reshape);
nuclear@0 38 glutKeyboardFunc(keyb);
nuclear@0 39 glutKeyboardUpFunc(keyb_up);
nuclear@0 40 glutSpecialFunc(skeyb);
nuclear@0 41 glutSpecialUpFunc(skeyb_up);
nuclear@0 42 glutMouseFunc(mouse);
nuclear@0 43 glutMotionFunc(motion);
nuclear@0 44 glutPassiveMotionFunc(motion);
nuclear@0 45
nuclear@2 46 glEnable(GL_DEPTH_TEST);
nuclear@2 47 glEnable(GL_CULL_FACE);
nuclear@2 48 glEnable(GL_LIGHTING);
nuclear@2 49 glEnable(GL_LIGHT0);
nuclear@2 50 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@2 51
nuclear@2 52 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@2 53 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
nuclear@2 54 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
nuclear@2 55
nuclear@0 56 glutMainLoop();
nuclear@0 57 return 0;
nuclear@0 58 }
nuclear@0 59
nuclear@0 60 void disp(void)
nuclear@0 61 {
nuclear@0 62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 63
nuclear@13 64 glMatrixMode(GL_MODELVIEW);
nuclear@13 65 glLoadIdentity();
nuclear@13 66 glMatrixMode(GL_PROJECTION);
nuclear@13 67 glPushMatrix();
nuclear@13 68 glLoadIdentity();
nuclear@2 69
nuclear@13 70 glPushAttrib(GL_ENABLE_BIT);
nuclear@13 71 glDisable(GL_LIGHTING);
nuclear@13 72 glDisable(GL_DEPTH_TEST);
nuclear@13 73
nuclear@13 74 glBegin(GL_QUADS);
nuclear@13 75 glColor3f(0.3, 0.4, 0.8);
nuclear@13 76 glVertex2f(-1, -1);
nuclear@13 77 glVertex2f(1, -1);
nuclear@13 78 glColor3f(0.7, 0.3, 0.2);
nuclear@13 79 glVertex2f(1, 1);
nuclear@13 80 glVertex2f(-1, 1);
nuclear@13 81 glEnd();
nuclear@13 82
nuclear@13 83 glPopAttrib();
nuclear@13 84 glPopMatrix();
nuclear@2 85
nuclear@2 86 glMatrixMode(GL_MODELVIEW);
nuclear@2 87 glLoadIdentity();
nuclear@13 88 glTranslatef(0, 0, -4);
nuclear@2 89 glRotatef(25, 1, 0, 0);
nuclear@3 90 glRotatef(angle, 0, 1, 0);
nuclear@2 91
nuclear@4 92 switch(objsel) {
nuclear@4 93 case 0:
nuclear@4 94 glFrontFace(GL_CW);
nuclear@4 95 glutSolidTeapot(1.0);
nuclear@4 96 glFrontFace(GL_CCW);
nuclear@4 97 break;
nuclear@4 98
nuclear@4 99 case 1:
nuclear@4 100 glutSolidTorus(0.5, 1, 12, 24);
nuclear@4 101 break;
nuclear@4 102
nuclear@4 103 case 2:
nuclear@4 104 glutSolidSphere(1.0, 24, 12);
nuclear@4 105 break;
nuclear@4 106
nuclear@4 107 default:
nuclear@4 108 break;
nuclear@4 109 }
nuclear@2 110
nuclear@2 111
nuclear@1 112 gui();
nuclear@1 113
nuclear@1 114 glutSwapBuffers();
nuclear@2 115 assert(glGetError() == GL_NO_ERROR);
nuclear@1 116 }
nuclear@1 117
nuclear@1 118 void gui(void)
nuclear@1 119 {
nuclear@1 120 static int bnshow;
nuclear@2 121 static char textbuf[256];
nuclear@2 122 static char textbuf2[256];
nuclear@3 123 static float val;
nuclear@10 124 static int prev_sel = 0;
nuclear@10 125 char *itemlist;
nuclear@1 126
nuclear@0 127 imtk_begin();
nuclear@20 128 imtk_layout_start(30, 50, 10, IMTK_VERTICAL);
nuclear@0 129
nuclear@13 130 /*glBegin(GL_QUADS);
nuclear@2 131 glColor3f(0.6, 0.6, 0.6);
nuclear@2 132 glVertex2f(0, 0);
nuclear@2 133 glVertex2f(200, 0);
nuclear@2 134 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
nuclear@2 135 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
nuclear@13 136 glEnd();*/
nuclear@2 137
nuclear@20 138 if(imtk_button(IMUID, "red", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 139 float color[] = {1, 0.4, 0.3, 1};
nuclear@2 140 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@2 141 glutPostRedisplay();
nuclear@0 142 }
nuclear@20 143 if(imtk_button(IMUID, "blue", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 144 float color[] = {0.3, 0.4, 1, 1};
nuclear@2 145 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@2 146 glutPostRedisplay();
nuclear@1 147 }
nuclear@1 148
nuclear@10 149 itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
nuclear@20 150 if((objsel = imtk_radiogroup(IMUID, itemlist, prev_sel, IMTK_AUTO, IMTK_AUTO)) != prev_sel) {
nuclear@4 151 prev_sel = objsel;
nuclear@4 152 glutPostRedisplay();
nuclear@4 153 }
nuclear@10 154 imtk_free_list(itemlist);
nuclear@4 155
nuclear@20 156 imtk_textbox(IMUID, textbuf, sizeof textbuf, IMTK_AUTO, IMTK_AUTO);
nuclear@20 157 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, IMTK_AUTO, IMTK_AUTO);
nuclear@2 158
nuclear@20 159 if((bnshow = imtk_checkbox(IMUID, "show hidden button", IMTK_AUTO, IMTK_AUTO, bnshow))) {
nuclear@20 160 if(imtk_button(IMUID, "yellow", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 161 float color[] = {0.8, 0.75, 0.3, 1};
nuclear@2 162 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@2 163 glutPostRedisplay();
nuclear@1 164 }
nuclear@1 165 }
nuclear@0 166
nuclear@20 167 val = imtk_slider(IMUID, angle, 0.0, 360.0, IMTK_AUTO, IMTK_AUTO);
nuclear@3 168 if(val != angle) {
nuclear@3 169 angle = val;
nuclear@3 170 glutPostRedisplay();
nuclear@3 171 }
nuclear@3 172
nuclear@20 173 imtk_progress(IMUID, val / 360.0, IMTK_AUTO, IMTK_AUTO);
nuclear@3 174
nuclear@20 175 imtk_layout_dir(IMTK_HORIZONTAL);
nuclear@20 176 imtk_label("alpha:", IMTK_AUTO, IMTK_AUTO);
nuclear@20 177 imtk_set_alpha(imtk_slider(IMUID, imtk_get_alpha(), 0.0, 1.0, IMTK_AUTO, IMTK_AUTO));
nuclear@20 178 imtk_layout_dir(IMTK_VERTICAL);
nuclear@13 179
nuclear@20 180 if(imtk_button(IMUID, "Quit", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 181 exit(0);
nuclear@2 182 }
nuclear@2 183
nuclear@0 184 imtk_end();
nuclear@0 185 }
nuclear@0 186
nuclear@0 187 void reshape(int x, int y)
nuclear@0 188 {
nuclear@2 189 xsz = x;
nuclear@2 190 ysz = y;
nuclear@2 191
nuclear@0 192 glViewport(0, 0, x, y);
nuclear@13 193 imtk_set_viewport(x, y);
nuclear@13 194
nuclear@13 195 glMatrixMode(GL_PROJECTION);
nuclear@13 196 glLoadIdentity();
nuclear@13 197 gluPerspective(45.0, (float)xsz / (float)ysz, 1.0, 1000.0);
nuclear@0 198 }
nuclear@0 199
nuclear@0 200 void keyb(unsigned char key, int x, int y)
nuclear@0 201 {
nuclear@0 202 switch(key) {
nuclear@0 203 case 27:
nuclear@0 204 exit(0);
nuclear@0 205
nuclear@0 206 default:
nuclear@0 207 break;
nuclear@0 208 }
nuclear@0 209
nuclear@0 210 imtk_inp_key(key, IMTK_DOWN);
nuclear@0 211 }
nuclear@0 212
nuclear@0 213 void keyb_up(unsigned char key, int x, int y)
nuclear@0 214 {
nuclear@0 215 imtk_inp_key(key, IMTK_UP);
nuclear@0 216 }
nuclear@0 217
nuclear@0 218 void skeyb(int key, int x, int y)
nuclear@0 219 {
nuclear@16 220 imtk_inp_key(key | 0x100, IMTK_DOWN);
nuclear@0 221 }
nuclear@0 222
nuclear@0 223 void skeyb_up(int key, int x, int y)
nuclear@0 224 {
nuclear@16 225 imtk_inp_key(key | 0x100, IMTK_UP);
nuclear@0 226 }
nuclear@0 227
nuclear@0 228 void mouse(int bn, int state, int x, int y)
nuclear@0 229 {
nuclear@0 230 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
nuclear@0 231 }
nuclear@0 232
nuclear@0 233 void motion(int x, int y)
nuclear@0 234 {
nuclear@0 235 imtk_inp_motion(x, y);
nuclear@0 236 }