Back to index

nux  3.0.0
GLSh_DrawFunction.cpp
Go to the documentation of this file.
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 = "            \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 = "                    \n\
00045         #ifdef GL_ES                                    \n\
00046         precision mediump float;                        \n\
00047         #endif                                          \n\
00048         uniform sampler2D TextureFunction;              \n\
00049         uniform vec4 RectPosition;                      \n\
00050         uniform vec4 RectDimension;                     \n\
00051         uniform vec4 Color;                             \n\
00052         void main()                                     \n\
00053         {                                               \n\
00054             float x = (gl_FragCoord.x - RectPosition.x) / RectDimension.x;  \n\
00055             float y = (gl_FragCoord.y - RectPosition.y) / RectDimension.y;  \n\
00056             float s = texture2D(TextureFunction, vec2(x, 0.0)).r;           \n\
00057             if (y > s)                                                       \n\
00058             {                                                               \n\
00059                 s = 0.0;                                                    \n\
00060                 gl_FragColor = Color;                                       \n\
00061                 discard;                                                    \n\
00062             }                                                               \n\
00063             else                                                            \n\
00064             {                                                               \n\
00065                 s = 1.0 - (s-y) / s;                                        \n\
00066                 gl_FragColor = Color;                                       \n\
00067             }                                                               \n\
00068         }";
00069 
00070 
00071   static NString AsmVtxShader = "!!ARBvp1.0                                 \n\
00072         ATTRIB iPos         = vertex.position;      \n\
00073         PARAM  mvp[4]       = {state.matrix.mvp};   \n\
00074         OUTPUT oPos         = result.position;      \n\
00075         # Transform the vertex to clip coordinates. \n\
00076         DP4   oPos.x, mvp[0], iPos;      \n\
00077         DP4   oPos.y, mvp[1], iPos;      \n\
00078         DP4   oPos.z, mvp[2], iPos;      \n\
00079         DP4   oPos.w, mvp[3], iPos;      \n\
00080         END";
00081 
00082   NString AsmFrgShader = "!!ARBfp1.0                  \n\
00083         PARAM RectPosition = program.local[0];              \n\
00084         PARAM RectDimension = program.local[1];             \n\
00085         PARAM Color = program.local[2];                     \n\
00086         TEMP temp0;                                         \n\
00087         TEMP temp1;                                         \n\
00088         TEMP tex0;                                          \n\
00089         SUB temp0.x, fragment.position.x, RectPosition.x;   \n\
00090         SUB temp0.y, fragment.position.y, RectPosition.y;   \n\
00091         RCP temp1.x, RectDimension.x;                       \n\
00092         RCP temp1.y, RectDimension.y;                       \n\
00093         MUL temp1.xy, temp0, temp1;                         \n\
00094         TEX tex0, temp1, texture[0], 2D;       \n\
00095         SUB temp0, tex0.xxxx, temp1.yyyy;                   \n\
00096         KIL temp0;              \n\
00097         MOV result.color, Color;                            \n\
00098         END";
00099 
00100 
00101   GLSh_DrawFunction::GLSh_DrawFunction()
00102     :   _ScreenOffsetX(0)
00103     ,   _ScreenOffsetY(0)
00104   {
00105     if (GetGraphicsDisplay()->GetGraphicsEngine()->UsingGLSLCodePath() && (GetGraphicsDisplay()->GetGpuDevice()->GetGPUBrand() != GPU_BRAND_INTEL))
00106     {
00107       sprog = GetGraphicsDisplay()->GetGpuDevice()->CreateShaderProgram();
00108       sprog->LoadVertexShader(VtxShader.GetTCharPtr(), NULL);
00109       sprog->LoadPixelShader(FrgShader.GetTCharPtr(), NULL);
00110       sprog->Link();
00111     }
00112     else
00113     {
00114       m_AsmProg = GetGraphicsDisplay()->GetGpuDevice()->CreateAsmShaderProgram();
00115       m_AsmProg->LoadVertexShader(AsmVtxShader.GetTCharPtr());
00116       m_AsmProg->LoadPixelShader(AsmFrgShader.GetTCharPtr());
00117       m_AsmProg->Link();
00118     }
00119   }
00120 
00121   GLSh_DrawFunction::~GLSh_DrawFunction()
00122   {
00123     sprog = ObjectPtr<IOpenGLShaderProgram> (0);
00124   }
00125 
00126   void GLSh_DrawFunction::SetBackgroundColor(Color const& color)
00127   {
00128     background_color_ = color;
00129   }
00130 
00131   void GLSh_DrawFunction::Render(int x, int y, int z, int width, int height, int WindowWidth, int WindowHeight)
00132   {
00133     float fx = x, fy = y;
00134     float VtxBuffer[] =
00135     {
00136       fx,          fy,          0.0f, 1.0f,
00137       fx,          fy + height, 0.0f, 1.0f,
00138       fx + width,  fy + height, 0.0f, 1.0f,
00139       fx + width,  fy,          0.0f, 1.0f,
00140     };
00141 
00142     if (GetGraphicsDisplay()->GetGraphicsEngine()->UsingGLSLCodePath() && (GetGraphicsDisplay()->GetGpuDevice()->GetGPUBrand() != GPU_BRAND_INTEL))
00143     {
00144       CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
00145       CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
00146       sprog->Begin();
00147 
00148       int VertexLocation = sprog->GetAttributeLocation("AVertex");
00149 
00150       int     VPMatrixLocation = sprog->GetUniformLocationARB("ViewProjectionMatrix");
00151       Matrix4 MVPMatrix = GetGraphicsDisplay()->GetGraphicsEngine()->GetOpenGLModelViewProjectionMatrix();
00152       sprog->SetUniformLocMatrix4fv((GLint) VPMatrixLocation, 1, false, (GLfloat *) & (MVPMatrix.m));
00153 
00154       GetGraphicsDisplay()->GetGraphicsEngine()->SetTexture(GL_TEXTURE0, m_device_texture);
00155 
00156       int ColorBase       = sprog->GetUniformLocationARB("Color");
00157       int RectPosition    = sprog->GetUniformLocationARB("RectPosition");
00158       int RectDimension   = sprog->GetUniformLocationARB("RectDimension");
00159       int TextureFunction = sprog->GetUniformLocationARB("TextureFunction");
00160 
00161       if (ColorBase != -1)
00162         CHECKGL(glUniform4fARB(ColorBase, background_color_.red, background_color_.green, background_color_.blue, background_color_.alpha));
00163 
00164       if (RectPosition != -1)
00165         CHECKGL(glUniform4fARB(RectPosition, x + _ScreenOffsetX, WindowHeight - y - height - _ScreenOffsetY, z, 0.0f));
00166 
00167       if (RectDimension != -1)
00168         CHECKGL(glUniform4fARB(RectDimension, width, height, 0.0f, 0.0f));
00169 
00170       if (TextureFunction != -1)
00171         CHECKGL(glUniform1iARB(TextureFunction, 0));
00172 
00173       CHECKGL(glEnableVertexAttribArrayARB(VertexLocation));
00174       CHECKGL(glVertexAttribPointerARB((GLuint) VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer));
00175 
00176       CHECKGL(glDrawArrays(GL_TRIANGLE_FAN, 0, 4));
00177 
00178       CHECKGL(glDisableVertexAttribArrayARB(VertexLocation));
00179 
00180       sprog->End();
00181     }
00182 #ifndef NUX_OPENGLES_20
00183     else
00184     {
00185       CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
00186       CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
00187       m_AsmProg->Begin();
00188 
00189       CHECKGL(glMatrixMode(GL_MODELVIEW));
00190       CHECKGL(glLoadIdentity());
00191       CHECKGL(glLoadMatrixf((FLOAT *) GetGraphicsDisplay()->GetGraphicsEngine()->GetOpenGLModelViewMatrix().m));
00192       CHECKGL(glMatrixMode(GL_PROJECTION));
00193       CHECKGL(glLoadIdentity());
00194       CHECKGL(glLoadMatrixf((FLOAT *) GetGraphicsDisplay()->GetGraphicsEngine()->GetOpenGLProjectionMatrix().m));
00195 
00196       int VertexLocation          = VTXATTRIB_POSITION;
00197 
00198       GetGraphicsDisplay()->GetGraphicsEngine()->SetTexture(GL_TEXTURE0, m_device_texture);
00199 
00200       CHECKGL(glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, x + _ScreenOffsetX, WindowHeight - y - height - _ScreenOffsetY, z, 0.0f));
00201       CHECKGL(glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, width, height, 0.0f, 0.0f));
00202       CHECKGL(glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 2, background_color_.red, background_color_.green, background_color_.blue, background_color_.alpha));
00203 
00204       CHECKGL(glEnableVertexAttribArrayARB(VertexLocation));
00205       CHECKGL(glVertexAttribPointerARB((GLuint) VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer));
00206 
00207       CHECKGL(glDrawArrays(GL_TRIANGLE_FAN, 0, 4));
00208 
00209       CHECKGL(glDisableVertexAttribArrayARB(VertexLocation));
00210 
00211       m_AsmProg->End();
00212     }
00213 #endif
00214   }
00215 
00216   void GLSh_DrawFunction::SetTextureFunction(ObjectPtr<IOpenGLBaseTexture> device_texture)
00217   {
00218     m_device_texture = device_texture;
00219   }
00220 
00221   void GLSh_DrawFunction::SetScreenPositionOffset(float x, float y)
00222   {
00223     _ScreenOffsetX = x;
00224     _ScreenOffsetY = y;
00225   }
00226 
00227 }