Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/RenderSystems/GL/src/OgreGLGpuProgram.cpp @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

File size: 5.9 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29
30#include "OgreGLGpuProgram.h"
31#include "OgreException.h"
32#include "OgreStringConverter.h"
33
34using namespace Ogre;
35
36GLGpuProgram::GLGpuProgram(ResourceManager* creator, const String& name, 
37    ResourceHandle handle, const String& group, bool isManual, 
38    ManualResourceLoader* loader) 
39    : GpuProgram(creator, name, handle, group, isManual, loader)
40{
41    if (createParamDictionary("GLGpuProgram"))
42    {
43        setupBaseParamDictionary();
44    }
45}
46
47GLGpuProgram::~GLGpuProgram()
48{
49    // have to call this here reather than in Resource destructor
50    // since calling virtual methods in base destructors causes crash
51    unload(); 
52}
53
54GLuint GLGpuProgram::getAttributeIndex(VertexElementSemantic semantic)
55{
56        // default implementation
57        switch(semantic)
58        {
59                case VES_POSITION:
60                case VES_NORMAL:
61                case VES_DIFFUSE:
62                case VES_SPECULAR:
63                case VES_TEXTURE_COORDINATES:
64                        assert(false && "Shouldn't be calling this for standard attributes!");
65                        break;
66                case VES_BLEND_INDICES:
67                        return 7; // default binding
68                case VES_BLEND_WEIGHTS:
69                        return 1; // default binding
70                case VES_TANGENT:
71                        return 14; // default binding
72                case VES_BINORMAL:
73                        return 15; // default binding
74        }
75
76        return 0;
77}
78
79bool GLGpuProgram::isAttributeValid(VertexElementSemantic semantic)
80{
81        // default implementation
82        switch(semantic)
83        {
84                case VES_POSITION:
85                case VES_NORMAL:
86                case VES_DIFFUSE:
87                case VES_SPECULAR:
88                case VES_TEXTURE_COORDINATES:
89                        assert(false && "Shouldn't be calling this for standard attributes!");
90                        break;
91                case VES_BLEND_WEIGHTS:
92                case VES_BLEND_INDICES:
93                case VES_BINORMAL:
94                case VES_TANGENT:
95                        return true; // with default binding
96        }
97
98    return false;
99}
100
101GLArbGpuProgram::GLArbGpuProgram(ResourceManager* creator, const String& name, 
102    ResourceHandle handle, const String& group, bool isManual, 
103    ManualResourceLoader* loader) 
104    : GLGpuProgram(creator, name, handle, group, isManual, loader)
105{
106    glGenProgramsARB(1, &mProgramID);
107}
108
109GLArbGpuProgram::~GLArbGpuProgram()
110{
111    // have to call this here reather than in Resource destructor
112    // since calling virtual methods in base destructors causes crash
113    unload(); 
114}
115
116void GLArbGpuProgram::setType(GpuProgramType t)
117{
118    GLGpuProgram::setType(t);
119    mProgramType = (mType == GPT_VERTEX_PROGRAM) ? GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB;
120}
121
122void GLArbGpuProgram::bindProgram(void)
123{
124    glEnable(mProgramType);
125    glBindProgramARB(mProgramType, mProgramID);
126}
127
128void GLArbGpuProgram::unbindProgram(void)
129{
130    glBindProgramARB(mProgramType, 0);
131    glDisable(mProgramType);
132}
133
134void GLArbGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params)
135{
136    GLenum type = (mType == GPT_VERTEX_PROGRAM) ? 
137        GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB;
138   
139        // only supports float constants
140        const GpuLogicalBufferStruct* floatStruct = params->getFloatLogicalBufferStruct();
141
142        for (GpuLogicalIndexUseMap::const_iterator i = floatStruct->map.begin();
143                i != floatStruct->map.end(); ++i)
144        {
145                size_t logicalIndex = i->first;
146                const float* pFloat = params->getFloatPointer(i->second.physicalIndex);
147                // Iterate over the params, set in 4-float chunks (low-level)
148                for (size_t j = 0; j < i->second.currentSize; j+=4)
149                {
150                        glProgramLocalParameter4fvARB(type, logicalIndex, pFloat);
151                        pFloat += 4;
152                        ++logicalIndex;
153                }
154        }
155}
156
157void GLArbGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)
158{
159    if (params->hasPassIterationNumber())
160    {
161                GLenum type = (mType == GPT_VERTEX_PROGRAM) ? 
162                        GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB;
163
164                size_t physicalIndex = params->getPassIterationNumberIndex();
165                size_t logicalIndex = params->getFloatLogicalIndexForPhysicalIndex(physicalIndex);
166                const float* pFloat = params->getFloatPointer(physicalIndex);
167        glProgramLocalParameter4fvARB(type, (GLuint)logicalIndex, pFloat);
168    }
169
170}
171
172void GLArbGpuProgram::unloadImpl(void)
173{
174    glDeleteProgramsARB(1, &mProgramID);
175}
176
177void GLArbGpuProgram::loadFromSource(void)
178{
179    glBindProgramARB(mProgramType, mProgramID);
180    glProgramStringARB(mProgramType, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)mSource.length(), mSource.c_str());
181
182    if (GL_INVALID_OPERATION == glGetError())
183    {
184        GLint errPos;
185        glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
186                String errPosStr = StringConverter::toString(errPos);
187        char* errStr = (char*)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
188        // XXX New exception code?
189        OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, 
190            "Cannot load GL vertex program " + mName + 
191            ".  Line " + errPosStr + ":\n" + errStr, mName);
192    }
193    glBindProgramARB(mProgramType, 0);
194}
195
Note: See TracBrowser for help on using the repository browser.