OpenGL ES 学习笔记五之三维变换

参考链接:http://blog.csdn.net/itianyi/article/details/51498993

OpenGL 使用列主序矩阵,即列矩阵因此我们总是倒过来算的(左乘矩阵,变换效果是按从右向左的顺序进行): 投影矩阵 × 视图矩阵 × 模型矩阵 × 3D位置。

在glsl中进行矩阵的乘法如下:

// variable pass into

attribute vec4 Position;    // position of vertex

attribute vec4 SourceColor; // color of vertex

uniform mat4 projectionMatrix;

uniform mat4 modelViewMatrix;

// variable pass out into fragment shader

// varying means that calculate the color of every pixel between two vertex linearly(smoothly) according to the 2 vertex's color

varying vec4 DestinationColor;

void main(void) {

DestinationColor = SourceColor;

// gl_Position is built-in pass-out variable. Must config for in vertex shader

//    gl_Position = Position;

gl_Position = projectionMatrix * modelViewMatrix * Position;

}

透视投影变换示意图:

    float width = self.view.frame.size.width;

    float height = self.view.frame.size.height;

    KSMatrix4 _projectionMatrix;

    ksMatrixLoadIdentity(&_projectionMatrix);

    float aspect = width / height; //长宽比

    ksPerspective(&_projectionMatrix, 30.0, aspect, 5.0f, 20.0f); //透视变换,视角30°

    //设置glsl里面的投影矩阵

    glUniformMatrix4fv(projectionMatrixSlot, 1, GL_FALSE, (GLfloat*)&_projectionMatrix.m[0][0]);

   glEnable(GL_CULL_FACE);


视锥体/视景体:

- (void)prejectionMatrix:(GLint)projectionSlot

{

//然后,使用math library来创建投影矩阵。通过这个让你指定坐标,以及远近屏位置的方式,来创建矩阵,会让事情比较简单。

CC3GLMatrix *projection = [CC3GLMatrix matrix];

float h =  self.view.frame.size.height / self.view.frame.size.width;

//    [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:5 andFar:20];

[projection populateFromFrustumLeft:-0.5 andRight:0.5 andBottom:-h/2 andTop:h/2 andNear:5 andFar:20];

//把数据传入到vertex shader的方式,叫做 glUniformMatrix4fv. 这个CC3GLMatrix类有一个很方便的方法 glMatrix,来把矩阵转换成OpenGL的array格式

glUniformMatrix4fv(projectionSlot, 1, 0, projection.glMatrix);

}

以上两种设置方法效果一样,使用不同的数学处理库,第一种基于网络库GLESMath,,第二种基于Cocos3DMathLib库,两种辅助数学处理库都可以方便的处理基本的运算,非常方便。

默认可视区域为Z轴负方向,因此,通过Z轴平移变换,将视图平移到Z轴负方向才能可见,可见大小比例受aspect面的大小的影响,也受near和far的影响。

平移和旋转变换

KSMatrix4 _modelViewMatrix;

ksMatrixLoadIdentity(&_modelViewMatrix);

//平移

ksTranslate(&_modelViewMatrix, 0.0, 0.0, -12.5);

KSMatrix4 _rotationMatrix;

ksMatrixLoadIdentity(&_rotationMatrix);

//旋转

ksRotate(&_rotationMatrix, degree, 1.0, 0.0, 0.0); //绕X轴

ksRotate(&_rotationMatrix, yDegree, 0.0, 1.0, 0.0); //绕Y轴

//把变换矩阵相乘,注意先后顺序

ksMatrixMultiply(&_modelViewMatrix, &_rotationMatrix, &_modelViewMatrix);

//    ksMatrixMultiply(&_modelViewMatrix, &_modelViewMatrix, &_rotationMatrix);

// Load the model-view matrix

glUniformMatrix4fv(modelViewMatrixSlot, 1, GL_FALSE, (GLfloat*)&_modelViewMatrix.m[0][0]);

另一种Cocos3DMathLib库的写法:

- (void)modelViewMatrix:(GLint)modelView

{

CC3GLMatrix *modelMatrix = [CC3GLMatrix matrix];

//每3.14秒,0 - -7循环一次  sin(CACurrentMediaTime())

[modelMatrix populateFromTranslation:CC3VectorMake(0, 0, -12.5)];

[modelMatrix rotateBy:CC3VectorMake(degree, yDegree, 0)];

glUniformMatrix4fv(modelView, 1, 0, modelMatrix.glMatrix);

}


degree,yDegree 可以作为动态参数,动态通过手势改变,或者根据时间改变。

通过手势改变:

#pragma mark--touch移动中-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event{

UITouch *touch=[touches anyObject];

//取得当前位置

CGPoint current=[touch locationInView:self.view];

//取得前一个位置

CGPoint previous=[touch previousLocationInView:self.view];

//移动偏移量

CGPoint offset=CGPointMake(current.x-previous.x, current.y-previous.y);

NSLog(@"X:%f Y:%f",offset.x,offset.y);

degree += offset.x * 1;

yDegree += offset.y * 1;

}

通过时间改变:

#pragma mark - timer=========================

// Add new method before init

- (void)setupDisplayLink {

self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateDisplay)];

self.displayLink.paused = NO;

[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

}

-(void)updateDisplay{

NSLog(@"updateTextColor________");

if (autoRotate) {

degree +=  1;

yDegree += 1;

}

[self render];

}

在时间目标方法里,每次都会渲染更新当前画面,另一种让视图旋转的平移参数是sin(CACurrentMediaTime())的返回值,可以进行正弦函数周期性位置变化。范围0~1.


效果图:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容