mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-19 04:26:52 -04:00
Initial work on texture packs.
This commit is contained in:
parent
689cfccb0c
commit
8f8ae2f36f
@ -258,6 +258,7 @@
|
||||
<Compile Include="Rendering\Env\EnvRenderer.cs" />
|
||||
<Compile Include="Rendering\Env\MapBordersRenderer.cs" />
|
||||
<Compile Include="Rendering\Env\MinimalEnvRenderer.cs" />
|
||||
<Compile Include="Rendering\Env\SkyboxRenderer.cs" />
|
||||
<Compile Include="Rendering\Env\StandardEnvRenderer.cs" />
|
||||
<Compile Include="Rendering\Env\WeatherRenderer.cs" />
|
||||
<Compile Include="Rendering\MapRenderer.Occlusion.cs" />
|
||||
|
@ -9,6 +9,10 @@ namespace ClassicalSharp.Events {
|
||||
public event EventHandler TerrainAtlasChanged;
|
||||
internal void RaiseTerrainAtlasChanged() { Raise( TerrainAtlasChanged ); }
|
||||
|
||||
/// <summary> Raised when the texture pack is changed. </summary>
|
||||
public event EventHandler TexturePackChanged;
|
||||
internal void RaiseTexturePackChanged() { Raise( TexturePackChanged ); }
|
||||
|
||||
/// <summary> Raised when a texture is changed. (such as "terrain", "rain") </summary>
|
||||
public event EventHandler<TextureEventArgs> TextureChanged;
|
||||
internal void RaiseTextureChanged( string name, byte[] data ) {
|
||||
|
116
ClassicalSharp/Rendering/Env/SkyboxRenderer.cs
Normal file
116
ClassicalSharp/Rendering/Env/SkyboxRenderer.cs
Normal file
@ -0,0 +1,116 @@
|
||||
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using ClassicalSharp.Events;
|
||||
using ClassicalSharp.GraphicsAPI;
|
||||
using OpenTK;
|
||||
|
||||
namespace ClassicalSharp.Renderers {
|
||||
|
||||
public sealed class SkyboxRenderer : IGameComponent {
|
||||
|
||||
int tex, vb = -1;
|
||||
Game game;
|
||||
const int count = 6 * 4;
|
||||
|
||||
public bool DoRender { get { return tex > 0; } }
|
||||
|
||||
public void Init( Game game ) {
|
||||
this.game = game;
|
||||
game.Events.TextureChanged += TextureChanged;
|
||||
game.Events.TexturePackChanged += TexturePackChanged;
|
||||
MakeVb();
|
||||
}
|
||||
|
||||
public void Reset( Game game ) {
|
||||
game.Graphics.DeleteTexture( ref tex );
|
||||
}
|
||||
|
||||
public void Ready( Game game ) { }
|
||||
public void OnNewMap( Game game ) { }
|
||||
public void OnNewMapLoaded( Game game ) { }
|
||||
|
||||
public void Dispose() {
|
||||
game.Graphics.DeleteTexture( ref tex );
|
||||
game.Graphics.DeleteVb( vb );
|
||||
game.Events.TextureChanged -= TextureChanged;
|
||||
game.Events.TexturePackChanged -= TexturePackChanged;
|
||||
}
|
||||
|
||||
void TexturePackChanged( object sender, EventArgs e ) {
|
||||
game.Graphics.DeleteTexture( ref tex );
|
||||
}
|
||||
|
||||
void TextureChanged( object sender, TextureEventArgs e ) {
|
||||
if( e.Name == "skybox.png" )
|
||||
game.UpdateTexture( ref tex, e.Data, false );
|
||||
}
|
||||
|
||||
|
||||
public void Render( double deltaTime ) {
|
||||
game.Graphics.DepthWrite = false;
|
||||
game.Graphics.Texturing = true;
|
||||
game.Graphics.BindTexture( tex );
|
||||
game.Graphics.SetBatchFormat( VertexFormat.P3fT2fC4b );
|
||||
|
||||
Vector3 pos = game.CurrentCameraPos;
|
||||
Matrix4 m = Matrix4.Identity;
|
||||
m *= Matrix4.RotateY( game.LocalPlayer.HeadYawRadians );
|
||||
m *= Matrix4.RotateX( game.LocalPlayer.PitchRadians );
|
||||
game.Graphics.LoadMatrix( ref m );
|
||||
|
||||
game.Graphics.BindVb( vb );
|
||||
game.Graphics.DrawIndexedVb( DrawMode.Triangles, count * 6 / 4, 0 );
|
||||
|
||||
game.Graphics.Texturing = false;
|
||||
game.Graphics.LoadMatrix( ref game.View );
|
||||
game.Graphics.DepthWrite = true;
|
||||
}
|
||||
|
||||
unsafe void MakeVb() {
|
||||
VertexP3fT2fC4b* vertices = stackalloc VertexP3fT2fC4b[count];
|
||||
IntPtr start = (IntPtr)vertices;
|
||||
const float pos = 0.5f;
|
||||
TextureRec rec;
|
||||
|
||||
// Render the front quad
|
||||
rec = new TextureRec( 1/4f, 1/2f, 1/4f, 1/2f );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, -pos, -pos, rec.U1, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, -pos, -pos, rec.U2, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, pos, -pos, rec.U2, rec.V1, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, pos, -pos, rec.U1, rec.V1, FastColour.White );
|
||||
// Render the left quad
|
||||
rec = new TextureRec( 0/4f, 1/2f, 1/4f, 1/2f );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, -pos, pos, rec.U1, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, -pos, -pos, rec.U2, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, pos, -pos, rec.U2, rec.V1, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, pos, pos, rec.U1, rec.V1, FastColour.White );
|
||||
// Render the back quad
|
||||
rec = new TextureRec( 3/4f, 1/2f, 1/4f, 1/2f );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, -pos, pos, rec.U1, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, -pos, pos, rec.U2, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, pos, pos, rec.U2, rec.V1, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, pos, pos, rec.U1, rec.V1, FastColour.White );
|
||||
// Render the right quad
|
||||
rec = new TextureRec( 2/4f, 1/2f, 1/4f, 1/2f );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, -pos, -pos, rec.U1, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, -pos, pos, rec.U2, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, pos, pos, rec.U2, rec.V1, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, pos, -pos, rec.U1, rec.V1, FastColour.White );
|
||||
// Render the top quad
|
||||
rec = new TextureRec( 1/4f, 0/2f, 1/4f, 1/2f );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, pos, -pos, rec.U1, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, pos, pos, rec.U1, rec.V1, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, pos, pos, rec.U2, rec.V1, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, pos, -pos, rec.U2, rec.V2, FastColour.White );
|
||||
// Render the bottom quad
|
||||
rec = new TextureRec( 2/4f, 0/2f, 1/4f, 1/2f );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, -pos, -pos, rec.U1, rec.V1, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( -pos, -pos, pos, rec.U1, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, -pos, pos, rec.U2, rec.V2, FastColour.White );
|
||||
*vertices++ = new VertexP3fT2fC4b( pos, -pos, -pos, rec.U2, rec.V1, FastColour.White );
|
||||
vb = game.Graphics.CreateVb( start, VertexFormat.P3fT2fC4b, count );
|
||||
}
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ namespace ClassicalSharp.TexturePack {
|
||||
public void Init( Game game ) {
|
||||
this.game = game;
|
||||
api = game.Graphics;
|
||||
game.Events.TexturePackChanged += TexturePackChanged;
|
||||
game.Events.TextureChanged += TextureChanged;
|
||||
}
|
||||
|
||||
@ -31,6 +32,10 @@ namespace ClassicalSharp.TexturePack {
|
||||
public void OnNewMap( Game game ) { }
|
||||
public void OnNewMapLoaded( Game game ) { }
|
||||
|
||||
void TexturePackChanged( object sender, EventArgs e ) {
|
||||
animations.Clear();
|
||||
}
|
||||
|
||||
void TextureChanged( object sender, TextureEventArgs e ) {
|
||||
if( e.Name == "animations.png" || e.Name == "animation.png" ) {
|
||||
MemoryStream stream = new MemoryStream( e.Data );
|
||||
@ -156,6 +161,7 @@ namespace ClassicalSharp.TexturePack {
|
||||
public void Dispose() {
|
||||
Clear();
|
||||
game.Events.TextureChanged -= TextureChanged;
|
||||
game.Events.TexturePackChanged -= TexturePackChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ namespace ClassicalSharp.TexturePack {
|
||||
|
||||
void Extract( Stream stream, Game game ) {
|
||||
this.game = game;
|
||||
game.Animations.Clear();
|
||||
game.Events.RaiseTexturePackChanged();
|
||||
ZipReader reader = new ZipReader();
|
||||
|
||||
reader.ShouldProcessZipEntry = (f) => true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user