invisible
changeset 19:f756fc9fdd3e
quick backup
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Sun, 10 Nov 2013 14:02:36 +0200 |
parents | ad78eb81988d |
children | 38bf3eec0fac |
files | .hgignore src/sdr.cc src/sdr.h src/tesquad.cc src/tesquad.h src/texture.cc src/texture.h |
diffstat | 7 files changed, 196 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- a/.hgignore Sun Nov 10 00:38:23 2013 +0200 1.2 +++ b/.hgignore Sun Nov 10 14:02:36 2013 +0200 1.3 @@ -7,3 +7,5 @@ 1.4 *.d 1.5 invisible 1.6 *.swp 1.7 +*.jpg 1.8 +*.png
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/sdr.cc Sun Nov 10 14:02:36 2013 +0200 2.3 @@ -0,0 +1,102 @@ 2.4 +#include <GL/glew.h> 2.5 +#include <GL/gl.h> 2.6 + 2.7 +#include <errno.h> 2.8 +#include <stdio.h> 2.9 +#include <stdlib.h> 2.10 +#include <string.h> 2.11 + 2.12 +#include "sdr.h" 2.13 + 2.14 +char* sdr_load(const char* fname) 2.15 +{ 2.16 + if(!fname) { 2.17 + return 0; 2.18 + } 2.19 + 2.20 + FILE *fp; 2.21 + int ctr = 0; 2.22 + 2.23 + if(!(fp = fopen(fname, "rb"))) { 2.24 + fprintf(stderr, "Failed to open file %s: %s\n", fname, strerror(errno)); 2.25 + return 0; 2.26 + } 2.27 + fseek(fp, 0, SEEK_END); 2.28 + ctr = ftell(fp); 2.29 + rewind(fp); 2.30 + 2.31 + char *glsl = NULL; 2.32 + if(!(glsl = (char*) malloc(ctr + 1))) { 2.33 + perror("Failed to allocate memory"); 2.34 + return 0; 2.35 + } 2.36 + ctr = fread(glsl, 1, ctr, fp); 2.37 + fclose(fp); 2.38 + 2.39 + return glsl; 2.40 +} 2.41 + 2.42 +bool sdr_compile(unsigned int sdr) 2.43 +{ 2.44 + int status, loglen; 2.45 + char infolog[512]; 2.46 + 2.47 + glCompileShader(sdr); 2.48 + glGetShaderiv(sdr, GL_COMPILE_STATUS, &status); 2.49 + glGetShaderInfoLog(sdr, sizeof infolog, &loglen, infolog); 2.50 + if(status == GL_FALSE) { 2.51 + fprintf(stderr, "Failed to compile shader: %s\n", infolog); 2.52 + return false; 2.53 + } else if(loglen) { 2.54 + fprintf(stderr, "%s\n", infolog); 2.55 + } 2.56 + return true; 2.57 +} 2.58 + 2.59 +unsigned int sdr_getprog(const char* vfname, const char* ffname) 2.60 +{ 2.61 + char *vsdr_glsl = NULL; 2.62 + char *fsdr_glsl = NULL; 2.63 + 2.64 + GLuint vsdr; GLuint fsdr; 2.65 + vsdr = glCreateShader(GL_VERTEX_SHADER); 2.66 + fsdr = glCreateShader(GL_FRAGMENT_SHADER); 2.67 + 2.68 + if(!(vsdr_glsl = sdr_load(vfname))) 2.69 + return 0; 2.70 + if(!(fsdr_glsl = sdr_load(ffname))) 2.71 + return 0; 2.72 + 2.73 + glShaderSource(vsdr, 1, (const GLchar**)&vsdr_glsl, NULL); 2.74 + glShaderSource(fsdr, 1, (const GLchar**)&fsdr_glsl, NULL); 2.75 + 2.76 + free(vsdr_glsl); 2.77 + free(fsdr_glsl); 2.78 + 2.79 + if(!sdr_compile(vsdr)) { 2.80 + fprintf(stderr, "Shader %s failed to compile.\n", vfname); 2.81 + return 0; 2.82 + } 2.83 + if(!sdr_compile(fsdr)) { 2.84 + fprintf(stderr, "Shader %s failed to compile.\n", ffname); 2.85 + return 0; 2.86 + } 2.87 + 2.88 + unsigned int sprog; 2.89 + int status, loglen; 2.90 + char infolog[512]; 2.91 + 2.92 + sprog = glCreateProgram(); 2.93 + glAttachShader(sprog, vsdr); 2.94 + glAttachShader(sprog, fsdr); 2.95 + glLinkProgram(sprog); 2.96 + glGetProgramiv(sprog, GL_LINK_STATUS, &status); 2.97 + glGetProgramInfoLog(sprog, sizeof infolog, &loglen, infolog); 2.98 + if(status == GL_FALSE) { 2.99 + fprintf(stderr, "Error while linking shader program:\n%s\n", infolog); 2.100 + return false; 2.101 + } else if(loglen) { 2.102 + fprintf(stderr, "%s\n", infolog); 2.103 + } 2.104 + return sprog; 2.105 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/sdr.h Sun Nov 10 14:02:36 2013 +0200 3.3 @@ -0,0 +1,8 @@ 3.4 +#ifndef SDR_H_ 3.5 +#define SDR_H_ 3.6 + 3.7 +char *sdr_load(const char *fname); 3.8 +bool sdr_compile(unsigned int sdr); 3.9 +unsigned int sdr_getprog(const char *vfname, const char *ffname); 3.10 + 3.11 +#endif // SDR_H_
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/tesquad.cc Sun Nov 10 14:02:36 2013 +0200 4.3 @@ -0,0 +1,46 @@ 4.4 +#include <GL/gl.h> 4.5 +#include "tesquad.h" 4.6 + 4.7 +void draw_tess_quad(float x, float y, float w, float h, int usub, int vsub, bool ortho) 4.8 +{ 4.9 + if(ortho) { 4.10 + glPushAttrib(GL_ENABLE_BIT); 4.11 + glDisable(GL_DEPTH_TEST); 4.12 + glDisable(GL_LIGHTING); 4.13 + 4.14 + glMatrixMode(GL_PROJECTION); 4.15 + glPushMatrix(); 4.16 + glLoadIdentity(); 4.17 + } 4.18 + 4.19 + float du = 1.0 / usub; 4.20 + float dv = 1.0 / vsub; 4.21 + 4.22 + float dx = du * w; 4.23 + float dy = dv * h; 4.24 + 4.25 + glBegin(GL_QUADS); 4.26 + for (int i=0; i<vsub; i++) { 4.27 + for (int j=0; j<usub; j++) { 4.28 + float u = j * du; 4.29 + float v = 1.0 - i * dv; 4.30 + float sx = x + j * dx; 4.31 + float sy = y + i * dy; 4.32 + 4.33 + glTexCoord2f(u, v); 4.34 + glVertex2f(sx, sy); 4.35 + glTexCoord2f(u + du, v); 4.36 + glVertex2f(sx + dx, sy); 4.37 + glTexCoord2f(u + du, v - dv); 4.38 + glVertex2f(sx + dx, sy + dy); 4.39 + glTexCoord2f(u, v - dv); 4.40 + glVertex2f(sx, sy + dy); 4.41 + } 4.42 + } 4.43 + glEnd(); 4.44 + 4.45 + if(ortho) { 4.46 + glPopMatrix(); 4.47 + glPopAttrib(); 4.48 + } 4.49 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/tesquad.h Sun Nov 10 14:02:36 2013 +0200 5.3 @@ -0,0 +1,6 @@ 5.4 +#ifndef TESQUAD_H_ 5.5 +#define TESQUAD_H_ 5.6 + 5.7 +void draw_tess_quad(float x, float y, float w, float h, int usub, int vsub, bool ortho = false); 5.8 + 5.9 +#endif //TESQUAD_H_
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/texture.cc Sun Nov 10 14:02:36 2013 +0200 6.3 @@ -0,0 +1,26 @@ 6.4 +#include <GL/gl.h> 6.5 +#include <highgui.h> 6.6 +#include <cv.h> 6.7 +#include "texture.h" 6.8 + 6.9 +unsigned int load_texture(const char* fname) 6.10 +{ 6.11 + IplImage* img = cvLoadImage(fname); 6.12 + if(!img) { 6.13 + fprintf(stderr, "Failed to load image: %s\n", fname); 6.14 + return 0; 6.15 + } 6.16 + cv::Mat mat = cv::Mat(img); 6.17 + 6.18 + unsigned int tex; 6.19 + glGenTextures(1, &tex); 6.20 + glBindTexture(GL_TEXTURE_2D, tex); 6.21 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 6.22 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 6.23 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 6.24 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 6.25 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mat.cols, mat.rows, 6.26 + 0, GL_BGR, GL_UNSIGNED_BYTE, mat.data); 6.27 + 6.28 + return tex; 6.29 +}