1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <GLES2/gl2.h>
17 #include <GLES2/gl2ext.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "color_one.h"
21 #include "common.h"
22 #include "vertex.h"
23 #include "shader.h"
24
25 #define LOG_TAG "color_one"
26 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
27 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
28
29 static const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f,
30 0.5f, -0.5f };
31 GLuint gProgram;
32 GLuint gvPositionHandle;
33 GLuint gvColorHandle;
34 int width;
35 int height;
36
37 float dataFloat[4];
initColorOne(int w,int h)38 void initColorOne(int w, int h){
39 GLuint vertexShader = loadShader(GL_VERTEX_SHADER, color_one_vertex_shader_one);
40 GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, color_one_fragment_shader_one);
41 gProgram = glCreateProgram();
42 LOGI("Program %d\n", gProgram);
43 width = w;
44 height = h;
45 glAttachShader(gProgram, vertexShader);
46 checkGlError("glAttachShader");
47 glAttachShader(gProgram, fragmentShader);
48 checkGlError("glAttachShader");
49 glBindAttribLocation(gProgram, 0, "vPosition");
50 glBindAttribLocation(gProgram, 1, "vColor");
51 glLinkProgram(gProgram);
52 GLint linkStatus = GL_FALSE;
53 glGetProgramiv(gProgram, GL_LINK_STATUS, &linkStatus);
54 if (linkStatus != GL_TRUE) {
55 GLint bufLength = 0;
56 glGetProgramiv(gProgram, GL_INFO_LOG_LENGTH, &bufLength);
57 if (bufLength) {
58 char* buf = (char*) malloc(bufLength);
59 if (buf) {
60 glGetProgramInfoLog(gProgram, bufLength, NULL, buf);
61 LOGE("Could not link program:\n%s\n", buf);
62 free(buf);
63 }
64 }
65 }
66 LOGI("w %d, h %d\n",w, h);
67 glViewport(0, 0, w, h);
68
69 checkGlError("glViewport");
70 gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
71 gvColorHandle = glGetAttribLocation(gProgram, "vColor");
72 GLsizei maxCount = 10;
73 GLsizei count;
74 GLuint shaders[maxCount];
75
76 glGetAttachedShaders(gProgram, maxCount,
77 &count,
78 shaders);
79 LOGI("Attached Shader First element : %d\n", *shaders);
80 LOGI("ShaderCount %d\n", count);
81 GLint error = glGetError();
82 return;
83 }
84
drawColorOne(float mColor[])85 float* drawColorOne(float mColor[]){
86 LOGI("drawColorOne start");
87 static float grey;
88 grey = 0.01f;
89
90 glClearColor(grey, grey, grey, 1.0f);
91 checkGlError("glClearColor");
92 glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
93 checkGlError("glClear");
94
95 glUseProgram(gProgram);
96 checkGlError("glUseProgram");
97
98 glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
99 checkGlError("glVertexAttribPointer");
100 glEnableVertexAttribArray(0);
101 checkGlError("glEnableVertexAttribArray");
102
103 glVertexAttribPointer(gvColorHandle,4, GL_FLOAT, GL_FALSE, 0, mColor);
104 checkGlError("glVertexAttribPointer");
105 glEnableVertexAttribArray(1);
106 checkGlError("glEnableVertexAttribArray");
107
108 glDrawArrays(GL_TRIANGLES, 0, 3);
109 checkGlError("glDrawArrays");
110 GLubyte data[4*1];
111
112
113 glReadPixels(width/2, height/2, 1,1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)&data);
114 for(size_t i = 0; i < sizeof(data); i++){
115 dataFloat[i] = data[i];
116 }
117
118 return dataFloat;
119 }
120
deleteColorOne()121 void deleteColorOne() {
122 glDeleteProgram(gProgram);
123 }
124
checkGlError(const char * op)125 static void checkGlError(const char* op) {
126 for (GLint error = glGetError(); error; error
127 = glGetError()) {
128 LOGI("after %s() glError (0x%x)\n", op, error);
129 }
130 }
131