OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file is here so other GLES2 related files can have a common set of | 5 // This file is here so other GLES2 related files can have a common set of |
6 // includes where appropriate. | 6 // includes where appropriate. |
7 | 7 |
| 8 #include <math.h> |
8 #include <GLES2/gl2.h> | 9 #include <GLES2/gl2.h> |
9 #include "gpu/command_buffer/client/gles2_demo_cc.h" | 10 #include "gpu/command_buffer/client/gles2_demo_cc.h" |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 GLuint g_texture = 0; | 14 GLuint g_texture = 0; |
14 int g_textureLoc = -1; | 15 int g_textureLoc = -1; |
15 GLuint g_programObject = 0; | 16 GLuint g_programObject = 0; |
| 17 GLuint g_worldMatrixLoc = 0; |
16 GLuint g_vbo = 0; | 18 GLuint g_vbo = 0; |
17 GLsizei g_texCoordOffset = 0; | 19 GLsizei g_texCoordOffset = 0; |
| 20 int g_angle = 0; |
18 | 21 |
19 void CheckGLError() { | 22 void CheckGLError() { |
20 GLenum error = glGetError(); | 23 GLenum error = glGetError(); |
21 if (error != GL_NO_ERROR) { | 24 if (error != GL_NO_ERROR) { |
22 DLOG(ERROR) << "GL Error: " << error; | 25 DLOG(ERROR) << "GL Error: " << error; |
23 } | 26 } |
24 } | 27 } |
25 | 28 |
26 GLuint LoadShader(GLenum type, const char* shaderSrc) { | 29 GLuint LoadShader(GLenum type, const char* shaderSrc) { |
27 GLuint shader = glCreateShader(type); | 30 GLuint shader = glCreateShader(type); |
(...skipping 14 matching lines...) Expand all Loading... |
42 std::string log(buffer, length); | 45 std::string log(buffer, length); |
43 DLOG(ERROR) << "Error compiling shader:" << log; | 46 DLOG(ERROR) << "Error compiling shader:" << log; |
44 glDeleteShader(shader); | 47 glDeleteShader(shader); |
45 return 0; | 48 return 0; |
46 } | 49 } |
47 return shader; | 50 return shader; |
48 } | 51 } |
49 | 52 |
50 void InitShaders() { | 53 void InitShaders() { |
51 static const char* vShaderStr = | 54 static const char* vShaderStr = |
| 55 "uniform mat4 worldMatrix;\n" |
52 "attribute vec3 g_Position;\n" | 56 "attribute vec3 g_Position;\n" |
53 "attribute vec2 g_TexCoord0;\n" | 57 "attribute vec2 g_TexCoord0;\n" |
54 "varying vec2 texCoord;\n" | 58 "varying vec2 texCoord;\n" |
55 "void main()\n" | 59 "void main()\n" |
56 "{\n" | 60 "{\n" |
57 " gl_Position = vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n" | 61 " gl_Position = worldMatrix *\n" |
| 62 " vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n" |
58 " texCoord = g_TexCoord0;\n" | 63 " texCoord = g_TexCoord0;\n" |
59 "}\n"; | 64 "}\n"; |
60 static const char* fShaderStr = | 65 static const char* fShaderStr = |
61 "uniform sampler2D tex;\n" | 66 "uniform sampler2D tex;\n" |
62 "varying vec2 texCoord;\n" | 67 "varying vec2 texCoord;\n" |
63 "void main()\n" | 68 "void main()\n" |
64 "{\n" | 69 "{\n" |
65 " gl_FragColor = texture2D(tex, texCoord);\n" | 70 " gl_FragColor = texture2D(tex, texCoord);\n" |
66 "}\n"; | 71 "}\n"; |
67 | 72 |
(...skipping 19 matching lines...) Expand all Loading... |
87 if (linked == 0) { | 92 if (linked == 0) { |
88 char buffer[1024]; | 93 char buffer[1024]; |
89 GLsizei length; | 94 GLsizei length; |
90 glGetProgramInfoLog(programObject, sizeof(buffer), &length, buffer); | 95 glGetProgramInfoLog(programObject, sizeof(buffer), &length, buffer); |
91 std::string log(buffer, length); | 96 std::string log(buffer, length); |
92 DLOG(ERROR) << "Error linking program:" << log; | 97 DLOG(ERROR) << "Error linking program:" << log; |
93 glDeleteProgram(programObject); | 98 glDeleteProgram(programObject); |
94 return; | 99 return; |
95 } | 100 } |
96 g_programObject = programObject; | 101 g_programObject = programObject; |
| 102 g_worldMatrixLoc = glGetUniformLocation(g_programObject, "worldMatrix"); |
97 g_textureLoc = glGetUniformLocation(g_programObject, "tex"); | 103 g_textureLoc = glGetUniformLocation(g_programObject, "tex"); |
98 glGenBuffers(1, &g_vbo); | 104 glGenBuffers(1, &g_vbo); |
99 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); | 105 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); |
100 static float vertices[] = { | 106 static float vertices[] = { |
101 0.25, 0.75, 0.0, | 107 0.25, 0.75, 0.0, |
102 -0.75, 0.75, 0.0, | 108 -0.75, 0.75, 0.0, |
103 -0.75, -0.25, 0.0, | 109 -0.75, -0.25, 0.0, |
104 0.25, 0.75, 0.0, | 110 0.25, 0.75, 0.0, |
105 -0.75, -0.25, 0.0, | 111 -0.75, -0.25, 0.0, |
106 0.25, -0.25, 0.0, | 112 0.25, -0.25, 0.0, |
(...skipping 10 matching lines...) Expand all Loading... |
117 glBufferData(GL_ARRAY_BUFFER, | 123 glBufferData(GL_ARRAY_BUFFER, |
118 sizeof(vertices) + sizeof(texCoords), | 124 sizeof(vertices) + sizeof(texCoords), |
119 NULL, | 125 NULL, |
120 GL_STATIC_DRAW); | 126 GL_STATIC_DRAW); |
121 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); | 127 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); |
122 glBufferSubData(GL_ARRAY_BUFFER, g_texCoordOffset, | 128 glBufferSubData(GL_ARRAY_BUFFER, g_texCoordOffset, |
123 sizeof(texCoords), texCoords); | 129 sizeof(texCoords), texCoords); |
124 CheckGLError(); | 130 CheckGLError(); |
125 } | 131 } |
126 | 132 |
| 133 #define PI 3.1415926535897932384626433832795f |
| 134 |
127 void Draw() { | 135 void Draw() { |
| 136 // TODO(kbr): base the angle on time rather than on ticks |
| 137 g_angle = (g_angle + 1) % 360; |
| 138 // Rotate about the Z axis |
| 139 GLfloat rot_matrix[16]; |
| 140 GLfloat cos_angle = cosf(static_cast<GLfloat>(g_angle) * PI / 180.0f); |
| 141 GLfloat sin_angle = sinf(static_cast<GLfloat>(g_angle) * PI / 180.0f); |
| 142 // OpenGL matrices are column-major |
| 143 rot_matrix[0] = cos_angle; |
| 144 rot_matrix[1] = sin_angle; |
| 145 rot_matrix[2] = 0.0f; |
| 146 rot_matrix[3] = 0.0f; |
| 147 |
| 148 rot_matrix[4] = -sin_angle; |
| 149 rot_matrix[5] = cos_angle; |
| 150 rot_matrix[6] = 0.0f; |
| 151 rot_matrix[7] = 0.0f; |
| 152 |
| 153 rot_matrix[8] = 0.0f; |
| 154 rot_matrix[9] = 0.0f; |
| 155 rot_matrix[10] = 1.0f; |
| 156 rot_matrix[11] = 0.0f; |
| 157 |
| 158 rot_matrix[12] = 0.0f; |
| 159 rot_matrix[13] = 0.0f; |
| 160 rot_matrix[14] = 0.0f; |
| 161 rot_matrix[15] = 1.0f; |
| 162 |
128 // Note: the viewport is automatically set up to cover the entire Canvas. | 163 // Note: the viewport is automatically set up to cover the entire Canvas. |
129 // Clear the color buffer | 164 // Clear the color buffer |
130 glClear(GL_COLOR_BUFFER_BIT); | 165 glClear(GL_COLOR_BUFFER_BIT); |
131 CheckGLError(); | 166 CheckGLError(); |
132 // Use the program object | 167 // Use the program object |
133 glUseProgram(g_programObject); | 168 glUseProgram(g_programObject); |
134 CheckGLError(); | 169 CheckGLError(); |
| 170 // Set up the model matrix |
| 171 glUniformMatrix4fv(g_worldMatrixLoc, 1, GL_FALSE, rot_matrix); |
| 172 |
135 // Load the vertex data | 173 // Load the vertex data |
136 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); | 174 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); |
137 glEnableVertexAttribArray(0); | 175 glEnableVertexAttribArray(0); |
138 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); | 176 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); |
139 glEnableVertexAttribArray(1); | 177 glEnableVertexAttribArray(1); |
140 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, | 178 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, |
141 reinterpret_cast<const void*>(g_texCoordOffset)); | 179 reinterpret_cast<const void*>(g_texCoordOffset)); |
142 CheckGLError(); | 180 CheckGLError(); |
143 // Bind the texture to texture unit 0 | 181 // Bind the texture to texture unit 0 |
144 glBindTexture(GL_TEXTURE_2D, g_texture); | 182 glBindTexture(GL_TEXTURE_2D, g_texture); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } // anonymous namespace. | 218 } // anonymous namespace. |
181 | 219 |
182 void GLFromCPPTestFunction() { | 220 void GLFromCPPTestFunction() { |
183 static bool initialized = false; | 221 static bool initialized = false; |
184 if (!initialized) { | 222 if (!initialized) { |
185 initialized = true; | 223 initialized = true; |
186 Init(); | 224 Init(); |
187 } | 225 } |
188 Draw(); | 226 Draw(); |
189 } | 227 } |
OLD | NEW |