stereoimg

view src/stereoimg.c @ 1:552f59cf0f73

ops
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Jun 2012 00:32:58 +0300
parents b2a2a355b633
children
line source
1 /*
2 Stereoimg - an OpenGL stereoscopic image viewer.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <assert.h>
23 #ifndef __APPLE__
24 #include <GL/glut.h>
25 #else
26 #include <GLUT/glut.h>
27 #endif
29 #include <imago2.h>
31 struct imgnode {
32 char *name;
33 int width, height;
34 unsigned int tex;
35 int swap;
37 struct imgnode *next;
38 };
40 void show_image(struct imgnode *node);
41 void disp(void);
42 void reshape(int x, int y);
43 void keyb(unsigned char key, int x, int y);
44 int parse_args(int argc, char **argv);
47 struct imgnode *imglist;
48 int swap_eyes;
51 int main(int argc, char **argv)
52 {
53 glutInit(&argc, argv);
54 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_STEREO);
55 glutInitWindowSize(128, 128);
56 glutCreateWindow("stereo image viewer");
58 if(parse_args(argc, argv) == -1) {
59 return 1;
60 }
62 glutDisplayFunc(disp);
63 glutReshapeFunc(reshape);
64 glutKeyboardFunc(keyb);
65 glutIdleFunc(glutPostRedisplay);
67 glEnable(GL_TEXTURE_2D);
68 show_image(imglist);
70 glutMainLoop();
71 return 0;
72 }
75 void show_image(struct imgnode *node)
76 {
77 glutReshapeWindow(node->width / 2, node->height);
78 glutSetWindowTitle(node->name);
80 glBindTexture(GL_TEXTURE_2D, node->tex);
82 swap_eyes = node->swap;
83 }
85 void disp(void)
86 {
87 glDrawBuffer(swap_eyes ? GL_BACK_RIGHT : GL_BACK_LEFT);
88 glClear(GL_COLOR_BUFFER_BIT);
90 glBegin(GL_QUADS);
91 glColor3f(1, 1, 1);
92 glTexCoord2f(0, 1); glVertex2f(-1, -1);
93 glTexCoord2f(0.5, 1); glVertex2f(1, -1);
94 glTexCoord2f(0.5, 0); glVertex2f(1, 1);
95 glTexCoord2f(0, 0); glVertex2f(-1, 1);
96 glEnd();
98 glDrawBuffer(swap_eyes ? GL_BACK_LEFT : GL_BACK_RIGHT);
99 glClear(GL_COLOR_BUFFER_BIT);
101 glBegin(GL_QUADS);
102 glColor3f(1, 1, 1);
103 glTexCoord2f(0.5, 1); glVertex2f(-1, -1);
104 glTexCoord2f(1, 1); glVertex2f(1, -1);
105 glTexCoord2f(1, 0); glVertex2f(1, 1);
106 glTexCoord2f(0.5, 0); glVertex2f(-1, 1);
107 glEnd();
109 glutSwapBuffers();
110 assert(glGetError() == GL_NO_ERROR);
111 }
113 void reshape(int x, int y)
114 {
115 glViewport(0, 0, x, y);
116 }
118 void keyb(unsigned char key, int x, int y)
119 {
120 switch(key) {
121 case 's':
122 swap_eyes = !swap_eyes;
123 glutPostRedisplay();
124 break;
126 case ' ':
127 imglist = imglist->next;
128 show_image(imglist);
129 glutPostRedisplay();
130 break;
132 case 27:
133 case 'q':
134 exit(0);
136 default:
137 break;
138 }
139 }
141 int parse_args(int argc, char **argv)
142 {
143 int i, cur_swap = 0;
144 struct imgnode *node;
145 struct imgnode *head = 0, *tail = 0;
147 for(i=1; i<argc; i++) {
148 if(argv[i][0] == '-' && argv[i][2] == 0) {
149 switch(argv[i][1]) {
150 case 's':
151 cur_swap = !cur_swap;
152 break;
154 default:
155 fprintf(stderr, "invalid option: %s\n", argv[i]);
156 return -1;
157 }
158 } else {
159 int xsz, ysz;
160 void *pix;
161 unsigned int tex;
163 if(!(pix = img_load_pixels(argv[i], &xsz, &ysz, IMG_FMT_RGBA32))) {
164 fprintf(stderr, "failed to open image: %s\n", argv[i]);
165 return -1;
166 }
167 glGenTextures(1, &tex);
168 glBindTexture(GL_TEXTURE_2D, tex);
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
172 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix);
174 img_free_pixels(pix);
176 if(!(node = malloc(sizeof *node))) {
177 perror("failed to allocate image node");
178 return -1;
179 }
180 node->name = argv[i];
181 node->width = xsz;
182 node->height = ysz;
183 node->tex = tex;
184 node->swap = cur_swap;
185 node->next = 0;
187 if(head) {
188 tail->next = node;
189 tail = node;
190 } else {
191 head = tail = node;
192 }
193 }
194 }
196 if(!head) {
197 fprintf(stderr, "you must specify one or more images to open\n");
198 return -1;
199 }
200 tail->next = head;
201 imglist = head;
202 return 0;
203 }