nux-1.16.0
GLSh_DrawFunction.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GLResource.h"
00024 #include "GLResourceManager.h"
00025 #include "GpuDevice.h"
00026 
00027 #include "GLTemplatePrimitiveBuffer.h"
00028 #include "GraphicsEngine.h"
00029 
00030 #include "GLSh_DrawFunction.h"
00031 
00032 namespace nux
00033 {
00034 
00035   static NString VtxShader = TEXT ("#version 110   \n\
00036         uniform mat4 ViewProjectionMatrix;      \n\
00037         attribute vec4 AVertex;                 \n\
00038         attribute vec4 VertexColor;             \n\
00039         void main()                             \n\
00040         {                                       \n\
00041             gl_Position = ViewProjectionMatrix * AVertex;   \n\
00042         }");
00043 
00044   static NString FrgShader = TEXT ("#version 110           \n\
00045         uniform sampler2D TextureFunction;              \n\
00046         uniform vec4 RectPosition;                      \n\
00047         uniform vec4 RectDimension;                     \n\
00048         uniform vec4 Color;                             \n\
00049         void main()                                     \n\
00050         {                                               \n\
00051             float x = (gl_FragCoord.x - RectPosition.x) / RectDimension.x;  \n\
00052             float y = (gl_FragCoord.y - RectPosition.y) / RectDimension.y;  \n\
00053             float s = texture2D(TextureFunction, vec2(x, 0.0)).r;           \n\
00054             if(y > s)                                                       \n\
00055             {                                                               \n\
00056                 s = 0.0;                                                    \n\
00057                 gl_FragColor = Color;                                       \n\
00058                 discard;                                                    \n\
00059             }                                                               \n\
00060             else                                                            \n\
00061             {                                                               \n\
00062                 s = 1.0 - (s-y) / s;                                        \n\
00063                 gl_FragColor = Color;                                       \n\
00064             }                                                               \n\
00065         }");
00066 
00067 
00068   static NString AsmVtxShader = TEXT ("!!ARBvp1.0                                 \n\
00069         ATTRIB iPos         = vertex.position;      \n\
00070         PARAM  mvp[4]       = {state.matrix.mvp};   \n\
00071         OUTPUT oPos         = result.position;      \n\
00072         # Transform the vertex to clip coordinates. \n\
00073         DP4   oPos.x, mvp[0], iPos;      \n\
00074         DP4   oPos.y, mvp[1], iPos;      \n\
00075         DP4   oPos.z, mvp[2], iPos;      \n\
00076         DP4   oPos.w, mvp[3], iPos;      \n\
00077         END");
00078 
00079   NString AsmFrgShader = TEXT ("!!ARBfp1.0                  \n\
00080         PARAM RectPosition = program.local[0];              \n\
00081         PARAM RectDimension = program.local[1];             \n\
00082         PARAM Color = program.local[2];                     \n\
00083         TEMP temp0;                                         \n\
00084         TEMP temp1;                                         \n\
00085         TEMP tex0;                                          \n\
00086         SUB temp0.x, fragment.position.x, RectPosition.x;   \n\
00087         SUB temp0.y, fragment.position.y, RectPosition.y;   \n\
00088         RCP temp1.x, RectDimension.x;                       \n\
00089         RCP temp1.y, RectDimension.y;                       \n\
00090         MUL temp1.xy, temp0, temp1;                         \n\
00091         TEX tex0, temp1, texture[0], 2D;       \n\
00092         SUB temp0, tex0.xxxx, temp1.yyyy;                   \n\
00093         KIL temp0;              \n\
00094         MOV result.color, Color;                            \n\
00095         END");
00096 
00097 
00098   GLSh_DrawFunction::GLSh_DrawFunction()
00099     :   _ScreenOffsetX (0)
00100     ,   _ScreenOffsetY (0)
00101   {
00102     if (GetGraphicsDisplay()->GetGraphicsEngine()->UsingGLSLCodePath() && (GetGraphicsDisplay()->GetGpuDevice()->GetGPUBrand() != GPU_BRAND_INTEL) )
00103     {
00104       sprog = GetGraphicsDisplay()->GetGpuDevice()->CreateShaderProgram();
00105       sprog->LoadVertexShader (VtxShader.GetTCharPtr(), NULL);
00106       sprog->LoadPixelShader (FrgShader.GetTCharPtr(), NULL);
00107       sprog->Link();
00108     }
00109     else
00110     {
00111       m_AsmProg = GetGraphicsDisplay()->GetGpuDevice()->CreateAsmShaderProgram();
00112       m_AsmProg->LoadVertexShader (AsmVtxShader.GetTCharPtr() );
00113       m_AsmProg->LoadPixelShader (AsmFrgShader.GetTCharPtr() );
00114       m_AsmProg->Link();
00115     }
00116   }
00117 
00118   GLSh_DrawFunction::~GLSh_DrawFunction()
00119   {
00120     sprog = ObjectPtr<IOpenGLShaderProgram> (0);
00121   }
00122 
00123   void GLSh_DrawFunction::SetBackgroundColor(Color const& color)
00124   {
00125     background_color_ = color;
00126   }
00127 
00128   void GLSh_DrawFunction::Render (int x, int y, int z, int width, int height, int WindowWidth, int WindowHeight)
00129   {
00130     float fx = x, fy = y;
00131     float VtxBuffer[] =
00132     {
00133       fx,          fy,          0.0f, 1.0f,
00134       fx,          fy + height, 0.0f, 1.0f,
00135       fx + width,  fy + height, 0.0f, 1.0f,
00136       fx + width,  fy,          0.0f, 1.0f,
00137     };
00138 
00139     if (GetGraphicsDisplay()->GetGraphicsEngine()->UsingGLSLCodePath() && (GetGraphicsDisplay()->GetGpuDevice()->GetGPUBrand() != GPU_BRAND_INTEL) )
00140     {
00141       CHECKGL (glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );
00142       CHECKGL (glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, 0) );
00143       sprog->Begin();
00144 
00145       int VertexLocation = sprog->GetAttributeLocation ("AVertex");
00146 
00147       int     VPMatrixLocation = sprog->GetUniformLocationARB ("ViewProjectionMatrix");
00148       Matrix4 MVPMatrix = GetGraphicsDisplay()->GetGraphicsEngine()->GetOpenGLModelViewProjectionMatrix();
00149       sprog->SetUniformLocMatrix4fv ( (GLint) VPMatrixLocation, 1, false, (GLfloat *) & (MVPMatrix.m) );
00150 
00151       GetGraphicsDisplay()->GetGraphicsEngine()->SetTexture (GL_TEXTURE0, m_device_texture);
00152 
00153       int ColorBase       = sprog->GetUniformLocationARB ("Color");
00154       int RectPosition    = sprog->GetUniformLocationARB ("RectPosition");
00155       int RectDimension   = sprog->GetUniformLocationARB ("RectDimension");
00156       int TextureFunction = sprog->GetUniformLocationARB ("TextureFunction");
00157 
00158       if (ColorBase != -1)
00159         CHECKGL ( glUniform4fARB (ColorBase, background_color_.red, background_color_.green, background_color_.blue, background_color_.alpha) );
00160 
00161       if (RectPosition != -1)
00162         CHECKGL ( glUniform4fARB (RectPosition, x + _ScreenOffsetX, WindowHeight - y - height - _ScreenOffsetY, z, 0.0f) );
00163 
00164       if (RectDimension != -1)
00165         CHECKGL ( glUniform4fARB (RectDimension, width, height, 0.0f, 0.0f) );
00166 
00167       if (TextureFunction != -1)
00168         CHECKGL ( glUniform1iARB (TextureFunction, 0) );
00169 
00170       CHECKGL ( glEnableVertexAttribArrayARB (VertexLocation) );
00171       CHECKGL ( glVertexAttribPointerARB ( (GLuint) VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer) );
00172 
00173       CHECKGL ( glDrawArrays (GL_TRIANGLE_FAN, 0, 4) );
00174 
00175       CHECKGL ( glDisableVertexAttribArrayARB (VertexLocation) );
00176 
00177       sprog->End();
00178     }
00179 #ifndef NUX_OPENGLES_20
00180     else
00181     {
00182       CHECKGL (glBindBufferARB (GL_ARRAY_BUFFER_ARB, 0) );
00183       CHECKGL (glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, 0) );
00184       m_AsmProg->Begin();
00185 
00186       CHECKGL ( glMatrixMode (GL_MODELVIEW) );
00187       CHECKGL ( glLoadIdentity() );
00188       CHECKGL ( glLoadMatrixf ( (FLOAT *) GetGraphicsDisplay()->GetGraphicsEngine()->GetOpenGLModelViewMatrix().m) );
00189       CHECKGL ( glMatrixMode (GL_PROJECTION) );
00190       CHECKGL ( glLoadIdentity() );
00191       CHECKGL ( glLoadMatrixf ( (FLOAT *) GetGraphicsDisplay()->GetGraphicsEngine()->GetOpenGLProjectionMatrix().m) );
00192 
00193       int VertexLocation          = VTXATTRIB_POSITION;
00194 
00195       GetGraphicsDisplay()->GetGraphicsEngine()->SetTexture (GL_TEXTURE0, m_device_texture);
00196 
00197       CHECKGL ( glProgramLocalParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 0, x + _ScreenOffsetX, WindowHeight - y - height - _ScreenOffsetY, z, 0.0f) );
00198       CHECKGL ( glProgramLocalParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 1, width, height, 0.0f, 0.0f) );
00199       CHECKGL ( glProgramLocalParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 2, background_color_.red, background_color_.green, background_color_.blue, background_color_.alpha) );
00200 
00201       CHECKGL ( glEnableVertexAttribArrayARB (VertexLocation) );
00202       CHECKGL ( glVertexAttribPointerARB ( (GLuint) VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer) );
00203 
00204       CHECKGL ( glDrawArrays (GL_TRIANGLE_FAN, 0, 4) );
00205 
00206       CHECKGL ( glDisableVertexAttribArrayARB (VertexLocation) );
00207 
00208       m_AsmProg->End();
00209     }
00210 #endif
00211   }
00212 
00213   void GLSh_DrawFunction::SetTextureFunction (ObjectPtr<IOpenGLBaseTexture> device_texture)
00214   {
00215     m_device_texture = device_texture;
00216   }
00217 
00218   void GLSh_DrawFunction::SetScreenPositionOffset (float x, float y)
00219   {
00220     _ScreenOffsetX = x;
00221     _ScreenOffsetY = y;
00222   }
00223 
00224 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends