1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * A versatile mesh processing toolbox                             o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005                                                \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 #ifndef RENDERHELPER_H
24 #define RENDERHELPER_H
25 
26 #include <QString>
27 #include <QImage>
28 #include <QGLFramebufferObject>
29 
30 #include <wrap/gl/shot.h>
31 #include <wrap/callback.h>
32 
33 #include "floatbuffer.h"
34 
35 class QGLFramebufferObject;
36 
37 class RenderHelper {
38 
39  public:
40 
41   GLuint vbo, nbo, cbo, ibo;  // vertex buffer object (vertices, normals, colors, indices)
42 
43   GLint programs[6];
44 
45   enum RenderingMode {FLAT=0, NORMAL=1, COLOR=2};
46   RenderingMode rendmode;
47 
48   //buffers for rendered images
49   unsigned char *render;
50   unsigned char *color;
51   unsigned char *normal;
52   floatbuffer   *depth;
53 
54   // min and max for normalization purposes (e.g. weighting)
55   float mindepth;
56   float maxdepth;
57 
58   RenderHelper();
59   ~RenderHelper();
60 
61   // init
62   int initializeGL(vcg::CallBackPos *cb);
63   int initializeMeshBuffers(MeshModel *mesh, vcg::CallBackPos *cb);
64 
65   // draw & readback
66   void renderScene(Shotm &view, MeshModel *mesh, RenderingMode mode, MLPluginGLContext* plugcontext, float camNear = 0, float camFar = 0);
67 
68  private:
69 
70   GLuint createShaderFromFiles(QString basename); // converted into shader/basename.vert .frag
71   GLuint createShaders(const char *vert, const char *frag);
72 
73 };
74 
75 //-------------------------------------------------------------------------------------------------------
76 //-------------------------------------------------------------------------------------------------------
77 
78 namespace ShaderUtils
79 {
80 
importShaders(const char * filename)81   const char *importShaders(const char *filename)
82   {
83       FILE *fp;
84       char *text = NULL;
85 
86       fp = fopen(filename, "rt");
87 
88       if (fp != NULL)
89       {
90           fseek(fp, 0, SEEK_END);
91           size_t count = ftell(fp);
92           fseek(fp, 0, 0);
93 
94           if (count > 0)
95           {
96               text = new char[count+1];
97               count = fread(text, sizeof(char), count, fp);
98               text[count] = '\0';
99           }
100 
101           fclose(fp);
102       }
103 
104       return text;
105   }
106 
107   // Compile shader and provide verbose output
compileShader(GLuint shader)108   void compileShader(GLuint shader)
109   {
110       static char shlog[2048];
111       GLint status;
112       int length;
113 
114       glCompileShader(shader);
115       glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
116       if (status != GL_TRUE)
117       {
118           glGetShaderInfoLog(shader, 2048, &length, shlog);
119           std::cout << std::endl;
120           std::cout << shlog << std::endl;
121       }
122     //else
123     //  std::cout << " OK!" << std::endl;
124   }
125 
126   // Link shader program and provide verbose output
linkShaderProgram(GLuint program)127   void linkShaderProgram(GLuint program)
128   {
129       static char proglog[2048];
130       GLint status;
131       int length;
132 
133       glLinkProgram(program);
134       glGetProgramiv(program, GL_LINK_STATUS, &status);
135       if (status != GL_TRUE)
136       {
137           glGetProgramInfoLog(program, 2048, &length, proglog);
138           std::cout << std::endl;
139           std::cout << proglog << std::endl;
140       }
141       //else
142     //  std::cout << " OK!" << std::endl;
143   }
144 
145 } /* end namespace */
146 
147 #endif
148