]> git.vomp.tv Git - vompclient.git/blob - glshader.cc
Some fixes on cancellation points
[vompclient.git] / glshader.cc
1 /*\r
2     Copyright 2012 Marten Richter\r
3 \r
4     This file is part of VOMP.\r
5 \r
6     VOMP is free software; you can redistribute it and/or modify\r
7     it under the terms of the GNU General Public License as published by\r
8     the Free Software Foundation; either version 2 of the License, or\r
9     (at your option) any later version.\r
10 \r
11     VOMP is distributed in the hope that it will be useful,\r
12     but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14     GNU General Public License for more details.\r
15 \r
16     You should have received a copy of the GNU General Public License\r
17     along with VOMP; if not, write to the Free Software\r
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
19 */\r
20 #include "glshader.h"\r
21 \r
22 GLShader::GLShader(const char* name)\r
23 {\r
24         initted=0;\r
25         objname=name;\r
26 }\r
27 \r
28 GLShader::~GLShader()\r
29 {\r
30         if (initted) {\r
31                 deinitShaders();\r
32         }\r
33 \r
34 \r
35 \r
36 }\r
37 \r
38 int GLShader::initShaders(const char * vertex_shad,const char *fragment_shad)\r
39 {\r
40           vertex_shader=CreateShader(vertex_shad, GL_VERTEX_SHADER);\r
41           frag_shader=CreateShader(fragment_shad, GL_FRAGMENT_SHADER);\r
42 \r
43           // Create the program for osd rendering\r
44           shad_program=glCreateProgram();\r
45           if (shad_program==0) {\r
46                   Log::getInstance()->log("GLShader", Log::WARN, "%s: Creating glsl program failed!%d",objname,glGetError());\r
47               return 0;\r
48           }\r
49           glAttachShader(shad_program,vertex_shader);\r
50           glAttachShader(shad_program,frag_shader);\r
51           if (!BindAttributes()) {\r
52                   return 0;\r
53           }\r
54           glLinkProgram(shad_program);\r
55 \r
56 \r
57           GLint link_status;\r
58           glGetProgramiv(shad_program,GL_LINK_STATUS, &link_status);\r
59 \r
60           if (!link_status) {\r
61                   char buffer[1024];\r
62                   glGetProgramInfoLog(shad_program,1024,NULL,buffer);\r
63                   Log::getInstance()->log("GLShader", Log::WARN, "%s: Compiling Programm failed!",objname);\r
64                   Log::getInstance()->log("GLShader", Log::WARN, "%s",buffer);\r
65                   glDeleteProgram(shad_program);\r
66                   return 0;\r
67           }\r
68 \r
69 }\r
70 \r
71 GLuint GLShader::CreateShader(const GLchar * source, GLenum type)\r
72 {\r
73         GLuint ret_shad=0;\r
74 \r
75         ret_shad=glCreateShader(type);\r
76         if (ret_shad==0 ) {\r
77                 Log::getInstance()->log("GLShader", Log::WARN, "%s: Creating Shader failed! %d",objname, glGetError());\r
78                 return 0;\r
79         }\r
80         glShaderSource(ret_shad,1,&source,NULL);\r
81         glCompileShader(ret_shad);\r
82         GLint comp_status;\r
83         glGetShaderiv(ret_shad,GL_COMPILE_STATUS, &comp_status);\r
84 \r
85         if (!comp_status) {\r
86                 char buffer[1024];\r
87                 Log::getInstance()->log("GLShader", Log::WARN, "%s: Compiling Shader failed!",objname);\r
88                 glGetShaderInfoLog(ret_shad,1024,NULL,buffer);\r
89                 Log::getInstance()->log("GLShader", Log::WARN, "%s: %s",objname,buffer);\r
90                 glDeleteShader(ret_shad);\r
91                 return 0;\r
92         }\r
93         return ret_shad;\r
94 }\r
95 \r
96 int GLShader::deinitShaders()\r
97 {\r
98          if (frag_shader!=0) glDeleteShader(frag_shader);\r
99          frag_shader=0;\r
100          if (vertex_shader!=0) glDeleteShader(vertex_shader);\r
101          vertex_shader=0;\r
102          if (shad_program!=0) glDeleteProgram(shad_program);\r
103          shad_program=0;\r
104          initted=0;\r
105          return 0;\r
106 }\r
107 \r