glamtk

view test.c @ 3:038e5577d527

added slider and progress bar widgets
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 Dec 2010 18:10:13 +0200
parents 3d661dd17af3
children 00a4ea4ee6dc
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;
24 int main(int argc, char **argv)
25 {
26 float lpos[] = {-1, 1, 1, 0};
27 float white[] = {1, 1, 1, 1};
28 float color[] = {0.9, 0.8, 0.73, 1};
30 glutInitWindowSize(800, 600);
31 glutInit(&argc, argv);
32 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
33 glutCreateWindow("imgui test");
35 glutDisplayFunc(disp);
36 glutReshapeFunc(reshape);
37 glutKeyboardFunc(keyb);
38 glutKeyboardUpFunc(keyb_up);
39 glutSpecialFunc(skeyb);
40 glutSpecialUpFunc(skeyb_up);
41 glutMouseFunc(mouse);
42 glutMotionFunc(motion);
43 glutPassiveMotionFunc(motion);
45 glEnable(GL_DEPTH_TEST);
46 glEnable(GL_CULL_FACE);
47 glEnable(GL_LIGHTING);
48 glEnable(GL_LIGHT0);
49 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
51 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
52 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
53 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
55 glutMainLoop();
56 return 0;
57 }
59 void disp(void)
60 {
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63 glViewport(200, 0, xsz - 200, ysz);
65 glMatrixMode(GL_PROJECTION);
66 glLoadIdentity();
67 gluPerspective(45.0, (float)(xsz - 200) / (float)ysz, 1.0, 1000.0);
69 glMatrixMode(GL_MODELVIEW);
70 glLoadIdentity();
71 glTranslatef(0, 0, -8);
72 glRotatef(25, 1, 0, 0);
73 glRotatef(angle, 0, 1, 0);
75 glFrontFace(GL_CW);
76 glutSolidTeapot(1.0);
77 glFrontFace(GL_CCW);
80 glViewport(0, 0, 200, ysz);
81 imtk_inp_reshape(200, ysz);
83 gui();
85 glutSwapBuffers();
86 assert(glGetError() == GL_NO_ERROR);
87 }
89 void gui(void)
90 {
91 static int bnshow;
92 static char textbuf[256];
93 static char textbuf2[256];
94 static float val;
96 imtk_begin();
98 glBegin(GL_QUADS);
99 glColor3f(0.6, 0.6, 0.6);
100 glVertex2f(0, 0);
101 glVertex2f(200, 0);
102 glVertex2f(200, glutGet(GLUT_WINDOW_HEIGHT));
103 glVertex2f(0, glutGet(GLUT_WINDOW_HEIGHT));
104 glEnd();
106 if(imtk_button(IMUID, "red", 30, 50)) {
107 float color[] = {1, 0.4, 0.3, 1};
108 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
109 glutPostRedisplay();
110 }
111 if(imtk_button(IMUID, "blue", 30, 100)) {
112 float color[] = {0.3, 0.4, 1, 1};
113 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
114 glutPostRedisplay();
115 }
117 imtk_textbox(IMUID, textbuf, sizeof textbuf, 30, 200);
118 imtk_textbox(IMUID, textbuf2, sizeof textbuf2, 30, 250);
120 if((bnshow = imtk_checkbox(IMUID, "show hidden button", 30, 300, bnshow))) {
121 if(imtk_button(IMUID, "yellow", 50, 340)) {
122 float color[] = {0.8, 0.75, 0.3, 1};
123 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
124 glutPostRedisplay();
125 }
126 }
128 val = imtk_slider(IMUID, angle, 0.0, 360.0, 30, 390);
129 if(val != angle) {
130 angle = val;
131 glutPostRedisplay();
132 }
134 imtk_progress(IMUID, val / 360.0, 30, 420);
136 if(imtk_button(IMUID, "Quit", 30, 500)) {
137 exit(0);
138 }
140 imtk_end();
141 }
143 void reshape(int x, int y)
144 {
145 xsz = x;
146 ysz = y;
148 glViewport(0, 0, x, y);
149 }
151 void keyb(unsigned char key, int x, int y)
152 {
153 switch(key) {
154 case 27:
155 exit(0);
157 default:
158 break;
159 }
161 imtk_inp_key(key, IMTK_DOWN);
162 }
164 void keyb_up(unsigned char key, int x, int y)
165 {
166 imtk_inp_key(key, IMTK_UP);
167 }
169 void skeyb(int key, int x, int y)
170 {
171 imtk_inp_key(key, IMTK_DOWN);
172 }
174 void skeyb_up(int key, int x, int y)
175 {
176 imtk_inp_key(key, IMTK_UP);
177 }
179 void mouse(int bn, int state, int x, int y)
180 {
181 imtk_inp_mouse(bn, state == GLUT_DOWN ? IMTK_DOWN : IMTK_UP);
182 }
184 void motion(int x, int y)
185 {
186 imtk_inp_motion(x, y);
187 }