- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在阅读 Oreilly 的 3D Programming for iOS 书,并将从 C++ 到 Objective - C 的所有内容翻译成多种用途,包括后期优化和对函数和 API 的深入理解。我宁愿学习利用新的 GLKit 而不是依赖 C++ 当前专门用于 iOS 开发的语言。以下是我迄今为止用于创建本书提供的 3D 圆锥体模型的翻译代码。不幸的是,底部圆盘和锥体本身都只出现了一条,我不知道为什么。任何人都可以在这方面帮助我。拜托,如果您看到任何优化(我还没有完成,因为我还在翻译)或关于更好地做任何事情的建议,我很乐意听到一些反馈。我真的很想帮助找到问题。找了几天没有结果。下面附上了我正在输出的图像(它应该是一个完整的 3D 圆锥体)。
//
// IRenderingEngine2.m
// HelloArrow
//
// Created by TheGamingArt on 3/4/13.
// Copyright (c) 2013 Brandon Levasseur. All rights reserved.
//
#import "IRenderingEngine2.h"
#define STRINGIFY(A) #A
#import "Simple.frag"
#import "Simple.vert"
static const float RevolutionsPerSecond = 1;
static const float AnimationDuration = 0.25f;
static const float coneSlices = 40.f;
static const int numberOfConeVerticies = ((coneSlices/*number of coneSlices*/ +1) *2);
static const int numberOfDiskVerticies = (coneSlices + 2);
typedef struct{
GLKVector3 Position;
GLKVector4 Color;
}Vertex;
typedef struct{
GLKQuaternion Start; //starting orientation
GLKQuaternion End; //ending orientation
GLKQuaternion Current; //current interpolated orientation
float Elapsed; //time span in seconds for a slerp fraction between 0 and 1
float Duration; //time span in seconds for a slerp fraction between 0 and 1
}Animation; //enables smooth 3D transitions
@interface IRenderingEngine2(){
GLuint framebuffer;
GLuint colorRenderbuffer;
GLuint depthRenderbuffer; //Because of this being 3D, need depthRender. If only 2d, only need colorRender
float currentAngle; //angles in degrees
float desiredAngle; //added for smooth rotation transition
Vertex cone[numberOfConeVerticies];
Vertex disk[numberOfDiskVerticies];
Animation animation;
GLuint simpleProgram;
}
-(float) getRotationDirection;
-(void)applyRotation:(float)degrees;
-(GLuint)buildProgramWithVertex:(const char *)vShaderSource andFragment:(const char *)fShaderSource;
-(void)applyOrthoWithMaxX:(float)maxX andMaxY:(float)maxY;
-(GLuint)buildShaderWithSource:(const char *)source shaderType:(GLenum)type;
-(GLKQuaternion) quaternionCreateFromVectors:(GLKVector3)v0 :(GLKVector3)v1;
@end
@implementation IRenderingEngine2
-(id)init{
self = [super init];
if (self) {
glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
}
return self;
}
-(void)setRenderWidth:(int)width andHeight:(int)height{
const float coneRadius = 0.5f;
const float coneHeight = 1.866f;
// const int coneSlices = 40;
{
//Generate vertices for the disk....
//Uses triangle fan so the total number of vertices is n+2: one exxtra vertex for the center and another for closing the loop
//Allocate space for the disk vertices.
//m_disk.resize(coneSlices + 2)
int vertexIterator = 0;
disk[vertexIterator].Color = GLKVector4Make(0.75f, 0.75f, 0.75f, 1.0f);
disk[vertexIterator].Position.x = 0.0f;
disk[vertexIterator].Position.y = 1.0f - coneHeight;
disk[vertexIterator].Position.z = 0.0f;
vertexIterator++;
//Initialize the rim vertices of the triangle fan
const float dtheta = M_2_PI / coneSlices;
for (float theta = 0.0f; vertexIterator != numberOfDiskVerticies; theta += dtheta) {
disk[vertexIterator].Color = GLKVector4Make(0.75f, 0.75f, 0.75f, 1.0f);
disk[vertexIterator].Position.x = coneRadius * cosf(theta);
disk[vertexIterator].Position.y = 1 - coneHeight;
disk[vertexIterator].Position.z = coneRadius * sinf(theta);
vertexIterator++;
}
}
{
//Generate vertices for body of cone
int vertexIterator = 0;
//Initialize the vertices of the triangle strip.
const float dtheta = M_2_PI /coneSlices;
for (float theta = 0; vertexIterator != numberOfConeVerticies ; theta += dtheta) {
//Grayscale gradient
float brightness = abs(sinf(theta)); // creates a grayscale gradient as a cheap way to simulate lighting.. aka baked lighting hack
GLKVector4 color = GLKVector4Make(brightness, brightness, brightness, 1);
//Apex vertex
cone[vertexIterator].Position = GLKVector3Make(0.0f, 1.0f, 0.0f);
cone[vertexIterator].Color = color;
vertexIterator++;
//Rim vertex
cone[vertexIterator].Position.x = coneRadius * cosf(theta);
cone[vertexIterator].Position.y = 1 - coneHeight;
cone[vertexIterator].Position.z = coneRadius * sinf(theta);
cone[vertexIterator].Color = color;
vertexIterator++;
}
}
//Create the depth buffer
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width * [[UIScreen mainScreen] scale], height * [[UIScreen mainScreen] scale]);
//Create the framebuffer object and attach the color buffer.
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
//Bind the color buffer for rendering
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
glViewport(0, 0, width * [[UIScreen mainScreen] scale], height * [[UIScreen mainScreen] scale]);
glEnable(GL_DEPTH_TEST);
simpleProgram = [self buildProgramWithVertex:SimpleVertexShader andFragment:SimpleFragmentShader];
glUseProgram(simpleProgram);
//Set the Projection Matrix
GLint projectionUniform = glGetUniformLocation(simpleProgram, "Projection");
GLKMatrix4 projectionMatrix = GLKMatrix4MakeFrustum(-1.6f, 1.6f, -2.4f, 2.4f, 5.0f, 10.0f);
glUniformMatrix4fv(projectionUniform, 1.0f, 0.0f, &projectionMatrix.m00);
}
-(void)render{
GLuint positionSlot = glGetAttribLocation(simpleProgram, "Position");
GLuint colorSlot = glGetAttribLocation(simpleProgram, "SourceColor");
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableVertexAttribArray(positionSlot);
glEnableVertexAttribArray(colorSlot);
// animation.Current.w = 1.0f;
// animation.End.w = 1.0f;
// animation.Start.w = 1.0f;
GLKMatrix4 rotation = GLKMatrix4MakeWithQuaternion(animation.Current);
//Set the model-view matrix
GLint modelviewUniform = glGetUniformLocation(simpleProgram, "Modelview");
GLKMatrix4 modelviewMatrix = GLKMatrix4Translate(rotation, 0.0f, 0.0f, -7.0f);
glUniformMatrix4fv(modelviewUniform, 1.0f, 0.0f, &modelviewMatrix.m00);
//Draw the cone
{
GLsizei stride = sizeof(Vertex);
const GLvoid *pCoords = &cone[0].Position.x;
const GLvoid *pColors = &cone[0].Color.r; //changed here to r from x for Red
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(cone)/sizeof(Vertex));
}
//Draw the disk that caps off the base of the cone
{
GLsizei stride = sizeof(Vertex);
const GLvoid *pCoords = &disk[0].Position.x;
const GLvoid *pColors = &disk[0].Color.r; //changed from x to r
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);
glDrawArrays(GL_TRIANGLE_FAN, 0, sizeof(disk)/sizeof(Vertex));
}
glDisableVertexAttribArray(positionSlot);
glDisableVertexAttribArray(colorSlot);
}
-(void)updateAnimationForTime:(float)timeStep{
NSString *currentQuaternion = NSStringFromGLKQuaternion(animation.Current);
NSString *endQuaternion = NSStringFromGLKQuaternion(animation.End);
if ([currentQuaternion isEqualToString:endQuaternion]) {
return;
}
animation.Elapsed += timeStep;
if (animation.Elapsed >= AnimationDuration) {
animation.Current = animation.End;
}
else{
float mu = animation.Elapsed / AnimationDuration;
animation.Current = GLKQuaternionSlerp(animation.Start, animation.End, mu);
}
}
-(void)onRotate:(enum DeviceOrientation) orientation{
GLKVector3 direction;
switch (orientation) {
case DeviceOrientationUnknown:
case DeviceOrientationPortrait:
direction = GLKVector3Make(0.0f, 1.0f, 0.0f);
break;
case DeviceOrientationPortraitUpsideDown:
direction = GLKVector3Make(0.0f, -1.0f, 0.0f);
break;
case DeviceOrientationFaceDown:
direction = GLKVector3Make(0.0f, 0.0f, -1.0f);
break;
case DeviceOrientationFaceUp:
direction = GLKVector3Make(0.0f, 0.0f, 1.0f);
break;
case DeviceOrientationLandscapeLeft:
direction = GLKVector3Make(+1.0f, 0.0f, 0.0f);
break;
case DeviceOrientationLandscapeRight:
direction = GLKVector3Make(-1.0f, 0.0f, 0.0f);
break;
}
animation.Elapsed = 0;
animation.Start = animation.Current = animation.End;
// animation.End = GLKQuaternionMakeWi
GLKVector3 vector = GLKVector3Make(0.0f, 1.0f, 0.0f);
animation.End = [self quaternionCreateFromVectors:vector :direction];
// (GLKVector3Make(0.0f, 1.0f, 0.0f), direction);
}
-(float)getRotationDirection{
float delta = desiredAngle - currentAngle;
// NSLog(@"delta: %f", delta);
if (delta == 0) {
return 0;
}
bool counterclockwise = ((delta > 0 && delta <= 180) || (delta < -180));
float test = counterclockwise ? +1.0 : -1.0;
NSLog(@"Return Value: %f",test );
return counterclockwise ? +1 : -1; //problem
}
-(void)applyRotation:(float)degrees{
}
-(void)applyOrthoWithMaxX
:(float)maxX andMaxY:(float)maxY{
}
-(GLuint)buildProgramWithVertex:(const char *)vShaderSource andFragment:(const char *)fShaderSource{
GLuint vertexShader = [self buildShaderWithSource:vShaderSource shaderType:GL_VERTEX_SHADER];
GLuint fragmentShader = [self buildShaderWithSource:fShaderSource shaderType:GL_FRAGMENT_SHADER];
GLuint programHandle = glCreateProgram();
glAttachShader(programHandle, vertexShader);
glAttachShader(programHandle, fragmentShader);
glLinkProgram(programHandle);
GLint linkSuccess;
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess);
if (linkSuccess == GL_FALSE) {
GLchar messages[256];
glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]);
NSLog(@"%s", messages);
exit(1);
}
return programHandle;
}
-(GLuint)buildShaderWithSource:(const char *)source shaderType:(GLenum)type{
GLuint shaderHandle = glCreateShader(type);
glShaderSource(shaderHandle, 1, &source, 0);
glCompileShader(shaderHandle);
GLint compileSuccess;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
if (compileSuccess == GL_FALSE) {
GLchar messages[256];
glGetShaderInfoLog(shaderHandle, sizeof(messages), 0, &messages[0]);
NSLog(@"%s", messages);
exit(1);
}
return shaderHandle;
}
-(GLKQuaternion) createFromAxis:(GLKVector3)axis withAngle:(float)radians //Minor calculating issues
{
GLKQuaternion q;
q.w = cosf(radians / 2);
q.x = q.y = q.z = sinf(radians / 2);
q.x *= axis.x;
q.y *= axis.y;
q.z *= axis.z;
return q;
}
-(GLKQuaternion) quaternionCreateFromVectors:(GLKVector3)v0 :(GLKVector3)v1 // Minor calculating issues
{
GLKVector3 v1Negative = GLKVector3Negate(v1);
NSLog(@"strings: v0: %@ v1:%@", NSStringFromGLKVector3(v0), NSStringFromGLKVector3(v1Negative));
if (/*NSStringFromGLKVector3(v0) == NSStringFromGLKVector3(v1Negative)*/ v0.g == v1Negative.g)
return [self createFromAxis:GLKVector3Make(1.0f, 0.0f, 0.0f) withAngle:M_1_PI];
GLKVector3 c = GLKVector3CrossProduct(v0, v1);// v0.Cross(v1);
int d = GLKVector3DotProduct(v0, v1); // v0.Dot(v1);
int s = sqrt((1 + d) *2);
GLKQuaternion q;
q.x = c.x / s;
q.y = c.y / s;
q.z = c.z / s;
q.w = s / 2.0f;
return q;
}
@end
在大多数情况下,我想让它运行,然后需要学习如何实现 GLKQuanternions 以添加方法,例如
m_animation.End = Quaternion::CreateFromVectors(vec3(0, 1, 0), direction);
又名:
inline QuaternionT<T> QuaternionT<T>::CreateFromVectors(const Vector3<T>& v0, const Vector3<T>& v1)
{
if (v0 == -v1)
return QuaternionT<T>::CreateFromAxisAngle(vec3(1, 0, 0), Pi);
Vector3<T> c = v0.Cross(v1);
T d = v0.Dot(v1);
T s = std::sqrt((1 + d) * 2);
QuaternionT<T> q;
q.x = c.x / s;
q.y = c.y / s;
q.z = c.z / s;
q.w = s / 2.0f;
return q;
}
作为临时插件,我在 Objective-C 中为四元数创建了相同的方法
-(GLKQuaternion) createFromAxis:(GLKVector3)axis withAngle:(float)radians //Minor calculating issues
{
GLKQuaternion q;
q.w = cosf(radians / 2);
q.x = q.y = q.z = sinf(radians / 2);
q.x *= axis.x;
q.y *= axis.y;
q.z *= axis.z;
return q;
}
-(GLKQuaternion) quaternionCreateFromVectors:(GLKVector3)v0 :(GLKVector3)v1 // Minor calculating issues
{
GLKVector3 v1Negative = GLKVector3Negate(v1);
NSLog(@"strings: v0: %@ v1:%@", NSStringFromGLKVector3(v0), NSStringFromGLKVector3(v1Negative));
if (/*NSStringFromGLKVector3(v0) == NSStringFromGLKVector3(v1Negative)*/ v0.g == v1Negative.g)
return [self createFromAxis:GLKVector3Make(1.0f, 0.0f, 0.0f) withAngle:M_1_PI];
GLKVector3 c = GLKVector3CrossProduct(v0, v1);// v0.Cross(v1);
int d = GLKVector3DotProduct(v0, v1); // v0.Dot(v1);
int s = sqrt((1 + d) *2);
GLKQuaternion q;
q.x = c.x / s;
q.y = c.y / s;
q.z = c.z / s;
q.w = s / 2.0f;
return q;
}
最佳答案
我似乎没有意识到 cmath 中的 Pi 函数是 const float Pi = 4 * std::stan(1.0f)。我所要做的就是重新计算为 const float dtheta = (M_PI * 2)/coneSlices;瞧。不过,我想要任何改进建议。
关于c++ - 将 C++ 从 Oreilly 书翻译成 Objective-C 的问题(iOS 的 3D 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15352741/
翻译自官方wiki: https://github.com/facebook/rocksdb/wiki/Write-Stalls 转载请注明出处: https://www.cnblogs.c
译者注:在微服务架构设计,构建API和服务间通信技术选型时,对 REST 和 gRPC 的理解和应用还存在知识盲区,近期看到国外的这篇文章: A detailed comparison of
rocksdb调试指引 翻译自官方wiki: https://github.com/facebook/rocksdb/wiki/RocksDB-Tuning-Guide 转载请注明出处: h
传统的ASP.NET Web Forms是一个非常好的主意,但现实需求非常复杂。随着时间的推移,现实世界的项目暴露出Web Forms的一些不足之处: “沉重的”视图状态:现实中在http请求之间
翻译自:Top 10 questions of Java Strings 简单地说,”==”测试两个字符串的引用是否相同,equals()测试两个字符串的值是否相同。除非你希望检
你好,今天我要和大家分享一些东西,举例来说这个在JavaScript中用的很多。我要讲讲回调(callbacks)。你知道什么时候用,怎么用这个吗?你真的理解了它在java环境中的用法了吗?当我也问
Java多线程面试问题 1. 进程和线程之间有什么不同? 一个进程是一个独立(self contained)的运行环境,它可以被看作一个程序或者一个应用。而线程是在进程中执行的一个
原文: [A Dive into .Net 8 Native AOT and Efficient Web Development] 作者: [sharmila subbiah] 引言 随着 .NE
这是Fiddle 是否可以在 angular-translate 中检查其他语言的键值是否可用,然后它可以从其他语言中提取该键值? 就像在示例中,我有英语和西类牙语。并且一个键值(例如“CONFIRM
我希望能够使用 $this->__('String to translate')在外部脚本中。我该怎么做呢? Magento 版本 1.5.1.0 . 最佳答案 我认为设置语言环境的正确方法是: Ma
我有一个开关小部件,它使用自定义数据属性值来标记自己。 .switch.switch-text .switch-label::before { right: 1px; color: #c2cf
是否有人遇到过这样的情况:用 Java 编写并由(例如)法国程序员编写的现有代码库必须转换为英语程序员可以理解的代码?这里的问题是变量/方法/类名称、注释等都将采用该特定语言。 现在有可用的自动化解决
维基百科和其他一些网站将解释器描述为将代码从某种高级语言翻译成某种低级语言的翻译器。然而,有很多解释,包括在 stackoverflow 中,它说解释器直接执行作为输入的指令,而无需事先转换。那么解释
我想将基本动画应用于自定义单元格中的某些元素,例如标签、图像:特别是,我想让这些动画在我触摸单元格内部时也启动。我是初学者,我只学会了使用 animateWithDuration 和 transiti
这个问题在这里已经有了答案: NSDateFormatter and current language in iOS11 (5 个回答) 已关闭 3 年前。 当使用这样的 DateComponentF
我想在点击 var about 时移动 div.willshow。但我单击那个 btn,只有它获得类 active。然后我再次单击那个 btn 它失去了类。如果我再点击一次,每项任务都无法正常工作。
我想要一个按钮在悬停时向下移动几个像素,但它又回来了。当您还在上面徘徊时,它不应该留在原处吗? Email Me .btn {background: #2ecc71; padding: .5em 1e
在我的应用程序中,我想添加功能将页面翻译为用户在浏览器中设置的所有语言,如果没有可用的语言,则翻译为默认英语...问题是浏览器与语言支持不一致。我找到了一个解决方法,我对一些返回用户语言的 Web 服
我的应用程序有一个 Help.htm 文件,用谷歌翻译翻译得相当好。我想将菜单项标记为“请勿翻译”,但我发现并尝试过的 HTML 标签都不起作用。对于以下内容,我使用了谷歌翻译网站 - 它翻译了我没想
我有以下代码: span { width:200px; height:100px; background-color:red; border:1px solid black; } span.c2 {
我是一名优秀的程序员,十分优秀!