• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2021 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 CubeMapActivity.java from The Android Open Source Project ApiDemos
8 // https://android.googlesource.com/platform/development/+/refs/heads/master/samples/ApiDemos/src/com/example/android/apis/graphics/CubeMapActivity.java
9 
10 #ifndef SAMPLE_TORUS_LIGHTING_H_
11 #define SAMPLE_TORUS_LIGHTING_H_
12 
13 #include <cmath>
14 
15 const float kPi      = 3.1415926535897f;
16 const GLushort kSize = 60;
17 
GenerateTorus(GLuint * vertexBuffer,GLuint * indexBuffer,GLsizei * indexCount)18 void GenerateTorus(GLuint *vertexBuffer, GLuint *indexBuffer, GLsizei *indexCount)
19 {
20     std::vector<GLushort> indices;
21     for (GLushort y = 0; y < kSize; y++)
22     {
23         for (GLushort x = 0; x < kSize; x++)
24         {
25             GLushort a = y * (kSize + 1) + x;
26             GLushort b = y * (kSize + 1) + x + 1;
27             GLushort c = (y + 1) * (kSize + 1) + x;
28             GLushort d = (y + 1) * (kSize + 1) + x + 1;
29 
30             indices.push_back(a);
31             indices.push_back(c);
32             indices.push_back(b);
33 
34             indices.push_back(b);
35             indices.push_back(c);
36             indices.push_back(d);
37         }
38     }
39     *indexCount = static_cast<GLsizei>(indices.size());
40 
41     std::vector<GLfloat> vertices;
42     for (uint32_t j = 0; j <= kSize; j++)
43     {
44         float angleV = kPi * 2 * j / kSize;
45         float cosV   = cosf(angleV);
46         float sinV   = sinf(angleV);
47         for (uint32_t i = 0; i <= kSize; i++)
48         {
49             float angleU = kPi * 2 * i / kSize;
50             float cosU   = cosf(angleU);
51             float sinU   = sinf(angleU);
52             float d      = 3.0f + 0.75f * cosU;
53 
54             float x = d * cosV;
55             float y = d * (-sinV);
56             float z = 0.75f * sinU;
57 
58             vertices.push_back(x);
59             vertices.push_back(y);
60             vertices.push_back(z);
61 
62             float nx = cosV * cosU;
63             float ny = -sinV * cosU;
64             float nz = sinU;
65 
66             float length = sqrtf(nx * nx + ny * ny + nz * nz);
67             nx /= length;
68             ny /= length;
69             nz /= length;
70 
71             vertices.push_back(nx);
72             vertices.push_back(ny);
73             vertices.push_back(nz);
74         }
75     }
76 
77     glGenBuffers(1, vertexBuffer);
78     glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer);
79     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(),
80                  GL_STATIC_DRAW);
81 
82     glGenBuffers(1, indexBuffer);
83     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *indexBuffer);
84     glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), indices.data(),
85                  GL_STATIC_DRAW);
86 }
87 
88 #endif  // SAMPLE_TORUS_LIGHTING_H_
89