最终,我试图通过为每个对象提供不同的纹理坐标来使用单个图像来对多个对象(六边形)进行纹理处理。
使用 OpenGL ES 2.0 和 GLKit 我想出了一个这样做的方法只是为了发现如果我 malloc 并手动初始化 Vertex 数组由于某种原因对象将不会呈现。我特别不明白为什么这是因为顶点数组的静态分配版本工作得很好。
前面是我正在使用的结构:
typedef struct {
GLKVector3 position;
GLKVector4 color;
GLKVector2 texCoords;
}Vertex;
因此,如果我在六边形的实现中执行此操作,我可以看到应用了纹理的各种六边形:
Vertex _Vertices[] = {
{{ 0, 0, 0}, {1, 1, 1, 0}, {0.50, 0.50}},//Z
{{ 1, 0, 0}, {1, 1, 1, 0}, {1.00, 0.50}},//A
{{ 0.5, -0.866, 0}, {1, 1, 1, 0}, {0.75, 0.00}},//B
{{-0.5, -0.866, 0}, {1, 1, 1, 0}, {0.25, 0.00}},//C
{{-1, 0, 0}, {1, 1, 1, 0}, {0, 0.50}},//D
{{-0.5, 0.866, 0}, {1, 1, 1, 0}, {0.25, 1.00}},//E
{{ 0.5, 0.866, 0}, {1, 1, 1, 0}, {0.75, 1.00}} //F
};
但是,如果我改为注释掉上面的声明并添加一个同名的实例变量(Vertex *Vertices 而不是 Vertex Vertices[],因为我不能直接 malloc 到数组)然后执行以下操作,则不会呈现任何内容:
-(void)setVertices{
_Vertices = malloc(sizeof(Vertex) * 7);
//Z
_Vertices[0].position.x = 0;
_Vertices[0].position.y = 0;
_Vertices[0].position.z = 0;
_Vertices[0].color.r = 1;
_Vertices[0].color.g = 1;
_Vertices[0].color.b = 1;
_Vertices[0].color.a = 0;
_Vertices[0].texCoords.x = 0.5;
_Vertices[0].texCoords.y = 0.5;
//A
_Vertices[1].position.x = 1;
_Vertices[1].position.y = 0;
_Vertices[1].position.z = 0;
_Vertices[1].color.r = 1;
_Vertices[1].color.g = 1;
_Vertices[1].color.b = 1;
_Vertices[1].color.a = 0;
_Vertices[1].texCoords.x = 1;
_Vertices[1].texCoords.y = 0.5;
//B
_Vertices[2].position.x = 0.5;
_Vertices[2].position.y = -0.866;
_Vertices[2].position.z = 0;
_Vertices[2].color.r = 1;
_Vertices[2].color.g = 1;
_Vertices[2].color.b = 1;
_Vertices[2].color.a = 0;
_Vertices[2].texCoords.x = 0.75;
_Vertices[2].texCoords.y = 0.00;
//C
_Vertices[3].position.x = -0.5;
_Vertices[3].position.y = -0.866;
_Vertices[3].position.z = 0;
_Vertices[3].color.r = 1;
_Vertices[3].color.g = 1;
_Vertices[3].color.b = 1;
_Vertices[3].color.a = 0;
_Vertices[3].texCoords.x = 0.25;
_Vertices[3].texCoords.y = 0;
//D
_Vertices[4].position.x = -1;
_Vertices[4].position.y = 0;
_Vertices[4].position.z = 0;
_Vertices[4].color.r = 1;
_Vertices[4].color.g = 1;
_Vertices[4].color.b = 1;
_Vertices[4].color.a = 0;
_Vertices[4].texCoords.x = 0;
_Vertices[4].texCoords.y = 0.5;
//E
_Vertices[5].position.x = -0.5;
_Vertices[5].position.y = 0.866;
_Vertices[5].position.z = 0;
_Vertices[5].color.r = 1;
_Vertices[5].color.g = 1;
_Vertices[5].color.b = 1;
_Vertices[5].color.a = 0;
_Vertices[5].texCoords.x = 0.25;
_Vertices[5].texCoords.y = 1;
//F
_Vertices[6].position.x = 0.5;
_Vertices[6].position.y = 0.866;
_Vertices[6].position.z = 0;
_Vertices[6].color.r = 1;
_Vertices[6].color.g = 1;
_Vertices[6].color.b = 1;
_Vertices[6].color.a = 0;
_Vertices[6].texCoords.x = 0.75;
_Vertices[6].texCoords.y = 1;
}
这是我的设置函数:
-(void)setupGL{
self.effect = [[GLKBaseEffect alloc] init];
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(_Vertices), _Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_Indices), _Indices, GL_STATIC_DRAW);
}
这是我的绘制函数:
-(void)draw{
glPushGroupMarkerEXT(0,"drawHex");
_effect.texture2d0.envMode = GLKTextureEnvModeReplace;
_effect.texture2d0.target = GLKTextureTarget2D;
_effect.texture2d0.name = _texture.name;
[self.effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, color));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, texCoords));
//draw
glDrawElements(GL_TRIANGLES, sizeof(_Indices)/sizeof(_Indices[0]),GL_UNSIGNED_BYTE, 0);
glPopGroupMarkerEXT();
}
如果重要的话,纹理会在对象外部创建一次,然后传入一个指向它的指针。像这样创建纹理:
-(void)setTextureImage:(UIImage *)image {
NSError *error;
_hexTexture = [GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error];
if (error) {
NSLog(@"Error loading texture from image: %@",error);
}}
我对这一切都是陌生的,试图弄清楚这里发生了什么让我感到非常沮丧。
在我广泛的搜索中,我看到了一些关于“捕获 opengl es 框架”的东西,但它似乎没有为 Xcode 中的 iphone 模拟器启用,所以我无法深入研究它:(
我真的很想知道为什么会这样。
如果有帮助,我可以提供更多代码或解释。
有什么想法吗?
最佳答案
关于 glBufferData 的参数, sizeof(_Vertices) 在静态和动态之间是不一样的。 如果你想使用动态,你必须使用 sizeof(Vertex)*7 而不是 sizeof(_Vertices)。
关于objective-c - 如果顶点是手动生成的(malloc 然后初始化),为什么我的对象不呈现,但如果静态分配则会呈现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112452/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun