OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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 <GLES2/gl2.h> | 8 #include <GLES2/gl2.h> |
9 #include "gpu/command_buffer/client/gles2_demo_cc.h" | 9 #include "gpu/command_buffer/client/gles2_demo_cc.h" |
10 | 10 |
| 11 namespace { |
| 12 |
| 13 int g_width = 512; |
| 14 int g_height = 512; |
| 15 GLuint g_texture = 0; |
| 16 int g_textureLoc = -1; |
| 17 GLuint g_programObject = 0; |
| 18 GLuint g_vbo = 0; |
| 19 GLsizei g_texCoordOffset = 0; |
| 20 |
| 21 void CheckGLError() { |
| 22 GLenum error = glGetError(); |
| 23 if (error != GL_NO_ERROR) { |
| 24 DLOG(ERROR) << "GL Error: " << error; |
| 25 } |
| 26 } |
| 27 |
| 28 GLuint LoadShader(GLenum type, const char* shaderSrc) { |
| 29 GLuint shader = glCreateShader(type); |
| 30 if (shader == 0) { |
| 31 return 0; |
| 32 } |
| 33 // Load the shader source |
| 34 glShaderSource(shader, 1, &shaderSrc, NULL); |
| 35 // Compile the shader |
| 36 glCompileShader(shader); |
| 37 // Check the compile status |
| 38 GLint value; |
| 39 glGetShaderiv(shader, GL_COMPILE_STATUS, &value); |
| 40 if (value == 0) { |
| 41 char buffer[1024]; |
| 42 GLsizei length; |
| 43 glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer); |
| 44 std::string log(buffer, length); |
| 45 DLOG(ERROR) << "Error compiling shader:" << log; |
| 46 glDeleteShader(shader); |
| 47 return 0; |
| 48 } |
| 49 return shader; |
| 50 } |
| 51 |
| 52 void InitShaders() { |
| 53 static const char* vShaderStr = |
| 54 "attribute vec3 g_Position;\n" |
| 55 "attribute vec2 g_TexCoord0;\n" |
| 56 "varying vec2 texCoord;\n" |
| 57 "void main()\n" |
| 58 "{\n" |
| 59 " gl_Position = vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n" |
| 60 " texCoord = g_TexCoord0;\n" |
| 61 "}\n"; |
| 62 static const char* fShaderStr = |
| 63 "uniform sampler2D tex;\n" |
| 64 "varying vec2 texCoord;\n" |
| 65 "void main()\n" |
| 66 "{\n" |
| 67 " gl_FragColor = texture2D(tex, texCoord);\n" |
| 68 "}\n"; |
| 69 |
| 70 GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr); |
| 71 GLuint fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr); |
| 72 // Create the program object |
| 73 GLuint programObject = glCreateProgram(); |
| 74 if (programObject == 0) { |
| 75 DLOG(ERROR) << "Creating program failed"; |
| 76 return; |
| 77 } |
| 78 glAttachShader(programObject, vertexShader); |
| 79 glAttachShader(programObject, fragmentShader); |
| 80 // Bind g_Position to attribute 0 |
| 81 // Bind g_TexCoord0 to attribute 1 |
| 82 glBindAttribLocation(programObject, 0, "g_Position"); |
| 83 glBindAttribLocation(programObject, 1, "g_TexCoord0"); |
| 84 // Link the program |
| 85 glLinkProgram(programObject); |
| 86 // Check the link status |
| 87 GLint linked; |
| 88 glGetProgramiv(programObject, GL_LINK_STATUS, &linked); |
| 89 if (linked == 0) { |
| 90 char buffer[1024]; |
| 91 GLsizei length; |
| 92 glGetProgramInfoLog(programObject, sizeof(buffer), &length, buffer); |
| 93 std::string log(buffer, length); |
| 94 DLOG(ERROR) << "Error linking program:" << log; |
| 95 glDeleteProgram(programObject); |
| 96 return; |
| 97 } |
| 98 g_programObject = programObject; |
| 99 g_textureLoc = glGetUniformLocation(g_programObject, "tex"); |
| 100 glGenBuffers(1, &g_vbo); |
| 101 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); |
| 102 static float vertices[] = { |
| 103 0.25, 0.75, 0.0, |
| 104 -0.75, 0.75, 0.0, |
| 105 -0.75, -0.25, 0.0, |
| 106 0.25, 0.75, 0.0, |
| 107 -0.75, -0.25, 0.0, |
| 108 0.25, -0.25, 0.0, |
| 109 }; |
| 110 static float texCoords[] = { |
| 111 1.0, 1.0, |
| 112 0.0, 1.0, |
| 113 0.0, 0.0, |
| 114 1.0, 1.0, |
| 115 0.0, 0.0, |
| 116 1.0, 0.0, |
| 117 }; |
| 118 g_texCoordOffset = sizeof(vertices); |
| 119 glBufferData(GL_ARRAY_BUFFER, |
| 120 sizeof(vertices) + sizeof(texCoords), |
| 121 NULL, |
| 122 GL_STATIC_DRAW); |
| 123 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); |
| 124 glBufferSubData(GL_ARRAY_BUFFER, g_texCoordOffset, |
| 125 sizeof(texCoords), texCoords); |
| 126 CheckGLError(); |
| 127 } |
| 128 |
| 129 void Draw() { |
| 130 // Note: the viewport is automatically set up to cover the entire Canvas. |
| 131 // Clear the color buffer |
| 132 glClear(GL_COLOR_BUFFER_BIT); |
| 133 CheckGLError(); |
| 134 // Use the program object |
| 135 glUseProgram(g_programObject); |
| 136 CheckGLError(); |
| 137 // Load the vertex data |
| 138 glBindBuffer(GL_ARRAY_BUFFER, g_vbo); |
| 139 glEnableVertexAttribArray(0); |
| 140 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); |
| 141 glEnableVertexAttribArray(1); |
| 142 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, |
| 143 reinterpret_cast<const void*>(g_texCoordOffset)); |
| 144 CheckGLError(); |
| 145 // Bind the texture to texture unit 0 |
| 146 glBindTexture(GL_TEXTURE_2D, g_texture); |
| 147 CheckGLError(); |
| 148 // Point the uniform sampler to texture unit 0 |
| 149 glUniform1i(g_textureLoc, 0); |
| 150 CheckGLError(); |
| 151 glDrawArrays(GL_TRIANGLES, 0, 6); |
| 152 CheckGLError(); |
| 153 glFlush(); |
| 154 } |
| 155 |
| 156 GLuint CreateCheckerboardTexture() { |
| 157 static unsigned char pixels[] = { |
| 158 255, 255, 255, |
| 159 0, 0, 0, |
| 160 0, 0, 0, |
| 161 255, 255, 255, |
| 162 }; |
| 163 GLuint texture; |
| 164 glGenTextures(1, &texture); |
| 165 glBindTexture(GL_TEXTURE_2D, texture); |
| 166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 170 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 171 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, |
| 172 pixels); |
| 173 return texture; |
| 174 } |
| 175 |
| 176 void Init() { |
| 177 glClearColor(0.f, 0.f, .7f, 1.f); |
| 178 g_texture = CreateCheckerboardTexture(); |
| 179 InitShaders(); |
| 180 } |
| 181 |
| 182 } // anonymous namespace. |
| 183 |
11 void GLFromCPPTestFunction() { | 184 void GLFromCPPTestFunction() { |
12 static bool foo = true; | 185 static bool initialized = false; |
13 foo = !foo; | 186 if (!initialized) { |
14 glClearColor( | 187 initialized = true; |
15 foo ? 1.0f : 0.0f, | 188 Init(); |
16 foo ? 0.0f : 1.0f, | 189 } |
17 1.0f, | 190 Draw(); |
18 1.0f); | |
19 } | 191 } |
20 | 192 |
21 | 193 |
| 194 |
OLD | NEW |