~thumper/nux/next-changes

« back to all changes in this revision

Viewing changes to NuxGraphics/GLSh_DrawFunction.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 21:15:42 UTC
  • Revision ID: neil.patel@canonical.com-20100901211542-cw2ce3ak28unouwb
Add NuxGraphics with licensing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it 
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but 
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of 
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public 
 
12
 * License for more details.
 
13
 * 
 
14
 * You should have received a copy of both the GNU Lesser General Public 
 
15
 * License version 3 along with this program.  If not, see 
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "GLResource.h"
 
24
#include "GLResourceManager.h"
 
25
#include "GLDeviceFactory.h"
 
26
 
 
27
#include "GLTemplatePrimitiveBuffer.h"
 
28
#include "OpenGLEngine.h"
 
29
 
 
30
#include "GLSh_DrawFunction.h"
 
31
 
 
32
NAMESPACE_BEGIN_OGL
 
33
 
 
34
extern bool USE_ARB_SHADERS;
 
35
 
 
36
static NString VtxShader = TEXT("#version 110   \n\
 
37
        uniform mat4 ViewProjectionMatrix;      \n\
 
38
        attribute vec4 AVertex;                 \n\
 
39
        attribute vec4 VertexColor;             \n\
 
40
        void main()                             \n\
 
41
        {                                       \n\
 
42
            gl_Position = ViewProjectionMatrix * AVertex;   \n\
 
43
        }");
 
44
 
 
45
static NString FrgShader = TEXT("#version 110           \n\
 
46
        uniform sampler2D TextureFunction;              \n\
 
47
        uniform vec4 RectPosition;                      \n\
 
48
        uniform vec4 RectDimension;                     \n\
 
49
        uniform vec4 Color;                             \n\
 
50
        void main()                                     \n\
 
51
        {                                               \n\
 
52
            float x = (gl_FragCoord.x - RectPosition.x) / RectDimension.x;  \n\
 
53
            float y = (gl_FragCoord.y - RectPosition.y) / RectDimension.y;  \n\
 
54
            float s = texture2D(TextureFunction, vec2(x, 0.0)).r;           \n\
 
55
            if(y > s)                                                       \n\
 
56
            {                                                               \n\
 
57
                s = 0.0;                                                    \n\
 
58
                gl_FragColor = Color;                                       \n\
 
59
                discard;                                                    \n\
 
60
            }                                                               \n\
 
61
            else                                                            \n\
 
62
            {                                                               \n\
 
63
                s = 1.0 - (s-y) / s;                                        \n\
 
64
                gl_FragColor = Color;                                       \n\
 
65
            }                                                               \n\
 
66
        }");
 
67
 
 
68
 
 
69
static NString AsmVtxShader = TEXT("!!ARBvp1.0                                 \n\
 
70
        ATTRIB iPos         = vertex.position;      \n\
 
71
        PARAM  mvp[4]       = {state.matrix.mvp};   \n\
 
72
        OUTPUT oPos         = result.position;      \n\
 
73
        # Transform the vertex to clip coordinates. \n\
 
74
        DP4   oPos.x, mvp[0], iPos;      \n\
 
75
        DP4   oPos.y, mvp[1], iPos;      \n\
 
76
        DP4   oPos.z, mvp[2], iPos;      \n\
 
77
        DP4   oPos.w, mvp[3], iPos;      \n\
 
78
        END");
 
79
 
 
80
NString AsmFrgShader = TEXT("!!ARBfp1.0                  \n\
 
81
        PARAM RectPosition = program.local[0];              \n\
 
82
        PARAM RectDimension = program.local[1];             \n\
 
83
        PARAM Color = program.local[2];                     \n\
 
84
        TEMP temp0;                                         \n\
 
85
        TEMP temp1;                                         \n\
 
86
        TEMP tex0;                                          \n\
 
87
        SUB temp0.x, fragment.position.x, RectPosition.x;   \n\
 
88
        SUB temp0.y, fragment.position.y, RectPosition.y;   \n\
 
89
        RCP temp1.x, RectDimension.x;                       \n\
 
90
        RCP temp1.y, RectDimension.y;                       \n\
 
91
        MUL temp1.xy, temp0, temp1;                         \n\
 
92
        TEX tex0, temp1, texture[0], 2D;       \n\
 
93
        SUB temp0, tex0.xxxx, temp1.yyyy;                   \n\
 
94
        KIL temp0;              \n\
 
95
        MOV result.color, Color;                            \n\
 
96
        END");
 
97
 
 
98
 
 
99
GLSh_DrawFunction::GLSh_DrawFunction()
 
100
:   _ScreenOffsetX(0)
 
101
,   _ScreenOffsetY(0)
 
102
{
 
103
    if(!USE_ARB_SHADERS && (GetThreadGLDeviceFactory()->GetGraphicsBoardVendor() != BOARD_INTEL))
 
104
    {
 
105
        sprog = GetThreadGLDeviceFactory()->CreateShaderProgram();
 
106
        sprog->LoadVertexShader(VtxShader.GetTCharPtr(), NULL);
 
107
        sprog->LoadPixelShader(FrgShader.GetTCharPtr(), NULL);
 
108
        sprog->Link();
 
109
    }
 
110
    else
 
111
    {
 
112
        m_AsmProg = GetThreadGLDeviceFactory()->CreateAsmShaderProgram();
 
113
        m_AsmProg->LoadVertexShader(AsmVtxShader.GetTCharPtr());
 
114
        m_AsmProg->LoadPixelShader(AsmFrgShader.GetTCharPtr());
 
115
        m_AsmProg->Link();
 
116
    }
 
117
}
 
118
 
 
119
GLSh_DrawFunction::~GLSh_DrawFunction()
 
120
{
 
121
    sprog = 0;
 
122
}
 
123
 
 
124
void GLSh_DrawFunction::SetBackgroundColor(float R, float G, float B, float A)
 
125
{
 
126
    _R = R;
 
127
    _G = G;
 
128
    _B = B;
 
129
    _A = A;
 
130
}
 
131
 
 
132
void GLSh_DrawFunction::SetBackgroundColor(Color color)
 
133
{
 
134
    _R = color.R();
 
135
    _G = color.G();
 
136
    _B = color.B();
 
137
    _A = color.A();
 
138
}
 
139
 
 
140
void GLSh_DrawFunction::Render(int x, int y, int z, int width, int height, int WindowWidth, int WindowHeight)
 
141
{
 
142
    float VtxBuffer[] =
 
143
    {
 
144
        x,          y,          0.0f, 1.0f,
 
145
        x,          y + height, 0.0f, 1.0f,
 
146
        x + width,  y + height, 0.0f, 1.0f,
 
147
        x + width,  y,          0.0f, 1.0f,
 
148
    };
 
149
 
 
150
    if(!USE_ARB_SHADERS && (GetThreadGLDeviceFactory()->GetGraphicsBoardVendor() != BOARD_INTEL))
 
151
    {
 
152
        CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
 
153
        CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
 
154
        sprog->Begin();
 
155
 
 
156
        int VertexLocation = sprog->GetAttributeLocation("AVertex");
 
157
 
 
158
        int VPMatrixLocation = sprog->GetUniformLocationARB("ViewProjectionMatrix");
 
159
        sprog->SetUniformLocMatrix4fv((GLint)VPMatrixLocation, 1, false, (GLfloat*)&(GetThreadGraphicsContext()->GetModelViewProjectionMatrix().m));
 
160
 
 
161
        GetThreadGraphicsContext()->SetTexture(GL_TEXTURE0, m_device_texture);
 
162
 
 
163
        int ColorBase       = sprog->GetUniformLocationARB("Color");
 
164
        int RectPosition    = sprog->GetUniformLocationARB("RectPosition");
 
165
        int RectDimension   = sprog->GetUniformLocationARB("RectDimension");
 
166
        int TextureFunction = sprog->GetUniformLocationARB("TextureFunction");
 
167
 
 
168
        if(ColorBase != -1)
 
169
            CHECKGL( glUniform4fARB(ColorBase, _R, _G, _B, _A) );
 
170
        if(RectPosition != -1)
 
171
            CHECKGL( glUniform4fARB(RectPosition, x + _ScreenOffsetX, WindowHeight - y - height - _ScreenOffsetY, z, 0.0f) );
 
172
        if(RectDimension != -1)
 
173
            CHECKGL( glUniform4fARB(RectDimension, width, height, 0.0f, 0.0f) );
 
174
        if(TextureFunction != -1)
 
175
            CHECKGL( glUniform1iARB(TextureFunction, 0) );
 
176
 
 
177
        CHECKGL( glEnableVertexAttribArrayARB(VertexLocation) );
 
178
        CHECKGL( glVertexAttribPointerARB((GLuint)VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer) );
 
179
 
 
180
        CHECKGL( glDrawArrays(GL_QUADS, 0, 4) );
 
181
 
 
182
        CHECKGL( glDisableVertexAttribArrayARB(VertexLocation) );
 
183
 
 
184
        sprog->End();
 
185
    }
 
186
    else
 
187
    {
 
188
        CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
 
189
        CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
 
190
        m_AsmProg->Begin();
 
191
 
 
192
        CHECKGL( glMatrixMode(GL_MODELVIEW) );
 
193
        CHECKGL( glLoadIdentity() );
 
194
        CHECKGL( glLoadMatrixf((FLOAT*) GetThreadGraphicsContext()->GetModelViewMatrix().m) );
 
195
        CHECKGL( glMatrixMode(GL_PROJECTION) );
 
196
        CHECKGL( glLoadIdentity() );
 
197
        CHECKGL( glLoadMatrixf((FLOAT*) GetThreadGraphicsContext()->GetProjectionMatrix().m) );
 
198
 
 
199
        int VertexLocation          = VTXATTRIB_POSITION;
 
200
 
 
201
        GetThreadGraphicsContext()->SetTexture(GL_TEXTURE0, m_device_texture);
 
202
 
 
203
        CHECKGL( glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, x + _ScreenOffsetX, WindowHeight - y - height - _ScreenOffsetY, z, 0.0f) );
 
204
        CHECKGL( glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, width, height, 0.0f, 0.0f) );
 
205
        CHECKGL( glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 2, _R, _G, _B, _A) );
 
206
 
 
207
        CHECKGL( glEnableVertexAttribArrayARB(VertexLocation) );
 
208
        CHECKGL( glVertexAttribPointerARB((GLuint)VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer) );
 
209
 
 
210
        CHECKGL( glDrawArrays(GL_QUADS, 0, 4) );
 
211
 
 
212
        CHECKGL( glDisableVertexAttribArrayARB(VertexLocation) );
 
213
 
 
214
        m_AsmProg->End();
 
215
    }
 
216
}
 
217
 
 
218
void GLSh_DrawFunction::CacheShader()
 
219
{
 
220
//    std::vector<ShaderDefinition> Definitions;
 
221
//    GLProgramObject::LoadCombinedShaderFile(TEXT("..//Shaders//DrawFunction.glsl"), TEXT("main"), TEXT("main"), Definitions);
 
222
}
 
223
 
 
224
void GLSh_DrawFunction::SetTextureFunction(TRefGL<IOpenGLTexture2D> device_texture)
 
225
{
 
226
    m_device_texture = device_texture;
 
227
}
 
228
 
 
229
void GLSh_DrawFunction::SetScreenPositionOffset(float x, float y)
 
230
{
 
231
    _ScreenOffsetX = x;
 
232
    _ScreenOffsetY = y;
 
233
}
 
234
 
 
235
NAMESPACE_END_OGL