2
* Copyright 2010 Inalogic Inc.
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.
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.
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/>
18
* Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
23
#include "GLResource.h"
24
#include "GLResourceManager.h"
25
#include "GLDeviceFactory.h"
27
#include "GLTemplatePrimitiveBuffer.h"
28
#include "OpenGLEngine.h"
30
#include "GLSh_DrawFunction.h"
34
extern bool USE_ARB_SHADERS;
36
static NString VtxShader = TEXT("#version 110 \n\
37
uniform mat4 ViewProjectionMatrix; \n\
38
attribute vec4 AVertex; \n\
39
attribute vec4 VertexColor; \n\
42
gl_Position = ViewProjectionMatrix * AVertex; \n\
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\
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\
58
gl_FragColor = Color; \n\
63
s = 1.0 - (s-y) / s; \n\
64
gl_FragColor = Color; \n\
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\
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\
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\
95
MOV result.color, Color; \n\
99
GLSh_DrawFunction::GLSh_DrawFunction()
103
if(!USE_ARB_SHADERS && (GetThreadGLDeviceFactory()->GetGraphicsBoardVendor() != BOARD_INTEL))
105
sprog = GetThreadGLDeviceFactory()->CreateShaderProgram();
106
sprog->LoadVertexShader(VtxShader.GetTCharPtr(), NULL);
107
sprog->LoadPixelShader(FrgShader.GetTCharPtr(), NULL);
112
m_AsmProg = GetThreadGLDeviceFactory()->CreateAsmShaderProgram();
113
m_AsmProg->LoadVertexShader(AsmVtxShader.GetTCharPtr());
114
m_AsmProg->LoadPixelShader(AsmFrgShader.GetTCharPtr());
119
GLSh_DrawFunction::~GLSh_DrawFunction()
124
void GLSh_DrawFunction::SetBackgroundColor(float R, float G, float B, float A)
132
void GLSh_DrawFunction::SetBackgroundColor(Color color)
140
void GLSh_DrawFunction::Render(int x, int y, int z, int width, int height, int WindowWidth, int WindowHeight)
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,
150
if(!USE_ARB_SHADERS && (GetThreadGLDeviceFactory()->GetGraphicsBoardVendor() != BOARD_INTEL))
152
CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
153
CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
156
int VertexLocation = sprog->GetAttributeLocation("AVertex");
158
int VPMatrixLocation = sprog->GetUniformLocationARB("ViewProjectionMatrix");
159
sprog->SetUniformLocMatrix4fv((GLint)VPMatrixLocation, 1, false, (GLfloat*)&(GetThreadGraphicsContext()->GetModelViewProjectionMatrix().m));
161
GetThreadGraphicsContext()->SetTexture(GL_TEXTURE0, m_device_texture);
163
int ColorBase = sprog->GetUniformLocationARB("Color");
164
int RectPosition = sprog->GetUniformLocationARB("RectPosition");
165
int RectDimension = sprog->GetUniformLocationARB("RectDimension");
166
int TextureFunction = sprog->GetUniformLocationARB("TextureFunction");
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) );
177
CHECKGL( glEnableVertexAttribArrayARB(VertexLocation) );
178
CHECKGL( glVertexAttribPointerARB((GLuint)VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer) );
180
CHECKGL( glDrawArrays(GL_QUADS, 0, 4) );
182
CHECKGL( glDisableVertexAttribArrayARB(VertexLocation) );
188
CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
189
CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
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) );
199
int VertexLocation = VTXATTRIB_POSITION;
201
GetThreadGraphicsContext()->SetTexture(GL_TEXTURE0, m_device_texture);
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) );
207
CHECKGL( glEnableVertexAttribArrayARB(VertexLocation) );
208
CHECKGL( glVertexAttribPointerARB((GLuint)VertexLocation, 4, GL_FLOAT, GL_FALSE, 16, VtxBuffer) );
210
CHECKGL( glDrawArrays(GL_QUADS, 0, 4) );
212
CHECKGL( glDisableVertexAttribArrayARB(VertexLocation) );
218
void GLSh_DrawFunction::CacheShader()
220
// std::vector<ShaderDefinition> Definitions;
221
// GLProgramObject::LoadCombinedShaderFile(TEXT("..//Shaders//DrawFunction.glsl"), TEXT("main"), TEXT("main"), Definitions);
224
void GLSh_DrawFunction::SetTextureFunction(TRefGL<IOpenGLTexture2D> device_texture)
226
m_device_texture = device_texture;
229
void GLSh_DrawFunction::SetScreenPositionOffset(float x, float y)