OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
glutil.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "glutil.h"
22 #include <fstream>
23 #include <iostream>
24 
26 
27 void cull(bool enabled, GLenum front, GLenum mode)
28 {
29  if (enabled)
30  {
31  glEnable(GL_CULL_FACE);
32  glFrontFace(front);
33  glCullFace(mode);
34  }
35  else
36  {
37  glDisable(GL_CULL_FACE);
38  }
39 }
40 
41 void depth_test(bool enabled, GLenum func)
42 {
43  if (enabled)
44  {
45  glEnable(GL_DEPTH_TEST);
46  glDepthFunc(func);
47  }
48  else
49  {
50  glDisable(GL_DEPTH_TEST);
51  }
52 }
53 
54 void depth_write(bool enabled)
55 {
56  if (enabled)
57  {
58  glDepthMask(GL_TRUE);
59  glDepthRangef(0.0f, 1.0f);
60  }
61  else
62  {
63  glDepthMask(GL_FALSE);
64  }
65 }
66 
67 void blend_mode(bool enabled, GLenum src, GLenum dest, GLenum func)
68 {
69  if (enabled)
70  {
71  glEnable(GL_BLEND);
72  glBlendFunc(src, dest);
73  glBlendEquation(func);
74  }
75  else
76  {
77  glDisable(GL_BLEND);
78  }
79 }
80 
81 void use_shader(Shader shader)
82 {
83  current = shader;
84  current.use();
85 }
86 
87 void attribfv(string name, GLsizei num_components, GLsizei stride, GLsizei offset)
88 {
89  current.set_attribfv(name, num_components, stride, offset);
90 }
91 
92 void unset_attrib(string name)
93 {
94  current.unset_attrib(name);
95 }
96 
97 void uniform(string name, const mat4 &v) { current.set_uniform(name, v); }
98 void uniform(string name, const vec4 &v) { current.set_uniform(name, v); }
99 void uniform(string name, const vec3 &v) { current.set_uniform(name, v); }
100 void uniform(string name, const vec2 &v) { current.set_uniform(name, v); }
101 void uniform(string name, double v) { current.set_uniform(name, v); }
102 void uniform(string name, float v) { current.set_uniform(name, v); }
103 void uniform(string name, int v) { current.set_uniform(name, v); }
104 void uniform(string name, unsigned int v) { current.set_uniform(name, v); }
105 
106 bool read_file(const std::string &path, std::string &dest)
107 {
108  std::ifstream in(path, std::ios::in | std::ios::binary);
109 
110  std::string msg = "Failed to open file " + path;
111  ASSERT(in.is_open() && in.good(), msg.c_str());
112 
113  in.seekg(0, std::ios::end); // Set get position to end
114  dest.resize(in.tellg()); // Resize string to support enough bytes
115  in.seekg(0, std::ios::beg); // Set get position to beginning
116  in.read(&dest[0], dest.size()); // Read file to string
117  in.close();
118 
119  return true;
120 }
121 
122 GLuint gen_buffer(GLenum target, GLenum usage, GLsizei size, const void *data)
123 {
124  GLuint buffer;
125  glGenBuffers(1, &buffer);
126  glBindBuffer(target, buffer);
127  glBufferData(target, size, data, usage);
128  glBindBuffer(target, 0);
129  return buffer;
130 }
131 
132 GLuint gen_buffer(GLenum target, GLsizei size, const void *data)
133 {
134  return gen_buffer(target, GL_STATIC_DRAW, size, data);
135 }
136 
138 {
139  glDeleteBuffers(1, &buffer);
140 }
Shader current
Definition: glutil.cpp:25
const GLfloat * v
Definition: gl2ext.h:2231
GLuint GLuint end
Definition: gl2ext.h:323
void set_attribfv(string name, GLsizei num_components, GLsizei stride, GLsizei offset)
Definition: shader.cpp:182
Definition: matrix.h:51
Definition: matrix.h:28
GLenum GLuint GLintptr offset
Definition: gl2ext.h:629
void del_buffer(GLuint buffer)
Definition: glutil.cpp:137
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: gl2ext.h:133
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
Definition: gl2ext.h:179
Definition: matrix.h:75
GLsizei GLsizei GLenum void * binary
Definition: gl2ext.h:396
bool read_file(const std::string &path, std::string &dest)
Definition: glutil.cpp:106
GLenum mode
Definition: gl2ext.h:302
GLenum target
Definition: gl2ext.h:720
void set_uniform(string name, const mat4 &v)
Definition: shader.cpp:202
GLfloat GLfloat f
Definition: gl2ext.h:2707
void cull(bool enabled, GLenum front, GLenum mode)
Definition: glutil.cpp:27
GLuint gen_buffer(GLenum target, GLenum usage, GLsizei size, const void *data)
Definition: glutil.cpp:122
Definition: shader.h:27
GLenum GLuint GLintptr GLsizeiptr size
Definition: gl2ext.h:629
void uniform(string name, const mat4 &v)
Definition: glutil.cpp:97
void attribfv(string name, GLsizei num_components, GLsizei stride, GLsizei offset)
Definition: glutil.cpp:87
GLuint name
Definition: gl2ext.h:139
void use()
Definition: shader.cpp:138
Definition: matrix.h:104
GLenum GLuint buffer
Definition: gl2ext.h:628
const void GLsizei GLsizei stride
Definition: gl2ext.h:1335
void use_shader(Shader shader)
Definition: glutil.cpp:81
#define ASSERT(x, s)
Definition: common.h:45
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
void depth_write(bool enabled)
Definition: glutil.cpp:54
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
void unset_attrib(string name)
Definition: shader.cpp:197
void unset_attrib(string name)
Definition: glutil.cpp:92
void depth_test(bool enabled, GLenum func)
Definition: glutil.cpp:41
GLenum src
Definition: gl2ext.h:304
void blend_mode(bool enabled, GLenum src, GLenum dest, GLenum func)
Definition: glutil.cpp:67