- 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/
我遵循了一本名为“Sitepoint Full Stack Javascript with MEAN”的书中的教程,我刚刚完成了第 6 章,应该已经创建了一个带有“数据库”的“服务器”。数据库只不过是
在 Jquery 中,我创建两个数组,一个嵌入另一个数组,就像这样...... arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arra
这个问题在这里已经有了答案: What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wa
我被放在别人的代码上,有一个类用作其他组件的基础。当我尝试 ng serve --aot(或 build --prod)时,我得到以下信息。 @Component({ ...,
我正在测试一些代码,并使用数据创建了一个 json 文件。 问题是我在警报中收到“[object Object],[object Object]”。没有数据。 我做错了什么? 这是代码:
我想打印 [object Object],[object Object] 以明智地 "[[{ 'x': '1', 'y': '0' }, { 'x': '2', 'y': '1' }]]"; 在 ja
我有一个功能 View ,我正在尝试以特殊格式的方式输出。但我无法让列表功能正常工作。 我得到的唯一返回是[object Object][object Object] [object Object]
在使用优秀的 Sim.js 和 Three.js 库处理 WebGL 项目时,我偶然发现了下一个问题: 一路走来,它使用了 THREE.Ray 的下一个构造函数: var ray = new THRE
我正在使用 Material UI 进行多重选择。这是我的代码。 {listStates.map(col => (
我的代码使用ajax: $("#keyword").keyup(function() { var keyword = $("#keyword").val(); if (keyword.
我遇到了下一个错误,无法理解如何解决它。 Can't resolve all parameters for AuthenticationService: ([object Object], ?, [o
我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。 这是组件的代码: import { Component, OnInit } from '@angul
我正在开发 NodeJs 应用程序,它是博客应用程序。我使用了快速验证器,我尝试在 UI 端使用快速闪存消息将帖子保存在数据库中之前使用闪存消息验证数据,我成功地将数据保存在数据库中,但在提交表单后消
我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数
我继承了一个非常草率的项目,我的任务是解释为什么它不好。我注意到他们在整个代码中都进行了这样的比较 (IQueryable).FirstOrDefault(x => x.Facility == fac
我只是在删除数组中的对象时偶然发现了这一点。 代码如下: friends = []; friends.push( { a: 'Nexus', b: 'Muffi
这两个代码片段有什么区别: object = nil; [object release] 对比 [object release]; object = nil; 哪个是最佳实践? 最佳答案 object
我应该为其他人将从中继承的第一个父对象传递哪个参数,哪个参数更有效 Object.create(Object.prototype) Object.create(Object) Object.creat
我在不同的对象上安排不同的选择器 [self performSelector:@selector(doSmth) withObject:objectA afterDelay:1]; [self per
NSLog(@"%p", &object); 和 NSLog(@"%p", object); 有什么区别? 两者似乎都打印出一个内存地址,但我不确定哪个是对象的实际内存地址。 最佳答案 这就是我喜欢的
我是一名优秀的程序员,十分优秀!