added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / 3dengfx / gfxprog.cpp
1 /*
2 This file is part of the 3dengfx, realtime visualization system.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 3dengfx 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 2 of the License, or
9 (at your option) any later version.
10
11 3dengfx 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 3dengfx; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "3dengfx_config.h"
22
23 #include <iostream>
24 #include <cstdio>
25 #include <cstring>
26 #include "gfxprog.hpp"
27 #include "3denginefx.hpp"
28 #include "opengl.h"
29 #include "common/err_msg.h"
30
31 using namespace std;
32 using namespace glext;
33
34 GfxProg::GfxProg(Shader vertex, Shader pixel) {
35         linked = false;
36         update_handler = 0;
37
38         prog = 0;
39         if(engfx_state::sys_caps.prog.shader_obj) {
40                 prog = glCreateProgramObject();
41
42                 if(vertex) add_shader(vertex);
43                 if(pixel) add_shader(pixel);
44         }
45 }
46
47 GfxProg::~GfxProg() {
48         if(engfx_state::sys_caps.prog.shader_obj) {
49                 list<Shader>::iterator iter = sdr_list.begin();
50                 while(iter != sdr_list.end()) {
51                         glDetachObject(prog, *iter++);
52                 }
53                 glDeleteObject(prog);
54         }
55 }
56
57 void GfxProg::add_shader(Shader sdr) {
58         if(engfx_state::sys_caps.prog.shader_obj) {
59                 glAttachObject(prog, sdr);
60                 sdr_list.push_back(sdr);
61         }
62 }
63
64 void GfxProg::link() {
65         int linked, log_size;
66         
67         if(!engfx_state::sys_caps.prog.shader_obj) {
68                 return;
69         }
70         
71         glLinkProgram(prog);
72         glGetObjectParameteriv(prog, GL_OBJECT_LINK_STATUS_ARB, &linked);
73         glGetObjectParameteriv(prog, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size);
74
75         char *err_str = 0;
76         if(log_size) {
77                 err_str = new char[log_size + 1];
78                 glGetInfoLog(prog, log_size, 0, err_str);
79         }
80
81         if(linked) {
82                 if(err_str) {
83                         info("linked: %s", err_str);
84                         delete [] err_str;
85                 } else {
86                         info("program linked successfully");
87                 }               
88         } else {
89                 if(err_str) {
90                         error("linking failed: %s", err_str);
91                         delete [] err_str;
92                 } else {
93                         error("program linking failed");
94                 }
95         }
96
97         this->linked = (bool)linked;
98 }
99
100 bool GfxProg::is_linked() const {
101         return linked;
102 }
103
104 unsigned int GfxProg::get_id() const {
105         if(!linked) {
106                 const_cast<GfxProg*>(this)->link();
107         }
108         return linked ? prog : 0;
109 }
110
111 bool GfxProg::set_parameter(const char *pname, int val) {
112         if(!engfx_state::sys_caps.prog.shader_obj) {
113                 return false;
114         }
115         glUseProgramObject(prog);
116         int loc = glGetUniformLocation(prog, pname);
117         if(loc != -1) {
118                 glUniform1i(loc, val);
119         }
120         glUseProgramObject(0);
121         return loc == -1 ? false : true;
122 }
123
124 bool GfxProg::set_parameter(const char *pname, scalar_t val) {
125         if(!engfx_state::sys_caps.prog.shader_obj) {
126                 return false;
127         }
128         glUseProgramObject(prog);
129         int loc = glGetUniformLocation(prog, pname);
130         if(loc != -1) {
131                 glUniform1f(loc, val);
132         }
133         glUseProgramObject(0);
134         return loc == -1 ? false : true;
135 }
136
137 bool GfxProg::set_parameter(const char *pname, const Vector2 &val) {
138         if(!engfx_state::sys_caps.prog.shader_obj) {
139                 return false;
140         }
141         glUseProgramObject(prog);
142         int loc = glGetUniformLocation(prog, pname);
143         if(loc != -1) {
144                 glUniform2f(loc, val.x, val.y);
145         }
146         glUseProgramObject(0);
147         return loc == -1 ? false : true;
148 }
149
150 bool GfxProg::set_parameter(const char *pname, const Vector3 &val) {
151         if(!engfx_state::sys_caps.prog.shader_obj) {
152                 return false;
153         }
154         glUseProgramObject(prog);
155         int loc = glGetUniformLocation(prog, pname);
156         if(loc != -1) {
157                 glUniform3f(loc, val.x, val.y, val.z);
158         }
159         glUseProgramObject(0);
160         return loc == -1 ? false : true;
161 }
162
163 bool GfxProg::set_parameter(const char *pname, const Vector4 &val) {
164         if(!engfx_state::sys_caps.prog.shader_obj) {
165                 return false;
166         }
167         glUseProgramObject(prog);
168         int loc = glGetUniformLocation(prog, pname);
169         if(loc != -1) {
170                 glUniform4f(loc, val.x, val.y, val.z, val.w);
171         }
172         glUseProgramObject(0);
173         return loc == -1 ? false : true;
174 }
175
176 bool GfxProg::set_parameter(const char *pname, const Matrix4x4 &val) {
177         if(!engfx_state::sys_caps.prog.shader_obj) {
178                 return false;
179         }
180         glUseProgramObject(prog);
181         int loc = glGetUniformLocation(prog, pname);
182         if(loc != -1) {
183                 glUniformMatrix4fv(loc, 1, 1, val.opengl_matrix());
184         }
185         glUseProgramObject(0);
186         return loc == -1 ? false : true;
187 }
188
189 void GfxProg::set_update_handler(void (*func)(GfxProg*)) {
190         update_handler = func;
191 }