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 "NuxCore/NKernel.h"
25
#include "NuxImage/Tga.h"
26
#include "NuxImage/ImageSurface.h"
27
#include "NuxMesh/NTextureArchiveManager.h"
29
#include "GLDeviceFactory.h"
30
#include "GLDeviceObjects.h"
31
#include "GLResourceManager.h"
33
#include "GLTextureResourceManager.h"
34
#include "GLVertexResourceManager.h"
36
#include "FontTexture.h"
37
#include "UIColorTheme.h"
39
#include "OpenGLEngine.h"
43
ROPConfig ROPConfig::Default;
44
ROPConfig::ROPConfig()
47
SrcBlend = GL_SRC_ALPHA;
48
DstBlend = GL_ONE_MINUS_SRC_ALPHA;
50
ROPConfig::~ROPConfig()
58
Color GTextColor = Color(0xFFE9E9E9);
60
GraphicsContext::GraphicsContext(GLWindowImpl& GlWindow)
62
, m_GLWindow(GlWindow)
68
GlWindow.m_GraphicsContext = this;
71
//Initialize the matrices
72
m_ProjectionMatrix.Identity();
73
m_ModelViewMatrix.Identity();
75
ResourceCache.InitializeResourceFactories();
77
// m_FilePath.AddSearchPath("./Data");
78
// m_FilePath.AddSearchPath("../Data");
79
// m_FilePath.AddSearchPath("../../Data");
80
// m_FilePath.AddSearchPath("../../../Data");
81
m_FilePath.AddSearchPath("./Data/UITextures");
83
NString font_file = INL_FIND_RESOURCE_LOCATION_NOFAIL(TEXT("Tahoma_size_8.txt"));
87
//GFont.reset(new FontTexture(TEXT("Courier New_size_10.txt")));
88
GFont.reset(new FontTexture(INL_FIND_RESOURCE_LOCATION_NOFAIL(TEXT("Tahoma_size_8.txt"))));
93
GFontBold.reset(new FontTexture(INL_FIND_RESOURCE_LOCATION_NOFAIL(TEXT("Tahoma_size_8_bold.txt"))));
96
m_CurrrentContext.x = 0;
97
m_CurrrentContext.y = 0;
98
m_CurrrentContext.width = m_GLWindow.GetWindowWidth();
99
m_CurrrentContext.height = m_GLWindow.GetWindowHeight();
101
SetViewport(0, 0, m_GLWindow.GetWindowWidth(), m_GLWindow.GetWindowHeight());
102
SetScissor(0, 0, m_GLWindow.GetWindowWidth(), m_GLWindow.GetWindowHeight());
103
SetScissorOffset(0, 0);
104
EnableScissoring(true);
106
//gUIColorTheme.Initialize();
109
//InitTextureBlendModeShader();
112
InitAsmColorShader();
113
InitAsmTextureShader();
114
InitAsmColorModTexMaskAlpha();
115
InitAsm4TextureAdd();
118
// InitSlColorShader();
119
// InitSlTextureShader();
120
// InitSl2TextureAdd();
121
// InitSl4TextureAdd();
122
// InitSlColorModTexMaskAlpha();
127
GraphicsContext::~GraphicsContext()
129
ResourceCache.Flush();
130
INL_SAFE_DELETE(m_FontRenderer);
133
void GraphicsContext::InitOpenGLEngine()
135
//LoadPainterImages();
139
void GraphicsContext::SetContext(int x, int y, int width, int height)
141
m_CurrrentContext.x = x;
142
m_CurrrentContext.y = y;
144
if(width <= 0 || height <= 0)
146
//nuxAssertMsg(0, TEXT("[GraphicsContext::SetContext] Incorrect context size.") );
147
if(m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject().IsValid())
149
m_CurrrentContext.width = m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject()->GetWidth();
150
m_CurrrentContext.height = m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject()->GetHeight();
154
m_CurrrentContext.width = GetWindowWidth();
155
m_CurrrentContext.height = GetWindowHeight();
160
m_CurrrentContext.width = width;
161
m_CurrrentContext.height = height;
165
void GraphicsContext::GetContextSize(int &w, int &h) const
167
w = m_CurrrentContext.width;
168
h = m_CurrrentContext.height;
171
int GraphicsContext::GetContextWidth() const
173
return m_CurrrentContext.width;
176
int GraphicsContext::GetContextHeight() const
178
return m_CurrrentContext.height;
181
int GraphicsContext::GetContextX() const
183
return m_CurrrentContext.x;
186
int GraphicsContext::GetContextY() const
188
return m_CurrrentContext.y;
191
void GraphicsContext::GetWindowSize(int &w, int &h) const
193
m_GLWindow.GetWindowSize(w, h);
196
int GraphicsContext::GetWindowWidth() const
198
return m_GLWindow.GetWindowWidth();
201
int GraphicsContext::GetWindowHeight() const
203
return m_GLWindow.GetWindowHeight();
206
void GraphicsContext::LoadFonts()
208
m_FontRenderer = new FontRenderer(*this);
211
int GraphicsContext::RenderColorText(const NFontPtr& Font, int x, int y, const NString& Str,
212
const Color& TextColor,
213
bool WriteAlphaChannel,
217
return m_FontRenderer->RenderColorText(Font, x, y, Str, TextColor, WriteAlphaChannel, NumCharacter);
221
int GraphicsContext::RenderColorTextLineStatic(const NFontPtr& Font, const PageBBox& pageSize, const NString& Str,
222
const Color& TextColor,
223
bool WriteAlphaChannel,
224
TextAlignment alignment)
227
return m_FontRenderer->RenderColorTextLineStatic(Font, pageSize, Str, TextColor, WriteAlphaChannel, alignment);
231
int GraphicsContext::RenderColorTextLineEdit(const NFontPtr& Font, const PageBBox& pageSize, const NString& Str,
232
const Color& TextColor,
233
bool WriteAlphaChannel,
234
const Color& SelectedTextColor,
235
const Color& SelectedTextBackgroundColor,
236
const Color& TextBlinkColor,
237
const Color& CursorColor,
238
bool ShowCursor, unsigned int CursorPosition, int offset, int selection_start, int selection_end)
241
return m_FontRenderer->RenderColorTextLineEdit(Font, pageSize, Str,
245
SelectedTextBackgroundColor,
248
ShowCursor, CursorPosition, offset, selection_start, selection_end);
252
void GraphicsContext::SetTexture(int TextureUnit, NTexture* Texture)
254
nuxAssertMsg(Texture != 0, TEXT("[GraphicsContext::SetTexture] Texture is NULL."));
255
if((TextureUnit < GL_TEXTURE0) || (TextureUnit > GL_TEXTURE31))
258
TRefGL< NGLTexture > CachedTexture = ResourceCache.GetCachedResource(Texture);
259
SetTexture(TextureUnit, CachedTexture->m_Texture);
262
void GraphicsContext::SetTexture(int TextureUnit, TRefGL< IOpenGLBaseTexture > DeviceTexture)
264
INL_RETURN_IF_FALSE(DeviceTexture.IsValid());
266
CHECKGL( glActiveTextureARB(TextureUnit) );
267
DeviceTexture->BindTextureToUnit(TextureUnit);
270
void GraphicsContext::EnableTextureMode(int TextureUnit, int TextureMode)
272
if((TextureUnit < GL_TEXTURE0) || (TextureUnit > GL_TEXTURE31))
274
CHECKGL( glActiveTextureARB(TextureUnit) );
275
CHECKGL( glEnable(TextureMode) );
278
void GraphicsContext::DisableTextureMode(int TextureUnit, int TextureMode)
280
if((TextureUnit < GL_TEXTURE0) || (TextureUnit > GL_TEXTURE31))
282
CHECKGL( glActiveTextureARB(TextureUnit) );
283
CHECKGL( glDisable(TextureMode) );
284
CHECKGL( glBindTexture(TextureMode, 0) );
287
void GraphicsContext::DisableAllTextureMode(int TextureUnit)
289
if((TextureUnit < GL_TEXTURE0) || (TextureUnit > GL_TEXTURE31))
292
GetThreadGLDeviceFactory()->InvalidateTextureUnit(TextureUnit);
295
//////////////////////
297
//////////////////////
298
void GraphicsContext::PushClippingRectangle(Rect A)
300
if(m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject().IsValid())
302
m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject()->PushClippingRegion(A);
306
A.OffsetPosition(m_CurrrentContext.x, m_CurrrentContext.y);
309
UINT stacksize = (UINT)ClippingRect.size();
312
int window_width, window_height;
313
window_width = m_ViewportWidth;
314
window_height = m_ViewportHeight;
318
B = Rect(0, 0, window_width, window_height);
322
B = ClippingRect[stacksize-1];
325
// http://www.codecomments.com/archive263-2004-12-350347.html
326
// If your rectangles are given in 2D as Top,Left,Bottom,Right coordinates, as typical for GUI programming, then it's simply:
327
// intersect.Left = max(a.Left, b.Left);
328
// intersect.Top = max(a.Top, b.Top);
329
// intersect.Right = min(a.Right, b.Right );
330
// intersect.Bottom = min(a.Bottom, b.Bottom);
331
// And the intersection is empty unless intersect.Right > intersect.Left && intersect.Bottom > intersect.Top
335
x1 = Min(A.x + A.width, B.x + B.width);
336
y1 = Min(A.y + A.height, B.y + B.height);
338
if((x1 > x0) && (y1 > y0))
340
ClippingRect.push_back(Rect(x0, y0, x1 - x0, y1 - y0));
342
EnableScissoring(true);
343
SetDrawClippingRegion(x0, window_height - y0 - (y1 - y0), x1 - x0, y1 - y0);
347
ClippingRect.push_back(Rect(0, 0, 0, 0));
348
EnableScissoring(true);
349
SetDrawClippingRegion(0, 0, 0, 0);
353
void GraphicsContext::PopClippingRectangle()
355
if(m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject().IsValid())
357
m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject()->PopClippingRegion();
361
INT window_width, window_height;
362
window_width = m_ViewportWidth;
363
window_height = m_ViewportHeight;
365
ClippingRect.pop_back();
366
UINT stacksize = (UINT)ClippingRect.size();
369
EnableScissoring(true);
370
SetDrawClippingRegion(0, 0, window_width, window_height);
374
Rect B = ClippingRect[stacksize-1];
375
EnableScissoring(true);
376
SetDrawClippingRegion(B.x, window_height - B.y - B.GetHeight(), B.GetWidth(), B.GetHeight());
380
void GraphicsContext::ApplyClippingRectangle()
382
if(m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject().IsValid())
384
m_GLWindow.m_DeviceFactory->GetCurrentFrameBufferObject()->ApplyClippingRegion();
388
INT window_width, window_height;
389
window_width = m_ViewportWidth;
390
window_height = m_ViewportHeight;
392
UINT stacksize = (UINT)ClippingRect.size();
395
EnableScissoring(true);
396
SetDrawClippingRegion(0, 0, window_width, window_height);
400
Rect B = ClippingRect[stacksize-1];
401
EnableScissoring(true);
402
SetDrawClippingRegion(B.x, window_height - B.y - B.GetHeight(), B.GetWidth(), B.GetHeight());
406
void GraphicsContext::EmptyClippingRegion()
408
INT window_width, window_height;
409
window_width = m_ViewportWidth;
410
window_height = m_ViewportHeight;
411
ClippingRect.clear();
413
EnableScissoring(true);
414
SetDrawClippingRegion(0, 0, window_width, window_height);
418
Rect GraphicsContext::GetClippingRegion() const
420
UINT stacksize = (UINT)ClippingRect.size();
423
return Rect(0, 0, m_ViewportWidth, m_ViewportHeight);
427
Rect r = ClippingRect[stacksize-1];
432
int GraphicsContext::GetNumberOfClippingRegions() const
434
return (int) ClippingRect.size();
437
void GraphicsContext::SetDrawClippingRegion(int x, int y, unsigned int width, unsigned int height)
439
SetScissor(x, y, width, height);
446
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
450
// From "OpenGL Programming Guide.pdf"
452
// If exact two-dimensional rasterization is desired, you must carefully specify both the orthographic
453
// projection and the vertices of primitives that are to be rasterized. The orthographic projection
454
// should be specified with integer coordinates, as shown in the following example:
455
// gluOrtho2D(0, width, 0, height);
456
// where width and height are the dimensions of the viewport. Given this projection matrix, polygon
457
// vertices and pixel image positions should be placed at integer coordinates to rasterize predictably.
458
// For example, glRecti(0, 0, 1, 1) reliably fills the lower left pixel of the viewport, and glRasterPos2i(0,
459
// 0) reliably positions an unzoomed image at the lower left of the viewport. Point vertices, line
460
// vertices, and bitmap positions should be placed at half-integer locations, however. For example, a
461
// line drawn from (x1, 0.5) to (x2, 0.5) will be reliably rendered along the bottom row of pixels int the
462
// viewport, and a point drawn at (0.5, 0.5) will reliably fill the same pixel as glRecti(0, 0, 1, 1).
463
// An optimum compromise that allows all primitives to be specified at integer positions, while still
464
// ensuring predictable rasterization, is to translate x and y by 0.375, as shown in the following code
465
// fragment. Such a translation keeps polygon and pixel image edges safely away from the centers of
466
// pixels, while moving line vertices close enough to the pixel centers.
467
// glViewport(0, 0, width, height);
468
// glMatrixMode(GL_PROJECTION);
470
// gluOrtho2D(0, width, 0, height);
471
// glMatrixMode(GL_MODELVIEW);
473
// glTranslatef(0.375, 0.375, 0.0);
474
/* render all primitives at integer positions */
476
const float RASTERIZATION_OFFSET = 0.375f;
477
void GraphicsContext::Push2DWindow(int w, int h)
479
CHECKGL( glMatrixMode(GL_MODELVIEW) );
481
m_ModelViewMatrix.Translate(m_CurrrentContext.x + RASTERIZATION_OFFSET, m_CurrrentContext.y + RASTERIZATION_OFFSET, 0);
483
std::list<Matrix4>::iterator it;
484
for(it = m_2DModelViewMatricesStack.begin(); it != m_2DModelViewMatricesStack.end(); it++)
486
temp = m_ModelViewMatrix;
487
m_ModelViewMatrix = temp * (*it);
489
// m_ModelViewMatrix is row_major while opengl is column major. We need to transpose.
490
m_ModelViewMatrix.Transpose();
491
CHECKGL( glLoadMatrixf((GLfloat*)(m_ModelViewMatrix.m)) );
493
CHECKGL( glMatrixMode(GL_PROJECTION) );
495
m_ProjectionMatrix.Orthographic(0, w, h, 0, -1.0f, 1.0f);
496
// m_ProjectionMatrix is row_major while opengl is column major. We need to transpose.
497
m_ProjectionMatrix.Transpose();
498
CHECKGL( glLoadMatrixf((GLfloat*)(m_ProjectionMatrix.m)) );
502
void GraphicsContext::Pop2DWindow()
504
CHECKGL( glMatrixMode(GL_PROJECTION) );
505
CHECKGL( glLoadIdentity() );
506
CHECKGL( glMatrixMode(GL_MODELVIEW) );
507
CHECKGL( glLoadIdentity() );
518
void GraphicsContext::Push2DModelViewMatrix(Matrix4 mat)
520
m_2DModelViewMatricesStack.push_back(mat);
522
CHECKGL( glMatrixMode(GL_MODELVIEW) );
523
m_ModelViewMatrix.Translate(m_CurrrentContext.x + RASTERIZATION_OFFSET, m_CurrrentContext.y + RASTERIZATION_OFFSET, 0);
525
std::list<Matrix4>::iterator it;
526
for(it = m_2DModelViewMatricesStack.begin(); it != m_2DModelViewMatricesStack.end(); it++)
528
temp = m_ModelViewMatrix;
529
m_ModelViewMatrix = (*it) * temp;
532
// m_ModelViewMatrix is row_major while opengl is column major. We need to transpose.
533
m_ModelViewMatrix.Transpose();
534
CHECKGL( glLoadMatrixf((GLfloat*)(m_ModelViewMatrix.m)) );
538
Matrix4 GraphicsContext::Pop2DModelViewMatrix()
542
if(m_2DModelViewMatricesStack.size() <= 0)
545
std::list<Matrix4>::iterator it;
546
it = m_2DModelViewMatricesStack.end();
549
m_2DModelViewMatricesStack.pop_back();
552
CHECKGL( glMatrixMode(GL_MODELVIEW) );
553
m_ModelViewMatrix.Translate(m_CurrrentContext.x + RASTERIZATION_OFFSET, m_CurrrentContext.y + RASTERIZATION_OFFSET, 0);
555
std::list<Matrix4>::iterator it;
556
for(it = m_2DModelViewMatricesStack.begin(); it != m_2DModelViewMatricesStack.end(); it++)
558
temp = m_ModelViewMatrix;
559
m_ModelViewMatrix = temp * (*it);
562
// m_ModelViewMatrix is row_major while opengl is column major. We need to transpose.
563
m_ModelViewMatrix.Transpose();
564
CHECKGL( glLoadMatrixf((GLfloat*)(m_ModelViewMatrix.m)) );
569
void GraphicsContext::Clear2DModelViewMatrix()
571
m_2DModelViewMatricesStack.clear();
574
CHECKGL( glMatrixMode(GL_MODELVIEW) );
575
m_ModelViewMatrix.Translate(m_CurrrentContext.x + RASTERIZATION_OFFSET, m_CurrrentContext.y + RASTERIZATION_OFFSET, 0);
577
std::list<Matrix4>::iterator it;
578
for(it = m_2DModelViewMatricesStack.begin(); it != m_2DModelViewMatricesStack.end(); it++)
580
temp = m_ModelViewMatrix;
581
m_ModelViewMatrix = temp * (*it);
584
// m_ModelViewMatrix is row_major while opengl is column major. We need to transpose.
585
m_ModelViewMatrix.Transpose();
586
CHECKGL( glLoadMatrixf((GLfloat*)(m_ModelViewMatrix.m)) );
590
Matrix4 GraphicsContext::GetProjectionMatrix()
592
return m_ProjectionMatrix;
595
Matrix4 GraphicsContext::GetModelViewMatrix()
597
return m_ModelViewMatrix;
600
Matrix4 GraphicsContext::GetModelViewProjectionMatrix()
602
return m_ModelViewMatrix*m_ProjectionMatrix;
605
Matrix4 GraphicsContext::GetOpenGLModelViewProjectionMatrix()
607
// This matrix is the transposed version of GetModelViewProjectionMatrix.
608
return m_ModelViewMatrix*m_ProjectionMatrix;
611
void GraphicsContext::SetEnvModeTextureAlphaBlend(int TextureUnit)
613
// Render RGBA bitmap texture and alpha blend with the background
614
// Make sure you call EnableBlending(bool b) before.
615
CHECKGL( glActiveTextureARB(TextureUnit) );
616
//glEnable(GL_BLEND);
617
CHECKGL( glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) );
618
// TextureEnvironment
619
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB) );
621
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE) );
622
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE) );
623
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR) );
625
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE) );
626
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE) );
627
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA) );
630
void GraphicsContext::SetEnvModeSelectTexture(int TextureUnit)
632
// Render RGBA bitmap texture.
633
CHECKGL( glActiveTextureARB(TextureUnit) );
634
// TextureEnvironment
635
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB) );
637
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE) );
638
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE) );
639
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR) );
641
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE) );
642
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE) );
643
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA) );
646
void GraphicsContext::SetEnvModeSelectColor(int TextureUnit)
649
CHECKGL( glActiveTextureARB(TextureUnit) );
650
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB) );
652
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE) );
653
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR) );
654
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR) );
656
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE) );
657
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PRIMARY_COLOR) );
658
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA) );
661
void GraphicsContext::SetEnvModeModulateColorWithTexture(int TextureUnit)
663
// Render RGBA bitmat texture and alpha blend with the background
664
CHECKGL( glActiveTextureARB(TextureUnit) );
665
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB) );
667
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE) );
668
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR) );
669
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR) );
670
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE) );
671
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR) );
673
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE) );
674
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE) );
675
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA) );
676
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE) );
677
CHECKGL( glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA) );
680
void GraphicsContext::SetViewport(int origin_x, int origin_y, int w, int h)
685
m_ViewportX = origin_x;
686
m_ViewportY = origin_y;
688
m_ViewportHeight = h;
690
if(m_ViewportWidth < 0)
692
nuxAssertMsg(0, TEXT("[GraphicsContext::SetViewport] Incorrect context size.") );
695
if(m_ViewportHeight < 0)
697
nuxAssertMsg(0, TEXT("[GraphicsContext::SetViewport] Incorrect context size.") );
698
m_ViewportHeight = 1;
700
CHECKGL( glViewport(origin_x, origin_y, m_ViewportWidth, m_ViewportHeight) );
703
Rect GraphicsContext::GetViewportRect()
705
return Rect(m_ViewportX, m_ViewportY, m_ViewportWidth, m_ViewportHeight);
708
void GraphicsContext::SetScissorOffset(int x, int y)
710
m_ScissorXOffset = x;
711
m_ScissorYOffset = y;
714
void GraphicsContext::SetScissor(int x, int y, int w, int h)
722
if(m_ScissorWidth < 0)
724
nuxAssertMsg(0, TEXT("[GraphicsContext::SetViewport] Incorrect context size.") );
727
if(m_ScissorHeight < 0)
729
nuxAssertMsg(0, TEXT("[GraphicsContext::SetViewport] Incorrect context size.") );
732
CHECKGL( glScissor(m_ScissorX + m_ScissorXOffset, m_ScissorY + m_ScissorYOffset, m_ScissorWidth, m_ScissorHeight) );
735
Rect GraphicsContext::GetScissorRect()
737
return Rect(m_ScissorX, m_ScissorY, m_ScissorWidth, m_ScissorHeight);
740
void GraphicsContext::EnableScissoring(bool b)
742
GetRenderStates().EnableScissor(b);
745
/////////////////////////////////////////
746
// 2D Area Clear Color Depth Stencil //
747
/////////////////////////////////////////
749
void GraphicsContext::ClearAreaColorDepthStencil(int x, int y, int width, int height, Color clearcolor, float cleardepth, int clearstencil)
751
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE0);
752
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE1);
753
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE2);
754
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE3);
755
EnableTextureMode(GL_TEXTURE0, GL_TEXTURE_2D);
756
SetEnvModeSelectColor(GL_TEXTURE0);
757
// enable stencil buffer
758
CHECKGL( glEnable(GL_STENCIL_TEST) );
759
// write a one to the stencil buffer everywhere we are about to draw
760
CHECKGL( glStencilFunc(GL_ALWAYS, clearstencil, 0xFFFFFFFF) );
761
// this is to always pass a one to the stencil buffer where we draw
762
CHECKGL( glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) );
766
CHECKGL( glEnable(GL_DEPTH_TEST) );
767
CHECKGL( glDepthFunc(GL_ALWAYS) );
771
glColor4f(clearcolor.R(), clearcolor.G(), clearcolor.B(), clearcolor.A());
772
glVertex4f(x, y, 0.0f, 1.0f);
773
glVertex4f(x + width, y, 0.0f, 1.0f);
774
glVertex4f(x + width, y + height, 0.0f, 1.0f);
775
glVertex4f(x, y + height, 0.0f, 1.0f);
779
CHECKGL( glDepthFunc(GL_LESS) );
780
CHECKGL( glDisable(GL_DEPTH_TEST) );
781
CHECKGL( glDisable(GL_STENCIL_TEST) );
784
void GraphicsContext::ClearAreaColor(int x, int y, int width, int height, Color clearcolor)
786
//glClear(GL_DEPTH_BUFFER_BIT);
788
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE0);
789
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE1);
790
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE2);
791
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE3);
792
EnableTextureMode(GL_TEXTURE0, GL_TEXTURE_2D);
793
SetEnvModeSelectColor(GL_TEXTURE0);
797
glColor4f(clearcolor.R(), clearcolor.G(), clearcolor.B(), clearcolor.A());
798
glVertex4f(x, y, 0.0f, 1.0f);
799
glVertex4f(x + width, y, 0.0f, 1.0f);
800
glVertex4f(x + width, y + height, 0.0f, 1.0f);
801
glVertex4f(x, y + height, 0.0f, 1.0f);
806
void GraphicsContext::ClearAreaDepthStencil(int x, int y, int width, int height, float cleardepth, int clearstencil)
808
//glClear(GL_DEPTH_BUFFER_BIT);
810
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE0);
811
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE1);
812
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE2);
813
GetThreadGLDeviceFactory()->InvalidateTextureUnit(GL_TEXTURE3);
814
EnableTextureMode(GL_TEXTURE0, GL_TEXTURE_2D);
815
SetEnvModeSelectColor(GL_TEXTURE0);
816
// enable stencil buffer
817
CHECKGL( glEnable(GL_STENCIL_TEST) );
818
// write a one to the stencil buffer everywhere we are about to draw
819
CHECKGL( glStencilFunc(GL_ALWAYS, clearstencil, 0xFFFFFFFF) );
820
// this is to always pass a one to the stencil buffer where we draw
821
CHECKGL( glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) );
823
CHECKGL( glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) );
824
CHECKGL( glEnable(GL_DEPTH_TEST) );
825
CHECKGL( glDepthFunc(GL_ALWAYS) );
829
glVertex4f(x, y, 0.0f, 1.0f);
830
glVertex4f(x + width, y, 0.0f, 1.0f);
831
glVertex4f(x + width, y + height, 0.0f, 1.0f);
832
glVertex4f(x, y + height, 0.0f, 1.0f);
836
CHECKGL( glDepthFunc(GL_LESS) );
837
CHECKGL( glDisable(GL_DEPTH_TEST) );
838
CHECKGL( glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
839
CHECKGL( glDisable(GL_STENCIL_TEST) );
844
void GraphicsContext::ResetStats()
847
m_quad_tex_stats = 0;
848
m_triangle_stats = 0;
849
m_triangle_tex_stats = 0;
853
TRefGL< NGLResource > GraphicsContext::CacheResource(NResource* Resource)
855
return ResourceCache.GetCachedResource(Resource);
858
void GraphicsContext::UpdateResource(NResource* Resource)
860
TRefGL< NGLResource > GLResource = ResourceCache.FindCachedResourceById(Resource->GetResourceIndex()); //(NGLResource*)(*(ResourceCache.ResourceMap.find(Resource->ResourceIndex))).second;
861
UBOOL bUpdated = FALSE;
863
if(GLResource.IsValid())
865
// Iterate through all resource updater types (list is sorted by subclass depth).
866
for (t_u32 i = 0; i < ResourceCache.GetResourceUpdaters().size(); ++i)
868
NResourceUpdater* ResourceUpdater = ResourceCache.GetResourceUpdaters()[i];
869
nuxAssert(ResourceUpdater);
871
// Check if the updater is valid for updating the resource.
872
if( ResourceUpdater->UpdatesThisResource(Resource) )
874
bUpdated = ResourceUpdater->UpdateResource(GLResource, Resource);
881
bool GraphicsContext::IsResourceCached(NResource* Resource)
883
return ResourceCache.IsCachedResource(Resource);