-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTGLPrimitiveModel.m
More file actions
173 lines (130 loc) · 4.3 KB
/
CTGLPrimitiveModel.m
File metadata and controls
173 lines (130 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// CTGLPrimitiveModel.m
// GLSDKCode
//
// Created by codew on 2017/8/12.
// Copyright © 2017年 codew. All rights reserved.
//
#import "CTGLPrimitiveModel.h"
@implementation CTGLPrimitiveModel
#pragma mark - 矩形
void rectangle(long *numVertices, long *numIndices,GLfloat **vVertices,GLfloat **vTextCoord, GLushort **indices)
{
int numVerticesTemp = 4;
int numTextCoordTemp = 4;
int numIndicesTemp = 6;
*numVertices = numVerticesTemp;
*numIndices = numIndicesTemp;
NSLog(@"顶点数-------%zd", numVerticesTemp);
if (vVertices != NULL) {
*vVertices = (GLfloat*) malloc(sizeof(GLfloat) * numVerticesTemp * 3);
}
if (vTextCoord != NULL) {
*vTextCoord = (GLfloat*) malloc(sizeof(GLfloat) * numTextCoordTemp * 2);
}
if (indices != NULL) {
*indices = (GLushort *) malloc(sizeof(GLushort) * numIndicesTemp);
}
(*vVertices)[0] = -1.0;
(*vVertices)[1] = -1.0;
(*vVertices)[2] = 0.0;
(*vVertices)[3] = 1.0;
(*vVertices)[4] = -1.0;
(*vVertices)[5] = 0.0;
(*vVertices)[6] = 1.0;
(*vVertices)[7] = 1.0;
(*vVertices)[8] = 0.0;
(*vVertices)[9] = -1.0;
(*vVertices)[10] = 1.0;
(*vVertices)[11] = 0.0;
(*vTextCoord)[0] = 0.0;
(*vTextCoord)[1] = 1.0;
(*vTextCoord)[2] = 1.0;
(*vTextCoord)[3] = 1.0;
(*vTextCoord)[4] = 1.0;
(*vTextCoord)[5] = 0.0;
(*vTextCoord)[6] = 0.0;
(*vTextCoord)[7] = 0.0;
(*indices)[0] = 0;
(*indices)[1] = 1;
(*indices)[2] = 2;
(*indices)[3] = 2;
(*indices)[4] = 3;
(*indices)[5] = 0;
}
- (instancetype)initWithType:(CT_GLPrimitiveModelType)primitiveModelType
{
self = [super init];
if (self) {
[self updateMatrix4MakeFrustumParameterWithType:primitiveModelType];
}
return self;
}
- (void)updateMatrix4MakeFrustumParameterWithType:(CT_GLPrimitiveModelType)primitiveModelType
{
self.primitiveModelType = primitiveModelType;
}
#pragma mark - Set Get
- (void)setPrimitiveModelType:(CT_GLPrimitiveModelType)primitiveModelType
{
_primitiveModelType = primitiveModelType;
if (_primitiveModelType == CT_GLKVCItemTypeRectangle){
_cam_scale = 2.0;
_near = 1.0;
_far = 4.0;
[self resettingEyePointOrigin];
_centerX = 0;
_centerY = 0;
_centerZ = 0;
_upX = 0;
_upY = 1;
_upZ = 0;
rectangle(&_numVerticesTextCoord, &_numIndices, &_vVertices, &_vTextCoord, &_indices);
}
}
#pragma mark - private Method
- (void)resettingEyePointOrigin
{
if (_primitiveModelType == CT_GLKVCItemTypeRectangle) {
_eyeX = 0;
_eyeY = 0;
_eyeZ = 2;
}
}
- (void)enableItem
{
//Indices
glGenBuffers(1, &_vertexIndicesBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.vertexIndicesBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.numIndices*sizeof(GLushort), _indices, GL_STATIC_DRAW);
// Vertex
//
//为缓存提供数据7步骤之 一~五
// 步骤之一: 创建唯一标识符
glGenBuffers(1, &_vertexBufferID);
// 步骤之二: 绑定, 告诉ES 为接下来的运算使用一个缓存
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBufferID);
// 步骤之三: 复制数据到缓存中
glBufferData(GL_ARRAY_BUFFER, _numVerticesTextCoord*3*sizeof(GLfloat), _vVertices, GL_STATIC_DRAW);
// 步骤之四: 启动 启动顶点渲染操作
glEnableVertexAttribArray(GLKVertexAttribPosition);
// 步骤之五: 设置指针,数据大小, 告诉ES数据在哪里, 怎么解释每个顶点保存的数据
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*3, NULL);
// Texture Coordinates
glGenBuffers(1, &_vertexTexCoordID);
glBindBuffer(GL_ARRAY_BUFFER, self.vertexTexCoordID);
glBufferData(GL_ARRAY_BUFFER, _numVerticesTextCoord*2*sizeof(GLfloat), _vTextCoord, GL_DYNAMIC_DRAW);
}
- (void)deleteBuffer
{
glDeleteBuffers (1,
&_vertexBufferID);
_vertexBufferID = 0;
glDeleteBuffers (1,
&_vertexTexCoordID);
_vertexTexCoordID = 0;
glDeleteBuffers (1,
&_vertexIndicesBufferID);
_vertexIndicesBufferID = 0;
}
@end