From 0a8f07b872729ed42044c3cf318f52ba957980a3 Mon Sep 17 00:00:00 2001 From: Matej Jan Date: Wed, 1 Sep 2010 14:55:24 +0000 Subject: [PATCH] Added Texture2D loading from Data. git-svn-id: http://xni.googlecode.com/svn/XNI@13 ac433895-eea3-a490-d80a-17149a75e588 --- .../Retronator/Xni/Framework/Graphics/Enums.h | 36 +++++----- .../Xni/Framework/Graphics/GraphicsDevice.h | 6 ++ .../Xni/Framework/Graphics/GraphicsDevice.m | 44 ++++++++++++ .../Xni/Framework/Graphics/GraphicsResource.h | 28 ++++++++ .../Xni/Framework/Graphics/GraphicsResource.m | 26 +++++++ ...etronator.Xni.Framework.Graphics.classes.h | 4 +- .../Retronator.Xni.Framework.Graphics.h | 4 ++ .../Xni/Framework/Graphics/Texture.h | 25 +++++++ .../Xni/Framework/Graphics/Texture.m | 26 +++++++ .../Xni/Framework/Graphics/Texture2D.h | 39 ++++++++++ .../Xni/Framework/Graphics/Texture2D.m | 71 +++++++++++++++++++ XNI.xcodeproj/project.pbxproj | 24 +++++++ 12 files changed, 314 insertions(+), 19 deletions(-) create mode 100644 Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.h create mode 100644 Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.m create mode 100644 Classes/Retronator/Xni/Framework/Graphics/Texture.h create mode 100644 Classes/Retronator/Xni/Framework/Graphics/Texture.m create mode 100644 Classes/Retronator/Xni/Framework/Graphics/Texture2D.h create mode 100644 Classes/Retronator/Xni/Framework/Graphics/Texture2D.m diff --git a/Classes/Retronator/Xni/Framework/Graphics/Enums.h b/Classes/Retronator/Xni/Framework/Graphics/Enums.h index 3062e1d..c69b19e 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/Enums.h +++ b/Classes/Retronator/Xni/Framework/Graphics/Enums.h @@ -73,25 +73,25 @@ typedef enum { typedef enum { SurfaceFormatColor, - SurfaceFormatBgr565, - SurfaceFormatBgra5551, - SurfaceFormatBgra4444, - SurfaceFormatDxt1, - SurfaceFormatDxt3, - SurfaceFormatDxt5, - SurfaceFormatNormalizedByte2, - SurfaceFormatNormalizedByte4, - SurfaceFormatRgba1010102, - SurfaceFormatRg32, - SurfaceFormatRgba64, + SurfaceFormatRgb565, + SurfaceFormatRgba5551, + SurfaceFormatRgba4444, + //SurfaceFormatDxt1, + //SurfaceFormatDxt3, + //SurfaceFormatDxt5, + //SurfaceFormatNormalizedByte2, + //SurfaceFormatNormalizedByte4, + //SurfaceFormatRgba1010102, + //SurfaceFormatRg32, + //SurfaceFormatRgba64, SurfaceFormatAlpha8, - SurfaceFormatSingle, - SurfaceFormatVector2, - SurfaceFormatVector4, - SurfaceFormatHalfSingle, - SurfaceFormatHalfVector2, - SurfaceFormatHalfVector4, - SurfaceFormatHdrBlendable + //SurfaceFormatSingle, + //SurfaceFormatVector2, + //SurfaceFormatVector4, + //SurfaceFormatHalfSingle, + //SurfaceFormatHalfVector2, + //SurfaceFormatHalfVector4, + //SurfaceFormatHdrBlendable } SurfaceFormat; typedef enum { diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h index d1acac4..cd463ca 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.h @@ -38,6 +38,12 @@ - (void) clearWithColor:(Color*)color; - (void) clearWithOptions:(ClearOptions)options color:(Color*)color depth:(float)depth stencil:(int)stencil; +// Low level methods +- (uint) createTexture; +//- (void) getData:(void*)data fromTexture2D:(Texture2D*)texture level:(int)level; +- (void) setData:(void*)data toTexture2D:(Texture2D*)texture level:(int)level; + +// Profile specific - (EAGLContext*) createContext; @end diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m index 3ae4b83..d50c43c 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsDevice.m @@ -91,6 +91,50 @@ glClear(options); } +// Low level methods +- (uint) createTexture { + GLuint textureId; + glGenTextures(1, &textureId); + return textureId; +} + +- (void) setData:(void*)data toTexture2D:(Texture2D*)texture level:(int)level { + GLenum format, type; + switch (texture.format) { + case SurfaceFormatColor: + format = GL_RGBA; + type = GL_UNSIGNED_BYTE; + break; + case SurfaceFormatAlpha8: + format = GL_ALPHA; + type = GL_UNSIGNED_BYTE; + break; + case SurfaceFormatRgb565: + format = GL_RGB; + type = GL_UNSIGNED_SHORT_5_6_5; + break; + case SurfaceFormatRgba4444: + format = GL_RGBA; + type = GL_UNSIGNED_SHORT_4_4_4_4; + break; + case SurfaceFormatRgba5551: + format = GL_RGBA; + type = GL_UNSIGNED_SHORT_5_5_5_1; + break; + default: + break; + } + glBindTexture(GL_TEXTURE_2D, texture.textureId); + glTexImage2D(GL_TEXTURE_2D, level, format, texture.width, texture.height, 0, format, type, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glBindTexture(GL_TEXTURE_2D, 0); +} + +// Profile specific + - (EAGLContext*) createContext { return nil; } @end diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.h b/Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.h new file mode 100644 index 0000000..b57ed61 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.h @@ -0,0 +1,28 @@ +// +// GraphicsResource.h +// XNI +// +// Created by Matej Jan on 1.9.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import + +#import "Retronator.Xni.Framework.classes.h" +#import "Retronator.Xni.Framework.Graphics.classes.h" + +@interface GraphicsResource : NSObject { + GraphicsDevice *graphicsDevice; + BOOL isDisposed; + NSString *name; + id tag; +} + +- (id) initWithGraphicsDevice:(GraphicsDevice*)theGraphicsDevice; + +@property (nonatomic, readonly) GraphicsDevice *graphicsDevice; +@property (nonatomic, readonly) BOOL isDisposed; +@property (nonatomic, retain) NSString *name; +@property (nonatomic, retain) id tag; + +@end diff --git a/Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.m b/Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.m new file mode 100644 index 0000000..e60af7a --- /dev/null +++ b/Classes/Retronator/Xni/Framework/Graphics/GraphicsResource.m @@ -0,0 +1,26 @@ +// +// GraphicsResource.m +// XNI +// +// Created by Matej Jan on 1.9.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import "GraphicsResource.h" + + +@implementation GraphicsResource + +- (id) initWithGraphicsDevice:(GraphicsDevice*)theGraphicsDevice { + if (self = [super init]) { + graphicsDevice = theGraphicsDevice; + } + return self; +} + +@synthesize graphicsDevice; +@synthesize isDisposed; +@synthesize name; +@synthesize tag; + +@end diff --git a/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.classes.h b/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.classes.h index 7959257..e0ff937 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.classes.h +++ b/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.classes.h @@ -1,4 +1,6 @@ #import "Enums.h" @protocol IGraphicsDeviceService; -@class GraphicsDevice, ReachGraphicsDevice, HiDefGraphicsDevice; \ No newline at end of file +@class GraphicsDevice, ReachGraphicsDevice, HiDefGraphicsDevice; + +@class GraphicsResource, Texture, Texture2D; \ No newline at end of file diff --git a/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.h b/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.h index fd44b59..e75eafe 100644 --- a/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.h +++ b/Classes/Retronator/Xni/Framework/Graphics/Retronator.Xni.Framework.Graphics.h @@ -3,3 +3,7 @@ #import "GraphicsDevice.h" #import "ReachGraphicsDevice.h" #import "HiDefGraphicsDevice.h" + +#import "GraphicsResource.h" +#import "Texture.h" +#import "Texture2D.h" \ No newline at end of file diff --git a/Classes/Retronator/Xni/Framework/Graphics/Texture.h b/Classes/Retronator/Xni/Framework/Graphics/Texture.h new file mode 100644 index 0000000..689ab15 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/Graphics/Texture.h @@ -0,0 +1,25 @@ +// +// Texture.h +// XNI +// +// Created by Matej Jan on 1.9.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import + +#import "GraphicsResource.h" + +@interface Texture : GraphicsResource { + uint textureId; + SurfaceFormat format; + int levelCount; +} + +- (id) initWithGraphicsDevice:(GraphicsDevice *)theGraphicsDevice SurfaceFormat:(SurfaceFormat)theFormat LevelCount:(int)theLevelCount; + +@property (nonatomic, readonly) uint textureId; +@property (nonatomic) SurfaceFormat format; +@property (nonatomic) int levelCount; + +@end diff --git a/Classes/Retronator/Xni/Framework/Graphics/Texture.m b/Classes/Retronator/Xni/Framework/Graphics/Texture.m new file mode 100644 index 0000000..ea8d677 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/Graphics/Texture.m @@ -0,0 +1,26 @@ +// +// Texture.m +// XNI +// +// Created by Matej Jan on 1.9.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import "Texture.h" + + +@implementation Texture + +- (id) initWithGraphicsDevice:(GraphicsDevice *)theGraphicsDevice SurfaceFormat:(SurfaceFormat)theFormat LevelCount:(int)theLevelCount { + if (self = [super initWithGraphicsDevice:theGraphicsDevice]) { + format = theFormat; + + } + return self; +} + +@synthesize textureId; +@synthesize format; +@synthesize levelCount; + +@end diff --git a/Classes/Retronator/Xni/Framework/Graphics/Texture2D.h b/Classes/Retronator/Xni/Framework/Graphics/Texture2D.h new file mode 100644 index 0000000..9154357 --- /dev/null +++ b/Classes/Retronator/Xni/Framework/Graphics/Texture2D.h @@ -0,0 +1,39 @@ +// +// Texture2D.h +// XNI +// +// Created by Matej Jan on 1.9.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import + +#import "Texture.h" + +@interface Texture2D : Texture { + int height; + int width; +} + +- (id) initWithGraphicsDevice:(GraphicsDevice*)theGraphicsDevice Width:(int)theWidth Height:(int)theHeight; +// - (id) initWithGraphicsDevice:(GraphicsDevice*)theGraphicsDevice Width:(int)theWidth Height:(int)theHeight MipMaps:(BOOL)generateMipMaps Format:(SurfaceFormat)theFormat; + +@property (nonatomic, readonly) Rectangle *bounds; +@property (nonatomic, readonly) int height; +@property (nonatomic, readonly) int width; + ++ (Texture2D*) fromData:(NSData*)textureData GraphicsDevice:(GraphicsDevice*)graphicsDevice; +//+ (Texture2D*) fromData:(NSData*)textureData GraphicsDevice:(GraphicsDevice*)graphicsDevice Width:(int)width Height:(int)height Zoom:(BOOL)zoom; + +// - (void) getDataTo:(void *)data; +// - (void) getDataTo:(void *)data StartIndex:(int)startIndex ElementCount:(int)elementCount; +// - (void) getDataFromLevel:(int)level SourceRectangle:(Rectangle*)rect To:(void *)data StartIndex:(int)startIndex ElementCount:(int)elementCount; + +- (void) setDataFrom:(void *)data; +// - (void) setDataFrom:(void *)data StartIndex:(int)startIndex ElementCount:(int)elementCount; +// - (void) setDataToLevel:(int)level SourceRectangle:(Rectangle*)rect From:(void *)data StartIndex:(int)startIndex ElementCount:(int)elementCount; + +// - (void) saveAsJpeg:(NSData*)textureData Width:(int)width Height:(int)height; +// - (void) saveAsPng:(NSData*)textureData Width:(int)width Height:(int)height; + +@end diff --git a/Classes/Retronator/Xni/Framework/Graphics/Texture2D.m b/Classes/Retronator/Xni/Framework/Graphics/Texture2D.m new file mode 100644 index 0000000..8d7c85c --- /dev/null +++ b/Classes/Retronator/Xni/Framework/Graphics/Texture2D.m @@ -0,0 +1,71 @@ +// +// Texture2D.m +// XNI +// +// Created by Matej Jan on 1.9.10. +// Copyright 2010 Retronator. All rights reserved. +// + +#import "Texture2D.h" +#import + +#import "Retronator.Xni.Framework.h" +#import "Retronator.Xni.Framework.Graphics.h" + +@implementation Texture2D + +- (id) initWithGraphicsDevice:(GraphicsDevice*)theGraphicsDevice Width:(int)theWidth Height:(int)theHeight { + if (self = [super initWithGraphicsDevice:theGraphicsDevice SurfaceFormat:SurfaceFormatColor LevelCount:1]) { + width = theWidth; + height = theHeight; + textureId = [graphicsDevice createTexture]; + } + return self; +} + +- (Rectangle*) bounds { + return [Rectangle rectangleWithX:0 Y:0 Width:width Height:height]; +} + +@synthesize width; +@synthesize height; + + ++ (Texture2D*) fromData:(NSData*)textureData GraphicsDevice:(GraphicsDevice*)graphicsDevice { + if (graphicsDevice == nil) { + [NSException raise:@"ArgumentNullException" format:@"The graphics device cannot be null."]; + } + + UIImage *image = [[UIImage alloc] initWithData:textureData]; + if (image == nil) { + [NSException raise:@"NotSupportedException" format:@"textureData contains an unknown format."]; + return nil; + } + + GLuint width = CGImageGetWidth(image.CGImage); + GLuint height = CGImageGetHeight(image.CGImage); + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + void *imageData = malloc(height * width * 4); + + CGContextRef textureContext = CGBitmapContextCreate(imageData, width, height, 8, 4 * width, colorSpace, + kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big ); + CGColorSpaceRelease(colorSpace); + CGContextClearRect(textureContext, CGRectMake(0, 0, width, height)); + CGContextTranslateCTM(textureContext, 0, 0); + CGContextDrawImage(textureContext, CGRectMake(0, 0, width, height), image.CGImage); + + Texture2D *texture = [[Texture2D alloc] initWithGraphicsDevice:graphicsDevice Width:(int)width Height:(int)height]; + [texture setDataFrom:imageData]; + + CGContextRelease(textureContext); + free(imageData); + [image release]; + + return texture; +} + +- (void) setDataFrom:(void *)data { + [graphicsDevice setData:data toTexture2D:self level:0]; +} + +@end diff --git a/XNI.xcodeproj/project.pbxproj b/XNI.xcodeproj/project.pbxproj index 7b398de..505ebe2 100644 --- a/XNI.xcodeproj/project.pbxproj +++ b/XNI.xcodeproj/project.pbxproj @@ -8,6 +8,12 @@ /* Begin PBXBuildFile section */ AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; }; + B50806FF122E4ECF00C330E2 /* Texture.h in Headers */ = {isa = PBXBuildFile; fileRef = B50806FD122E4ECF00C330E2 /* Texture.h */; settings = {ATTRIBUTES = (Public, ); }; }; + B5080700122E4ECF00C330E2 /* Texture.m in Sources */ = {isa = PBXBuildFile; fileRef = B50806FE122E4ECF00C330E2 /* Texture.m */; }; + B5080703122E4EE900C330E2 /* Texture2D.h in Headers */ = {isa = PBXBuildFile; fileRef = B5080701122E4EE900C330E2 /* Texture2D.h */; settings = {ATTRIBUTES = (Public, ); }; }; + B5080704122E4EE900C330E2 /* Texture2D.m in Sources */ = {isa = PBXBuildFile; fileRef = B5080702122E4EE900C330E2 /* Texture2D.m */; }; + B508070C122E4FBB00C330E2 /* GraphicsResource.h in Headers */ = {isa = PBXBuildFile; fileRef = B508070A122E4FBB00C330E2 /* GraphicsResource.h */; settings = {ATTRIBUTES = (Public, ); }; }; + B508070D122E4FBB00C330E2 /* GraphicsResource.m in Sources */ = {isa = PBXBuildFile; fileRef = B508070B122E4FBB00C330E2 /* GraphicsResource.m */; }; B5DDE7EB11FF04E3000DB38B /* Game.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DDE7E911FF04E3000DB38B /* Game.h */; settings = {ATTRIBUTES = (Public, ); }; }; B5DDE7EC11FF04E3000DB38B /* Game.m in Sources */ = {isa = PBXBuildFile; fileRef = B5DDE7EA11FF04E3000DB38B /* Game.m */; }; B5DDE7EF11FF06BB000DB38B /* GameServiceContainer.h in Headers */ = {isa = PBXBuildFile; fileRef = B5DDE7ED11FF06BB000DB38B /* GameServiceContainer.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -65,6 +71,12 @@ /* Begin PBXFileReference section */ AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + B50806FD122E4ECF00C330E2 /* Texture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Texture.h; sourceTree = ""; }; + B50806FE122E4ECF00C330E2 /* Texture.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Texture.m; sourceTree = ""; }; + B5080701122E4EE900C330E2 /* Texture2D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Texture2D.h; sourceTree = ""; }; + B5080702122E4EE900C330E2 /* Texture2D.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Texture2D.m; sourceTree = ""; }; + B508070A122E4FBB00C330E2 /* GraphicsResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsResource.h; sourceTree = ""; }; + B508070B122E4FBB00C330E2 /* GraphicsResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GraphicsResource.m; sourceTree = ""; }; B5DDE7E911FF04E3000DB38B /* Game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Game.h; sourceTree = ""; }; B5DDE7EA11FF04E3000DB38B /* Game.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Game.m; sourceTree = ""; }; B5DDE7ED11FF06BB000DB38B /* GameServiceContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameServiceContainer.h; sourceTree = ""; }; @@ -190,6 +202,12 @@ B5DDE8C211FF29E8000DB38B /* ReachGraphicsDevice.m */, B5DDE8C511FF2A23000DB38B /* HiDefGraphicsDevice.h */, B5DDE8C611FF2A23000DB38B /* HiDefGraphicsDevice.m */, + B508070A122E4FBB00C330E2 /* GraphicsResource.h */, + B508070B122E4FBB00C330E2 /* GraphicsResource.m */, + B50806FD122E4ECF00C330E2 /* Texture.h */, + B50806FE122E4ECF00C330E2 /* Texture.m */, + B5080701122E4EE900C330E2 /* Texture2D.h */, + B5080702122E4EE900C330E2 /* Texture2D.m */, ); path = Graphics; sourceTree = ""; @@ -308,6 +326,9 @@ B5DDE90911FF352E000DB38B /* Color.h in Headers */, B5DDE99111FF546B000DB38B /* GraphicsDeviceManager.h in Headers */, B5F4E2A912095FAF00B2FC0F /* XNI_Prefix.pch in Headers */, + B50806FF122E4ECF00C330E2 /* Texture.h in Headers */, + B5080703122E4EE900C330E2 /* Texture2D.h in Headers */, + B508070C122E4FBB00C330E2 /* GraphicsResource.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -376,6 +397,9 @@ B5DDE8C811FF2A23000DB38B /* HiDefGraphicsDevice.m in Sources */, B5DDE90A11FF352E000DB38B /* Color.m in Sources */, B5DDE99211FF546B000DB38B /* GraphicsDeviceManager.m in Sources */, + B5080700122E4ECF00C330E2 /* Texture.m in Sources */, + B5080704122E4EE900C330E2 /* Texture2D.m in Sources */, + B508070D122E4FBB00C330E2 /* GraphicsResource.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };