ZED_UINT32 Game::PreInitialise( ) { m_pWindow = new ZED::System::LinuxWindow( ); if( !m_pWindow ) { zedTrace( "[Blood Bullet::Game::PreInitialise] <ERROR> " "Failed to create a new window\n" ); return ZED_FAIL; } m_pRenderer = new ZED::Renderer::LinuxRendererOGL3( ); if( !m_pRenderer ) { zedTrace( "[Blood Bullet::Game::PreInitialise] <ERROR> " "Failed to create a new renderer\n" ); return ZED_FAIL; } m_pInputManager = new ZED::System::LinuxInputManager( ); if( !m_pInputManager ) { zedTrace( "[Blood Bullet::Game::PreInitialise] <ERROR> " "Failed to create new input manager\n" ); return ZED_FAIL; } return ZED_OK; }
Matrix3x3 &Matrix3x3::Inverse( ) { // Compute determinate ZED_FLOAT32 Det = Determinate( ); if( ZED::Arithmetic::IsZero( Det ) ) { zedTrace( "Error: Matrix::Inverse -- singular matrix!" ); return *this; } // Set this matrix as the adjoint and multiply by 1/Det to get the // inverse ZED_FLOAT32 InverseDet = 1.0f / Det; *this = Adjoint( ); m_M[ 0 ] *= InverseDet; m_M[ 1 ] *= InverseDet; m_M[ 2 ] *= InverseDet; m_M[ 3 ] *= InverseDet; m_M[ 4 ] *= InverseDet; m_M[ 5 ] *= InverseDet; m_M[ 6 ] *= InverseDet; m_M[ 7 ] *= InverseDet; m_M[ 8 ] *= InverseDet; return *this; }
ZED_FLOAT32 DistanceSqSSE4a( const Vector4 &p_Vec1, const Vector4 &p_Vec2 ) { zedTrace( "You shouldn't have called this function\n\n" ); zedAssert( false ); return 2.0f; }
ZED_UINT32 WindowsRendererOGL3::SetHDC( const HDC &p_HDC ) { if( !p_HDC ) { zedTrace( "Failed to set the HDC" ); return ZED_FAIL; } m_HDC = p_HDC; return ZED_OK; }
ZED_UINT32 WindowsRendererOGL2::SetHDC( const HDC &p_HDC ) { if( !p_HDC ) { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|SetHDC]" " <ERROR> Failed to set HDC.\n" ); return ZED_FAIL; } m_HDC = p_HDC; return ZED_OK; }
Vector4 Vector4::operator/( const ZED_FLOAT32 p_Scalar ) const { // Just check if the scalar is zero... if( ZED::Arithmetic::IsZero( p_Scalar ) ) { zedAssert( ZED_FALSE ); zedTrace( "[ERROR | Vector4::operator/] Scalar is zero\n" ); return *this; } return Vector4( m_X/p_Scalar, m_Y/p_Scalar, m_Z/p_Scalar, m_W/p_Scalar ); }
//-------- // [Function] // Array constructor // [Parameters] // p_Capacity - The initial capacity of the array being created // p_Growth - How much to grow the array each time the capcaity is // exceeded // [Description] // Creates an array of size p_Capacity and sets the growth rate // when the array needs to allocate more slots. //-------- Array( ZED_UINT32 p_Capacity, ZED_UINT32 p_Growth ) { if( p_Capacity <= 0 ) { // Nothing to do zedTrace( "Error, array could not be created sucessfully\n" ); } else { m_pArray = new T[ p_Capacity ]; m_Capacity = p_Capacity; m_Growth = p_Growth; } }
ZED_UINT32 GLShader::SetAttributeTypes( const ZED_SHADER_ATTRIBUTE_MAP *p_pAttributes, const ZED_MEMSIZE p_Count ) { m_pAttributes = new ZED_SHADER_ATTRIBUTE_MAP[ p_Count ]; m_AttributeCount = p_Count; // If already linked, set the linked flag to zero if( ( m_Flags && 0x3 ) ) { m_Flags != 0x3; } // Copy over the attributes (need to copy the string in this // manner) ZED_MEMSIZE VertexIndex = 0; ZED_MEMSIZE FragmentIndex = 0; for( ZED_MEMSIZE i = 0; i < p_Count; i++ ) { switch( p_pAttributes[ i ].Type ) { case ZED_VERTEX_SHADER: { m_pAttributes[ i ].Index = VertexIndex; VertexIndex++; break; } case ZED_FRAGMENT_SHADER: { m_pAttributes[ i ].Index = FragmentIndex; FragmentIndex++; break; } default: { zedTrace( "[ZED::Renderer::GLShader::" "SetAttributeTypes] <WARN> Unknown attribute " "type.\n" ); break; } } m_pAttributes[ i ].Type = p_pAttributes[ i ].Type; ZED_MEMSIZE NameSize = strlen( p_pAttributes[ i ].pName ); m_pAttributes[ i ].pName = new char[ NameSize ]; strcpy( m_pAttributes[ i ].pName, p_pAttributes[ i ].pName ); } return ZED_OK; }
Vector4 &Vector4::operator/=( const ZED_FLOAT32 p_Scalar ) { if( ZED::Arithmetic::IsZero( p_Scalar ) ) { zedAssert( ZED_FALSE ); zedTrace( "[ERROR | Vector4::operator/=] Scalar is zero\n" ); return Vector4( m_X, m_Y, m_Z, m_W ); } m_X /= p_Scalar; m_Y /= p_Scalar; m_Z /= p_Scalar; m_W /= p_Scalar; return *this; }
void Vector2::Normalise( ) { ZED_FLOAT32 Length = m_X*m_X + m_Y*m_Y; zedAssert( ZED_FALSE ); if( ZED::Arithmetic::IsZero( Length ) ) { zedTrace( "[ZED:Arithmetic:Vector2:Normalise] <WARN> " "Magnitude is zero\n" ); } else { ZED_FLOAT32 Factor = 1.0f / Length; m_X *= Factor; m_Y *= Factor; } }
ZED_UINT32 GLShader::AttachShaders( ) { // Get rid of the program as it's now being re-compiled if( m_ProgramID != 0 ) { // Also, detach the shaders if they weren't already if( zglIsShader( m_VertexID ) ) { zglDetachShader( m_ProgramID, m_VertexID ); } if( zglIsShader( m_FragmentID ) ) { zglDetachShader( m_ProgramID, m_FragmentID ); } zglDeleteProgram( m_ProgramID ); } // Now that (hopefully) the shaders are good to go, attach // them, linking is done upon calling Link (naturally) m_ProgramID = zglCreateProgram( ); // Was the program created? if( m_ProgramID == 0 ) { zedAssert( ZED_FALSE ); zedTrace( "[ ZED | GLSahder | Activate | ERROR] " "Program was not created" ); return ZED_GRAPHICS_ERROR; } if( zglIsShader( m_VertexID ) ) { zglAttachShader( m_ProgramID, m_VertexID ); } if( zglIsShader( m_FragmentID ) ) { zglAttachShader( m_ProgramID, m_FragmentID ); } return ZED_OK; }
ZED_UINT32 GLShader::SetVariable( const ZED_UINT32 p_Index, const void *p_pValue ) { switch( m_pUniformMap[ p_Index ].Type ) { case ZED_FLOAT1: { zglUniform1f( m_pUniformMap[ p_Index ].Location, *( reinterpret_cast< GLfloat * >( &p_pValue ) ) ); break; } case ZED_FLOAT3: { zglUniform3fv( m_pUniformMap[ p_Index ].Location, 1, static_cast< const GLfloat * >( p_pValue ) ); break; } case ZED_INT1: { zglUniform1i( m_pUniformMap[ p_Index ].Location, *( reinterpret_cast< const GLint * >( &p_pValue ) ) ); break; } case ZED_MAT4X4: { zglUniformMatrix4fv( m_pUniformMap[ p_Index ].Location, 1, ZED_FALSE, static_cast< const GLfloat * >( p_pValue ) ); break; } default: { zedTrace( "[ZED::Renderer::GLShader::SetVariable] <WARN> " "Unknown uniform type.\n" ); break; } } return ZED_OK; }
ZED_UINT32 XboxRenderer::SetMode( const ZED_UINT32 p_Stage, const ZED_VIEWMODE p_Mode ) { D3DVIEWPORT8 Viewport; if( !m_Running ) { return ZED_FAIL; } if( ( m_Stage > 3 ) || ( m_Stage < 0 ) ) { m_Stage = 0; } if( m_Mode != p_Mode ) { m_Mode = p_Mode; } // Flush all Vertex Caches before the mode switch makes things // not work //UNCOMMENTm_pVertexManager->ForceFlushAll( ); // If the mode is in screen space, use its matrices instead if( p_Mode == ZED_VIEW_SCREEN ) { Viewport.X = 0; Viewport.Y = 0; Viewport.Width = m_Canvas.GetWidth( ); Viewport.Height = m_Canvas.GetHeight( ); Viewport.MinZ = 0.0f; Viewport.MaxZ = 1.0f; if( FAILED( m_pDevice->SetViewport( &Viewport ) ) ) { return ZED_FAIL; } } // Perspective or orthogonal projection else { m_Stage = p_Stage; // Setup the viewport Viewport.X = m_Viewport[ p_Stage ].X; Viewport.Y = m_Viewport[ p_Stage ].Y; Viewport.Width = m_Viewport[ p_Stage ].Width; Viewport.Height = m_Viewport[ p_Stage ].Height; Viewport.MinZ = 0.0f; Viewport.MaxZ = 1.0f; if( FAILED( m_pDevice->SetViewport( &Viewport ) ) ) { zedTrace( "XboxRenderer::SetMode - Could not set viewport for " "stage %d", p_Stage ); return ZED_FAIL; } CalcViewProjMatrix( ); CalcWorldViewProjMatrix( ); } return ZED_OK; }
ZED_UINT32 WindowsRendererOGL3::Create( GraphicsAdapter *p_pAdapter, const CanvasDescription &p_Canvas ) { // Grab the canvas m_Canvas = p_Canvas; if( !m_HDC ) { zedTrace( "The HDC has not been set" ); return ZED_FAIL; } switch( m_Canvas.GetBPP( ) ) { case ZED_FORMAT_A8R8G8B8: { m_PixelFormat.cColorBits = 32; m_PixelFormat.iPixelType = PFD_TYPE_RGBA; break; } default: { zedTrace( "Failed to set the colour format" ); return ZED_FAIL; } } switch( m_Canvas.GetDepthStencil( ) ) { case ZED_FORMAT_D24S8: { m_PixelFormat.cDepthBits = 24; m_PixelFormat.cStencilBits = 8; break; } default: { zedTrace( "Failed to set the depth/stencil format" ); return ZED_FAIL; } } m_PixelFormat.nSize = sizeof( PIXELFORMATDESCRIPTOR ); m_PixelFormat.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA; m_PixelFormat.iLayerType = PFD_MAIN_PLANE; // Select the most appropriate pixel format ZED_UINT32 PixelFormat = ChoosePixelFormat( m_HDC, &m_PixelFormat ); if( PixelFormat == 0 ) { zedTrace( "Failed to choose the pixel format" ); return ZED_FAIL; } ZED_BOOL PixelResult = SetPixelFormat( m_HDC, PixelFormat, &m_PixelFormat ); if( PixelResult == ZED_FALSE ) { zedTrace( "Failed to set the pixel format\n" ); return ZED_FAIL; } // Create a fallback GL RC (this is necessary, anyway) HGLRC TempHGLRC = wglCreateContext( m_HDC ); if( wglMakeCurrent( m_HDC, TempHGLRC ) == ZED_FALSE ) { zedTrace( "Could not make the HGLRC current\n" ); return ZED_FAIL; } m_Ext = GLExtender( m_HDC ); // List of OpenGL versions to try and use for the context creation const ZED_INT32 OGLVersions[ ] = { 3, 0, 3, 1, 3, 2, 3, 3, }; ZED_INT32 OpenGLVersion [ 2 ]; glGetIntegerv( GL_MAJOR_VERSION, &OpenGLVersion[ 0 ] ); glGetIntegerv( GL_MINOR_VERSION, &OpenGLVersion[ 1 ] ); zedTrace( "OpenGL Version in use: " " [ %d.%d ]\n", OpenGLVersion[ 0 ], OpenGLVersion[ 1 ] ); // Set the OpenGL Version member m_GLVersion.Major = OpenGLVersion[ 0 ]; m_GLVersion.Minor = OpenGLVersion[ 1 ]; m_Ext.Initialise( m_GLVersion ); ZED_INT32 Major = OGLVersions[ ( sizeof( OGLVersions ) / 4 ) - 2 ], Minor = OGLVersions[ ( sizeof( OGLVersions ) / 4 ) - 1 ]; ZED_INT32 Attribs[ ] = { WGL_CONTEXT_MAJOR_VERSION_ARB, Major, WGL_CONTEXT_MINOR_VERSION_ARB, Minor, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, #if ZED_BUILD_DEBUG WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB, #endif 0 }; // Attempt to create an OGL 3.3 context if( m_Ext.IsWindowExtSupported( "WGL_ARB_create_context" ) == ZED_TRUE ) { // Try to use the highest available OpenGL version, keep trying // until no more can be tried for( ZED_INT32 i = 1; i < ( sizeof( OGLVersions ) / 4 ) / 2; i++ ) { m_HGLRC = zglCreateContextAttribs( m_HDC, 0, Attribs ); wglMakeCurrent( NULL, NULL ); wglDeleteContext( TempHGLRC ); wglMakeCurrent( m_HDC, m_HGLRC ); if( !m_HGLRC ) { // Loop, again zedTrace( "Failed to create GL Render Context for Version" " [ %d.%d ]\n", Major, Minor ); char Message[ 255 ] = { '\0' }; sprintf( Message, "Failed to create GL Render Context for Version" " [ %d.%d ]", Major, Minor ); MessageBoxA( NULL, Message, "[ZED|Renderer|WindowsRendererOGL3] ERROR", MB_ICONERROR | MB_OK ); } else { ZED_INT32 OpenGLVersion [ 2 ]; glGetIntegerv( GL_MAJOR_VERSION, &OpenGLVersion[ 0 ] ); glGetIntegerv( GL_MINOR_VERSION, &OpenGLVersion[ 1 ] ); // Report the OpenGL version in use const GLubyte *GLVersionString = glGetString( GL_VERSION ); zedTrace( "Created GL Render Context for Version" " [ %d.%d ]\n", OpenGLVersion[ 0 ], OpenGLVersion[ 1 ] ); char Message[ 255 ] = { '\0' }; sprintf( Message, "Created GL Render Context for Version" " [ %d.%d ]\n", Major, Minor ); // Break out break; } Major = OGLVersions[ ( sizeof( OGLVersions ) / 4 ) - ( 2 + ( i*2) ) ]; Minor = OGLVersions[ ( sizeof( OGLVersions ) / 4 ) - ( 1 + ( i*2 ) ) ]; // Use the next major and minor versions of OpenGL in the // list Attribs[ 1 ] = Major; Attribs[ 3 ] = Minor; } } else { m_HGLRC = TempHGLRC; } if( !m_HGLRC ) { zedTrace( "Failed to create GL Render Context" ); return ZED_FAIL; } // Set the viewport ResizeCanvas( m_Canvas.GetWidth( ), m_Canvas.GetHeight( ) ); // Create the Vertex Cache Manager m_pVertexCacheManager = new GLVertexCacheManager( ); return ZED_OK; }
ZED_FLOAT32 MagnitudeSqSSE4a( const Vector3 &p_Vec ) { zedTrace( "You shouldn't have called this function\n\n" ); zedAssert( false ); return 2.0f; }
ZED_UINT32 Game::Initialise( ) { m_pWindow = new ZED::System::LinuxWindow( ); if( !m_pWindow ) { zedTrace( "[Papanak::Game::Initialise] <ERROR> " "Failed to create a new window\n" ); return ZED_FAIL; } m_pRenderer = new ZED::Renderer::LinuxRendererOGL3( ); if( !m_pRenderer ) { zedTrace( "[Papanak::Game::Initialise] <ERROR> " "Failed to create a new renderer\n" ); return ZED_FAIL; } ZED_UINT32 X = 0, Y = 0, Width = 1280, Height = 720; ZED_UINT32 WindowStyle = ZED_WINDOW_STYLE_MINIMISE | ZED_WINDOW_STYLE_CLOSE | ZED_WINDOW_STYLE_TITLEBAR | ZED_WINDOW_STYLE_MOVE; if( m_pWindow->Create( X, Y, Width, Height, 0, 0, WindowStyle ) != ZED_OK ) { zedTrace( "[Papanak::Game::Initialise] <ERROR> " "Failed to create window\n" ); return ZED_FAIL; } m_Canvas.Width( Width ); m_Canvas.Height( Height ); m_Canvas.BackBufferCount( 1 ); m_Canvas.ColourFormat( ZED_FORMAT_ARGB8 ); m_Canvas.DepthStencilFormat( ZED_FORMAT_D24S8 ); if( m_pRenderer->Create( m_Canvas, ( *m_pWindow ) ) != ZED_OK ) { zedTrace( "[Papanak::Game::Initialise] <ERROR> " "Failed to create renderer\n" ); return ZED_FAIL; } m_pRenderer->ClearColour( 0.125, 0.0f, 0.125f ); m_pRenderer->RenderState( ZED_RENDERSTATE_CULLMODE, ZED_CULLMODE_NONE ); m_pRenderer->RenderState( ZED_RENDERSTATE_DEPTH, ZED_ENABLE ); ZED::System::WINDOWDATA WindowData = m_pWindow->WindowData( ); m_pInputManager = new ZED::System::LinuxInputManager( WindowData ); if( !m_pInputManager ) { zedTrace( "[Papanak::Game::Initialise] <ERROR> " "Failed to create new input manager\n" ); } m_pInputManager->AddDevice( &m_Keyboard ); return ZED_OK; }
ZED_UINT32 GLShader::SetUniformTypes( const ZED_SHADER_UNIFORM_MAP *p_pTypes, const ZED_MEMSIZE p_Count ) { // Link before getting the location // Check if the shader has been linked already if( !( m_Flags && 0x3 ) ) { // Link the program m_ProgramID = zglCreateProgram( ); if( m_ProgramID == 0 ) { zedTrace( "[ZED::Renderer::GLShader::Activate] <ERROR> " "Failed to create program for shader.\n" ); return ZED_GRAPHICS_ERROR; } // Attach shader if( zglIsShader( m_VertexID ) ) { zglAttachShader( m_ProgramID, m_VertexID ); } if( zglIsShader( m_FragmentID ) ) { zglAttachShader( m_ProgramID, m_FragmentID ); } // Need to set up the attributes (if any) for( ZED_MEMSIZE i = 0; i < m_AttributeCount; i++ ) { switch( m_pAttributes[ i ].Type ) { case ZED_VERTEX_SHADER: { zglBindAttribLocation( m_ProgramID, m_pAttributes[ i ].Index, m_pAttributes[ i ].pName ); break; } case ZED_FRAGMENT_SHADER: { zglBindFragDataLocation( m_ProgramID, m_pAttributes[ i ].Index, m_pAttributes[ i ].pName ); break; } default: { zedTrace( "[ZED::Renderer::GLShader::" "SetUniformTypes] <WARN> Unknown shader " "attribute type.\n" ); } } } zglLinkProgram( m_ProgramID ); // Was this linked properly? GLint Error; zglGetProgramiv( m_ProgramID, GL_LINK_STATUS, &Error ); if( Error == GL_FALSE ) { zedTrace( "[ZED::Renderer::GLShader::SetUnifromTypes] " "<ERROR> Failed to link program.\n" ); GLsizei Length = 0; GLchar *pLog = ZED_NULL; zglGetProgramiv( m_ProgramID, GL_INFO_LOG_LENGTH, &Length ); pLog = new GLchar[ Length ]; zglGetProgramInfoLog( m_ProgramID, Length, &Length, pLog ); zedTrace( "%s\n", pLog ); delete [ ] pLog; return ZED_FAIL; } // Set the linked status m_Flags |= 0x3; } // NO ERROR CHECKING! m_pUniformMap = new ZED_SHADER_UNIFORM_MAP[ p_Count ]; // Find each location and set it up for( ZED_MEMSIZE i = 0; i < p_Count; i++ ) { m_pUniformMap[ i ].Location = zglGetUniformLocation( m_ProgramID, p_pTypes[ i ].pName ); m_pUniformMap[ i ].Index = p_pTypes[ i ].Index; m_pUniformMap[ i ].Type = p_pTypes[ i ].Type; // For debugging purposes, keep the name #ifdef ZED_BUILD_DEBUG m_pUniformMap[ i ].pName = p_pTypes[ i ].pName; #endif } return ZED_OK; }
ZED_UINT32 GLShader::Compile( const ZED_CHAR8 **p_ppData, const ZED_SHADER_TYPE p_Type, const ZED_BOOL p_FromFile ) { // Common locals ZED_INT32 CompileStatus = 0; // If p_FromFile is true, p_Data is the filepath if( p_FromFile == ZED_FALSE ) { // Get the Data as a GLchar pointer pointer const GLchar **ppData = reinterpret_cast< const GLchar** >( p_ppData ); if( p_Type == ZED_VERTEX_SHADER ) { // Check for a valid shader if( !zglIsShader( m_VertexID ) ) { // Add the shader type this->AddType( p_Type ); } zglShaderSource( m_VertexID, 1, ppData, ZED_NULL ); zglCompileShader( m_VertexID ); // Did the shader compilation go well? zglGetShaderiv( m_VertexID, GL_COMPILE_STATUS, &CompileStatus ); if( !CompileStatus ) { // Something went horribly wrong... zedAssert( ZED_FALSE ); ZED_INT32 LogLength; GLchar *pLog = ZED_NULL; zglGetShaderiv( m_VertexID, GL_INFO_LOG_LENGTH, &LogLength ); pLog = new GLchar[ LogLength ]; zglGetShaderInfoLog( m_VertexID, LogLength, &LogLength, pLog ); zedTrace( "[GLShader | Compile | FAILED] Vertex Shader" " Log:\n\t%s\n", pLog ); delete [ ] pLog; } /* #ifdef ZED_BUILD_DEBUG else { // Copy the source for debugging // Initialise the inner and outer chars to something // other than '\0' ZED_CHAR8 OuterChar = **p_ppData, InnerChar = *p_ppData[ 0 ]; while( OuterChar != '\0' ) { // Copy the lines over to m_ppVertexSrc while( InnerChar != '\0' ) { } } } #endif */ } if( p_Type == ZED_FRAGMENT_SHADER ) { if( !zglIsShader( m_FragmentID ) ) { this->AddType( p_Type ); } zglShaderSource( m_FragmentID, 1, ppData, ZED_NULL ); zglCompileShader( m_FragmentID ); // Did the shader compile go well? zglGetShaderiv( m_FragmentID, GL_COMPILE_STATUS, &CompileStatus ); if( !CompileStatus ) { // Something went horribly wrong zedAssert( ZED_FALSE ); ZED_INT32 LogLength; GLchar *pLog = ZED_NULL; zglGetShaderiv( m_FragmentID, GL_INFO_LOG_LENGTH, &LogLength ); pLog = new GLchar[ LogLength ]; zglGetShaderInfoLog( m_FragmentID, LogLength, &LogLength, pLog ); zedTrace( "[ZED | GLShader | Compile( ) | FAILED] " "Fragment Shader Log:\n\t%s\n", pLog ); delete pLog; } } } else { // Extract the source from the file passed in FILE *pFile = fopen( *p_ppData, "rb" ); if( pFile == ZED_NULL ) { zedTrace( "[ZED::Renderer::GLShader::Compile] <ERROR> " "Failed to open file: %s\n", *p_ppData ); return ZED_FAIL; } GLint CompileStatus = 0; // Read the source from the file ZED_MEMSIZE FileSize = 0; // Get the file size for zglShaderSource fseek( pFile, 0, SEEK_END ); FileSize = ftell( pFile ); rewind( pFile ); GLchar **ppSource = new GLchar*[ 1 ]; ppSource[ 0 ] = new GLchar[ FileSize ]; fread( ppSource[ 0 ], sizeof( ZED_BYTE ), FileSize, pFile ); // Print the source char *pPrintSource = ZED_NULL; pPrintSource = new char[ FileSize+1 ]; strncpy( pPrintSource, ppSource[ 0 ], FileSize ); // Add a zero to ensure when tracing, it doesn't go into unkown // memory space pPrintSource[ FileSize ] = '\0'; zedTrace( "[ZED::Renderer::GLShader::Compile] <INFO> " "Size: %d\nSource\n******\n%s\n", FileSize, pPrintSource ); delete [ ] pPrintSource; pPrintSource = ZED_NULL; // Compile as a vertex, fragment, or geometry shader if( p_Type == ZED_VERTEX_SHADER ) { // Check for a valid vertex shader ID if( !zglIsShader( m_VertexID ) ) { // Create a shader this->AddType( p_Type ); } zglShaderSource( m_VertexID, 1, const_cast< const GLchar ** >( ppSource ), reinterpret_cast< const GLint * >( &FileSize ) ); zglCompileShader( m_VertexID ); // Was the compile successful? zglGetShaderiv( m_VertexID, GL_COMPILE_STATUS, &CompileStatus ); if( !CompileStatus ) { GLsizei LogLength = 0; GLchar *pLog = ZED_NULL; zglGetShaderiv( m_VertexID, GL_INFO_LOG_LENGTH, &LogLength ); pLog = new GLchar[ LogLength ]; zglGetShaderInfoLog( m_VertexID, LogLength, &LogLength, pLog ); zedTrace( "[ZED::Renderer::GLShader::Compile] <ERROR> " "Vertex Shader Log:\n%s\n", pLog ); delete [ ] pLog; if( pFile != ZED_NULL ) { fclose( pFile ); pFile = ZED_NULL; } if( ppSource[ 0 ] != ZED_NULL ) { delete [ ] ppSource[ 0 ]; delete [ ] ppSource; } return ZED_GRAPHICS_ERROR; } } if( p_Type == ZED_FRAGMENT_SHADER ) { // Check for a valid vertex shader ID if( !zglIsShader( m_FragmentID ) ) { // Create a shader this->AddType( p_Type ); } zglShaderSource( m_FragmentID, 1, const_cast< const GLchar ** >( ppSource ), reinterpret_cast< const GLint * >( &FileSize ) ); zglCompileShader( m_FragmentID ); // Was the compile successful? zglGetShaderiv( m_FragmentID, GL_COMPILE_STATUS, &CompileStatus ); if( !CompileStatus ) { GLsizei LogLength = 0; GLchar *pLog = ZED_NULL; zglGetShaderiv( m_FragmentID, GL_INFO_LOG_LENGTH, &LogLength ); pLog = new GLchar[ LogLength ]; zglGetShaderInfoLog( m_FragmentID, LogLength, &LogLength, pLog ); zedTrace( "[ZED::Renderer::GLShader::Compile] <ERROR> " "Fragment Shader Log:\n%s\n", pLog ); delete [ ] pLog; if( pFile != ZED_NULL ) { fclose( pFile ); pFile = ZED_NULL; } if( ppSource[ 0 ] != ZED_NULL ) { delete [ ] ppSource[ 0 ]; delete [ ] ppSource; } return ZED_GRAPHICS_ERROR; } } if( ppSource[ 0 ] != ZED_NULL ) { delete [ ] ppSource[ 0 ]; ppSource[ 0 ] = ZED_NULL; delete [ ] ppSource; ppSource = ZED_NULL; } if( pFile != ZED_NULL ) { fclose( pFile ); pFile = ZED_NULL; } } return ZED_OK; }
void NormaliseSSE41( Vector3 &p_Vec ) { zedTrace( "You shouldn't have called this function\n\n" ); zedAssert( false ); }
ZED_UINT32 GLShader::Activate( ) { // Check if the shader has been linked already if( !( m_Flags && 0x3 ) ) { m_ProgramID = zglCreateProgram( ); // Something went wrong if( m_ProgramID == 0 ) { zedTrace( "[ZED::Renderer::GLShader::Activate] <ERROR> " "Failed to create program for shader.\n" ); return ZED_GRAPHICS_ERROR; } // Attach shaders if( zglIsShader( m_VertexID ) ) { zglAttachShader( m_ProgramID, m_VertexID ); } if( zglIsShader( m_FragmentID ) ) { zglAttachShader( m_ProgramID, m_FragmentID ); } // Need to set up the attributes (if any) for( ZED_MEMSIZE i = 0; i < m_AttributeCount; i++ ) { switch( m_pAttributes[ i ].Type ) { case ZED_VERTEX_SHADER: { zglBindAttribLocation( m_ProgramID, m_pAttributes[ i ].Index, m_pAttributes[ i ].pName ); break; } case ZED_FRAGMENT_SHADER: { zglBindFragDataLocation( m_ProgramID, m_pAttributes[ i ].Index, m_pAttributes[ i ].pName ); break; } default: { zedTrace( "[ZED::Renderer::GLShader::" "SetUniformTypes] <WARN> Unknown shader " "attribute type.\n" ); } } } zglLinkProgram( m_ProgramID ); // Was this linked properly? GLint Error; zglGetProgramiv( m_ProgramID, GL_LINK_STATUS, &Error ); if( Error == GL_FALSE ) { zedTrace( "[ZED::Renderer::GLShader::Activate] <ERROR> " "Failed to link program.\n" ); GLsizei Length = 0; GLchar *pLog = ZED_NULL; zglGetProgramiv( m_ProgramID, GL_INFO_LOG_LENGTH, &Length ); pLog = new GLchar[ Length ]; zglGetProgramInfoLog( m_ProgramID, Length, &Length, pLog ); zedTrace( "%s\n", pLog ); delete [ ] pLog; return ZED_FAIL; } // Set the linked flag m_Flags |= 0x3; } // NO CHECKING OF VALID PROGRAM! if( zglIsProgram( m_ProgramID ) ) { zglUseProgram( m_ProgramID ); } return ZED_OK; }
ZED_UINT32 XboxRenderer::Create( GraphicsAdapter *p_Adapter, const CanvasDescription &p_Canvas ) { ZED_UINT32 ReturnStatus = ZED_FAIL; if( NULL ==( m_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) ) { return ZED_FAIL; } m_Canvas = p_Canvas; m_PresentParams.BackBufferWidth = m_Canvas.GetWidth( ); m_PresentParams.BackBufferHeight = m_Canvas.GetHeight( ); m_PresentParams.BackBufferCount = m_Canvas.GetBackBufferCount( ); m_PresentParams.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Get the colour switch( m_Canvas.GetBPP( ) ) { case ZED_FORMAT_ARGB8: { m_PresentParams.BackBufferFormat = D3DFMT_A8R8G8B8; ReturnStatus = ZED_OK; break; } default: { zedTrace( "Failed to set the back buffer format" ); return ZED_FAIL; } } // Get the Depth/Stencil switch( m_Canvas.GetDepthStencil( ) ) { case ZED_FORMAT_D24S8: { m_PresentParams.EnableAutoDepthStencil = ZED_TRUE; m_PresentParams.AutoDepthStencilFormat = D3DFMT_D24S8; ReturnStatus = ZED_OK; break; } default: { zedTrace( "Failed to set the depth/stencil format" ); return ZED_FAIL; } } // This needs to be exposed, but how? m_PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; if( FAILED( m_pD3D->CreateDevice( 0, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_PresentParams, &m_pDevice ) ) ) { zedTrace( "Failed to create device" ); return ZED_FAIL; } #ifdef ZED_BUILD_DEBUG ZED_UINT32 ModeCount = m_pD3D->GetAdapterModeCount( D3DADAPTER_DEFAULT ); D3DDISPLAYMODE *pModes = new D3DDISPLAYMODE[ ModeCount ]; for( ZED_MEMSIZE i = 0; i < ModeCount; i++ ) { if( FAILED( m_pD3D->EnumAdapterModes( D3DADAPTER_DEFAULT, i, &pModes[ i ] ) ) ) { delete pModes; return ZED_FAIL; } ZED_MEMSIZE Len = strlen( FlagsToString( pModes[ i ].Flags ) ); char *pFlags; if( Len > 0 ) { pFlags = new char[ Len ]; strncpy( pFlags, FlagsToString( pModes[ i ].Flags ), Len-1 ); pFlags[ Len-1 ] = '\0'; } if( Len == 0 ) { // Add one for null-termination Len = strlen( "NO FLAGS" )+1; pFlags = new char[ Len ]; strncpy( pFlags, "NO FLAGS", Len ); } // Print out the adapter's capabilities zedTrace( "Mode %d:\n" "\tWidth: %d | Height: %d | Refresh Rate: %d | Flags: %s \n", i, pModes[ i ].Width, pModes[ i ].Height, pModes[ i ].RefreshRate, pFlags ); delete pFlags; } #endif return ReturnStatus; }
void Normalise3DNOWGEO( Vector4 &p_Vec ) { zedTrace( "You shouldn't have called this function\n\n" ); zedAssert( false ); }
ZED_UINT32 WindowsRendererOGL2::Create( GraphicsAdapter *p_pAdapter, const CanvasDescription &p_Canvas ) { // Grab the canvas for later m_Canvas = p_Canvas; if( !m_HDC ) { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|Create]" " <ERROR> The HDC has not been set.\n" ); return ZED_FAIL; } switch( m_Canvas.GetBPP( ) ) { case ZED_FORMAT_A8R8G8B8: { m_PixelFormat.cColorBits = 32; m_PixelFormat.iPixelType = PFD_TYPE_RGBA; break; } default: { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|Create]" " <ERROR> Unsupported colour depth.\n" ); return ZED_FAIL; } } switch( m_Canvas.GetDepthStencil( ) ) { case ZED_FORMAT_D24S8: { m_PixelFormat.cDepthBits = 24; m_PixelFormat.cStencilBits = 8; break; } default: { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|Create]" " <ERROR> Unsupported depth/stencil format.\n" ); return ZED_FAIL; } } m_PixelFormat.nSize = sizeof( PIXELFORMATDESCRIPTOR ); m_PixelFormat.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA; m_PixelFormat.iLayerType = PFD_MAIN_PLANE; // Select the most appropriate pixel format ZED_UINT32 PixelFormat = ChoosePixelFormat( m_HDC, &m_PixelFormat ); if( PixelFormat == 0 ) { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|Create]" " <ERROR> Failed to choose the preferred pixel format.\n" ); } ZED_BOOL PixelResult = SetPixelFormat( m_HDC, PixelFormat, &m_PixelFormat ); if( PixelResult == ZED_FALSE ) { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|Create]" " <ERROR> Failed to set the preferred pixel format.\n" ); return ZED_FAIL; } m_HGLRC = wglCreateContext( m_HDC ); if( wglMakeCurrent( m_HDC, m_HGLRC ) == ZED_FALSE ) { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|Create]" " <ERROR> Failed ro create the OpenGL Render Context.\n" ); return ZED_FAIL; } // Make absolutely certain the GL RC is fine if( !m_HGLRC ) { zedTrace( "[ZED|Renderer|WindowsRendererOGL2|Create]" " <ERROR> Failed to create GL Render Context.\n" ); return ZED_FAIL; } m_Ext = GLExtender( m_HDC ); // Get the OpenGL version, both for testing and for GL extensions const GLubyte *GLVersionString = glGetString( GL_VERSION ); ZED_INT32 OpenGLVersion[ 2 ]; // Extract the version number from the string OpenGLVersion[ 0 ] = ( GLVersionString[ 0 ] - 48 ); OpenGLVersion[ 1 ] = ( GLVersionString[ 2 ] - 48 ); zedTrace( "<INFO> OpenGL Version in use: " " [ %d.%d ]\n", OpenGLVersion[ 0 ], OpenGLVersion[ 1 ] ); // Set the class-side OpenGL Version information m_GLVersion.Major = OpenGLVersion[ 0 ]; m_GLVersion.Minor = OpenGLVersion[ 1 ]; if( m_Ext.Initialise( m_GLVersion ) != ZED_OK ) { zedAssert( ZED_FALSE ); zedTrace( "<ERROR> Failed to initialise OpenGL extensions.\n" ); MessageBoxA( NULL, "Failed to initialise OpenGL extensions", "ZED|Error", MB_OK ); return ZED_FAIL; } zedTrace( "Created GL Render Context for Version" " [ %s ]\n", GLVersionString ); char Message[ 255 ] = { '\0' }; sprintf( Message, "Created GL Render Context for Version" " [ %d.%d ]\n", OpenGLVersion[ 0 ], OpenGLVersion[ 1 ] ); /* MessageBoxA( NULL, Message, "[ZED|Renderer|WindowsRendererOGL3] INFO", MB_ICONINFORMATION | MB_OK );*/ // Set up the viewport ResizeCanvas( m_Canvas.GetWidth( ), m_Canvas.GetHeight( ) ); return ZED_OK; }
ZED_UINT32 Model::Load( const char *p_pFilename ) { HANDLE ModelFile = ZED_NULL; ModelFile = CreateFile( p_pFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, ZED_NULL ); if( ModelFile == ZED_NULL ) { zedTrace( "Failed to load model: %s\n", p_pFilename ); return ZED_FAIL; } // TEMP! // Don't care about the header's correctness SetFilePointer( ModelFile, 87, ZED_NULL, FILE_CURRENT ); // !TEMP // Get the amount of vertices to process ZED_UINT64 RawVertCount = 0; DWORD ReadIn = 0; ReadFile( ModelFile, &RawVertCount, sizeof( ZED_UINT64 ), &ReadIn, NULL ); ZED_UINT64 VertCount = ( ( RawVertCount/4 )/3 )/2; m_pVertexData = ( LPVERTEX_V1 )malloc( VertCount*sizeof( VERTEX_V1 ) );/*XPhysicalAlloc( VertCount*sizeof( VERTEX_V1 ), MAXULONG_PTR, 0, PAGE_READWRITE );*/ ReadFile( ModelFile, m_pVertexData, VertCount*sizeof( VERTEX_V1 ), &ReadIn, NULL ); if( ReadIn != VertCount*sizeof( VERTEX_V1 ) ) { zedTrace( "[ERROR] Failed to read the vertex data for %s\n", p_pFilename ); return ZED_FAIL; } // Skip over the indices and face normal header SetFilePointer( ModelFile, 12, ZED_NULL, FILE_CURRENT ); // NOT WORKING!! ZED_UINT64 RawFaceCount = 0; // Should be 268272 for GraveGuy.zed ReadFile( ModelFile, &RawFaceCount, sizeof( ZED_UINT64 ), &ReadIn, NULL ); ZED_UINT64 FaceCount = ( RawFaceCount/12 )/3; m_pFaceData = ( LPFACE_V1 )malloc( sizeof( LPFACE_V1 )*FaceCount );/*XPhysicalAlloc( sizeof( ZED_UINT16 )*FaceCount, MAXULONG_PTR, MAXULONG_PTR, PAGE_READWRITE );*/ ReadFile( ModelFile, m_pFaceData, sizeof( FACE_V1 )*FaceCount, &ReadIn, NULL ); if( ReadIn != sizeof( FACE_V1 )*FaceCount ) { zedTrace( "[ERROR] Failed to read the face data for %s\n", p_pFilename ); } CloseHandle( ModelFile ); // Set up the index and vertex buffers m_pVertexBuffer = new IDirect3DVertexBuffer8( ); m_pIndexBuffer = new IDirect3DIndexBuffer8( ); XGSetVertexBufferHeader( 0, 0, 0, 0, m_pVertexBuffer, 0 ); void *pVertData = XPhysicalAlloc( RawVertCount, MAXULONG_PTR, MAXULONG_PTR, PAGE_READWRITE ); m_pVertexBuffer->Register( pVertData ); memcpy( pVertData, m_pVertexData, RawVertCount ); XGSetIndexBufferHeader( 0, 0, D3DFMT_INDEX16, 0, m_pIndexBuffer, 0 ); return ZED_OK; }