2010-09-03 44 views
0

我移植Android应用我到iPhone制造和运行到问题的语法(我认为)Objective-C的阵列和类变量

我从 http://iphonedevelopment.blogspot.com/2009/04/opengl-es-from-ground-up-part-2-look-at.html

基础项目落例子

我想从渲染过程中拉出几何图形以保持代码模块化,但我似乎无法使其工作。我创建了一个名为二十面体类(评论是我发生了什么事情的理解) Icosahedron.h

#import <Foundation/Foundation.h> 
#import "OpenGLCommon.h" 



@interface Icosahedron : NSObject { 
Vertex3D *vertices[12]; //allocate a group of 12 Vertex3D pointers 
Vertex3D *normals[12]; // ditto 
GLubyte *faces[60]; // ditto but with 60 face values 
} 

// Declare methods 
-(Vertex3D *) vertices; 
-(void) setVertices:(Vector3D *)setVerts; 

-(Vertex3D *) normals; 
-(void) setNormals:(Vector3D *)setNorms; 

-(GLubyte *) faces; 
-(void) setFaces:(GLubyte *)setFaces; 

@end 

Icosahedron.m

#import "Icosahedron.h" 


@implementation Icosahedron 

// Returns the pointer to the vertices instance variable 
-(Vector3D *) vertices{ 
return *vertices; 
} 

-(void) setVertices:(Vector3D *)setVerts 
{ 
//vertices=setVerts[0]; 
} 

-(Vector3D *) normals{ 
return *normals; 
} 

-(void) setNormals:(Vector3D *)setNorms 
{ 
//normals=setNorms; 
} 

-(GLubyte *) faces{ 
return *faces; 
} 

-(void) setFaces:(GLubyte *)setFaces 
{ 
//faces=setFaces; 
} 
/**/ 
-(id)init 
{ 
// super method 
self=[super init]; 

    // create 12 Vector3D objects and populate them... 
Vector3D tempVert[12]={ 
     {0, -0.525731, 0.850651},    // vertices[0] 
     {0.850651, 0, 0.525731},    // vertices[1] 
     {0.850651, 0, -0.525731},    // vertices[2] 
     {-0.850651, 0, -0.525731},   // vertices[3] 
     {-0.850651, 0, 0.525731},    // vertices[4] 
     {-0.525731, 0.850651, 0},    // vertices[5] 
     {0.525731, 0.850651, 0},    // vertices[6] 
     {0.525731, -0.850651, 0},    // vertices[7] 
     {-0.525731, -0.850651, 0},   // vertices[8] 
     {0, -0.525731, -0.850651},   // vertices[9] 
     {0, 0.525731, -0.850651},    // vertices[10] 
     {0, 0.525731, 0.850651}    // vertices[11] 
    }; 
    // same... 
Vector3D tempNorm[12]={ 
     {0.000000, -0.417775, 0.675974}, 
     {0.675973, 0.000000, 0.417775}, 
     {0.675973, -0.000000, -0.417775}, 
     {-0.675973, 0.000000, -0.417775}, 
     {-0.675973, -0.000000, 0.417775}, 
     {-0.417775, 0.675974, 0.000000}, 
     {0.417775, 0.675973, -0.000000}, 
     {0.417775, -0.675974, 0.000000}, 
     {-0.417775, -0.675974, 0.000000}, 
     {0.000000, -0.417775, -0.675973}, 
     {0.000000, 0.417775, -0.675974}, 
     {0.000000, 0.417775, 0.675973}, 
    }; 


// face values 
GLubyte tempFaces[60]={ 
     1, 2, 6, 
     1, 7, 2, 
     3, 4, 5, 
     4, 3, 8, 
     6, 5, 11, 
     5, 6, 10, 
     9, 10, 2, 
     10, 9, 3, 
     7, 8, 9, 
     8, 7, 0, 
     11, 0, 1, 
     0, 11, 4, 
     6, 2, 10, 
     1, 6, 11, 
     3, 5, 10, 
     5, 4, 11, 
     2, 7, 9, 
     7, 1, 0, 
     3, 9, 8, 
     4, 8, 0, 
    }; 

    // set the instance pointers to the temp values 
*vertices=tempVert; 
*normals=tempNorm; 
*faces=tempFaces; 

在这一点上没有正确的填充值,只有第一个值是正确的。

return self; 

} 

@end 

所有我想要做的是能够调用类似

... 
ico=[[Icosahedron alloc] init]; 
glVertexPointer(3, GL_FLOAT, 0, [ico vertices]); 
... 

在渲染部分,但它最远的我已经能够得到的是设置Vertex3Ds的第一个值在Icosahedron类中,我在调试器中为渲染类中的任何二十面体的值“超出范围”。

我怀疑这只是我学习Objective-C的怪癖,但我已经尝试了许多不同的方法几天,似乎没有任何东西能让我得到任何东西。

请帮助我Overflow-Wan,你是我唯一的希望。

回答

2

你已经混淆了你的指针和数组。你会想是这样的:

Vertex3D vertices[12]; 

- (void) setVertices: (Vertex3D *)newVertices; 
{ 
    memcpy(vertices, newVertices, 12 * sizeof(Vertex3D)); 
} 

- (Vertex3D *) vertices; 
{ 
    return vertices; 
} 

要复制在C数组,你必须使用memcpy(或手写的循环),你不能赋值运算符做到这一点。

+0

啊我怀疑这样的事情可能是必要的,但我没有看到任何符合我所做事情的例子。非常感谢。 – 2010-09-05 00:53:31

+0

只是想我会跟进 - 我实施了这种方法,并纠正了我的顶点/ Vector3D错误,它的工作就像一个魅力。感谢大家的帮助。 – 2010-09-07 21:14:29

2

首先,您似乎可以互换使用“Vertex3D”和“Vector3D”。我不知道这是否是一个问题...

我认为数组应该被声明为数组的数组,而不是指针数组。所以...

Vertex3D vertices[12]; //allocate a group of 12 Vertex3D pointers 
Vertex3D normals[12]; // ditto 
GLubyte faces[60]; // ditto but with 60 face values 

这样你就有了所有值的空间,而不仅仅是指向其他向量(你还没有分配)的指针。

顺便说一句,典型的初始化顺序是:

-(id)init 
{ 
    if (self = [super init]) 
    { 
     //do initialization stuff 
    } 
    return self; 
} 

它更优雅地处理错误。

+0

啊我甚至没有看到顶点/矢量不匹配,非常感谢。 – 2010-09-05 00:51:59

1

你的verticesnormalsfaces声明应的指针到相关实体阵列。看起来你真正想要的是拥有结构体/值本身的数组。所以,你的界面应该说:然后

Vertex3D vertices[12]; 
Vertex3D normals[12]; 
GLubyte faces[60]; 

你的对象将拥有含12个顶点,12个法线和60面内存​​块(嗯,不管这些都是 - GLubyte值,反正)。

指针和数组是排序的在C/Objective-C中可以互换。因此verticesnormals也可以被认为是Vertex3D*的值,并且faces被认为是GLubyte*,并且它们可以被如此传递给需要这些类型的函数。

为了清楚说明什么是什么,最好将初始值作为单个static const数组放在实现文件的顶层,然后在初始化时将这些值复制到对象拥有的数组中。您可以通过几种方式来完成此操作,但最简单的方法可能是使用memcpy,如Sven's answer

+0

我曾尝试过使用指针与值的一些组合,但每次使用值时,编译器都会向我抛出。我将把你的信息与memcpy整合在一起,并且(希望)能够将它全部解决。干杯! – 2010-09-05 00:59:44