2023-10-03 20:37:00 +02:00
// Copyright (C) 2002-2008 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
# include "COGLESDriver.h"
# include <cassert>
# include "CNullDriver.h"
# include "IContextManager.h"
# ifdef _IRR_COMPILE_WITH_OGLES1_
# include "COpenGLCoreTexture.h"
# include "COpenGLCoreRenderTarget.h"
# include "COpenGLCoreCacheHandler.h"
# include "COGLESMaterialRenderer.h"
# include "EVertexAttributes.h"
# include "CImage.h"
# include "os.h"
# ifdef _IRR_COMPILE_WITH_ANDROID_DEVICE_
# include "android_native_app_glue.h"
# endif
namespace irr
{
namespace video
{
2024-03-20 19:35:52 +01:00
COGLES1Driver : : COGLES1Driver ( const SIrrlichtCreationParameters & params , io : : IFileSystem * io , IContextManager * contextManager ) :
CNullDriver ( io , params . WindowSize ) , COGLES1ExtensionHandler ( ) , CacheHandler ( 0 ) , CurrentRenderMode ( ERM_NONE ) ,
ResetRenderStates ( true ) , Transformation3DChanged ( true ) , AntiAlias ( params . AntiAlias ) ,
ColorFormat ( ECF_R8G8B8 ) , Params ( params ) , ContextManager ( contextManager )
2023-10-03 20:37:00 +02:00
{
# ifdef _DEBUG
setDebugName ( " COGLESDriver " ) ;
# endif
2024-03-20 19:35:52 +01:00
core : : dimension2d < u32 > windowSize ( 0 , 0 ) ;
2023-10-03 20:37:00 +02:00
if ( ! ContextManager )
return ;
ContextManager - > grab ( ) ;
ContextManager - > generateSurface ( ) ;
ContextManager - > generateContext ( ) ;
ExposedData = ContextManager - > getContext ( ) ;
ContextManager - > activateContext ( ExposedData , false ) ;
windowSize = params . WindowSize ;
2024-03-20 19:35:52 +01:00
genericDriverInit ( windowSize , params . Stencilbuffer ) ;
2023-10-03 20:37:00 +02:00
}
COGLES1Driver : : ~ COGLES1Driver ( )
{
deleteMaterialRenders ( ) ;
CacheHandler - > getTextureCache ( ) . clear ( ) ;
removeAllRenderTargets ( ) ;
deleteAllTextures ( ) ;
removeAllOcclusionQueries ( ) ;
removeAllHardwareBuffers ( ) ;
delete CacheHandler ;
2024-03-20 19:35:52 +01:00
if ( ContextManager ) {
2023-10-03 20:37:00 +02:00
ContextManager - > destroyContext ( ) ;
ContextManager - > destroySurface ( ) ;
ContextManager - > terminate ( ) ;
ContextManager - > drop ( ) ;
}
}
// -----------------------------------------------------------------------
// METHODS
// -----------------------------------------------------------------------
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : genericDriverInit ( const core : : dimension2d < u32 > & screenSize , bool stencilBuffer )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
Name = glGetString ( GL_VERSION ) ;
2023-10-03 20:37:00 +02:00
printVersion ( ) ;
// print renderer information
VendorName = glGetString ( GL_VENDOR ) ;
os : : Printer : : log ( VendorName . c_str ( ) , ELL_INFORMATION ) ;
// load extensions
initExtensions ( ) ;
// reset cache handler
delete CacheHandler ;
CacheHandler = new COGLES1CacheHandler ( this ) ;
StencilBuffer = stencilBuffer ;
DriverAttributes - > setAttribute ( " MaxTextures " , ( s32 ) Feature . MaxTextureUnits ) ;
DriverAttributes - > setAttribute ( " MaxSupportedTextures " , ( s32 ) Feature . MaxTextureUnits ) ;
DriverAttributes - > setAttribute ( " MaxAnisotropy " , MaxAnisotropy ) ;
DriverAttributes - > setAttribute ( " MaxIndices " , ( s32 ) MaxIndices ) ;
DriverAttributes - > setAttribute ( " MaxTextureSize " , ( s32 ) MaxTextureSize ) ;
DriverAttributes - > setAttribute ( " MaxTextureLODBias " , MaxTextureLODBias ) ;
DriverAttributes - > setAttribute ( " Version " , Version ) ;
DriverAttributes - > setAttribute ( " AntiAlias " , AntiAlias ) ;
glPixelStorei ( GL_PACK_ALIGNMENT , 1 ) ;
UserClipPlane . reallocate ( MaxUserClipPlanes ) ;
UserClipPlaneEnabled . resize ( MaxUserClipPlanes ) ;
2024-03-20 19:35:52 +01:00
for ( s32 i = 0 ; i < MaxUserClipPlanes ; + + i ) {
2023-10-03 20:37:00 +02:00
UserClipPlane . push_back ( core : : plane3df ( ) ) ;
UserClipPlaneEnabled [ i ] = false ;
}
for ( s32 i = 0 ; i < ETS_COUNT ; + + i )
setTransform ( static_cast < E_TRANSFORMATION_STATE > ( i ) , core : : IdentityMatrix ) ;
setAmbientLight ( SColorf ( 0.0f , 0.0f , 0.0f , 0.0f ) ) ;
glClearDepthf ( 1.0f ) ;
glHint ( GL_PERSPECTIVE_CORRECTION_HINT , GL_FASTEST ) ;
glHint ( GL_GENERATE_MIPMAP_HINT , GL_FASTEST ) ;
glDepthFunc ( GL_LEQUAL ) ;
glFrontFace ( GL_CW ) ;
glAlphaFunc ( GL_GREATER , 0.f ) ;
// create material renderers
createMaterialRenderers ( ) ;
// set the renderstates
setRenderStates3DMode ( ) ;
// set fog mode
setFog ( FogColor , FogType , FogStart , FogEnd , FogDensity , PixelFog , RangeFog ) ;
// create matrix for flipping textures
TextureFlipMatrix . buildTextureTransform ( 0.0f , core : : vector2df ( 0 , 0 ) , core : : vector2df ( 0 , 1.0f ) , core : : vector2df ( 1.0f , - 1.0f ) ) ;
// We need to reset once more at the beginning of the first rendering.
// This fixes problems with intermediate changes to the material during texture load.
ResetRenderStates = true ;
testGLError ( __LINE__ ) ;
return true ;
}
void COGLES1Driver : : createMaterialRenderers ( )
{
addAndDropMaterialRenderer ( new COGLES1MaterialRenderer_SOLID ( this ) ) ;
addAndDropMaterialRenderer ( new COGLES1MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL ( this ) ) ;
addAndDropMaterialRenderer ( new COGLES1MaterialRenderer_TRANSPARENT_ALPHA_CHANNEL_REF ( this ) ) ;
addAndDropMaterialRenderer ( new COGLES1MaterialRenderer_TRANSPARENT_VERTEX_ALPHA ( this ) ) ;
addAndDropMaterialRenderer ( new COGLES1MaterialRenderer_ONETEXTURE_BLEND ( this ) ) ;
}
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : beginScene ( u16 clearFlag , SColor clearColor , f32 clearDepth , u8 clearStencil , const SExposedVideoData & videoData , core : : rect < s32 > * sourceRect )
2023-10-03 20:37:00 +02:00
{
CNullDriver : : beginScene ( clearFlag , clearColor , clearDepth , clearStencil , videoData , sourceRect ) ;
if ( ContextManager )
ContextManager - > activateContext ( videoData , true ) ;
clearBuffers ( clearFlag , clearColor , clearDepth , clearStencil ) ;
return true ;
}
bool COGLES1Driver : : endScene ( )
{
CNullDriver : : endScene ( ) ;
glFlush ( ) ;
if ( ContextManager )
return ContextManager - > swapBuffers ( ) ;
return false ;
}
//! Returns the transformation set by setTransform
2024-03-20 19:35:52 +01:00
const core : : matrix4 & COGLES1Driver : : getTransform ( E_TRANSFORMATION_STATE state ) const
2023-10-03 20:37:00 +02:00
{
return Matrices [ state ] ;
}
//! sets transformation
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : setTransform ( E_TRANSFORMATION_STATE state , const core : : matrix4 & mat )
2023-10-03 20:37:00 +02:00
{
Matrices [ state ] = mat ;
Transformation3DChanged = true ;
2024-03-20 19:35:52 +01:00
switch ( state ) {
2023-10-03 20:37:00 +02:00
case ETS_VIEW :
2024-03-20 19:35:52 +01:00
case ETS_WORLD : {
// OGLES1 only has a model matrix, view and world is not existent. so lets fake these two.
glMatrixMode ( GL_MODELVIEW ) ;
glLoadMatrixf ( ( Matrices [ ETS_VIEW ] * Matrices [ ETS_WORLD ] ) . pointer ( ) ) ;
// we have to update the clip planes to the latest view matrix
for ( u32 i = 0 ; i < MaxUserClipPlanes ; + + i )
if ( UserClipPlaneEnabled [ i ] )
uploadClipPlane ( i ) ;
} break ;
case ETS_PROJECTION : {
GLfloat glmat [ 16 ] ;
getGLMatrix ( glmat , mat ) ;
// flip z to compensate OGLES1s right-hand coordinate system
glmat [ 12 ] * = - 1.0f ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadMatrixf ( glmat ) ;
} break ;
2023-10-03 20:37:00 +02:00
default :
break ;
}
}
bool COGLES1Driver : : updateVertexHardwareBuffer ( SHWBufferLink_opengl * HWBuffer )
{
if ( ! HWBuffer )
return false ;
2024-03-20 19:35:52 +01:00
const scene : : IMeshBuffer * mb = HWBuffer - > MeshBuffer ;
const void * vertices = mb - > getVertices ( ) ;
const u32 vertexCount = mb - > getVertexCount ( ) ;
const E_VERTEX_TYPE vType = mb - > getVertexType ( ) ;
2023-10-03 20:37:00 +02:00
const u32 vertexSize = getVertexPitchFromType ( vType ) ;
2024-03-20 19:35:52 +01:00
// buffer vertex data, and convert colours...
2023-10-03 20:37:00 +02:00
core : : array < c8 > buffer ( vertexSize * vertexCount ) ;
buffer . set_used ( vertexSize * vertexCount ) ;
memcpy ( buffer . pointer ( ) , vertices , vertexSize * vertexCount ) ;
// in order to convert the colors into opengl format (RGBA)
2024-03-20 19:35:52 +01:00
switch ( vType ) {
case EVT_STANDARD : {
S3DVertex * pb = reinterpret_cast < S3DVertex * > ( buffer . pointer ( ) ) ;
const S3DVertex * po = static_cast < const S3DVertex * > ( vertices ) ;
for ( u32 i = 0 ; i < vertexCount ; i + + ) {
po [ i ] . Color . toOpenGLColor ( ( u8 * ) & ( pb [ i ] . Color . color ) ) ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
} break ;
case EVT_2TCOORDS : {
S3DVertex2TCoords * pb = reinterpret_cast < S3DVertex2TCoords * > ( buffer . pointer ( ) ) ;
const S3DVertex2TCoords * po = static_cast < const S3DVertex2TCoords * > ( vertices ) ;
for ( u32 i = 0 ; i < vertexCount ; i + + ) {
po [ i ] . Color . toOpenGLColor ( ( u8 * ) & ( pb [ i ] . Color . color ) ) ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
} break ;
case EVT_TANGENTS : {
S3DVertexTangents * pb = reinterpret_cast < S3DVertexTangents * > ( buffer . pointer ( ) ) ;
const S3DVertexTangents * po = static_cast < const S3DVertexTangents * > ( vertices ) ;
for ( u32 i = 0 ; i < vertexCount ; i + + ) {
po [ i ] . Color . toOpenGLColor ( ( u8 * ) & ( pb [ i ] . Color . color ) ) ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
} break ;
default : {
return false ;
}
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
// get or create buffer
bool newBuffer = false ;
if ( ! HWBuffer - > vbo_verticesID ) {
2023-10-03 20:37:00 +02:00
glGenBuffers ( 1 , & HWBuffer - > vbo_verticesID ) ;
2024-03-20 19:35:52 +01:00
if ( ! HWBuffer - > vbo_verticesID )
return false ;
newBuffer = true ;
} else if ( HWBuffer - > vbo_verticesSize < vertexCount * vertexSize ) {
newBuffer = true ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
glBindBuffer ( GL_ARRAY_BUFFER , HWBuffer - > vbo_verticesID ) ;
2023-10-03 20:37:00 +02:00
// copy data to graphics card
if ( ! newBuffer )
glBufferSubData ( GL_ARRAY_BUFFER , 0 , vertexCount * vertexSize , buffer . const_pointer ( ) ) ;
2024-03-20 19:35:52 +01:00
else {
HWBuffer - > vbo_verticesSize = vertexCount * vertexSize ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Vertex = = scene : : EHM_STATIC )
2023-10-03 20:37:00 +02:00
glBufferData ( GL_ARRAY_BUFFER , vertexCount * vertexSize , buffer . const_pointer ( ) , GL_STATIC_DRAW ) ;
else
glBufferData ( GL_ARRAY_BUFFER , vertexCount * vertexSize , buffer . const_pointer ( ) , GL_DYNAMIC_DRAW ) ;
}
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
return ( ! testGLError ( __LINE__ ) ) ;
}
bool COGLES1Driver : : updateIndexHardwareBuffer ( SHWBufferLink_opengl * HWBuffer )
{
if ( ! HWBuffer )
return false ;
2024-03-20 19:35:52 +01:00
const scene : : IMeshBuffer * mb = HWBuffer - > MeshBuffer ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
const void * indices = mb - > getIndices ( ) ;
u32 indexCount = mb - > getIndexCount ( ) ;
2023-10-03 20:37:00 +02:00
GLenum indexSize ;
2024-03-20 19:35:52 +01:00
switch ( mb - > getIndexType ( ) ) {
case ( EIT_16BIT ) : {
indexSize = sizeof ( u16 ) ;
break ;
}
case ( EIT_32BIT ) : {
indexSize = sizeof ( u32 ) ;
break ;
}
default : {
return false ;
}
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
// get or create buffer
bool newBuffer = false ;
if ( ! HWBuffer - > vbo_indicesID ) {
2023-10-03 20:37:00 +02:00
glGenBuffers ( 1 , & HWBuffer - > vbo_indicesID ) ;
2024-03-20 19:35:52 +01:00
if ( ! HWBuffer - > vbo_indicesID )
return false ;
newBuffer = true ;
} else if ( HWBuffer - > vbo_indicesSize < indexCount * indexSize ) {
newBuffer = true ;
2023-10-03 20:37:00 +02:00
}
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , HWBuffer - > vbo_indicesID ) ;
// copy data to graphics card
if ( ! newBuffer )
glBufferSubData ( GL_ELEMENT_ARRAY_BUFFER , 0 , indexCount * indexSize , indices ) ;
2024-03-20 19:35:52 +01:00
else {
HWBuffer - > vbo_indicesSize = indexCount * indexSize ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Index = = scene : : EHM_STATIC )
2023-10-03 20:37:00 +02:00
glBufferData ( GL_ELEMENT_ARRAY_BUFFER , indexCount * indexSize , indices , GL_STATIC_DRAW ) ;
else
glBufferData ( GL_ELEMENT_ARRAY_BUFFER , indexCount * indexSize , indices , GL_DYNAMIC_DRAW ) ;
}
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
return ( ! testGLError ( __LINE__ ) ) ;
}
//! updates hardware buffer if needed
bool COGLES1Driver : : updateHardwareBuffer ( SHWBufferLink * HWBuffer )
{
if ( ! HWBuffer )
return false ;
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Vertex ! = scene : : EHM_NEVER ) {
if ( HWBuffer - > ChangedID_Vertex ! = HWBuffer - > MeshBuffer - > getChangedID_Vertex ( ) | | ! static_cast < SHWBufferLink_opengl * > ( HWBuffer ) - > vbo_verticesID ) {
2023-10-03 20:37:00 +02:00
HWBuffer - > ChangedID_Vertex = HWBuffer - > MeshBuffer - > getChangedID_Vertex ( ) ;
2024-03-20 19:35:52 +01:00
if ( ! updateVertexHardwareBuffer ( static_cast < SHWBufferLink_opengl * > ( HWBuffer ) ) )
2023-10-03 20:37:00 +02:00
return false ;
}
}
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Index ! = scene : : EHM_NEVER ) {
if ( HWBuffer - > ChangedID_Index ! = HWBuffer - > MeshBuffer - > getChangedID_Index ( ) | | ! ( ( SHWBufferLink_opengl * ) HWBuffer ) - > vbo_indicesID ) {
2023-10-03 20:37:00 +02:00
HWBuffer - > ChangedID_Index = HWBuffer - > MeshBuffer - > getChangedID_Index ( ) ;
2024-03-20 19:35:52 +01:00
if ( ! updateIndexHardwareBuffer ( static_cast < SHWBufferLink_opengl * > ( HWBuffer ) ) )
2023-10-03 20:37:00 +02:00
return false ;
}
}
return true ;
}
//! Create hardware buffer from meshbuffer
2024-03-20 19:35:52 +01:00
COGLES1Driver : : SHWBufferLink * COGLES1Driver : : createHardwareBuffer ( const scene : : IMeshBuffer * mb )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
if ( ! mb | | ( mb - > getHardwareMappingHint_Index ( ) = = scene : : EHM_NEVER & & mb - > getHardwareMappingHint_Vertex ( ) = = scene : : EHM_NEVER ) )
2023-10-03 20:37:00 +02:00
return 0 ;
2024-03-20 19:35:52 +01:00
SHWBufferLink_opengl * HWBuffer = new SHWBufferLink_opengl ( mb ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
// add to map
2023-10-03 20:37:00 +02:00
HWBuffer - > listPosition = HWBufferList . insert ( HWBufferList . end ( ) , HWBuffer ) ;
2024-03-20 19:35:52 +01:00
HWBuffer - > ChangedID_Vertex = HWBuffer - > MeshBuffer - > getChangedID_Vertex ( ) ;
HWBuffer - > ChangedID_Index = HWBuffer - > MeshBuffer - > getChangedID_Index ( ) ;
HWBuffer - > Mapped_Vertex = mb - > getHardwareMappingHint_Vertex ( ) ;
HWBuffer - > Mapped_Index = mb - > getHardwareMappingHint_Index ( ) ;
HWBuffer - > vbo_verticesID = 0 ;
HWBuffer - > vbo_indicesID = 0 ;
HWBuffer - > vbo_verticesSize = 0 ;
HWBuffer - > vbo_indicesSize = 0 ;
if ( ! updateHardwareBuffer ( HWBuffer ) ) {
2023-10-03 20:37:00 +02:00
deleteHardwareBuffer ( HWBuffer ) ;
return 0 ;
}
return HWBuffer ;
}
void COGLES1Driver : : deleteHardwareBuffer ( SHWBufferLink * _HWBuffer )
{
if ( ! _HWBuffer )
return ;
2024-03-20 19:35:52 +01:00
SHWBufferLink_opengl * HWBuffer = static_cast < SHWBufferLink_opengl * > ( _HWBuffer ) ;
if ( HWBuffer - > vbo_verticesID ) {
2023-10-03 20:37:00 +02:00
glDeleteBuffers ( 1 , & HWBuffer - > vbo_verticesID ) ;
2024-03-20 19:35:52 +01:00
HWBuffer - > vbo_verticesID = 0 ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > vbo_indicesID ) {
2023-10-03 20:37:00 +02:00
glDeleteBuffers ( 1 , & HWBuffer - > vbo_indicesID ) ;
2024-03-20 19:35:52 +01:00
HWBuffer - > vbo_indicesID = 0 ;
2023-10-03 20:37:00 +02:00
}
CNullDriver : : deleteHardwareBuffer ( _HWBuffer ) ;
}
//! Draw hardware buffer
void COGLES1Driver : : drawHardwareBuffer ( SHWBufferLink * _HWBuffer )
{
if ( ! _HWBuffer )
return ;
2024-03-20 19:35:52 +01:00
SHWBufferLink_opengl * HWBuffer = static_cast < SHWBufferLink_opengl * > ( _HWBuffer ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
updateHardwareBuffer ( HWBuffer ) ; // check if update is needed
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
const scene : : IMeshBuffer * mb = HWBuffer - > MeshBuffer ;
const void * vertices = mb - > getVertices ( ) ;
const void * indexList = mb - > getIndices ( ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Vertex ! = scene : : EHM_NEVER ) {
2023-10-03 20:37:00 +02:00
glBindBuffer ( GL_ARRAY_BUFFER , HWBuffer - > vbo_verticesID ) ;
2024-03-20 19:35:52 +01:00
vertices = 0 ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Index ! = scene : : EHM_NEVER ) {
2023-10-03 20:37:00 +02:00
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , HWBuffer - > vbo_indicesID ) ;
2024-03-20 19:35:52 +01:00
indexList = 0 ;
2023-10-03 20:37:00 +02:00
}
drawVertexPrimitiveList ( vertices , mb - > getVertexCount ( ) , indexList ,
mb - > getPrimitiveCount ( ) , mb - > getVertexType ( ) ,
mb - > getPrimitiveType ( ) , mb - > getIndexType ( ) ) ;
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Vertex ! = scene : : EHM_NEVER )
2023-10-03 20:37:00 +02:00
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
2024-03-20 19:35:52 +01:00
if ( HWBuffer - > Mapped_Index ! = scene : : EHM_NEVER )
2023-10-03 20:37:00 +02:00
glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , 0 ) ;
}
2024-03-20 19:35:52 +01:00
IRenderTarget * COGLES1Driver : : addRenderTarget ( )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
COGLES1RenderTarget * renderTarget = new COGLES1RenderTarget ( this ) ;
2023-10-03 20:37:00 +02:00
RenderTargets . push_back ( renderTarget ) ;
return renderTarget ;
}
// small helper function to create vertex buffer object adress offsets
2024-03-20 19:35:52 +01:00
static inline u8 * buffer_offset ( const long offset )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
return ( ( u8 * ) 0 + offset ) ;
2023-10-03 20:37:00 +02:00
}
//! draws a vertex primitive list
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : drawVertexPrimitiveList ( const void * vertices , u32 vertexCount ,
const void * indexList , u32 primitiveCount ,
2023-10-03 20:37:00 +02:00
E_VERTEX_TYPE vType , scene : : E_PRIMITIVE_TYPE pType , E_INDEX_TYPE iType )
{
if ( ! checkPrimitiveCount ( primitiveCount ) )
return ;
setRenderStates3DMode ( ) ;
2024-03-20 19:35:52 +01:00
drawVertexPrimitiveList2d3d ( vertices , vertexCount , ( const u16 * ) indexList , primitiveCount , vType , pType , iType ) ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : drawVertexPrimitiveList2d3d ( const void * vertices , u32 vertexCount ,
const void * indexList , u32 primitiveCount ,
2023-10-03 20:37:00 +02:00
E_VERTEX_TYPE vType , scene : : E_PRIMITIVE_TYPE pType , E_INDEX_TYPE iType , bool threed )
{
if ( ! primitiveCount | | ! vertexCount )
return ;
if ( ! threed & & ! checkPrimitiveCount ( primitiveCount ) )
return ;
CNullDriver : : drawVertexPrimitiveList ( vertices , vertexCount , indexList , primitiveCount , vType , pType , iType ) ;
2024-03-20 19:35:52 +01:00
if ( vertices ) {
2023-10-03 20:37:00 +02:00
// convert colors to gl color format.
2024-03-20 19:35:52 +01:00
vertexCount * = 4 ; // reused as color component count
2023-10-03 20:37:00 +02:00
ColorBuffer . set_used ( vertexCount ) ;
u32 i ;
2024-03-20 19:35:52 +01:00
switch ( vType ) {
case EVT_STANDARD : {
const S3DVertex * p = static_cast < const S3DVertex * > ( vertices ) ;
for ( i = 0 ; i < vertexCount ; i + = 4 ) {
p - > Color . toOpenGLColor ( & ColorBuffer [ i ] ) ;
+ + p ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
} break ;
case EVT_2TCOORDS : {
const S3DVertex2TCoords * p = static_cast < const S3DVertex2TCoords * > ( vertices ) ;
for ( i = 0 ; i < vertexCount ; i + = 4 ) {
p - > Color . toOpenGLColor ( & ColorBuffer [ i ] ) ;
+ + p ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
} break ;
case EVT_TANGENTS : {
const S3DVertexTangents * p = static_cast < const S3DVertexTangents * > ( vertices ) ;
for ( i = 0 ; i < vertexCount ; i + = 4 ) {
p - > Color . toOpenGLColor ( & ColorBuffer [ i ] ) ;
+ + p ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
} break ;
2023-10-03 20:37:00 +02:00
}
}
// draw everything
glClientActiveTexture ( GL_TEXTURE0 ) ;
glEnableClientState ( GL_COLOR_ARRAY ) ;
glEnableClientState ( GL_VERTEX_ARRAY ) ;
2024-03-20 19:35:52 +01:00
if ( ( pType ! = scene : : EPT_POINTS ) & & ( pType ! = scene : : EPT_POINT_SPRITES ) )
2023-10-03 20:37:00 +02:00
glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
# ifdef GL_OES_point_size_array
2024-03-20 19:35:52 +01:00
else if ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_point_size_array ] & & ( Material . Thickness = = 0.0f ) )
2023-10-03 20:37:00 +02:00
glEnableClientState ( GL_POINT_SIZE_ARRAY_OES ) ;
# endif
2024-03-20 19:35:52 +01:00
if ( threed & & ( pType ! = scene : : EPT_POINTS ) & & ( pType ! = scene : : EPT_POINT_SPRITES ) )
2023-10-03 20:37:00 +02:00
glEnableClientState ( GL_NORMAL_ARRAY ) ;
if ( vertices )
glColorPointer ( 4 , GL_UNSIGNED_BYTE , 0 , & ColorBuffer [ 0 ] ) ;
2024-03-20 19:35:52 +01:00
switch ( vType ) {
case EVT_STANDARD :
if ( vertices ) {
if ( threed )
glNormalPointer ( GL_FLOAT , sizeof ( S3DVertex ) , & ( static_cast < const S3DVertex * > ( vertices ) ) [ 0 ] . Normal ) ;
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex ) , & ( static_cast < const S3DVertex * > ( vertices ) ) [ 0 ] . TCoords ) ;
glVertexPointer ( ( threed ? 3 : 2 ) , GL_FLOAT , sizeof ( S3DVertex ) , & ( static_cast < const S3DVertex * > ( vertices ) ) [ 0 ] . Pos ) ;
} else {
glNormalPointer ( GL_FLOAT , sizeof ( S3DVertex ) , buffer_offset ( 12 ) ) ;
glColorPointer ( 4 , GL_UNSIGNED_BYTE , sizeof ( S3DVertex ) , buffer_offset ( 24 ) ) ;
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex ) , buffer_offset ( 28 ) ) ;
glVertexPointer ( 3 , GL_FLOAT , sizeof ( S3DVertex ) , 0 ) ;
}
if ( Feature . MaxTextureUnits > 0 & & CacheHandler - > getTextureCache ( ) . get ( 1 ) ) {
glClientActiveTexture ( GL_TEXTURE0 + 1 ) ;
glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
2023-10-03 20:37:00 +02:00
if ( vertices )
2024-03-20 19:35:52 +01:00
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex ) , & ( static_cast < const S3DVertex * > ( vertices ) ) [ 0 ] . TCoords ) ;
2023-10-03 20:37:00 +02:00
else
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex ) , buffer_offset ( 28 ) ) ;
2024-03-20 19:35:52 +01:00
}
break ;
case EVT_2TCOORDS :
if ( vertices ) {
if ( threed )
glNormalPointer ( GL_FLOAT , sizeof ( S3DVertex2TCoords ) , & ( static_cast < const S3DVertex2TCoords * > ( vertices ) ) [ 0 ] . Normal ) ;
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex2TCoords ) , & ( static_cast < const S3DVertex2TCoords * > ( vertices ) ) [ 0 ] . TCoords ) ;
glVertexPointer ( ( threed ? 3 : 2 ) , GL_FLOAT , sizeof ( S3DVertex2TCoords ) , & ( static_cast < const S3DVertex2TCoords * > ( vertices ) ) [ 0 ] . Pos ) ;
} else {
glNormalPointer ( GL_FLOAT , sizeof ( S3DVertex2TCoords ) , buffer_offset ( 12 ) ) ;
glColorPointer ( 4 , GL_UNSIGNED_BYTE , sizeof ( S3DVertex2TCoords ) , buffer_offset ( 24 ) ) ;
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex2TCoords ) , buffer_offset ( 28 ) ) ;
glVertexPointer ( 3 , GL_FLOAT , sizeof ( S3DVertex2TCoords ) , buffer_offset ( 0 ) ) ;
}
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( Feature . MaxTextureUnits > 0 ) {
glClientActiveTexture ( GL_TEXTURE0 + 1 ) ;
glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
2023-10-03 20:37:00 +02:00
if ( vertices )
2024-03-20 19:35:52 +01:00
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex2TCoords ) , & ( static_cast < const S3DVertex2TCoords * > ( vertices ) ) [ 0 ] . TCoords2 ) ;
2023-10-03 20:37:00 +02:00
else
2024-03-20 19:35:52 +01:00
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertex2TCoords ) , buffer_offset ( 36 ) ) ;
}
break ;
case EVT_TANGENTS :
if ( vertices ) {
if ( threed )
glNormalPointer ( GL_FLOAT , sizeof ( S3DVertexTangents ) , & ( static_cast < const S3DVertexTangents * > ( vertices ) ) [ 0 ] . Normal ) ;
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertexTangents ) , & ( static_cast < const S3DVertexTangents * > ( vertices ) ) [ 0 ] . TCoords ) ;
glVertexPointer ( ( threed ? 3 : 2 ) , GL_FLOAT , sizeof ( S3DVertexTangents ) , & ( static_cast < const S3DVertexTangents * > ( vertices ) ) [ 0 ] . Pos ) ;
} else {
glNormalPointer ( GL_FLOAT , sizeof ( S3DVertexTangents ) , buffer_offset ( 12 ) ) ;
glColorPointer ( 4 , GL_UNSIGNED_BYTE , sizeof ( S3DVertexTangents ) , buffer_offset ( 24 ) ) ;
glTexCoordPointer ( 2 , GL_FLOAT , sizeof ( S3DVertexTangents ) , buffer_offset ( 28 ) ) ;
glVertexPointer ( 3 , GL_FLOAT , sizeof ( S3DVertexTangents ) , buffer_offset ( 0 ) ) ;
}
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( Feature . MaxTextureUnits > 0 ) {
glClientActiveTexture ( GL_TEXTURE0 + 1 ) ;
glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
2023-10-03 20:37:00 +02:00
if ( vertices )
2024-03-20 19:35:52 +01:00
glTexCoordPointer ( 3 , GL_FLOAT , sizeof ( S3DVertexTangents ) , & ( static_cast < const S3DVertexTangents * > ( vertices ) ) [ 0 ] . Tangent ) ;
2023-10-03 20:37:00 +02:00
else
2024-03-20 19:35:52 +01:00
glTexCoordPointer ( 3 , GL_FLOAT , sizeof ( S3DVertexTangents ) , buffer_offset ( 36 ) ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
glClientActiveTexture ( GL_TEXTURE0 + 2 ) ;
glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
if ( vertices )
glTexCoordPointer ( 3 , GL_FLOAT , sizeof ( S3DVertexTangents ) , & ( static_cast < const S3DVertexTangents * > ( vertices ) ) [ 0 ] . Binormal ) ;
else
glTexCoordPointer ( 3 , GL_FLOAT , sizeof ( S3DVertexTangents ) , buffer_offset ( 48 ) ) ;
}
break ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
GLenum indexSize = 0 ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
switch ( iType ) {
case ( EIT_16BIT ) : {
indexSize = GL_UNSIGNED_SHORT ;
break ;
}
case ( EIT_32BIT ) : {
2023-10-03 20:37:00 +02:00
# ifdef GL_OES_element_index_uint
# ifndef GL_UNSIGNED_INT
2024-03-20 19:35:52 +01:00
# define GL_UNSIGNED_INT 0x1405
2023-10-03 20:37:00 +02:00
# endif
2024-03-20 19:35:52 +01:00
if ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_element_index_uint ] )
indexSize = GL_UNSIGNED_INT ;
else
2023-10-03 20:37:00 +02:00
# endif
2024-03-20 19:35:52 +01:00
indexSize = GL_UNSIGNED_SHORT ;
break ;
}
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
switch ( pType ) {
case scene : : EPT_POINTS :
case scene : : EPT_POINT_SPRITES : {
2023-10-03 20:37:00 +02:00
# ifdef GL_OES_point_sprite
2024-03-20 19:35:52 +01:00
if ( pType = = scene : : EPT_POINT_SPRITES & & FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_point_sprite ] )
glEnable ( GL_POINT_SPRITE_OES ) ;
2023-10-03 20:37:00 +02:00
# endif
2024-03-20 19:35:52 +01:00
// if ==0 we use the point size array
if ( Material . Thickness ! = 0.f ) {
float quadratic [ ] = { 0.0f , 0.0f , 10.01f } ;
glPointParameterfv ( GL_POINT_DISTANCE_ATTENUATION , quadratic ) ;
float maxParticleSize = 1.0f ;
glGetFloatv ( GL_POINT_SIZE_MAX , & maxParticleSize ) ;
// maxParticleSize=maxParticleSize<Material.Thickness?maxParticleSize:Material.Thickness;
// extGlPointParameterf(GL_POINT_SIZE_MAX,maxParticleSize);
// extGlPointParameterf(GL_POINT_SIZE_MIN,Material.Thickness);
glPointParameterf ( GL_POINT_FADE_THRESHOLD_SIZE , 60.0f ) ;
glPointSize ( Material . Thickness ) ;
}
2023-10-03 20:37:00 +02:00
# ifdef GL_OES_point_sprite
2024-03-20 19:35:52 +01:00
if ( pType = = scene : : EPT_POINT_SPRITES & & FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_point_sprite ] )
glTexEnvf ( GL_POINT_SPRITE_OES , GL_COORD_REPLACE_OES , GL_TRUE ) ;
2023-10-03 20:37:00 +02:00
# endif
2024-03-20 19:35:52 +01:00
glDrawArrays ( GL_POINTS , 0 , primitiveCount ) ;
2023-10-03 20:37:00 +02:00
# ifdef GL_OES_point_sprite
2024-03-20 19:35:52 +01:00
if ( pType = = scene : : EPT_POINT_SPRITES & & FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_point_sprite ] ) {
glDisable ( GL_POINT_SPRITE_OES ) ;
glTexEnvf ( GL_POINT_SPRITE_OES , GL_COORD_REPLACE_OES , GL_FALSE ) ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
# endif
} break ;
case scene : : EPT_LINE_STRIP :
glDrawElements ( GL_LINE_STRIP , primitiveCount + 1 , indexSize , indexList ) ;
break ;
case scene : : EPT_LINE_LOOP :
glDrawElements ( GL_LINE_LOOP , primitiveCount , indexSize , indexList ) ;
break ;
case scene : : EPT_LINES :
glDrawElements ( GL_LINES , primitiveCount * 2 , indexSize , indexList ) ;
break ;
case scene : : EPT_TRIANGLE_STRIP :
glDrawElements ( GL_TRIANGLE_STRIP , primitiveCount + 2 , indexSize , indexList ) ;
break ;
case scene : : EPT_TRIANGLE_FAN :
glDrawElements ( GL_TRIANGLE_FAN , primitiveCount + 2 , indexSize , indexList ) ;
break ;
case scene : : EPT_TRIANGLES :
glDrawElements ( ( LastMaterial . Wireframe ) ? GL_LINES : ( LastMaterial . PointCloud ) ? GL_POINTS
: GL_TRIANGLES ,
primitiveCount * 3 , indexSize , indexList ) ;
break ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
if ( Feature . MaxTextureUnits > 0 ) {
if ( vType = = EVT_TANGENTS ) {
2023-10-03 20:37:00 +02:00
glClientActiveTexture ( GL_TEXTURE0 + 2 ) ;
glDisableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
}
2024-03-20 19:35:52 +01:00
if ( ( vType ! = EVT_STANDARD ) | | CacheHandler - > getTextureCache ( ) . get ( 1 ) ) {
2023-10-03 20:37:00 +02:00
glClientActiveTexture ( GL_TEXTURE0 + 1 ) ;
glDisableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
}
glClientActiveTexture ( GL_TEXTURE0 ) ;
}
# ifdef GL_OES_point_size_array
2024-03-20 19:35:52 +01:00
if ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_point_size_array ] & & ( Material . Thickness = = 0.0f ) )
2023-10-03 20:37:00 +02:00
glDisableClientState ( GL_POINT_SIZE_ARRAY_OES ) ;
# endif
glDisableClientState ( GL_COLOR_ARRAY ) ;
glDisableClientState ( GL_VERTEX_ARRAY ) ;
glDisableClientState ( GL_NORMAL_ARRAY ) ;
glDisableClientState ( GL_TEXTURE_COORD_ARRAY ) ;
}
//! draws a 2d image, using a color and the alpha channel of the texture
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw2DImage ( const video : : ITexture * texture ,
const core : : position2d < s32 > & pos ,
const core : : rect < s32 > & sourceRect ,
const core : : rect < s32 > * clipRect , SColor color ,
bool useAlphaChannelOfTexture )
2023-10-03 20:37:00 +02:00
{
if ( ! texture )
return ;
if ( ! sourceRect . isValid ( ) )
return ;
core : : position2d < s32 > targetPos ( pos ) ;
core : : position2d < s32 > sourcePos ( sourceRect . UpperLeftCorner ) ;
core : : dimension2d < s32 > sourceSize ( sourceRect . getSize ( ) ) ;
2024-03-20 19:35:52 +01:00
if ( clipRect ) {
if ( targetPos . X < clipRect - > UpperLeftCorner . X ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width + = targetPos . X - clipRect - > UpperLeftCorner . X ;
if ( sourceSize . Width < = 0 )
return ;
sourcePos . X - = targetPos . X - clipRect - > UpperLeftCorner . X ;
targetPos . X = clipRect - > UpperLeftCorner . X ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . X + sourceSize . Width > clipRect - > LowerRightCorner . X ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width - = ( targetPos . X + sourceSize . Width ) - clipRect - > LowerRightCorner . X ;
if ( sourceSize . Width < = 0 )
return ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y < clipRect - > UpperLeftCorner . Y ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height + = targetPos . Y - clipRect - > UpperLeftCorner . Y ;
if ( sourceSize . Height < = 0 )
return ;
sourcePos . Y - = targetPos . Y - clipRect - > UpperLeftCorner . Y ;
targetPos . Y = clipRect - > UpperLeftCorner . Y ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y + sourceSize . Height > clipRect - > LowerRightCorner . Y ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height - = ( targetPos . Y + sourceSize . Height ) - clipRect - > LowerRightCorner . Y ;
if ( sourceSize . Height < = 0 )
return ;
}
}
// clip these coordinates
2024-03-20 19:35:52 +01:00
if ( targetPos . X < 0 ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width + = targetPos . X ;
if ( sourceSize . Width < = 0 )
return ;
sourcePos . X - = targetPos . X ;
targetPos . X = 0 ;
}
2024-03-20 19:35:52 +01:00
const core : : dimension2d < u32 > & renderTargetSize = getCurrentRenderTargetSize ( ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( targetPos . X + sourceSize . Width > ( s32 ) renderTargetSize . Width ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width - = ( targetPos . X + sourceSize . Width ) - renderTargetSize . Width ;
if ( sourceSize . Width < = 0 )
return ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y < 0 ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height + = targetPos . Y ;
if ( sourceSize . Height < = 0 )
return ;
sourcePos . Y - = targetPos . Y ;
targetPos . Y = 0 ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y + sourceSize . Height > ( s32 ) renderTargetSize . Height ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height - = ( targetPos . Y + sourceSize . Height ) - renderTargetSize . Height ;
if ( sourceSize . Height < = 0 )
return ;
}
// ok, we've clipped everything.
// now draw it.
// texcoords need to be flipped horizontally for RTTs
const bool isRTT = texture - > isRenderTarget ( ) ;
2024-03-20 19:35:52 +01:00
const core : : dimension2d < u32 > & ss = texture - > getOriginalSize ( ) ;
2023-10-03 20:37:00 +02:00
const f32 invW = 1.f / static_cast < f32 > ( ss . Width ) ;
const f32 invH = 1.f / static_cast < f32 > ( ss . Height ) ;
const core : : rect < f32 > tcoords (
sourcePos . X * invW ,
2024-03-20 19:35:52 +01:00
( isRTT ? ( sourcePos . Y + sourceSize . Height ) : sourcePos . Y ) * invH ,
2023-10-03 20:37:00 +02:00
( sourcePos . X + sourceSize . Width ) * invW ,
2024-03-20 19:35:52 +01:00
( isRTT ? sourcePos . Y : ( sourcePos . Y + sourceSize . Height ) ) * invH ) ;
2023-10-03 20:37:00 +02:00
const core : : rect < s32 > poss ( targetPos , sourceSize ) ;
if ( ! CacheHandler - > getTextureCache ( ) . set ( 0 , texture ) )
return ;
2024-03-20 19:35:52 +01:00
setRenderStates2DMode ( color . getAlpha ( ) < 255 , true , useAlphaChannelOfTexture ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
u16 indices [ ] = { 0 , 1 , 2 , 3 } ;
2023-10-03 20:37:00 +02:00
S3DVertex vertices [ 4 ] ;
2024-03-20 19:35:52 +01:00
vertices [ 0 ] = S3DVertex ( ( f32 ) poss . UpperLeftCorner . X , ( f32 ) poss . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . UpperLeftCorner . X , tcoords . UpperLeftCorner . Y ) ;
vertices [ 1 ] = S3DVertex ( ( f32 ) poss . LowerRightCorner . X , ( f32 ) poss . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . LowerRightCorner . X , tcoords . UpperLeftCorner . Y ) ;
vertices [ 2 ] = S3DVertex ( ( f32 ) poss . LowerRightCorner . X , ( f32 ) poss . LowerRightCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . LowerRightCorner . X , tcoords . LowerRightCorner . Y ) ;
vertices [ 3 ] = S3DVertex ( ( f32 ) poss . UpperLeftCorner . X , ( f32 ) poss . LowerRightCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . UpperLeftCorner . X , tcoords . LowerRightCorner . Y ) ;
2023-10-03 20:37:00 +02:00
drawVertexPrimitiveList2d3d ( vertices , 4 , indices , 2 , video : : EVT_STANDARD , scene : : EPT_TRIANGLE_FAN , EIT_16BIT , false ) ;
}
//! The same, but with a four element array of colors, one for each vertex
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw2DImage ( const video : : ITexture * texture , const core : : rect < s32 > & destRect ,
const core : : rect < s32 > & sourceRect , const core : : rect < s32 > * clipRect ,
const video : : SColor * const colors , bool useAlphaChannelOfTexture )
2023-10-03 20:37:00 +02:00
{
if ( ! texture )
return ;
// texcoords need to be flipped horizontally for RTTs
const bool isRTT = texture - > isRenderTarget ( ) ;
2024-03-20 19:35:52 +01:00
const core : : dimension2du & ss = texture - > getOriginalSize ( ) ;
2023-10-03 20:37:00 +02:00
const f32 invW = 1.f / static_cast < f32 > ( ss . Width ) ;
const f32 invH = 1.f / static_cast < f32 > ( ss . Height ) ;
const core : : rect < f32 > tcoords (
sourceRect . UpperLeftCorner . X * invW ,
2024-03-20 19:35:52 +01:00
( isRTT ? sourceRect . LowerRightCorner . Y : sourceRect . UpperLeftCorner . Y ) * invH ,
2023-10-03 20:37:00 +02:00
sourceRect . LowerRightCorner . X * invW ,
2024-03-20 19:35:52 +01:00
( isRTT ? sourceRect . UpperLeftCorner . Y : sourceRect . LowerRightCorner . Y ) * invH ) ;
2023-10-03 20:37:00 +02:00
2024-03-21 15:22:20 +01:00
const video : : SColor temp [ 4 ] = {
0xFFFFFFFF ,
0xFFFFFFFF ,
0xFFFFFFFF ,
0xFFFFFFFF ,
} ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
const video : : SColor * const useColor = colors ? colors : temp ;
2023-10-03 20:37:00 +02:00
if ( ! CacheHandler - > getTextureCache ( ) . set ( 0 , texture ) )
return ;
2024-03-20 19:35:52 +01:00
setRenderStates2DMode ( useColor [ 0 ] . getAlpha ( ) < 255 | | useColor [ 1 ] . getAlpha ( ) < 255 | |
useColor [ 2 ] . getAlpha ( ) < 255 | | useColor [ 3 ] . getAlpha ( ) < 255 ,
2023-10-03 20:37:00 +02:00
true , useAlphaChannelOfTexture ) ;
2024-03-20 19:35:52 +01:00
if ( clipRect ) {
2023-10-03 20:37:00 +02:00
if ( ! clipRect - > isValid ( ) )
return ;
glEnable ( GL_SCISSOR_TEST ) ;
2024-03-20 19:35:52 +01:00
const core : : dimension2d < u32 > & renderTargetSize = getCurrentRenderTargetSize ( ) ;
glScissor ( clipRect - > UpperLeftCorner . X , renderTargetSize . Height - clipRect - > LowerRightCorner . Y ,
clipRect - > getWidth ( ) , clipRect - > getHeight ( ) ) ;
2023-10-03 20:37:00 +02:00
}
2024-03-20 19:35:52 +01:00
u16 indices [ ] = { 0 , 1 , 2 , 3 } ;
2023-10-03 20:37:00 +02:00
S3DVertex vertices [ 4 ] ;
2024-03-20 19:35:52 +01:00
vertices [ 0 ] = S3DVertex ( ( f32 ) destRect . UpperLeftCorner . X , ( f32 ) destRect . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , useColor [ 0 ] , tcoords . UpperLeftCorner . X , tcoords . UpperLeftCorner . Y ) ;
vertices [ 1 ] = S3DVertex ( ( f32 ) destRect . LowerRightCorner . X , ( f32 ) destRect . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , useColor [ 3 ] , tcoords . LowerRightCorner . X , tcoords . UpperLeftCorner . Y ) ;
vertices [ 2 ] = S3DVertex ( ( f32 ) destRect . LowerRightCorner . X , ( f32 ) destRect . LowerRightCorner . Y , 0 , 0 , 0 , 1 , useColor [ 2 ] , tcoords . LowerRightCorner . X , tcoords . LowerRightCorner . Y ) ;
vertices [ 3 ] = S3DVertex ( ( f32 ) destRect . UpperLeftCorner . X , ( f32 ) destRect . LowerRightCorner . Y , 0 , 0 , 0 , 1 , useColor [ 1 ] , tcoords . UpperLeftCorner . X , tcoords . LowerRightCorner . Y ) ;
2023-10-03 20:37:00 +02:00
drawVertexPrimitiveList2d3d ( vertices , 4 , indices , 2 , video : : EVT_STANDARD , scene : : EPT_TRIANGLE_FAN , EIT_16BIT , false ) ;
if ( clipRect )
glDisable ( GL_SCISSOR_TEST ) ;
}
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw2DImage ( const video : : ITexture * texture , u32 layer , bool flip )
2023-10-03 20:37:00 +02:00
{
if ( ! texture | | ! CacheHandler - > getTextureCache ( ) . set ( 0 , texture ) )
return ;
setRenderStates2DMode ( false , true , true ) ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity ( ) ;
glMatrixMode ( GL_MODELVIEW ) ;
glLoadIdentity ( ) ;
Transformation3DChanged = true ;
2024-03-20 19:35:52 +01:00
u16 indices [ ] = { 0 , 1 , 2 , 3 } ;
2023-10-03 20:37:00 +02:00
S3DVertex vertices [ 4 ] ;
vertices [ 0 ] . Pos = core : : vector3df ( - 1.f , 1.f , 0.f ) ;
vertices [ 1 ] . Pos = core : : vector3df ( 1.f , 1.f , 0.f ) ;
vertices [ 2 ] . Pos = core : : vector3df ( 1.f , - 1.f , 0.f ) ;
vertices [ 3 ] . Pos = core : : vector3df ( - 1.f , - 1.f , 0.f ) ;
f32 modificator = ( flip ) ? 1.f : 0.f ;
vertices [ 0 ] . TCoords = core : : vector2df ( 0.f , 0.f + modificator ) ;
vertices [ 1 ] . TCoords = core : : vector2df ( 1.f , 0.f + modificator ) ;
vertices [ 2 ] . TCoords = core : : vector2df ( 1.f , 1.f - modificator ) ;
vertices [ 3 ] . TCoords = core : : vector2df ( 0.f , 1.f - modificator ) ;
vertices [ 0 ] . Color = 0xFFFFFFFF ;
vertices [ 1 ] . Color = 0xFFFFFFFF ;
vertices [ 2 ] . Color = 0xFFFFFFFF ;
vertices [ 3 ] . Color = 0xFFFFFFFF ;
drawVertexPrimitiveList2d3d ( vertices , 4 , indices , 2 , video : : EVT_STANDARD , scene : : EPT_TRIANGLE_FAN , EIT_16BIT , false ) ;
}
//! draws a set of 2d images, using a color and the alpha channel of the texture if desired.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw2DImageBatch ( const video : : ITexture * texture ,
const core : : array < core : : position2d < s32 > > & positions ,
const core : : array < core : : rect < s32 > > & sourceRects ,
const core : : rect < s32 > * clipRect ,
SColor color ,
bool useAlphaChannelOfTexture )
2023-10-03 20:37:00 +02:00
{
if ( ! texture )
return ;
const u32 drawCount = core : : min_ < u32 > ( positions . size ( ) , sourceRects . size ( ) ) ;
if ( ! drawCount )
return ;
2024-03-20 19:35:52 +01:00
const core : : dimension2d < u32 > & ss = texture - > getOriginalSize ( ) ;
2023-10-03 20:37:00 +02:00
if ( ! ss . Width | | ! ss . Height )
return ;
const f32 invW = 1.f / static_cast < f32 > ( ss . Width ) ;
const f32 invH = 1.f / static_cast < f32 > ( ss . Height ) ;
2024-03-20 19:35:52 +01:00
const core : : dimension2d < u32 > & renderTargetSize = getCurrentRenderTargetSize ( ) ;
2023-10-03 20:37:00 +02:00
if ( ! CacheHandler - > getTextureCache ( ) . set ( 0 , texture ) )
return ;
2024-03-20 19:35:52 +01:00
setRenderStates2DMode ( color . getAlpha ( ) < 255 , true , useAlphaChannelOfTexture ) ;
2023-10-03 20:37:00 +02:00
core : : array < S3DVertex > vertices ;
core : : array < u16 > quadIndices ;
2024-03-20 19:35:52 +01:00
vertices . reallocate ( drawCount * 4 ) ;
quadIndices . reallocate ( drawCount * 6 ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
for ( u32 i = 0 ; i < drawCount ; + + i ) {
2023-10-03 20:37:00 +02:00
if ( ! sourceRects [ i ] . isValid ( ) )
continue ;
core : : position2d < s32 > targetPos ( positions [ i ] ) ;
core : : position2d < s32 > sourcePos ( sourceRects [ i ] . UpperLeftCorner ) ;
// This needs to be signed as it may go negative.
core : : dimension2d < s32 > sourceSize ( sourceRects [ i ] . getSize ( ) ) ;
2024-03-20 19:35:52 +01:00
if ( clipRect ) {
if ( targetPos . X < clipRect - > UpperLeftCorner . X ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width + = targetPos . X - clipRect - > UpperLeftCorner . X ;
if ( sourceSize . Width < = 0 )
continue ;
sourcePos . X - = targetPos . X - clipRect - > UpperLeftCorner . X ;
targetPos . X = clipRect - > UpperLeftCorner . X ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . X + sourceSize . Width > clipRect - > LowerRightCorner . X ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width - = ( targetPos . X + sourceSize . Width ) - clipRect - > LowerRightCorner . X ;
if ( sourceSize . Width < = 0 )
continue ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y < clipRect - > UpperLeftCorner . Y ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height + = targetPos . Y - clipRect - > UpperLeftCorner . Y ;
if ( sourceSize . Height < = 0 )
continue ;
sourcePos . Y - = targetPos . Y - clipRect - > UpperLeftCorner . Y ;
targetPos . Y = clipRect - > UpperLeftCorner . Y ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y + sourceSize . Height > clipRect - > LowerRightCorner . Y ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height - = ( targetPos . Y + sourceSize . Height ) - clipRect - > LowerRightCorner . Y ;
if ( sourceSize . Height < = 0 )
continue ;
}
}
// clip these coordinates
2024-03-20 19:35:52 +01:00
if ( targetPos . X < 0 ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width + = targetPos . X ;
if ( sourceSize . Width < = 0 )
continue ;
sourcePos . X - = targetPos . X ;
targetPos . X = 0 ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . X + sourceSize . Width > ( s32 ) renderTargetSize . Width ) {
2023-10-03 20:37:00 +02:00
sourceSize . Width - = ( targetPos . X + sourceSize . Width ) - renderTargetSize . Width ;
if ( sourceSize . Width < = 0 )
continue ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y < 0 ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height + = targetPos . Y ;
if ( sourceSize . Height < = 0 )
continue ;
sourcePos . Y - = targetPos . Y ;
targetPos . Y = 0 ;
}
2024-03-20 19:35:52 +01:00
if ( targetPos . Y + sourceSize . Height > ( s32 ) renderTargetSize . Height ) {
2023-10-03 20:37:00 +02:00
sourceSize . Height - = ( targetPos . Y + sourceSize . Height ) - renderTargetSize . Height ;
if ( sourceSize . Height < = 0 )
continue ;
}
// ok, we've clipped everything.
const core : : rect < f32 > tcoords (
sourcePos . X * invW ,
sourcePos . Y * invH ,
( sourcePos . X + sourceSize . Width ) * invW ,
( sourcePos . Y + sourceSize . Height ) * invH ) ;
const core : : rect < s32 > poss ( targetPos , sourceSize ) ;
const u32 vstart = vertices . size ( ) ;
2024-03-20 19:35:52 +01:00
vertices . push_back ( S3DVertex ( ( f32 ) poss . UpperLeftCorner . X , ( f32 ) poss . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . UpperLeftCorner . X , tcoords . UpperLeftCorner . Y ) ) ;
vertices . push_back ( S3DVertex ( ( f32 ) poss . LowerRightCorner . X , ( f32 ) poss . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . LowerRightCorner . X , tcoords . UpperLeftCorner . Y ) ) ;
vertices . push_back ( S3DVertex ( ( f32 ) poss . LowerRightCorner . X , ( f32 ) poss . LowerRightCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . LowerRightCorner . X , tcoords . LowerRightCorner . Y ) ) ;
vertices . push_back ( S3DVertex ( ( f32 ) poss . UpperLeftCorner . X , ( f32 ) poss . LowerRightCorner . Y , 0 , 0 , 0 , 1 , color , tcoords . UpperLeftCorner . X , tcoords . LowerRightCorner . Y ) ) ;
2023-10-03 20:37:00 +02:00
quadIndices . push_back ( vstart ) ;
2024-03-20 19:35:52 +01:00
quadIndices . push_back ( vstart + 1 ) ;
quadIndices . push_back ( vstart + 2 ) ;
2023-10-03 20:37:00 +02:00
quadIndices . push_back ( vstart ) ;
2024-03-20 19:35:52 +01:00
quadIndices . push_back ( vstart + 2 ) ;
quadIndices . push_back ( vstart + 3 ) ;
2023-10-03 20:37:00 +02:00
}
if ( vertices . size ( ) )
drawVertexPrimitiveList2d3d ( vertices . pointer ( ) , vertices . size ( ) ,
2024-03-20 19:35:52 +01:00
quadIndices . pointer ( ) , vertices . size ( ) / 2 ,
2023-10-03 20:37:00 +02:00
video : : EVT_STANDARD , scene : : EPT_TRIANGLES ,
EIT_16BIT , false ) ;
}
//! draw a 2d rectangle
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw2DRectangle ( SColor color , const core : : rect < s32 > & position ,
const core : : rect < s32 > * clip )
2023-10-03 20:37:00 +02:00
{
setRenderStates2DMode ( color . getAlpha ( ) < 255 , false , false ) ;
core : : rect < s32 > pos = position ;
if ( clip )
pos . clipAgainst ( * clip ) ;
if ( ! pos . isValid ( ) )
return ;
2024-03-20 19:35:52 +01:00
u16 indices [ ] = { 0 , 1 , 2 , 3 } ;
2023-10-03 20:37:00 +02:00
S3DVertex vertices [ 4 ] ;
2024-03-20 19:35:52 +01:00
vertices [ 0 ] = S3DVertex ( ( f32 ) pos . UpperLeftCorner . X , ( f32 ) pos . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , color , 0 , 0 ) ;
vertices [ 1 ] = S3DVertex ( ( f32 ) pos . LowerRightCorner . X , ( f32 ) pos . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , color , 0 , 0 ) ;
vertices [ 2 ] = S3DVertex ( ( f32 ) pos . LowerRightCorner . X , ( f32 ) pos . LowerRightCorner . Y , 0 , 0 , 0 , 1 , color , 0 , 0 ) ;
vertices [ 3 ] = S3DVertex ( ( f32 ) pos . UpperLeftCorner . X , ( f32 ) pos . LowerRightCorner . Y , 0 , 0 , 0 , 1 , color , 0 , 0 ) ;
2023-10-03 20:37:00 +02:00
drawVertexPrimitiveList2d3d ( vertices , 4 , indices , 2 , video : : EVT_STANDARD , scene : : EPT_TRIANGLE_FAN , EIT_16BIT , false ) ;
}
//! draw an 2d rectangle
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw2DRectangle ( const core : : rect < s32 > & position ,
SColor colorLeftUp , SColor colorRightUp , SColor colorLeftDown , SColor colorRightDown ,
const core : : rect < s32 > * clip )
2023-10-03 20:37:00 +02:00
{
core : : rect < s32 > pos = position ;
if ( clip )
pos . clipAgainst ( * clip ) ;
if ( ! pos . isValid ( ) )
return ;
setRenderStates2DMode ( colorLeftUp . getAlpha ( ) < 255 | |
2024-03-20 19:35:52 +01:00
colorRightUp . getAlpha ( ) < 255 | |
colorLeftDown . getAlpha ( ) < 255 | |
colorRightDown . getAlpha ( ) < 255 ,
false , false ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
u16 indices [ ] = { 0 , 1 , 2 , 3 } ;
2023-10-03 20:37:00 +02:00
S3DVertex vertices [ 4 ] ;
2024-03-20 19:35:52 +01:00
vertices [ 0 ] = S3DVertex ( ( f32 ) pos . UpperLeftCorner . X , ( f32 ) pos . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , colorLeftUp , 0 , 0 ) ;
vertices [ 1 ] = S3DVertex ( ( f32 ) pos . LowerRightCorner . X , ( f32 ) pos . UpperLeftCorner . Y , 0 , 0 , 0 , 1 , colorRightUp , 0 , 0 ) ;
vertices [ 2 ] = S3DVertex ( ( f32 ) pos . LowerRightCorner . X , ( f32 ) pos . LowerRightCorner . Y , 0 , 0 , 0 , 1 , colorRightDown , 0 , 0 ) ;
vertices [ 3 ] = S3DVertex ( ( f32 ) pos . UpperLeftCorner . X , ( f32 ) pos . LowerRightCorner . Y , 0 , 0 , 0 , 1 , colorLeftDown , 0 , 0 ) ;
2023-10-03 20:37:00 +02:00
drawVertexPrimitiveList2d3d ( vertices , 4 , indices , 2 , video : : EVT_STANDARD , scene : : EPT_TRIANGLE_FAN , EIT_16BIT , false ) ;
}
//! Draws a 2d line.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw2DLine ( const core : : position2d < s32 > & start ,
const core : : position2d < s32 > & end ,
SColor color )
2023-10-03 20:37:00 +02:00
{
setRenderStates2DMode ( color . getAlpha ( ) < 255 , false , false ) ;
2024-03-20 19:35:52 +01:00
u16 indices [ ] = { 0 , 1 } ;
2023-10-03 20:37:00 +02:00
S3DVertex vertices [ 2 ] ;
2024-03-20 19:35:52 +01:00
vertices [ 0 ] = S3DVertex ( ( f32 ) start . X , ( f32 ) start . Y , 0 , 0 , 0 , 1 , color , 0 , 0 ) ;
vertices [ 1 ] = S3DVertex ( ( f32 ) end . X , ( f32 ) end . Y , 0 , 0 , 0 , 1 , color , 1 , 1 ) ;
2023-10-03 20:37:00 +02:00
drawVertexPrimitiveList2d3d ( vertices , 2 , indices , 1 , video : : EVT_STANDARD , scene : : EPT_LINES , EIT_16BIT , false ) ;
}
//! creates a matrix in supplied GLfloat array to pass to OGLES1
2024-03-20 19:35:52 +01:00
inline void COGLES1Driver : : getGLMatrix ( GLfloat gl_matrix [ 16 ] , const core : : matrix4 & m )
2023-10-03 20:37:00 +02:00
{
memcpy ( gl_matrix , m . pointer ( ) , 16 * sizeof ( f32 ) ) ;
}
//! creates a opengltexturematrix from a D3D style texture matrix
2024-03-20 19:35:52 +01:00
inline void COGLES1Driver : : getGLTextureMatrix ( GLfloat * o , const core : : matrix4 & m )
2023-10-03 20:37:00 +02:00
{
o [ 0 ] = m [ 0 ] ;
o [ 1 ] = m [ 1 ] ;
o [ 2 ] = 0.f ;
o [ 3 ] = 0.f ;
o [ 4 ] = m [ 4 ] ;
o [ 5 ] = m [ 5 ] ;
o [ 6 ] = 0.f ;
o [ 7 ] = 0.f ;
o [ 8 ] = 0.f ;
o [ 9 ] = 0.f ;
o [ 10 ] = 1.f ;
o [ 11 ] = 0.f ;
o [ 12 ] = m [ 8 ] ;
o [ 13 ] = m [ 9 ] ;
o [ 14 ] = 0.f ;
o [ 15 ] = 1.f ;
}
2024-03-20 19:35:52 +01:00
ITexture * COGLES1Driver : : createDeviceDependentTexture ( const io : : path & name , IImage * image )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
core : : array < IImage * > imageArray ( 1 ) ;
2023-10-03 20:37:00 +02:00
imageArray . push_back ( image ) ;
2024-03-20 19:35:52 +01:00
COGLES1Texture * texture = new COGLES1Texture ( name , imageArray , ETT_2D , this ) ;
2023-10-03 20:37:00 +02:00
return texture ;
}
2024-03-20 19:35:52 +01:00
ITexture * COGLES1Driver : : createDeviceDependentTextureCubemap ( const io : : path & name , const core : : array < IImage * > & image )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
COGLES1Texture * texture = new COGLES1Texture ( name , image , ETT_CUBEMAP , this ) ;
2023-10-03 20:37:00 +02:00
return texture ;
}
//! Sets a material. All 3d drawing functions draw geometry now using this material.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : setMaterial ( const SMaterial & material )
2023-10-03 20:37:00 +02:00
{
Material = material ;
OverrideMaterial . apply ( Material ) ;
for ( u32 i = 0 ; i < Feature . MaxTextureUnits ; + + i )
setTransform ( ( E_TRANSFORMATION_STATE ) ( ETS_TEXTURE_0 + i ) , material . getTextureMatrix ( i ) ) ;
}
//! prints error if an error happened.
bool COGLES1Driver : : testGLError ( int code )
{
# ifdef _DEBUG
GLenum g = glGetError ( ) ;
2024-03-20 19:35:52 +01:00
switch ( g ) {
2023-10-03 20:37:00 +02:00
case GL_NO_ERROR :
return false ;
case GL_INVALID_ENUM :
2024-03-20 19:35:52 +01:00
os : : Printer : : log ( " GL_INVALID_ENUM " , core : : stringc ( code ) . c_str ( ) , ELL_ERROR ) ;
break ;
2023-10-03 20:37:00 +02:00
case GL_INVALID_VALUE :
2024-03-20 19:35:52 +01:00
os : : Printer : : log ( " GL_INVALID_VALUE " , core : : stringc ( code ) . c_str ( ) , ELL_ERROR ) ;
break ;
2023-10-03 20:37:00 +02:00
case GL_INVALID_OPERATION :
2024-03-20 19:35:52 +01:00
os : : Printer : : log ( " GL_INVALID_OPERATION " , core : : stringc ( code ) . c_str ( ) , ELL_ERROR ) ;
break ;
2023-10-03 20:37:00 +02:00
case GL_STACK_OVERFLOW :
2024-03-20 19:35:52 +01:00
os : : Printer : : log ( " GL_STACK_OVERFLOW " , core : : stringc ( code ) . c_str ( ) , ELL_ERROR ) ;
break ;
2023-10-03 20:37:00 +02:00
case GL_STACK_UNDERFLOW :
2024-03-20 19:35:52 +01:00
os : : Printer : : log ( " GL_STACK_UNDERFLOW " , core : : stringc ( code ) . c_str ( ) , ELL_ERROR ) ;
break ;
2023-10-03 20:37:00 +02:00
case GL_OUT_OF_MEMORY :
2024-03-20 19:35:52 +01:00
os : : Printer : : log ( " GL_OUT_OF_MEMORY " , core : : stringc ( code ) . c_str ( ) , ELL_ERROR ) ;
break ;
2023-10-03 20:37:00 +02:00
} ;
2024-03-20 19:35:52 +01:00
// _IRR_DEBUG_BREAK_IF(true);
2023-10-03 20:37:00 +02:00
return true ;
# else
return false ;
# endif
}
//! sets the needed renderstates
void COGLES1Driver : : setRenderStates3DMode ( )
{
2024-03-20 19:35:52 +01:00
if ( CurrentRenderMode ! = ERM_3D ) {
2023-10-03 20:37:00 +02:00
// Reset Texture Stages
CacheHandler - > setBlend ( false ) ;
glDisable ( GL_ALPHA_TEST ) ;
CacheHandler - > setBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ) ;
// switch back the matrices
glMatrixMode ( GL_MODELVIEW ) ;
glLoadMatrixf ( ( Matrices [ ETS_VIEW ] * Matrices [ ETS_WORLD ] ) . pointer ( ) ) ;
GLfloat glmat [ 16 ] ;
getGLMatrix ( glmat , Matrices [ ETS_PROJECTION ] ) ;
glmat [ 12 ] * = - 1.0f ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadMatrixf ( glmat ) ;
ResetRenderStates = true ;
}
2024-03-20 19:35:52 +01:00
if ( ResetRenderStates | | LastMaterial ! = Material ) {
2023-10-03 20:37:00 +02:00
// unset old material
if ( LastMaterial . MaterialType ! = Material . MaterialType & &
static_cast < u32 > ( LastMaterial . MaterialType ) < MaterialRenderers . size ( ) )
MaterialRenderers [ LastMaterial . MaterialType ] . Renderer - > OnUnsetMaterial ( ) ;
// set new material.
if ( static_cast < u32 > ( Material . MaterialType ) < MaterialRenderers . size ( ) )
MaterialRenderers [ Material . MaterialType ] . Renderer - > OnSetMaterial (
2024-03-20 19:35:52 +01:00
Material , LastMaterial , ResetRenderStates , this ) ;
2023-10-03 20:37:00 +02:00
LastMaterial = Material ;
CacheHandler - > correctCacheMaterial ( LastMaterial ) ;
ResetRenderStates = false ;
}
if ( static_cast < u32 > ( Material . MaterialType ) < MaterialRenderers . size ( ) )
MaterialRenderers [ Material . MaterialType ] . Renderer - > OnRender ( this , video : : EVT_STANDARD ) ;
CurrentRenderMode = ERM_3D ;
}
GLint COGLES1Driver : : getTextureWrapMode ( u8 clamp ) const
{
2024-03-20 19:35:52 +01:00
switch ( clamp ) {
case ETC_CLAMP :
// return GL_CLAMP; not supported in ogl-es
return GL_CLAMP_TO_EDGE ;
break ;
case ETC_CLAMP_TO_EDGE :
return GL_CLAMP_TO_EDGE ;
break ;
case ETC_CLAMP_TO_BORDER :
// return GL_CLAMP_TO_BORDER; not supported in ogl-es
return GL_CLAMP_TO_EDGE ;
break ;
case ETC_MIRROR :
2023-10-03 20:37:00 +02:00
# ifdef GL_OES_texture_mirrored_repeat
2024-03-20 19:35:52 +01:00
if ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_texture_mirrored_repeat ] )
return GL_MIRRORED_REPEAT_OES ;
else
2023-10-03 20:37:00 +02:00
# endif
return GL_REPEAT ;
2024-03-20 19:35:52 +01:00
break ;
// the next three are not yet supported at all
case ETC_MIRROR_CLAMP :
case ETC_MIRROR_CLAMP_TO_EDGE :
case ETC_MIRROR_CLAMP_TO_BORDER :
2023-10-03 20:37:00 +02:00
# ifdef GL_OES_texture_mirrored_repeat
2024-03-20 19:35:52 +01:00
if ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_texture_mirrored_repeat ] )
return GL_MIRRORED_REPEAT_OES ;
else
2023-10-03 20:37:00 +02:00
# endif
return GL_CLAMP_TO_EDGE ;
2024-03-20 19:35:52 +01:00
break ;
case ETC_REPEAT :
default :
return GL_REPEAT ;
break ;
2023-10-03 20:37:00 +02:00
}
}
//! Can be called by an IMaterialRenderer to make its work easier.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : setBasicRenderStates ( const SMaterial & material , const SMaterial & lastmaterial ,
bool resetAllRenderStates )
2023-10-03 20:37:00 +02:00
{
if ( resetAllRenderStates | |
2024-03-20 19:35:52 +01:00
lastmaterial . ColorMaterial ! = material . ColorMaterial ) {
2023-10-03 20:37:00 +02:00
// we only have diffuse_and_ambient in ogl-es
if ( material . ColorMaterial = = ECM_DIFFUSE_AND_AMBIENT )
glEnable ( GL_COLOR_MATERIAL ) ;
else
glDisable ( GL_COLOR_MATERIAL ) ;
}
if ( resetAllRenderStates | |
2024-03-20 19:35:52 +01:00
lastmaterial . AmbientColor ! = material . AmbientColor | |
lastmaterial . DiffuseColor ! = material . DiffuseColor | |
lastmaterial . EmissiveColor ! = material . EmissiveColor | |
lastmaterial . ColorMaterial ! = material . ColorMaterial ) {
2023-10-03 20:37:00 +02:00
GLfloat color [ 4 ] ;
const f32 inv = 1.0f / 255.0f ;
if ( ( material . ColorMaterial ! = video : : ECM_AMBIENT ) & &
2024-03-20 19:35:52 +01:00
( material . ColorMaterial ! = video : : ECM_DIFFUSE_AND_AMBIENT ) ) {
2023-10-03 20:37:00 +02:00
color [ 0 ] = material . AmbientColor . getRed ( ) * inv ;
color [ 1 ] = material . AmbientColor . getGreen ( ) * inv ;
color [ 2 ] = material . AmbientColor . getBlue ( ) * inv ;
color [ 3 ] = material . AmbientColor . getAlpha ( ) * inv ;
glMaterialfv ( GL_FRONT_AND_BACK , GL_AMBIENT , color ) ;
}
if ( ( material . ColorMaterial ! = video : : ECM_DIFFUSE ) & &
2024-03-20 19:35:52 +01:00
( material . ColorMaterial ! = video : : ECM_DIFFUSE_AND_AMBIENT ) ) {
2023-10-03 20:37:00 +02:00
color [ 0 ] = material . DiffuseColor . getRed ( ) * inv ;
color [ 1 ] = material . DiffuseColor . getGreen ( ) * inv ;
color [ 2 ] = material . DiffuseColor . getBlue ( ) * inv ;
color [ 3 ] = material . DiffuseColor . getAlpha ( ) * inv ;
glMaterialfv ( GL_FRONT_AND_BACK , GL_DIFFUSE , color ) ;
}
2024-03-20 19:35:52 +01:00
if ( material . ColorMaterial ! = video : : ECM_EMISSIVE ) {
2023-10-03 20:37:00 +02:00
color [ 0 ] = material . EmissiveColor . getRed ( ) * inv ;
color [ 1 ] = material . EmissiveColor . getGreen ( ) * inv ;
color [ 2 ] = material . EmissiveColor . getBlue ( ) * inv ;
color [ 3 ] = material . EmissiveColor . getAlpha ( ) * inv ;
glMaterialfv ( GL_FRONT_AND_BACK , GL_EMISSION , color ) ;
}
}
if ( resetAllRenderStates | |
2024-03-20 19:35:52 +01:00
lastmaterial . SpecularColor ! = material . SpecularColor | |
lastmaterial . Shininess ! = material . Shininess ) {
GLfloat color [ ] = { 0.f , 0.f , 0.f , 1.f } ;
2023-10-03 20:37:00 +02:00
const f32 inv = 1.0f / 255.0f ;
// disable Specular colors if no shininess is set
if ( ( material . Shininess ! = 0.0f ) & &
2024-03-20 19:35:52 +01:00
( material . ColorMaterial ! = video : : ECM_SPECULAR ) ) {
2023-10-03 20:37:00 +02:00
# ifdef GL_EXT_separate_specular_color
if ( FeatureAvailable [ IRR_EXT_separate_specular_color ] )
glLightModeli ( GL_LIGHT_MODEL_COLOR_CONTROL , GL_SEPARATE_SPECULAR_COLOR ) ;
# endif
glMaterialf ( GL_FRONT_AND_BACK , GL_SHININESS , material . Shininess ) ;
color [ 0 ] = material . SpecularColor . getRed ( ) * inv ;
color [ 1 ] = material . SpecularColor . getGreen ( ) * inv ;
color [ 2 ] = material . SpecularColor . getBlue ( ) * inv ;
color [ 3 ] = material . SpecularColor . getAlpha ( ) * inv ;
glMaterialfv ( GL_FRONT_AND_BACK , GL_SPECULAR , color ) ;
}
# ifdef GL_EXT_separate_specular_color
2024-03-20 19:35:52 +01:00
else if ( FeatureAvailable [ IRR_EXT_separate_specular_color ] )
glLightModeli ( GL_LIGHT_MODEL_COLOR_CONTROL , GL_SINGLE_COLOR ) ;
2023-10-03 20:37:00 +02:00
# endif
}
2024-03-20 19:35:52 +01:00
// TODO ogl-es
2023-10-03 20:37:00 +02:00
// fillmode
2024-03-20 19:35:52 +01:00
// if (resetAllRenderStates || (lastmaterial.Wireframe != material.Wireframe) || (lastmaterial.PointCloud != material.PointCloud))
// glPolygonMode(GL_FRONT_AND_BACK, material.Wireframe ? GL_LINE : material.PointCloud? GL_POINT : GL_FILL);
2023-10-03 20:37:00 +02:00
// shademode
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | ( lastmaterial . GouraudShading ! = material . GouraudShading ) ) {
2023-10-03 20:37:00 +02:00
if ( material . GouraudShading )
glShadeModel ( GL_SMOOTH ) ;
else
glShadeModel ( GL_FLAT ) ;
}
// lighting
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | ( lastmaterial . Lighting ! = material . Lighting ) ) {
2023-10-03 20:37:00 +02:00
if ( material . Lighting )
glEnable ( GL_LIGHTING ) ;
else
glDisable ( GL_LIGHTING ) ;
}
// zbuffer
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | lastmaterial . ZBuffer ! = material . ZBuffer ) {
switch ( material . ZBuffer ) {
case ECFN_DISABLED :
glDisable ( GL_DEPTH_TEST ) ;
break ;
case ECFN_LESSEQUAL :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_LEQUAL ) ;
break ;
case ECFN_EQUAL :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_EQUAL ) ;
break ;
case ECFN_LESS :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_LESS ) ;
break ;
case ECFN_NOTEQUAL :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_NOTEQUAL ) ;
break ;
case ECFN_GREATEREQUAL :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_GEQUAL ) ;
break ;
case ECFN_GREATER :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_GREATER ) ;
break ;
case ECFN_ALWAYS :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_ALWAYS ) ;
break ;
case ECFN_NEVER :
glEnable ( GL_DEPTH_TEST ) ;
glDepthFunc ( GL_NEVER ) ;
break ;
2023-10-03 20:37:00 +02:00
}
}
// zwrite
2024-03-20 19:35:52 +01:00
if ( getWriteZBuffer ( material ) ) {
2023-10-03 20:37:00 +02:00
glDepthMask ( GL_TRUE ) ;
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
glDepthMask ( GL_FALSE ) ;
}
// back face culling
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | ( lastmaterial . FrontfaceCulling ! = material . FrontfaceCulling ) | | ( lastmaterial . BackfaceCulling ! = material . BackfaceCulling ) ) {
if ( ( material . FrontfaceCulling ) & & ( material . BackfaceCulling ) ) {
2023-10-03 20:37:00 +02:00
glCullFace ( GL_FRONT_AND_BACK ) ;
glEnable ( GL_CULL_FACE ) ;
2024-03-20 19:35:52 +01:00
} else if ( material . BackfaceCulling ) {
2023-10-03 20:37:00 +02:00
glCullFace ( GL_BACK ) ;
glEnable ( GL_CULL_FACE ) ;
2024-03-20 19:35:52 +01:00
} else if ( material . FrontfaceCulling ) {
2023-10-03 20:37:00 +02:00
glCullFace ( GL_FRONT ) ;
glEnable ( GL_CULL_FACE ) ;
2024-03-20 19:35:52 +01:00
} else
2023-10-03 20:37:00 +02:00
glDisable ( GL_CULL_FACE ) ;
}
// fog
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | lastmaterial . FogEnable ! = material . FogEnable ) {
2023-10-03 20:37:00 +02:00
if ( material . FogEnable )
glEnable ( GL_FOG ) ;
else
glDisable ( GL_FOG ) ;
}
// normalization
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | lastmaterial . NormalizeNormals ! = material . NormalizeNormals ) {
2023-10-03 20:37:00 +02:00
if ( material . NormalizeNormals )
glEnable ( GL_NORMALIZE ) ;
else
glDisable ( GL_NORMALIZE ) ;
}
// Color Mask
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | lastmaterial . ColorMask ! = material . ColorMask ) {
2023-10-03 20:37:00 +02:00
glColorMask (
2024-03-20 19:35:52 +01:00
( material . ColorMask & ECP_RED ) ? GL_TRUE : GL_FALSE ,
( material . ColorMask & ECP_GREEN ) ? GL_TRUE : GL_FALSE ,
( material . ColorMask & ECP_BLUE ) ? GL_TRUE : GL_FALSE ,
( material . ColorMask & ECP_ALPHA ) ? GL_TRUE : GL_FALSE ) ;
2023-10-03 20:37:00 +02:00
}
// Blend Equation
if ( material . BlendOperation = = EBO_NONE )
CacheHandler - > setBlend ( false ) ;
2024-03-20 19:35:52 +01:00
else {
2023-10-03 20:37:00 +02:00
CacheHandler - > setBlend ( true ) ;
2024-03-20 19:35:52 +01:00
if ( queryFeature ( EVDF_BLEND_OPERATIONS ) ) {
switch ( material . BlendOperation ) {
2023-10-03 20:37:00 +02:00
case EBO_ADD :
# if defined(GL_OES_blend_subtract)
CacheHandler - > setBlendEquation ( GL_FUNC_ADD_OES ) ;
# endif
break ;
case EBO_SUBTRACT :
# if defined(GL_OES_blend_subtract)
CacheHandler - > setBlendEquation ( GL_FUNC_SUBTRACT_OES ) ;
# endif
break ;
case EBO_REVSUBTRACT :
# if defined(GL_OES_blend_subtract)
CacheHandler - > setBlendEquation ( GL_FUNC_REVERSE_SUBTRACT_OES ) ;
# endif
break ;
default :
break ;
}
}
}
2024-03-20 19:35:52 +01:00
// Blend Factor
if ( IR ( material . BlendFactor ) & 0xFFFFFFFF // TODO: why the & 0xFFFFFFFF?
& & material . MaterialType ! = EMT_ONETEXTURE_BLEND ) {
E_BLEND_FACTOR srcRGBFact = EBF_ZERO ;
E_BLEND_FACTOR dstRGBFact = EBF_ZERO ;
E_BLEND_FACTOR srcAlphaFact = EBF_ZERO ;
E_BLEND_FACTOR dstAlphaFact = EBF_ZERO ;
E_MODULATE_FUNC modulo = EMFN_MODULATE_1X ;
u32 alphaSource = 0 ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
unpack_textureBlendFuncSeparate ( srcRGBFact , dstRGBFact , srcAlphaFact , dstAlphaFact , modulo , alphaSource , material . BlendFactor ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( queryFeature ( EVDF_BLEND_SEPARATE ) ) {
2023-10-03 20:37:00 +02:00
CacheHandler - > setBlendFuncSeparate ( getGLBlend ( srcRGBFact ) , getGLBlend ( dstRGBFact ) ,
2024-03-20 19:35:52 +01:00
getGLBlend ( srcAlphaFact ) , getGLBlend ( dstAlphaFact ) ) ;
} else {
2023-10-03 20:37:00 +02:00
CacheHandler - > setBlendFunc ( getGLBlend ( srcRGBFact ) , getGLBlend ( dstRGBFact ) ) ;
2024-03-20 19:35:52 +01:00
}
2023-10-03 20:37:00 +02:00
}
// TODO: Polygon Offset. Not sure if it was left out deliberately or if it won't work with this driver.
// thickness
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | lastmaterial . Thickness ! = material . Thickness ) {
if ( AntiAlias ) {
// glPointSize(core::clamp(static_cast<GLfloat>(material.Thickness), DimSmoothedPoint[0], DimSmoothedPoint[1]));
2023-10-03 20:37:00 +02:00
// we don't use point smoothing
glPointSize ( core : : clamp ( static_cast < GLfloat > ( material . Thickness ) , DimAliasedPoint [ 0 ] , DimAliasedPoint [ 1 ] ) ) ;
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
glPointSize ( core : : clamp ( static_cast < GLfloat > ( material . Thickness ) , DimAliasedPoint [ 0 ] , DimAliasedPoint [ 1 ] ) ) ;
glLineWidth ( core : : clamp ( static_cast < GLfloat > ( material . Thickness ) , DimAliasedLine [ 0 ] , DimAliasedLine [ 1 ] ) ) ;
}
}
// Anti aliasing
2024-03-20 19:35:52 +01:00
if ( resetAllRenderStates | | lastmaterial . AntiAliasing ! = material . AntiAliasing ) {
2024-02-17 22:01:37 +01:00
if ( material . AntiAliasing & EAAM_ALPHA_TO_COVERAGE )
glEnable ( GL_SAMPLE_ALPHA_TO_COVERAGE ) ;
else if ( lastmaterial . AntiAliasing & EAAM_ALPHA_TO_COVERAGE )
glDisable ( GL_SAMPLE_ALPHA_TO_COVERAGE ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( ( AntiAlias > = 2 ) & & ( material . AntiAliasing & ( EAAM_SIMPLE | EAAM_QUALITY ) ) )
2024-02-17 22:01:37 +01:00
glEnable ( GL_MULTISAMPLE ) ;
else
glDisable ( GL_MULTISAMPLE ) ;
2023-10-03 20:37:00 +02:00
}
// Texture parameters
setTextureRenderStates ( material , resetAllRenderStates ) ;
}
//! Compare in SMaterial doesn't check texture parameters, so we should call this on each OnRender call.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : setTextureRenderStates ( const SMaterial & material , bool resetAllRenderstates )
2023-10-03 20:37:00 +02:00
{
// Set textures to TU/TIU and apply filters to them
2024-03-20 19:35:52 +01:00
for ( s32 i = Feature . MaxTextureUnits - 1 ; i > = 0 ; - - i ) {
2023-10-03 20:37:00 +02:00
CacheHandler - > getTextureCache ( ) . set ( i , material . TextureLayers [ i ] . Texture ) ;
2024-03-20 19:35:52 +01:00
const COGLES1Texture * tmpTexture = CacheHandler - > getTextureCache ( ) . get ( i ) ;
2023-10-03 20:37:00 +02:00
if ( ! tmpTexture )
continue ;
GLenum tmpTextureType = tmpTexture - > getOpenGLTextureType ( ) ;
CacheHandler - > setActiveTexture ( GL_TEXTURE0 + i ) ;
{
const bool isRTT = tmpTexture - > isRenderTarget ( ) ;
glMatrixMode ( GL_TEXTURE ) ;
if ( ! isRTT & & Matrices [ ETS_TEXTURE_0 + i ] . isIdentity ( ) )
glLoadIdentity ( ) ;
2024-03-20 19:35:52 +01:00
else {
2023-10-03 20:37:00 +02:00
GLfloat glmat [ 16 ] ;
if ( isRTT )
getGLTextureMatrix ( glmat , Matrices [ ETS_TEXTURE_0 + i ] * TextureFlipMatrix ) ;
else
getGLTextureMatrix ( glmat , Matrices [ ETS_TEXTURE_0 + i ] ) ;
glLoadMatrixf ( glmat ) ;
}
}
2024-03-20 19:35:52 +01:00
COGLES1Texture : : SStatesCache & statesCache = tmpTexture - > getStatesCache ( ) ;
2023-10-03 20:37:00 +02:00
if ( resetAllRenderstates )
statesCache . IsCached = false ;
2024-02-17 21:00:17 +01:00
# if defined(GL_EXT_texture_lod_bias)
2024-03-20 19:35:52 +01:00
if ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_EXT_texture_lod_bias ] ) {
if ( material . TextureLayers [ i ] . LODBias ) {
2023-10-03 20:37:00 +02:00
const float tmp = core : : clamp ( material . TextureLayers [ i ] . LODBias * 0.125f , - MaxTextureLODBias , MaxTextureLODBias ) ;
glTexEnvf ( GL_TEXTURE_FILTER_CONTROL_EXT , GL_TEXTURE_LOD_BIAS_EXT , tmp ) ;
2024-03-20 19:35:52 +01:00
} else
2023-10-03 20:37:00 +02:00
glTexEnvf ( GL_TEXTURE_FILTER_CONTROL_EXT , GL_TEXTURE_LOD_BIAS_EXT , 0.f ) ;
}
# endif
2024-03-20 19:35:52 +01:00
if ( ! statesCache . IsCached | | material . TextureLayers [ i ] . MagFilter ! = statesCache . MagFilter ) {
2023-10-03 20:37:00 +02:00
E_TEXTURE_MAG_FILTER magFilter = material . TextureLayers [ i ] . MagFilter ;
glTexParameteri ( tmpTextureType , GL_TEXTURE_MAG_FILTER ,
2024-03-20 19:35:52 +01:00
magFilter = = ETMAGF_NEAREST ? GL_NEAREST : ( assert ( magFilter = = ETMAGF_LINEAR ) , GL_LINEAR ) ) ;
2023-10-03 20:37:00 +02:00
statesCache . MagFilter = magFilter ;
}
2024-03-20 19:35:52 +01:00
if ( material . UseMipMaps & & tmpTexture - > hasMipMaps ( ) ) {
2023-10-03 20:37:00 +02:00
if ( ! statesCache . IsCached | | material . TextureLayers [ i ] . MinFilter ! = statesCache . MinFilter | |
2024-03-20 19:35:52 +01:00
! statesCache . MipMapStatus ) {
2023-10-03 20:37:00 +02:00
E_TEXTURE_MIN_FILTER minFilter = material . TextureLayers [ i ] . MinFilter ;
glTexParameteri ( tmpTextureType , GL_TEXTURE_MIN_FILTER ,
2024-03-20 19:35:52 +01:00
minFilter = = ETMINF_NEAREST_MIPMAP_NEAREST ? GL_NEAREST_MIPMAP_NEAREST : minFilter = = ETMINF_LINEAR_MIPMAP_NEAREST ? GL_LINEAR_MIPMAP_NEAREST
2024-03-21 17:30:28 +01:00
: minFilter = = ETMINF_NEAREST_MIPMAP_LINEAR ? GL_NEAREST_MIPMAP_LINEAR
2024-03-20 19:35:52 +01:00
: ( assert ( minFilter = = ETMINF_LINEAR_MIPMAP_LINEAR ) , GL_LINEAR_MIPMAP_LINEAR ) ) ;
2023-10-03 20:37:00 +02:00
statesCache . MinFilter = minFilter ;
statesCache . MipMapStatus = true ;
}
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
if ( ! statesCache . IsCached | | material . TextureLayers [ i ] . MinFilter ! = statesCache . MinFilter | |
2024-03-20 19:35:52 +01:00
statesCache . MipMapStatus ) {
2023-10-03 20:37:00 +02:00
E_TEXTURE_MIN_FILTER minFilter = material . TextureLayers [ i ] . MinFilter ;
glTexParameteri ( tmpTextureType , GL_TEXTURE_MIN_FILTER ,
2024-03-20 19:35:52 +01:00
( minFilter = = ETMINF_NEAREST_MIPMAP_NEAREST | | minFilter = = ETMINF_NEAREST_MIPMAP_LINEAR ) ? GL_NEAREST : ( assert ( minFilter = = ETMINF_LINEAR_MIPMAP_NEAREST | | minFilter = = ETMINF_LINEAR_MIPMAP_LINEAR ) , GL_LINEAR ) ) ;
2023-10-03 20:37:00 +02:00
statesCache . MinFilter = minFilter ;
statesCache . MipMapStatus = false ;
}
}
# ifdef GL_EXT_texture_filter_anisotropic
if ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_EXT_texture_filter_anisotropic ] & &
2024-03-20 19:35:52 +01:00
( ! statesCache . IsCached | | material . TextureLayers [ i ] . AnisotropicFilter ! = statesCache . AnisotropicFilter ) ) {
2023-10-03 20:37:00 +02:00
glTexParameteri ( tmpTextureType , GL_TEXTURE_MAX_ANISOTROPY_EXT ,
2024-03-20 19:35:52 +01:00
material . TextureLayers [ i ] . AnisotropicFilter > 1 ? core : : min_ ( MaxAnisotropy , material . TextureLayers [ i ] . AnisotropicFilter ) : 1 ) ;
2023-10-03 20:37:00 +02:00
statesCache . AnisotropicFilter = material . TextureLayers [ i ] . AnisotropicFilter ;
}
# endif
2024-03-20 19:35:52 +01:00
if ( ! statesCache . IsCached | | material . TextureLayers [ i ] . TextureWrapU ! = statesCache . WrapU ) {
2023-10-03 20:37:00 +02:00
glTexParameteri ( tmpTextureType , GL_TEXTURE_WRAP_S , getTextureWrapMode ( material . TextureLayers [ i ] . TextureWrapU ) ) ;
statesCache . WrapU = material . TextureLayers [ i ] . TextureWrapU ;
}
2024-03-20 19:35:52 +01:00
if ( ! statesCache . IsCached | | material . TextureLayers [ i ] . TextureWrapV ! = statesCache . WrapV ) {
2023-10-03 20:37:00 +02:00
glTexParameteri ( tmpTextureType , GL_TEXTURE_WRAP_T , getTextureWrapMode ( material . TextureLayers [ i ] . TextureWrapV ) ) ;
statesCache . WrapV = material . TextureLayers [ i ] . TextureWrapV ;
}
statesCache . IsCached = true ;
}
// be sure to leave in texture stage 0
CacheHandler - > setActiveTexture ( GL_TEXTURE0 ) ;
}
//! sets the needed renderstates
void COGLES1Driver : : setRenderStates2DMode ( bool alpha , bool texture , bool alphaChannel )
{
2024-03-20 19:35:52 +01:00
if ( CurrentRenderMode ! = ERM_2D | | Transformation3DChanged ) {
2023-10-03 20:37:00 +02:00
// unset last 3d material
2024-03-20 19:35:52 +01:00
if ( CurrentRenderMode = = ERM_3D ) {
2023-10-03 20:37:00 +02:00
if ( static_cast < u32 > ( LastMaterial . MaterialType ) < MaterialRenderers . size ( ) )
MaterialRenderers [ LastMaterial . MaterialType ] . Renderer - > OnUnsetMaterial ( ) ;
}
2024-03-20 19:35:52 +01:00
if ( Transformation3DChanged ) {
2023-10-03 20:37:00 +02:00
glMatrixMode ( GL_PROJECTION ) ;
2024-03-20 19:35:52 +01:00
const core : : dimension2d < u32 > & renderTargetSize = getCurrentRenderTargetSize ( ) ;
2023-10-03 20:37:00 +02:00
core : : matrix4 m ( core : : matrix4 : : EM4CONST_NOTHING ) ;
m . buildProjectionMatrixOrthoLH ( f32 ( renderTargetSize . Width ) , f32 ( - ( s32 ) ( renderTargetSize . Height ) ) , - 1.0f , 1.0f ) ;
m . setTranslation ( core : : vector3df ( - 1 , 1 , 0 ) ) ;
glLoadMatrixf ( m . pointer ( ) ) ;
glMatrixMode ( GL_MODELVIEW ) ;
glLoadIdentity ( ) ;
Transformation3DChanged = false ;
}
}
Material = ( OverrideMaterial2DEnabled ) ? OverrideMaterial2D : InitMaterial2D ;
Material . Lighting = false ;
2024-03-20 19:35:52 +01:00
Material . TextureLayers [ 0 ] . Texture = ( texture ) ? const_cast < COGLES1Texture * > ( CacheHandler - > getTextureCache ( ) . get ( 0 ) ) : 0 ;
2023-10-03 20:37:00 +02:00
setTransform ( ETS_TEXTURE_0 , core : : IdentityMatrix ) ;
setBasicRenderStates ( Material , LastMaterial , false ) ;
LastMaterial = Material ;
CacheHandler - > correctCacheMaterial ( LastMaterial ) ;
// no alphaChannel without texture
alphaChannel & = texture ;
2024-03-20 19:35:52 +01:00
if ( alphaChannel | | alpha ) {
2023-10-03 20:37:00 +02:00
CacheHandler - > setBlend ( true ) ;
CacheHandler - > setBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ) ;
CacheHandler - > setBlendEquation ( GL_FUNC_ADD ) ;
glEnable ( GL_ALPHA_TEST ) ;
glAlphaFunc ( GL_GREATER , 0.f ) ;
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
CacheHandler - > setBlend ( false ) ;
glDisable ( GL_ALPHA_TEST ) ;
}
2024-03-20 19:35:52 +01:00
if ( texture ) {
2023-10-03 20:37:00 +02:00
// Due to the transformation change, the previous line would call a reset each frame
// but we can safely reset the variable as it was false before
Transformation3DChanged = false ;
2024-03-20 19:35:52 +01:00
if ( alphaChannel ) {
2023-10-03 20:37:00 +02:00
// if alpha and alpha texture just modulate, otherwise use only the alpha channel
2024-03-20 19:35:52 +01:00
if ( alpha ) {
2023-10-03 20:37:00 +02:00
glTexEnvf ( GL_TEXTURE_ENV , GL_TEXTURE_ENV_MODE , GL_MODULATE ) ;
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
glTexEnvf ( GL_TEXTURE_ENV , GL_TEXTURE_ENV_MODE , GL_COMBINE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_COMBINE_ALPHA , GL_REPLACE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_SRC0_ALPHA , GL_TEXTURE ) ;
// rgb always modulates
glTexEnvf ( GL_TEXTURE_ENV , GL_COMBINE_RGB , GL_MODULATE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_SRC0_RGB , GL_TEXTURE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_SRC1_RGB , GL_PRIMARY_COLOR ) ;
}
2024-03-20 19:35:52 +01:00
} else {
if ( alpha ) {
2023-10-03 20:37:00 +02:00
glTexEnvf ( GL_TEXTURE_ENV , GL_TEXTURE_ENV_MODE , GL_COMBINE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_COMBINE_ALPHA , GL_REPLACE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_SRC0_ALPHA , GL_PRIMARY_COLOR ) ;
// rgb always modulates
glTexEnvf ( GL_TEXTURE_ENV , GL_COMBINE_RGB , GL_MODULATE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_SRC0_RGB , GL_TEXTURE ) ;
glTexEnvf ( GL_TEXTURE_ENV , GL_SRC1_RGB , GL_PRIMARY_COLOR ) ;
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
glTexEnvi ( GL_TEXTURE_ENV , GL_TEXTURE_ENV_MODE , GL_MODULATE ) ;
}
}
}
CurrentRenderMode = ERM_2D ;
}
//! \return Returns the name of the video driver.
2024-03-20 19:35:52 +01:00
const char * COGLES1Driver : : getName ( ) const
2023-10-03 20:37:00 +02:00
{
return Name . c_str ( ) ;
}
//! Sets the dynamic ambient light color.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : setAmbientLight ( const SColorf & color )
2023-10-03 20:37:00 +02:00
{
CNullDriver : : setAmbientLight ( color ) ;
GLfloat data [ 4 ] = { color . r , color . g , color . b , color . a } ;
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT , data ) ;
}
// this code was sent in by Oliver Klems, thank you
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : setViewPort ( const core : : rect < s32 > & area )
2023-10-03 20:37:00 +02:00
{
core : : rect < s32 > vp = area ;
core : : rect < s32 > rendert ( 0 , 0 , getCurrentRenderTargetSize ( ) . Width , getCurrentRenderTargetSize ( ) . Height ) ;
vp . clipAgainst ( rendert ) ;
if ( vp . getHeight ( ) > 0 & & vp . getWidth ( ) > 0 )
CacheHandler - > setViewport ( vp . UpperLeftCorner . X , getCurrentRenderTargetSize ( ) . Height - vp . UpperLeftCorner . Y - vp . getHeight ( ) , vp . getWidth ( ) , vp . getHeight ( ) ) ;
ViewPort = vp ;
}
void COGLES1Driver : : setViewPortRaw ( u32 width , u32 height )
{
CacheHandler - > setViewport ( 0 , 0 , width , height ) ;
ViewPort = core : : recti ( 0 , 0 , width , height ) ;
}
//! Sets the fog mode.
void COGLES1Driver : : setFog ( SColor c , E_FOG_TYPE fogType , f32 start ,
2024-03-20 19:35:52 +01:00
f32 end , f32 density , bool pixelFog , bool rangeFog )
2023-10-03 20:37:00 +02:00
{
CNullDriver : : setFog ( c , fogType , start , end , density , pixelFog , rangeFog ) ;
2024-03-20 19:35:52 +01:00
glFogf ( GL_FOG_MODE , GLfloat ( ( fogType = = EFT_FOG_LINEAR ) ? GL_LINEAR : ( fogType = = EFT_FOG_EXP ) ? GL_EXP
: GL_EXP2 ) ) ;
2023-10-03 20:37:00 +02:00
# ifdef GL_EXT_fog_coord
if ( FeatureAvailable [ IRR_EXT_fog_coord ] )
glFogi ( GL_FOG_COORDINATE_SOURCE , GL_FRAGMENT_DEPTH ) ;
# endif
2024-03-20 19:35:52 +01:00
if ( fogType = = EFT_FOG_LINEAR ) {
2023-10-03 20:37:00 +02:00
glFogf ( GL_FOG_START , start ) ;
glFogf ( GL_FOG_END , end ) ;
2024-03-20 19:35:52 +01:00
} else
2023-10-03 20:37:00 +02:00
glFogf ( GL_FOG_DENSITY , density ) ;
if ( pixelFog )
glHint ( GL_FOG_HINT , GL_NICEST ) ;
else
glHint ( GL_FOG_HINT , GL_FASTEST ) ;
SColorf color ( c ) ;
GLfloat data [ 4 ] = { color . r , color . g , color . b , color . a } ;
glFogfv ( GL_FOG_COLOR , data ) ;
}
//! Draws a 3d line.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : draw3DLine ( const core : : vector3df & start ,
const core : : vector3df & end , SColor color )
2023-10-03 20:37:00 +02:00
{
setRenderStates3DMode ( ) ;
2024-03-20 19:35:52 +01:00
u16 indices [ ] = { 0 , 1 } ;
2023-10-03 20:37:00 +02:00
S3DVertex vertices [ 2 ] ;
2024-03-20 19:35:52 +01:00
vertices [ 0 ] = S3DVertex ( start . X , start . Y , start . Z , 0 , 0 , 1 , color , 0 , 0 ) ;
vertices [ 1 ] = S3DVertex ( end . X , end . Y , end . Z , 0 , 0 , 1 , color , 0 , 0 ) ;
2023-10-03 20:37:00 +02:00
drawVertexPrimitiveList2d3d ( vertices , 2 , indices , 1 , video : : EVT_STANDARD , scene : : EPT_LINES ) ;
}
//! Only used by the internal engine. Used to notify the driver that
//! the window was resized.
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : OnResize ( const core : : dimension2d < u32 > & size )
2023-10-03 20:37:00 +02:00
{
CNullDriver : : OnResize ( size ) ;
CacheHandler - > setViewport ( 0 , 0 , size . Width , size . Height ) ;
Transformation3DChanged = true ;
}
//! Returns type of video driver
E_DRIVER_TYPE COGLES1Driver : : getDriverType ( ) const
{
return EDT_OGLES1 ;
}
//! returns color format
ECOLOR_FORMAT COGLES1Driver : : getColorFormat ( ) const
{
return ColorFormat ;
}
//! Get a vertex shader constant index.
2024-03-20 19:35:52 +01:00
s32 COGLES1Driver : : getVertexShaderConstantID ( const c8 * name )
2023-10-03 20:37:00 +02:00
{
return getPixelShaderConstantID ( name ) ;
}
//! Get a pixel shader constant index.
2024-03-20 19:35:52 +01:00
s32 COGLES1Driver : : getPixelShaderConstantID ( const c8 * name )
2023-10-03 20:37:00 +02:00
{
os : : Printer : : log ( " Error: Please use IMaterialRendererServices from IShaderConstantSetCallBack::OnSetConstants not VideoDriver->getPixelShaderConstantID(). " ) ;
return - 1 ;
}
//! Sets a constant for the vertex shader based on an index.
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setVertexShaderConstant ( s32 index , const f32 * floats , int count )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
// pass this along, as in GLSL the same routine is used for both vertex and fragment shaders
2023-10-03 20:37:00 +02:00
return setPixelShaderConstant ( index , floats , count ) ;
}
//! Int interface for the above.
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setVertexShaderConstant ( s32 index , const s32 * ints , int count )
2023-10-03 20:37:00 +02:00
{
return setPixelShaderConstant ( index , ints , count ) ;
}
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setVertexShaderConstant ( s32 index , const u32 * ints , int count )
2023-10-03 20:37:00 +02:00
{
return setPixelShaderConstant ( index , ints , count ) ;
}
//! Sets a constant for the pixel shader based on an index.
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setPixelShaderConstant ( s32 index , const f32 * floats , int count )
2023-10-03 20:37:00 +02:00
{
os : : Printer : : log ( " Error: Please use IMaterialRendererServices from IShaderConstantSetCallBack::OnSetConstants not VideoDriver->setPixelShaderConstant(). " ) ;
return false ;
}
//! Int interface for the above.
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setPixelShaderConstant ( s32 index , const s32 * ints , int count )
2023-10-03 20:37:00 +02:00
{
os : : Printer : : log ( " Error: Please use IMaterialRendererServices from IShaderConstantSetCallBack::OnSetConstants not VideoDriver->setPixelShaderConstant(). " ) ;
return false ;
}
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setPixelShaderConstant ( s32 index , const u32 * ints , int count )
2023-10-03 20:37:00 +02:00
{
os : : Printer : : log ( " Error: Please use IMaterialRendererServices from IShaderConstantSetCallBack::OnSetConstants not VideoDriver->setPixelShaderConstant(). " ) ;
return false ;
}
//! Adds a new material renderer to the VideoDriver, using GLSL to render geometry.
s32 COGLES1Driver : : addHighLevelShaderMaterial (
2024-03-20 19:35:52 +01:00
const c8 * vertexShaderProgram ,
const c8 * vertexShaderEntryPointName ,
E_VERTEX_SHADER_TYPE vsCompileTarget ,
const c8 * pixelShaderProgram ,
const c8 * pixelShaderEntryPointName ,
E_PIXEL_SHADER_TYPE psCompileTarget ,
const c8 * geometryShaderProgram ,
const c8 * geometryShaderEntryPointName ,
E_GEOMETRY_SHADER_TYPE gsCompileTarget ,
scene : : E_PRIMITIVE_TYPE inType ,
scene : : E_PRIMITIVE_TYPE outType ,
u32 verticesOut ,
IShaderConstantSetCallBack * callback ,
E_MATERIAL_TYPE baseMaterial ,
s32 userData )
2023-10-03 20:37:00 +02:00
{
os : : Printer : : log ( " No shader support. " ) ;
return - 1 ;
}
//! Returns a pointer to the IVideoDriver interface. (Implementation for
//! IMaterialRendererServices)
2024-03-20 19:35:52 +01:00
IVideoDriver * COGLES1Driver : : getVideoDriver ( )
2023-10-03 20:37:00 +02:00
{
return this ;
}
//! Returns pointer to the IGPUProgrammingServices interface.
2024-03-20 19:35:52 +01:00
IGPUProgrammingServices * COGLES1Driver : : getGPUProgrammingServices ( )
2023-10-03 20:37:00 +02:00
{
return this ;
}
2024-03-20 19:35:52 +01:00
ITexture * COGLES1Driver : : addRenderTargetTexture ( const core : : dimension2d < u32 > & size ,
const io : : path & name , const ECOLOR_FORMAT format )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
// disable mip-mapping
2023-10-03 20:37:00 +02:00
bool generateMipLevels = getTextureCreationFlag ( ETCF_CREATE_MIP_MAPS ) ;
setTextureCreationFlag ( ETCF_CREATE_MIP_MAPS , false ) ;
bool supportForFBO = ( Feature . ColorAttachment > 0 ) ;
core : : dimension2du destSize ( size ) ;
2024-03-20 19:35:52 +01:00
if ( ! supportForFBO ) {
2023-10-03 20:37:00 +02:00
destSize = core : : dimension2d < u32 > ( core : : min_ ( size . Width , ScreenSize . Width ) , core : : min_ ( size . Height , ScreenSize . Height ) ) ;
destSize = destSize . getOptimalSize ( ( size = = size . getOptimalSize ( ) ) , false , false ) ;
}
2024-03-20 19:35:52 +01:00
COGLES1Texture * renderTargetTexture = new COGLES1Texture ( name , destSize , ETT_2D , format , this ) ;
2023-10-03 20:37:00 +02:00
addTexture ( renderTargetTexture ) ;
renderTargetTexture - > drop ( ) ;
2024-03-20 19:35:52 +01:00
// restore mip-mapping
2023-10-03 20:37:00 +02:00
setTextureCreationFlag ( ETCF_CREATE_MIP_MAPS , generateMipLevels ) ;
return renderTargetTexture ;
}
2024-03-20 19:35:52 +01:00
ITexture * COGLES1Driver : : addRenderTargetTextureCubemap ( const irr : : u32 sideLen , const io : : path & name , const ECOLOR_FORMAT format )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
// disable mip-mapping
2023-10-03 20:37:00 +02:00
bool generateMipLevels = getTextureCreationFlag ( ETCF_CREATE_MIP_MAPS ) ;
setTextureCreationFlag ( ETCF_CREATE_MIP_MAPS , false ) ;
bool supportForFBO = ( Feature . ColorAttachment > 0 ) ;
const core : : dimension2d < u32 > size ( sideLen , sideLen ) ;
core : : dimension2du destSize ( size ) ;
2024-03-20 19:35:52 +01:00
if ( ! supportForFBO ) {
2023-10-03 20:37:00 +02:00
destSize = core : : dimension2d < u32 > ( core : : min_ ( size . Width , ScreenSize . Width ) , core : : min_ ( size . Height , ScreenSize . Height ) ) ;
destSize = destSize . getOptimalSize ( ( size = = size . getOptimalSize ( ) ) , false , false ) ;
}
2024-03-20 19:35:52 +01:00
COGLES1Texture * renderTargetTexture = new COGLES1Texture ( name , destSize , ETT_CUBEMAP , format , this ) ;
2023-10-03 20:37:00 +02:00
addTexture ( renderTargetTexture ) ;
renderTargetTexture - > drop ( ) ;
2024-03-20 19:35:52 +01:00
// restore mip-mapping
2023-10-03 20:37:00 +02:00
setTextureCreationFlag ( ETCF_CREATE_MIP_MAPS , generateMipLevels ) ;
return renderTargetTexture ;
}
//! Returns the maximum amount of primitives
u32 COGLES1Driver : : getMaximalPrimitiveCount ( ) const
{
return 65535 ;
}
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setRenderTargetEx ( IRenderTarget * target , u16 clearFlag , SColor clearColor , f32 clearDepth , u8 clearStencil )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
if ( target & & target - > getDriverType ( ) ! = EDT_OGLES1 ) {
2023-10-03 20:37:00 +02:00
os : : Printer : : log ( " Fatal Error: Tried to set a render target not owned by OpenGL driver. " , ELL_ERROR ) ;
return false ;
}
bool supportForFBO = ( Feature . ColorAttachment > 0 ) ;
core : : dimension2d < u32 > destRenderTargetSize ( 0 , 0 ) ;
2024-03-20 19:35:52 +01:00
if ( target ) {
COGLES1RenderTarget * renderTarget = static_cast < COGLES1RenderTarget * > ( target ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( supportForFBO ) {
2023-10-03 20:37:00 +02:00
CacheHandler - > setFBO ( renderTarget - > getBufferID ( ) ) ;
renderTarget - > update ( ) ;
}
destRenderTargetSize = renderTarget - > getSize ( ) ;
setViewPortRaw ( destRenderTargetSize . Width , destRenderTargetSize . Height ) ;
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
if ( supportForFBO )
CacheHandler - > setFBO ( 0 ) ;
2024-03-20 19:35:52 +01:00
else {
COGLES1RenderTarget * prevRenderTarget = static_cast < COGLES1RenderTarget * > ( CurrentRenderTarget ) ;
COGLES1Texture * renderTargetTexture = static_cast < COGLES1Texture * > ( prevRenderTarget - > getTexture ( ) ) ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( renderTargetTexture ) {
const COGLES1Texture * prevTexture = CacheHandler - > getTextureCache ( ) . get ( 0 ) ;
2023-10-03 20:37:00 +02:00
CacheHandler - > getTextureCache ( ) . set ( 0 , renderTargetTexture ) ;
const core : : dimension2d < u32 > size = renderTargetTexture - > getSize ( ) ;
glCopyTexSubImage2D ( GL_TEXTURE_2D , 0 , 0 , 0 , 0 , 0 , size . Width , size . Height ) ;
CacheHandler - > getTextureCache ( ) . set ( 0 , prevTexture ) ;
}
}
destRenderTargetSize = core : : dimension2d < u32 > ( 0 , 0 ) ;
setViewPortRaw ( ScreenSize . Width , ScreenSize . Height ) ;
}
2024-03-20 19:35:52 +01:00
if ( CurrentRenderTargetSize ! = destRenderTargetSize ) {
2023-10-03 20:37:00 +02:00
CurrentRenderTargetSize = destRenderTargetSize ;
Transformation3DChanged = true ;
}
CurrentRenderTarget = target ;
2024-03-20 19:35:52 +01:00
if ( ! supportForFBO ) {
2023-10-03 20:37:00 +02:00
clearFlag | = ECBF_COLOR ;
clearFlag | = ECBF_DEPTH ;
}
clearBuffers ( clearFlag , clearColor , clearDepth , clearStencil ) ;
return true ;
}
void COGLES1Driver : : clearBuffers ( u16 flag , SColor color , f32 depth , u8 stencil )
{
GLbitfield mask = 0 ;
2024-03-20 19:35:52 +01:00
if ( flag & ECBF_COLOR ) {
2023-10-03 20:37:00 +02:00
glColorMask ( GL_TRUE , GL_TRUE , GL_TRUE , GL_TRUE ) ;
const f32 inv = 1.0f / 255.0f ;
glClearColor ( color . getRed ( ) * inv , color . getGreen ( ) * inv ,
2024-03-20 19:35:52 +01:00
color . getBlue ( ) * inv , color . getAlpha ( ) * inv ) ;
2023-10-03 20:37:00 +02:00
mask | = GL_COLOR_BUFFER_BIT ;
}
2024-03-20 19:35:52 +01:00
if ( flag & ECBF_DEPTH ) {
2023-10-03 20:37:00 +02:00
glDepthMask ( GL_TRUE ) ;
glClearDepthf ( depth ) ;
mask | = GL_DEPTH_BUFFER_BIT ;
}
2024-03-20 19:35:52 +01:00
if ( flag & ECBF_STENCIL ) {
2023-10-03 20:37:00 +02:00
glClearStencil ( stencil ) ;
mask | = GL_STENCIL_BUFFER_BIT ;
}
if ( mask )
glClear ( mask ) ;
}
//! Returns an image created from the last rendered frame.
// We want to read the front buffer to get the latest render finished.
// This is not possible under ogl-es, though, so one has to call this method
// outside of the render loop only.
2024-03-20 19:35:52 +01:00
IImage * COGLES1Driver : : createScreenShot ( video : : ECOLOR_FORMAT format , video : : E_RENDER_TARGET target )
2023-10-03 20:37:00 +02:00
{
2024-03-20 19:35:52 +01:00
if ( target = = video : : ERT_MULTI_RENDER_TEXTURES | | target = = video : : ERT_RENDER_TEXTURE | | target = = video : : ERT_STEREO_BOTH_BUFFERS )
2023-10-03 20:37:00 +02:00
return 0 ;
2024-03-20 19:35:52 +01:00
GLint internalformat = GL_RGBA ;
GLint type = GL_UNSIGNED_BYTE ;
if ( false & & ( FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_IMG_read_format ] | | FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_OES_read_format ] | | FeatureAvailable [ COGLESCoreExtensionHandler : : IRR_GL_EXT_read_format_bgra ] ) ) {
2023-10-03 20:37:00 +02:00
# ifdef GL_IMPLEMENTATION_COLOR_READ_TYPE_OES
glGetIntegerv ( GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES , & internalformat ) ;
glGetIntegerv ( GL_IMPLEMENTATION_COLOR_READ_TYPE_OES , & type ) ;
# endif
// there are formats we don't support ATM
2024-03-20 19:35:52 +01:00
if ( GL_UNSIGNED_SHORT_4_4_4_4 = = type )
type = GL_UNSIGNED_SHORT_5_5_5_1 ;
2023-10-03 20:37:00 +02:00
# ifdef GL_EXT_read_format_bgra
2024-03-20 19:35:52 +01:00
else if ( GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT = = type )
type = GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT ;
2023-10-03 20:37:00 +02:00
# endif
}
2024-03-20 19:35:52 +01:00
IImage * newImage = 0 ;
if ( ( GL_RGBA = = internalformat )
2023-10-03 20:37:00 +02:00
# ifdef GL_EXT_read_format_bgra
2024-03-20 19:35:52 +01:00
| | ( GL_BGRA_EXT = = internalformat )
2023-10-03 20:37:00 +02:00
# endif
2024-03-20 19:35:52 +01:00
) {
if ( GL_UNSIGNED_BYTE = = type )
2023-10-03 20:37:00 +02:00
newImage = new CImage ( ECF_A8R8G8B8 , ScreenSize ) ;
else
newImage = new CImage ( ECF_A1R5G5B5 , ScreenSize ) ;
2024-03-20 19:35:52 +01:00
} else {
if ( GL_UNSIGNED_BYTE = = type )
2023-10-03 20:37:00 +02:00
newImage = new CImage ( ECF_R8G8B8 , ScreenSize ) ;
else
newImage = new CImage ( ECF_R5G6B5 , ScreenSize ) ;
}
2024-03-20 19:35:52 +01:00
u8 * pixels = static_cast < u8 * > ( newImage - > getData ( ) ) ;
if ( ! pixels ) {
2023-10-03 20:37:00 +02:00
newImage - > drop ( ) ;
return 0 ;
}
glReadPixels ( 0 , 0 , ScreenSize . Width , ScreenSize . Height , internalformat , type , pixels ) ;
// opengl images are horizontally flipped, so we have to fix that here.
2024-03-20 19:35:52 +01:00
const s32 pitch = newImage - > getPitch ( ) ;
u8 * p2 = pixels + ( ScreenSize . Height - 1 ) * pitch ;
u8 * tmpBuffer = new u8 [ pitch ] ;
for ( u32 i = 0 ; i < ScreenSize . Height ; i + = 2 ) {
2023-10-03 20:37:00 +02:00
memcpy ( tmpBuffer , pixels , pitch ) ;
memcpy ( pixels , p2 , pitch ) ;
memcpy ( p2 , tmpBuffer , pitch ) ;
pixels + = pitch ;
p2 - = pitch ;
}
2024-03-20 19:35:52 +01:00
delete [ ] tmpBuffer ;
2023-10-03 20:37:00 +02:00
2024-03-20 19:35:52 +01:00
if ( testGLError ( __LINE__ ) ) {
2023-10-03 20:37:00 +02:00
newImage - > drop ( ) ;
return 0 ;
}
return newImage ;
}
2024-03-20 19:35:52 +01:00
void COGLES1Driver : : removeTexture ( ITexture * texture )
2023-10-03 20:37:00 +02:00
{
CacheHandler - > getTextureCache ( ) . remove ( texture ) ;
CNullDriver : : removeTexture ( texture ) ;
}
//! Set/unset a clipping plane.
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : setClipPlane ( u32 index , const core : : plane3df & plane , bool enable )
2023-10-03 20:37:00 +02:00
{
if ( index > = MaxUserClipPlanes )
return false ;
2024-03-20 19:35:52 +01:00
UserClipPlane [ index ] = plane ;
2023-10-03 20:37:00 +02:00
enableClipPlane ( index , enable ) ;
return true ;
}
void COGLES1Driver : : uploadClipPlane ( u32 index )
{
// opengl needs an array of doubles for the plane equation
float clip_plane [ 4 ] ;
clip_plane [ 0 ] = UserClipPlane [ index ] . Normal . X ;
clip_plane [ 1 ] = UserClipPlane [ index ] . Normal . Y ;
clip_plane [ 2 ] = UserClipPlane [ index ] . Normal . Z ;
clip_plane [ 3 ] = UserClipPlane [ index ] . D ;
glClipPlanef ( GL_CLIP_PLANE0 + index , clip_plane ) ;
}
//! Enable/disable a clipping plane.
void COGLES1Driver : : enableClipPlane ( u32 index , bool enable )
{
if ( index > = MaxUserClipPlanes )
return ;
2024-03-20 19:35:52 +01:00
if ( enable ) {
if ( ! UserClipPlaneEnabled [ index ] ) {
2023-10-03 20:37:00 +02:00
uploadClipPlane ( index ) ;
glEnable ( GL_CLIP_PLANE0 + index ) ;
}
2024-03-20 19:35:52 +01:00
} else
2023-10-03 20:37:00 +02:00
glDisable ( GL_CLIP_PLANE0 + index ) ;
2024-03-20 19:35:52 +01:00
UserClipPlaneEnabled [ index ] = enable ;
2023-10-03 20:37:00 +02:00
}
core : : dimension2du COGLES1Driver : : getMaxTextureSize ( ) const
{
return core : : dimension2du ( MaxTextureSize , MaxTextureSize ) ;
}
GLenum COGLES1Driver : : getGLBlend ( E_BLEND_FACTOR factor ) const
{
2024-03-21 15:22:20 +01:00
static GLenum const blendTable [ ] = {
GL_ZERO ,
GL_ONE ,
GL_DST_COLOR ,
GL_ONE_MINUS_DST_COLOR ,
GL_SRC_COLOR ,
GL_ONE_MINUS_SRC_COLOR ,
GL_SRC_ALPHA ,
GL_ONE_MINUS_SRC_ALPHA ,
GL_DST_ALPHA ,
GL_ONE_MINUS_DST_ALPHA ,
GL_SRC_ALPHA_SATURATE ,
} ;
2023-10-03 20:37:00 +02:00
return blendTable [ factor ] ;
}
GLenum COGLES1Driver : : getZBufferBits ( ) const
{
GLenum bits = 0 ;
2024-03-20 19:35:52 +01:00
switch ( Params . ZBufferBits ) {
2023-10-03 20:37:00 +02:00
case 24 :
# if defined(GL_OES_depth24)
if ( queryGLESFeature ( COGLESCoreExtensionHandler : : IRR_GL_OES_depth24 ) )
bits = GL_DEPTH_COMPONENT24_OES ;
else
# endif
bits = GL_DEPTH_COMPONENT16 ;
break ;
case 32 :
# if defined(GL_OES_depth32)
if ( queryGLESFeature ( COGLESCoreExtensionHandler : : IRR_GL_OES_depth32 ) )
bits = GL_DEPTH_COMPONENT32_OES ;
else
# endif
bits = GL_DEPTH_COMPONENT16 ;
break ;
default :
bits = GL_DEPTH_COMPONENT16 ;
break ;
}
return bits ;
}
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : getColorFormatParameters ( ECOLOR_FORMAT format , GLint & internalFormat , GLenum & pixelFormat ,
GLenum & pixelType , void ( * * converter ) ( const void * , s32 , void * ) ) const
2023-10-03 20:37:00 +02:00
{
bool supported = false ;
internalFormat = GL_RGBA ;
pixelFormat = GL_RGBA ;
pixelType = GL_UNSIGNED_BYTE ;
* converter = 0 ;
2024-03-20 19:35:52 +01:00
switch ( format ) {
2023-10-03 20:37:00 +02:00
case ECF_A1R5G5B5 :
supported = true ;
internalFormat = GL_RGBA ;
pixelFormat = GL_RGBA ;
pixelType = GL_UNSIGNED_SHORT_5_5_5_1 ;
* converter = CColorConverter : : convert_A1R5G5B5toR5G5B5A1 ;
break ;
case ECF_R5G6B5 :
supported = true ;
internalFormat = GL_RGB ;
pixelFormat = GL_RGB ;
pixelType = GL_UNSIGNED_SHORT_5_6_5 ;
break ;
case ECF_R8G8B8 :
supported = true ;
internalFormat = GL_RGB ;
pixelFormat = GL_RGB ;
pixelType = GL_UNSIGNED_BYTE ;
break ;
case ECF_A8R8G8B8 :
supported = true ;
if ( queryGLESFeature ( COGLESCoreExtensionHandler : : IRR_GL_IMG_texture_format_BGRA8888 ) | |
2024-03-20 19:35:52 +01:00
queryGLESFeature ( COGLESCoreExtensionHandler : : IRR_GL_EXT_texture_format_BGRA8888 ) | |
queryGLESFeature ( COGLESCoreExtensionHandler : : IRR_GL_APPLE_texture_format_BGRA8888 ) ) {
2023-10-03 20:37:00 +02:00
internalFormat = GL_BGRA ;
pixelFormat = GL_BGRA ;
2024-03-20 19:35:52 +01:00
} else {
2023-10-03 20:37:00 +02:00
internalFormat = GL_RGBA ;
pixelFormat = GL_RGBA ;
* converter = CColorConverter : : convert_A8R8G8B8toA8B8G8R8 ;
}
pixelType = GL_UNSIGNED_BYTE ;
break ;
case ECF_D16 :
supported = true ;
internalFormat = GL_DEPTH_COMPONENT16 ;
pixelFormat = GL_DEPTH_COMPONENT ;
pixelType = GL_UNSIGNED_SHORT ;
break ;
case ECF_D32 :
# if defined(GL_OES_depth32)
2024-03-20 19:35:52 +01:00
if ( queryGLESFeature ( COGLESCoreExtensionHandler : : IRR_GL_OES_depth32 ) ) {
2023-10-03 20:37:00 +02:00
supported = true ;
internalFormat = GL_DEPTH_COMPONENT32_OES ;
pixelFormat = GL_DEPTH_COMPONENT ;
pixelType = GL_UNSIGNED_INT ;
}
# endif
break ;
case ECF_D24S8 :
# ifdef GL_OES_packed_depth_stencil
2024-03-20 19:35:52 +01:00
if ( queryGLESFeature ( COGLESCoreExtensionHandler : : IRR_GL_OES_packed_depth_stencil ) ) {
2023-10-03 20:37:00 +02:00
supported = true ;
internalFormat = GL_DEPTH24_STENCIL8_OES ;
pixelFormat = GL_DEPTH_STENCIL_OES ;
pixelType = GL_UNSIGNED_INT_24_8_OES ;
}
# endif
break ;
case ECF_R8 :
break ;
case ECF_R8G8 :
break ;
case ECF_R16 :
break ;
case ECF_R16G16 :
break ;
case ECF_R16F :
break ;
case ECF_G16R16F :
break ;
case ECF_A16B16G16R16F :
break ;
case ECF_R32F :
break ;
case ECF_G32R32F :
break ;
case ECF_A32B32G32R32F :
break ;
default :
break ;
}
# ifdef _IRR_IOS_PLATFORM_
if ( internalFormat = = GL_BGRA )
internalFormat = GL_RGBA ;
# endif
return supported ;
}
bool COGLES1Driver : : queryTextureFormat ( ECOLOR_FORMAT format ) const
{
GLint dummyInternalFormat ;
GLenum dummyPixelFormat ;
GLenum dummyPixelType ;
2024-03-20 19:35:52 +01:00
void ( * dummyConverter ) ( const void * , s32 , void * ) ;
2023-10-03 20:37:00 +02:00
return getColorFormatParameters ( format , dummyInternalFormat , dummyPixelFormat , dummyPixelType , & dummyConverter ) ;
}
2024-03-20 19:35:52 +01:00
bool COGLES1Driver : : needsTransparentRenderPass ( const irr : : video : : SMaterial & material ) const
2023-10-03 20:37:00 +02:00
{
return CNullDriver : : needsTransparentRenderPass ( material ) | | material . isAlphaBlendOperation ( ) ;
}
2024-03-20 19:35:52 +01:00
COGLES1CacheHandler * COGLES1Driver : : getCacheHandler ( ) const
2023-10-03 20:37:00 +02:00
{
return CacheHandler ;
}
} // end namespace
} // end namespace
# endif // _IRR_COMPILE_WITH_OGLES1_
namespace irr
{
namespace video
{
# ifndef _IRR_COMPILE_WITH_OGLES1_
class IVideoDriver ;
class IContextManager ;
# endif
2024-03-20 19:35:52 +01:00
IVideoDriver * createOGLES1Driver ( const SIrrlichtCreationParameters & params , io : : IFileSystem * io , IContextManager * contextManager )
2023-10-03 20:37:00 +02:00
{
# ifdef _IRR_COMPILE_WITH_OGLES1_
return new COGLES1Driver ( params , io , contextManager ) ;
# else
return 0 ;
# endif // _IRR_COMPILE_WITH_OGLES1_
}
} // end namespace
} // end namespace