1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.stevestreeting.com/ogre/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Teameeting
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/gpl.html.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "OgreGLGpuProgram.h"
|
---|
27 | #include "OgreException.h"
|
---|
28 |
|
---|
29 | using namespace Ogre;
|
---|
30 |
|
---|
31 | GLGpuProgram::GLGpuProgram(ResourceManager* creator, const String& name,
|
---|
32 | ResourceHandle handle, const String& group, bool isManual,
|
---|
33 | ManualResourceLoader* loader)
|
---|
34 | : GpuProgram(creator, name, handle, group, isManual, loader)
|
---|
35 | {
|
---|
36 | if (createParamDictionary("GLGpuProgram"))
|
---|
37 | {
|
---|
38 | setupBaseParamDictionary();
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | GLGpuProgram::~GLGpuProgram()
|
---|
43 | {
|
---|
44 | // have to call this here reather than in Resource destructor
|
---|
45 | // since calling virtual methods in base destructors causes crash
|
---|
46 | unload();
|
---|
47 | }
|
---|
48 |
|
---|
49 | GLArbGpuProgram::GLArbGpuProgram(ResourceManager* creator, const String& name,
|
---|
50 | ResourceHandle handle, const String& group, bool isManual,
|
---|
51 | ManualResourceLoader* loader)
|
---|
52 | : GLGpuProgram(creator, name, handle, group, isManual, loader)
|
---|
53 | {
|
---|
54 | glGenProgramsARB(1, &mProgramID);
|
---|
55 | }
|
---|
56 |
|
---|
57 | GLArbGpuProgram::~GLArbGpuProgram()
|
---|
58 | {
|
---|
59 | // have to call this here reather than in Resource destructor
|
---|
60 | // since calling virtual methods in base destructors causes crash
|
---|
61 | unload();
|
---|
62 | }
|
---|
63 |
|
---|
64 | void GLArbGpuProgram::setType(GpuProgramType t)
|
---|
65 | {
|
---|
66 | GLGpuProgram::setType(t);
|
---|
67 | mProgramType = (mType == GPT_VERTEX_PROGRAM) ? GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB;
|
---|
68 | }
|
---|
69 |
|
---|
70 | void GLArbGpuProgram::bindProgram(void)
|
---|
71 | {
|
---|
72 | glEnable(mProgramType);
|
---|
73 | glBindProgramARB(mProgramType, mProgramID);
|
---|
74 | }
|
---|
75 |
|
---|
76 | void GLArbGpuProgram::unbindProgram(void)
|
---|
77 | {
|
---|
78 | glBindProgramARB(mProgramType, 0);
|
---|
79 | glDisable(mProgramType);
|
---|
80 | }
|
---|
81 |
|
---|
82 | void GLArbGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params)
|
---|
83 | {
|
---|
84 | GLenum type = (mType == GPT_VERTEX_PROGRAM) ?
|
---|
85 | GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB;
|
---|
86 |
|
---|
87 | if (params->hasRealConstantParams())
|
---|
88 | {
|
---|
89 | // Iterate over params and set the relevant ones
|
---|
90 | GpuProgramParameters::RealConstantIterator realIt =
|
---|
91 | params->getRealConstantIterator();
|
---|
92 | unsigned int index = 0;
|
---|
93 | while (realIt.hasMoreElements())
|
---|
94 | {
|
---|
95 | const GpuProgramParameters::RealConstantEntry* e = realIt.peekNextPtr();
|
---|
96 | if (e->isSet)
|
---|
97 | {
|
---|
98 | glProgramLocalParameter4fvARB(type, index, e->val);
|
---|
99 | }
|
---|
100 | index++;
|
---|
101 | realIt.moveNext();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | void GLArbGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)
|
---|
108 | {
|
---|
109 | GLenum type = (mType == GPT_VERTEX_PROGRAM) ?
|
---|
110 | GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB;
|
---|
111 |
|
---|
112 | GpuProgramParameters::RealConstantEntry* realEntry = params->getPassIterationEntry();
|
---|
113 |
|
---|
114 | if (realEntry)
|
---|
115 | {
|
---|
116 | glProgramLocalParameter4fvARB(type, (GLuint)params->getPassIterationEntryIndex(), realEntry->val);
|
---|
117 | }
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 | void GLArbGpuProgram::unloadImpl(void)
|
---|
122 | {
|
---|
123 | glDeleteProgramsARB(1, &mProgramID);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void GLArbGpuProgram::loadFromSource(void)
|
---|
127 | {
|
---|
128 | glBindProgramARB(mProgramType, mProgramID);
|
---|
129 | glProgramStringARB(mProgramType, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)mSource.length(), mSource.c_str());
|
---|
130 |
|
---|
131 | if (GL_INVALID_OPERATION == glGetError())
|
---|
132 | {
|
---|
133 | GLint errPos;
|
---|
134 | glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
|
---|
135 | char errPosStr[16];
|
---|
136 | snprintf(errPosStr, 16, "%d", errPos);
|
---|
137 | char* errStr = (char*)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
|
---|
138 | // XXX New exception code?
|
---|
139 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
|
---|
140 | "Cannot load GL vertex program " + mName +
|
---|
141 | ". Line " + errPosStr + ":\n" + errStr, mName);
|
---|
142 | }
|
---|
143 | glBindProgramARB(mProgramType, 0);
|
---|
144 | }
|
---|
145 |
|
---|