1
0
mirror of https://github.com/thes3m/XNI synced 2024-12-26 13:26:06 +01:00

Added Texture2D loading from Data.

git-svn-id: http://xni.googlecode.com/svn/XNI@13 ac433895-eea3-a490-d80a-17149a75e588
This commit is contained in:
Matej Jan 2010-09-01 14:55:24 +00:00
parent bb98737bff
commit 0a8f07b872
12 changed files with 314 additions and 19 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,28 @@
//
// GraphicsResource.h
// XNI
//
// Created by Matej Jan on 1.9.10.
// Copyright 2010 Retronator. All rights reserved.
//
#import <Foundation/Foundation.h>
#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

View File

@ -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

View File

@ -1,4 +1,6 @@
#import "Enums.h"
@protocol IGraphicsDeviceService;
@class GraphicsDevice, ReachGraphicsDevice, HiDefGraphicsDevice;
@class GraphicsDevice, ReachGraphicsDevice, HiDefGraphicsDevice;
@class GraphicsResource, Texture, Texture2D;

View File

@ -3,3 +3,7 @@
#import "GraphicsDevice.h"
#import "ReachGraphicsDevice.h"
#import "HiDefGraphicsDevice.h"
#import "GraphicsResource.h"
#import "Texture.h"
#import "Texture2D.h"

View File

@ -0,0 +1,25 @@
//
// Texture.h
// XNI
//
// Created by Matej Jan on 1.9.10.
// Copyright 2010 Retronator. All rights reserved.
//
#import <Foundation/Foundation.h>
#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

View File

@ -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

View File

@ -0,0 +1,39 @@
//
// Texture2D.h
// XNI
//
// Created by Matej Jan on 1.9.10.
// Copyright 2010 Retronator. All rights reserved.
//
#import <Foundation/Foundation.h>
#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

View File

@ -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 <UIKit/UIKit.h>
#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

View File

@ -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 = "<group>"; };
B50806FE122E4ECF00C330E2 /* Texture.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Texture.m; sourceTree = "<group>"; };
B5080701122E4EE900C330E2 /* Texture2D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Texture2D.h; sourceTree = "<group>"; };
B5080702122E4EE900C330E2 /* Texture2D.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Texture2D.m; sourceTree = "<group>"; };
B508070A122E4FBB00C330E2 /* GraphicsResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsResource.h; sourceTree = "<group>"; };
B508070B122E4FBB00C330E2 /* GraphicsResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GraphicsResource.m; sourceTree = "<group>"; };
B5DDE7E911FF04E3000DB38B /* Game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Game.h; sourceTree = "<group>"; };
B5DDE7EA11FF04E3000DB38B /* Game.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Game.m; sourceTree = "<group>"; };
B5DDE7ED11FF06BB000DB38B /* GameServiceContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameServiceContainer.h; sourceTree = "<group>"; };
@ -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 = "<group>";
@ -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;
};