imtk

annotate test.c @ 24:f416d8def7ef

can't remember
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 05 Sep 2011 03:43:30 +0300
parents c7a7ddbe7714
children
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@24 128 imtk_layout_start(30, 50);
nuclear@24 129 imtk_layout_spacing(10);
nuclear@24 130 imtk_layout_dir(IMTK_VERTICAL);
nuclear@0 131
nuclear@13 132 /*glBegin(GL_QUADS);
nuclear@2 133 glColor3f(0.6, 0.6, 0.6);
nuclear@2 134 glVertex2f(0, 0);
nuclear@2 135 glVertex2f(200, 0);
nuclear@2 136 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
nuclear@2 137 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
nuclear@13 138 glEnd();*/
nuclear@2 139
nuclear@20 140 if(imtk_button(IMUID, "red", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 141 float color[] = {1, 0.4, 0.3, 1};
nuclear@2 142 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@2 143 glutPostRedisplay();
nuclear@0 144 }
nuclear@20 145 if(imtk_button(IMUID, "blue", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 146 float color[] = {0.3, 0.4, 1, 1};
nuclear@2 147 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@2 148 glutPostRedisplay();
nuclear@1 149 }
nuclear@1 150
nuclear@10 151 itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
nuclear@20 152 if((objsel = imtk_radiogroup(IMUID, itemlist, prev_sel, IMTK_AUTO, IMTK_AUTO)) != prev_sel) {
nuclear@4 153 prev_sel = objsel;
nuclear@4 154 glutPostRedisplay();
nuclear@4 155 }
nuclear@10 156 imtk_free_list(itemlist);
nuclear@4 157
nuclear@20 158 imtk_textbox(IMUID, textbuf, sizeof textbuf, IMTK_AUTO, IMTK_AUTO);
nuclear@20 159 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, IMTK_AUTO, IMTK_AUTO);
nuclear@2 160
nuclear@20 161 if((bnshow = imtk_checkbox(IMUID, "show hidden button", IMTK_AUTO, IMTK_AUTO, bnshow))) {
nuclear@20 162 if(imtk_button(IMUID, "yellow", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 163 float color[] = {0.8, 0.75, 0.3, 1};
nuclear@2 164 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
nuclear@2 165 glutPostRedisplay();
nuclear@1 166 }
nuclear@1 167 }
nuclear@0 168
nuclear@20 169 val = imtk_slider(IMUID, angle, 0.0, 360.0, IMTK_AUTO, IMTK_AUTO);
nuclear@3 170 if(val != angle) {
nuclear@3 171 angle = val;
nuclear@3 172 glutPostRedisplay();
nuclear@3 173 }
nuclear@3 174
nuclear@20 175 imtk_progress(IMUID, val / 360.0, IMTK_AUTO, IMTK_AUTO);
nuclear@3 176
nuclear@20 177 imtk_layout_dir(IMTK_HORIZONTAL);
nuclear@20 178 imtk_label("alpha:", IMTK_AUTO, IMTK_AUTO);
nuclear@20 179 imtk_set_alpha(imtk_slider(IMUID, imtk_get_alpha(), 0.0, 1.0, IMTK_AUTO, IMTK_AUTO));
nuclear@20 180 imtk_layout_dir(IMTK_VERTICAL);
nuclear@13 181
nuclear@20 182 if(imtk_button(IMUID, "Quit", IMTK_AUTO, IMTK_AUTO)) {
nuclear@2 183 exit(0);
nuclear@2 184 }
nuclear@2 185
nuclear@0 186 imtk_end();
nuclear@0 187 }
nuclear@0 188
nuclear@0 189 void reshape(int x, int y)
nuclear@0 190 {
nuclear@2 191 xsz = x;
nuclear@2 192 ysz = y;
nuclear@2 193
nuclear@0 194 glViewport(0, 0, x, y);
nuclear@13 195 imtk_set_viewport(x, y);
nuclear@13 196
nuclear@13 197 glMatrixMode(GL_PROJECTION);
nuclear@13 198 glLoadIdentity();
nuclear@13 199 gluPerspective(45.0, (float)xsz / (float)ysz, 1.0, 1000.0);
nuclear@0 200 }
nuclear@0 201
nuclear@0 202 void keyb(unsigned char key, int x, int y)
nuclear@0 203 {
nuclear@0 204 switch(key) {
nuclear@0 205 case 27:
nuclear@0 206 exit(0);
nuclear@0 207
nuclear@0 208 default:
nuclear@0 209 break;
nuclear@0 210 }
nuclear@0 211
nuclear@0 212 imtk_inp_key(key, IMTK_DOWN);
nuclear@0 213 }
nuclear@0 214
nuclear@0 215 void keyb_up(unsigned char key, int x, int y)
nuclear@0 216 {
nuclear@0 217 imtk_inp_key(key, IMTK_UP);
nuclear@0 218 }
nuclear@0 219
nuclear@0 220 void skeyb(int key, int x, int y)
nuclear@0 221 {
nuclear@16 222 imtk_inp_key(key | 0x100, IMTK_DOWN);
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 void skeyb_up(int key, int x, int y)
nuclear@0 226 {
nuclear@16 227 imtk_inp_key(key | 0x100, IMTK_UP);
nuclear@0 228 }
nuclear@0 229
nuclear@0 230 void mouse(int bn, int state, int x, int y)
nuclear@0 231 {
nuclear@0 232 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
nuclear@0 233 }
nuclear@0 234
nuclear@0 235 void motion(int x, int y)
nuclear@0 236 {
nuclear@0 237 imtk_inp_motion(x, y);
nuclear@0 238 }