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
// Note in order t use this code in the iOS Opengl Project Template,
// 1. The vertex shader needs to be updated to take a ModelViewMatrix as input.
// 2. The fragment shader needs to be edited to accept color as a uniform instead of a varying


- (void)render
{	
    static const GLfloat xAxes[] = 
	{
		0.0f,  0.0f, 0.0f,
		0.75f, 0.0f, 0.0f,
		
		0.0f, 0.0f, 0.0f,
		-0.75f, 0.0f, 0.0f
	};	
	
    static const GLfloat yAxes[] = 
	{
		0.0f,  0.0f,  0.0f, 
		0.0f,  0.75f,  0.0f,
		
		0.0f,  0.0f,  0.0f, 
		0.0f,  -0.75f,  0.0f
	};		
	
    static const GLfloat zAxes[] = 
	{
		0.0f,  0.0f,  0.0f,
		0.0f,  0.0f,  0.75f,
		
		0.0f,  0.0f,  0.0f,
		0.0f,  0.0f,  -0.50f
	};			
	
	GLfloat vRed[4]		= {1.0f, 0.0f, 0.0f, 1.0f};
	GLfloat vGreen[4]	= {0.0f, 1.0f, 0.0f, 1.0f};	
	GLfloat vBlue[4]	= {0.0f, 0.0f, 1.0f, 1.0f};		
	GLfloat vTorquise[4]	= {0.60f, 0.40f, 0.80f, 1.0f};			
	GLfloat vBlack[4]	= {0.0f, 0.0f, 0.0f, 1.0f};
	
	GLfloat mvpMatrixZRotation[16];
	
	GLfloat cosTheta = cosf(0.261f); // 15 degrees is 0.261 radians
	GLfloat sinTheta = sinf(0.261f);
	
	//-------------------------------------	
	
	mvpMatrixZRotation[0] = cosTheta;//1.0f;
	mvpMatrixZRotation[1] = -sinTheta;//0.0f;
	mvpMatrixZRotation[2] = 0.0f;
	mvpMatrixZRotation[3] = 0.0f;
	
	mvpMatrixZRotation[4] = sinTheta;//0.0f;
	mvpMatrixZRotation[5] = cosTheta;
	mvpMatrixZRotation[6] = 0.0f;
	mvpMatrixZRotation[7] = 0.0f;
	
	mvpMatrixZRotation[8] = 0.0f;
	mvpMatrixZRotation[9] = 0.0f;
	mvpMatrixZRotation[10] = 1.0f;//1.0f;//cosTheta;
	mvpMatrixZRotation[11] = 0.0f;
	
	mvpMatrixZRotation[12] = 0.0f;
	mvpMatrixZRotation[13] = 0.0f;
	mvpMatrixZRotation[14] = 0.0f;
	mvpMatrixZRotation[15] = 1.0f;

    // This application only creates a single context which is already set current at this point.
    // This call is redundant, but needed if dealing with multiple contexts.
    [EAGLContext setCurrentContext:context];

    // This application only creates a single default framebuffer which is already bound at this point.
    // This call is redundant, but needed if dealing with multiple framebuffers.
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);

    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // Use shader program
    glUseProgram(program);

    // Update uniform value
    glUniformMatrix4fv(uniforms[UNIFORM_MVP], 16, GL_FALSE, mvpMatrixZRotation);	

	glUniform4fv(uniforms[UNIFORM_COLOR], 4, vRed);
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, xAxes);
    glEnableVertexAttribArray(ATTRIB_VERTEX);

    glDrawArrays(GL_LINES, 0, 2);
	
	glUniform4fv(uniforms[UNIFORM_COLOR], 4, vBlue);
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, yAxes);
    glEnableVertexAttribArray(ATTRIB_VERTEX);	
    glDrawArrays(GL_LINES, 0, 2);	
	
	glUniform4fv(uniforms[UNIFORM_COLOR], 4, vGreen);	
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, zAxes);
    glEnableVertexAttribArray(ATTRIB_VERTEX);	
	
    glDrawArrays(GL_LINES, 0, 2);	

    // This application only creates a single color renderbuffer which is already bound at this point.
    // This call is redundant, but needed if dealing with multiple renderbuffers.
    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER];
}