Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2016 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 
     17 #define _USE_MATH_DEFINES
     18 #include <math.h>
     19 #include "utils.h"
     20 
     21 float DegToRad(float deg) {
     22   return deg * (float)M_PI / 180.0f;
     23 }
     24 
     25 bool CompileShader(GLuint shader, const std::string& shader_string) {
     26   std::string prefix = "#version 300 es\n";
     27   std::string string_with_prefix = prefix + shader_string;
     28   const char* shader_str[] = { string_with_prefix.data() };
     29   glShaderSource(shader, 1, shader_str, NULL);
     30   glCompileShader(shader);
     31 
     32   GLint success;
     33   GLchar infoLog[512];
     34   glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
     35   if (!success)
     36   {
     37     glGetShaderInfoLog(shader, 512, NULL, infoLog);
     38     LOGI("Shader Failed to compile: %s -- %s\n", *shader_str, infoLog);
     39     return false;
     40   }
     41   return true;
     42 }
     43 
     44 bool LinkProgram(GLuint program, GLuint vertex_shader, GLuint fragment_shader) {
     45   glAttachShader(program, vertex_shader);
     46   glAttachShader(program, fragment_shader);
     47   glLinkProgram(program);
     48 
     49   // Check for linking errors
     50   GLint success;
     51   GLchar infoLog[512];
     52   glGetProgramiv(program, GL_LINK_STATUS, &success);
     53   if (!success) {
     54     glGetProgramInfoLog(program, 512, NULL, infoLog);
     55     LOGE("Shader failed to link: %s\n", infoLog);
     56     return false;
     57   }
     58 
     59   return true;
     60 }
     61 
     62 GLenum GLCheckError() {
     63   return GLCheckErrorStr("");
     64 }
     65 
     66 GLenum GLCheckErrorStr(std::string msg) {
     67   GLenum e = glGetError();
     68   std::string str;
     69   if (e != GL_NO_ERROR) {
     70     switch (e) {
     71     case GL_INVALID_ENUM:
     72       str = "GL_INVALID_ENUM";
     73       break;
     74     case GL_INVALID_OPERATION:
     75       str = "GL_INVALID_OPERATION";
     76       break;
     77     case GL_INVALID_VALUE:
     78       str = "GL_INVALID_VALUE";
     79       break;
     80     case GL_OUT_OF_MEMORY:
     81       str = "GL_OUT_OF_MEMORY";
     82       break;
     83     case GL_INVALID_FRAMEBUFFER_OPERATION:
     84       str = "GL_INVALID_FRAMEBUFFER_OPERATION";
     85       break;
     86     }
     87     LOGE("OpenGL error : %s : %s (%#08x)\n", msg.c_str(), str.c_str(), e);
     88   }
     89   return e;
     90 }
     91