2 This file is part of the 3dengfx, realtime visualization system.
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
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.
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.
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
21 #include "3dengfx_config.h"
26 #include "gfxprog.hpp"
27 #include "3denginefx.hpp"
29 #include "common/err_msg.h"
32 using namespace glext;
34 GfxProg::GfxProg(Shader vertex, Shader pixel) {
39 if(engfx_state::sys_caps.prog.shader_obj) {
40 prog = glCreateProgramObject();
42 if(vertex) add_shader(vertex);
43 if(pixel) add_shader(pixel);
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++);
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);
64 void GfxProg::link() {
67 if(!engfx_state::sys_caps.prog.shader_obj) {
72 glGetObjectParameteriv(prog, GL_OBJECT_LINK_STATUS_ARB, &linked);
73 glGetObjectParameteriv(prog, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size);
77 err_str = new char[log_size + 1];
78 glGetInfoLog(prog, log_size, 0, err_str);
83 info("linked: %s", err_str);
86 info("program linked successfully");
90 error("linking failed: %s", err_str);
93 error("program linking failed");
97 this->linked = (bool)linked;
100 bool GfxProg::is_linked() const {
104 unsigned int GfxProg::get_id() const {
106 const_cast<GfxProg*>(this)->link();
108 return linked ? prog : 0;
111 bool GfxProg::set_parameter(const char *pname, int val) {
112 if(!engfx_state::sys_caps.prog.shader_obj) {
115 glUseProgramObject(prog);
116 int loc = glGetUniformLocation(prog, pname);
118 glUniform1i(loc, val);
120 glUseProgramObject(0);
121 return loc == -1 ? false : true;
124 bool GfxProg::set_parameter(const char *pname, scalar_t val) {
125 if(!engfx_state::sys_caps.prog.shader_obj) {
128 glUseProgramObject(prog);
129 int loc = glGetUniformLocation(prog, pname);
131 glUniform1f(loc, val);
133 glUseProgramObject(0);
134 return loc == -1 ? false : true;
137 bool GfxProg::set_parameter(const char *pname, const Vector2 &val) {
138 if(!engfx_state::sys_caps.prog.shader_obj) {
141 glUseProgramObject(prog);
142 int loc = glGetUniformLocation(prog, pname);
144 glUniform2f(loc, val.x, val.y);
146 glUseProgramObject(0);
147 return loc == -1 ? false : true;
150 bool GfxProg::set_parameter(const char *pname, const Vector3 &val) {
151 if(!engfx_state::sys_caps.prog.shader_obj) {
154 glUseProgramObject(prog);
155 int loc = glGetUniformLocation(prog, pname);
157 glUniform3f(loc, val.x, val.y, val.z);
159 glUseProgramObject(0);
160 return loc == -1 ? false : true;
163 bool GfxProg::set_parameter(const char *pname, const Vector4 &val) {
164 if(!engfx_state::sys_caps.prog.shader_obj) {
167 glUseProgramObject(prog);
168 int loc = glGetUniformLocation(prog, pname);
170 glUniform4f(loc, val.x, val.y, val.z, val.w);
172 glUseProgramObject(0);
173 return loc == -1 ? false : true;
176 bool GfxProg::set_parameter(const char *pname, const Matrix4x4 &val) {
177 if(!engfx_state::sys_caps.prog.shader_obj) {
180 glUseProgramObject(prog);
181 int loc = glGetUniformLocation(prog, pname);
183 glUniformMatrix4fv(loc, 1, 1, val.opengl_matrix());
185 glUseProgramObject(0);
186 return loc == -1 ? false : true;
189 void GfxProg::set_update_handler(void (*func)(GfxProg*)) {
190 update_handler = func;