1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // Based on Simple_TextureCubemap.c from
8 // Book: OpenGL(R) ES 2.0 Programming Guide
9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
10 // ISBN-10: 0321502795
11 // ISBN-13: 9780321502797
12 // Publisher: Addison-Wesley Professional
13 // URLs: http://safari.informit.com/9780321563835
14 // http://www.opengles-book.com
15
16 #include "SampleApplication.h"
17
18 #include "texture_utils.h"
19 #include "util/geometry_utils.h"
20 #include "util/shader_utils.h"
21
22 class SimpleTextureCubemapSample : public SampleApplication
23 {
24 public:
SimpleTextureCubemapSample(int argc,char ** argv)25 SimpleTextureCubemapSample(int argc, char **argv)
26 : SampleApplication("SimpleTextureCubemap", argc, argv)
27 {}
28
initialize()29 bool initialize() override
30 {
31 constexpr char kVS[] = R"(attribute vec4 a_position;
32 attribute vec3 a_normal;
33 varying vec3 v_normal;
34 void main()
35 {
36 gl_Position = a_position;
37 v_normal = a_normal;
38 })";
39
40 constexpr char kFS[] = R"(precision mediump float;
41 varying vec3 v_normal;
42 uniform samplerCube s_texture;
43 void main()
44 {
45 gl_FragColor = textureCube(s_texture, v_normal);
46 })";
47
48 mProgram = CompileProgram(kVS, kFS);
49 if (!mProgram)
50 {
51 return false;
52 }
53
54 // Get the attribute locations
55 mPositionLoc = glGetAttribLocation(mProgram, "a_position");
56 mNormalLoc = glGetAttribLocation(mProgram, "a_normal");
57
58 // Get the sampler locations
59 mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");
60
61 // Load the texture
62 mTexture = CreateSimpleTextureCubemap();
63
64 // Generate the geometry data
65 CreateSphereGeometry(128, 0.75f, &mSphere);
66
67 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
68 glCullFace(GL_BACK);
69 glEnable(GL_CULL_FACE);
70
71 return true;
72 }
73
destroy()74 void destroy() override
75 {
76 glDeleteProgram(mProgram);
77 glDeleteTextures(1, &mTexture);
78 }
79
draw()80 void draw() override
81 {
82 // Set the viewport
83 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
84
85 // Clear the color buffer
86 glClear(GL_COLOR_BUFFER_BIT);
87
88 // Use the program object
89 glUseProgram(mProgram);
90
91 // Load the vertex position
92 glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mSphere.positions.data());
93 glEnableVertexAttribArray(mPositionLoc);
94
95 // Load the normal
96 glVertexAttribPointer(mNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, mSphere.normals.data());
97 glEnableVertexAttribArray(mNormalLoc);
98
99 // Bind the texture
100 glActiveTexture(GL_TEXTURE0);
101 glBindTexture(GL_TEXTURE_CUBE_MAP, mTexture);
102
103 // Set the texture sampler to texture unit to 0
104 glUniform1i(mSamplerLoc, 0);
105
106 glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mSphere.indices.size()),
107 GL_UNSIGNED_SHORT, mSphere.indices.data());
108 }
109
110 private:
111 // Handle to a program object
112 GLuint mProgram;
113
114 // Attribute locations
115 GLint mPositionLoc;
116 GLint mNormalLoc;
117
118 // Sampler location
119 GLint mSamplerLoc;
120
121 // Texture handle
122 GLuint mTexture;
123
124 // Geometry data
125 SphereGeometry mSphere;
126 };
127
main(int argc,char ** argv)128 int main(int argc, char **argv)
129 {
130 SimpleTextureCubemapSample app(argc, argv);
131 return app.run();
132 }
133