VMTK
shaders.cpp
1 /*
2  * shaders.cpp: compila, liga e transfere shaders para GPU
3  *
4  * Copyright (C) 2013 Wu Shin-Ting, FEEC, Unicamp
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "shaders.h"
25 
26 #define printOpenGLError() printOglError(__FILE__, __LINE__)
27 
28 char *Shaders::textFileRead(const char *fn) {
29  FILE *fp;
30  char *content = NULL;
31 
32  int count=0;
33 
34  if (fn != NULL) {
35  fp = fopen(fn,"rt");
36 
37  if (fp != NULL) {
38 
39  fseek(fp, 0, SEEK_END);
40  count = ftell(fp);
41  rewind(fp);
42 
43  if (count > 0) {
44  content = (char *)malloc(sizeof(char) * (count+1));
45  count = fread(content,sizeof(char),count,fp);
46  content[count] = '\0';
47  }
48  fclose(fp);
49  }
50  else{
51  std::cerr << "Shader not found!" << std::endl;
52  }
53  }
54 
55  return content;
56 }
57 
58 int Shaders::printOglError(char *file, int line)
59 {
60  //
61  // Returns 1 if an OpenGL error occurred, 0 otherwise.
62  //
63  GLenum glErr;
64  int retCode = 0;
65 
66  glErr = glGetError();
67  while (glErr != GL_NO_ERROR)
68  {
69  printf("glError in file %s @ line %d: %s\n", file, line, gluErrorString(glErr));
70  retCode = 1;
71  glErr = glGetError();
72  }
73  return retCode;
74 }
75 
76 void Shaders::printShaderInfoLog(GLuint obj)
77 {
78  int status = 0;
79  int infologLength = 0;
80  int charsWritten = 0;
81  char *infoLog;
82 
83  glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
84  if (infologLength > 0)
85  {
86  infoLog = (char *)malloc(infologLength);
87  glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
88  printf("%s\n",infoLog);
89  free(infoLog);
90  }
91 
92  glGetShaderiv(obj, GL_COMPILE_STATUS,&status);
93  if (status)
94  printf("Shader compiled\n");
95  else
96  printf("Shader not compiled\n");
97 
98 }
99 
100 void Shaders::printProgramInfoLog(GLuint obj)
101 {
102  int infologLength = 0;
103  int charsWritten = 0;
104  char *infoLog;
105 
106  glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
107 
108  if (infologLength > 0)
109  {
110  infoLog = (char *)malloc(infologLength);
111  glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
112  printf("%s\n",infoLog);
113  free(infoLog);
114  }
115 }
116 
117 GLuint Shaders::carregueShaders(const char *vertexFileName, const char *fragmentFileName) {
118 
119  const GLchar *vs = NULL,*fs = NULL;
120 
121  GLuint p,v,f;
122 
123  // Cria e compila shader de vĂ©rtices
124  v = glCreateShader(GL_VERTEX_SHADER);
125  vs = (const GLchar *)textFileRead(vertexFileName);
126  glShaderSource(v, 1, &vs, NULL);
127  free((char *)vs);
128  glCompileShader(v);
129  printShaderInfoLog(v);
130 
131  // Cria e compila shader de fragmentos
132  f = glCreateShader(GL_FRAGMENT_SHADER);
133  fs = (const GLchar *)textFileRead(fragmentFileName);
134  glShaderSource(f, 1, &fs,NULL);
135  free((char *)fs);
136  glCompileShader(f);
137  printShaderInfoLog(f);
138 
139  // Cria um programa e anexa os shaders a ele
140  p = glCreateProgram();
141  glAttachShader(p,v);
142  glAttachShader(p,f);
143 
144  // Liga os elementos do programa
145  glLinkProgram(p);
146  printProgramInfoLog(p);
147 
148  return(p);
149 }
150 
151