3dphotoshoot

annotate src/pc/camera.c @ 26:a460b1e5af4a

added GLUT frontend
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Jun 2015 03:55:05 +0300
parents ac80210d5fbe
children 3d082c566b53
rev   line source
nuclear@26 1 #include <math.h>
nuclear@26 2 #include "opengl.h"
nuclear@25 3 #include "camera.h"
nuclear@25 4
nuclear@26 5 static unsigned int tex;
nuclear@26 6 static float tex_matrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@26 7
nuclear@26 8 #define TESTPAT_WIDTH 256
nuclear@26 9 #define TESTPAT_HEIGHT 256
nuclear@26 10
nuclear@25 11 int cam_init(void *platform_data)
nuclear@25 12 {
nuclear@26 13 unsigned char pal[2][2][3] = {
nuclear@26 14 {{255, 128, 64}, {64, 128, 255}},
nuclear@26 15 {{128, 255, 64}, {128, 64, 255}}
nuclear@26 16 };
nuclear@26 17
nuclear@26 18 int i, j;
nuclear@26 19 unsigned char pixels[TESTPAT_WIDTH * TESTPAT_HEIGHT * 3];
nuclear@26 20 unsigned char *pptr = pixels;
nuclear@26 21
nuclear@26 22 for(i=0; i<TESTPAT_HEIGHT; i++) {
nuclear@26 23 for(j=0; j<TESTPAT_WIDTH; j++) {
nuclear@26 24 int chess = ((i >> 3) & 1) == ((j >> 3) & 1);
nuclear@26 25 float x = 2.0 * (float)j / (float)TESTPAT_WIDTH - 1.0;
nuclear@26 26 float y = 2.0 * (float)i / (float)TESTPAT_HEIGHT - 1.0;
nuclear@26 27 float len = sqrt(x * x + y * y);
nuclear@26 28 float wave = cos(len * 4.0 * M_PI);
nuclear@26 29 int band = wave >= 0 ? 0 : 1;
nuclear@26 30
nuclear@26 31 *pptr++ = pal[band][chess][0];
nuclear@26 32 *pptr++ = pal[band][chess][1];
nuclear@26 33 *pptr++ = pal[band][chess][2];
nuclear@26 34 }
nuclear@26 35 }
nuclear@26 36
nuclear@26 37 glGenTextures(1, &tex);
nuclear@26 38 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@26 39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@26 40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@26 41 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TESTPAT_WIDTH, TESTPAT_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
nuclear@26 42
nuclear@26 43 return 0;
nuclear@26 44 }
nuclear@26 45
nuclear@26 46 void cam_shutdown(void)
nuclear@26 47 {
nuclear@26 48 glDeleteTextures(1, &tex);
nuclear@26 49 }
nuclear@26 50
nuclear@26 51 unsigned int cam_texture(void)
nuclear@26 52 {
nuclear@26 53 return tex;
nuclear@26 54 }
nuclear@26 55
nuclear@26 56 const float *cam_texture_matrix(void)
nuclear@26 57 {
nuclear@26 58 return tex_matrix;
nuclear@26 59 }
nuclear@26 60
nuclear@26 61 int cam_start_video(void)
nuclear@26 62 {
nuclear@26 63 return 0;
nuclear@26 64 }
nuclear@26 65
nuclear@26 66 int cam_stop_video(void)
nuclear@26 67 {
nuclear@26 68 return 0;
nuclear@26 69 }
nuclear@26 70
nuclear@26 71 int cam_update(void)
nuclear@26 72 {
nuclear@26 73 return 0;
nuclear@26 74 }
nuclear@26 75
nuclear@26 76 int cam_is_capturing(void)
nuclear@26 77 {
nuclear@26 78 return 1;
nuclear@26 79 }
nuclear@26 80
nuclear@26 81 int cam_video_size(int *xsz, int *ysz)
nuclear@26 82 {
nuclear@26 83 *xsz = TESTPAT_WIDTH;
nuclear@26 84 *ysz = TESTPAT_HEIGHT;
nuclear@26 85 return 0;
nuclear@26 86 }
nuclear@26 87
nuclear@26 88 int cam_take_picture(void)
nuclear@26 89 {
nuclear@25 90 return -1;
nuclear@25 91 }
nuclear@25 92
nuclear@26 93 void cam_draw_preview(void)
nuclear@25 94 {
nuclear@26 95 glPushAttrib(GL_ENABLE_BIT);
nuclear@26 96 glDisable(GL_DEPTH_TEST);
nuclear@26 97 glDisable(GL_LIGHTING);
nuclear@26 98 glEnable(GL_TEXTURE_2D);
nuclear@26 99 glDisable(GL_BLEND);
nuclear@26 100
nuclear@26 101 glUseProgram(0);
nuclear@26 102
nuclear@26 103 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@26 104
nuclear@26 105 glBegin(GL_QUADS);
nuclear@26 106 glTexCoord2f(0, 0);
nuclear@26 107 glVertex2f(-1, -1);
nuclear@26 108 glTexCoord2f(1, 0);
nuclear@26 109 glVertex2f(1, -1);
nuclear@26 110 glTexCoord2f(1, 1);
nuclear@26 111 glVertex2f(1, 1);
nuclear@26 112 glTexCoord2f(0, 1);
nuclear@26 113 glVertex2f(-1, 1);
nuclear@26 114 glEnd();
nuclear@26 115
nuclear@26 116 glPopAttrib();
nuclear@25 117 }