Path: blob/main_old/samples/torus_lighting/torus.h
1694 views
//1// Copyright 2021 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56// Based on CubeMapActivity.java from The Android Open Source Project ApiDemos7// https://android.googlesource.com/platform/development/+/refs/heads/master/samples/ApiDemos/src/com/example/android/apis/graphics/CubeMapActivity.java89#ifndef SAMPLE_TORUS_LIGHTING_H_10#define SAMPLE_TORUS_LIGHTING_H_1112#include <cmath>1314const float kPi = 3.1415926535897f;15const GLushort kSize = 60;1617void GenerateTorus(GLuint *vertexBuffer, GLuint *indexBuffer, GLsizei *indexCount)18{19std::vector<GLushort> indices;20for (GLushort y = 0; y < kSize; y++)21{22for (GLushort x = 0; x < kSize; x++)23{24GLushort a = y * (kSize + 1) + x;25GLushort b = y * (kSize + 1) + x + 1;26GLushort c = (y + 1) * (kSize + 1) + x;27GLushort d = (y + 1) * (kSize + 1) + x + 1;2829indices.push_back(a);30indices.push_back(c);31indices.push_back(b);3233indices.push_back(b);34indices.push_back(c);35indices.push_back(d);36}37}38*indexCount = static_cast<GLsizei>(indices.size());3940std::vector<GLfloat> vertices;41for (uint32_t j = 0; j <= kSize; j++)42{43float angleV = kPi * 2 * j / kSize;44float cosV = cosf(angleV);45float sinV = sinf(angleV);46for (uint32_t i = 0; i <= kSize; i++)47{48float angleU = kPi * 2 * i / kSize;49float cosU = cosf(angleU);50float sinU = sinf(angleU);51float d = 3.0f + 0.75f * cosU;5253float x = d * cosV;54float y = d * (-sinV);55float z = 0.75f * sinU;5657vertices.push_back(x);58vertices.push_back(y);59vertices.push_back(z);6061float nx = cosV * cosU;62float ny = -sinV * cosU;63float nz = sinU;6465float length = sqrtf(nx * nx + ny * ny + nz * nz);66nx /= length;67ny /= length;68nz /= length;6970vertices.push_back(nx);71vertices.push_back(ny);72vertices.push_back(nz);73}74}7576glGenBuffers(1, vertexBuffer);77glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer);78glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(),79GL_STATIC_DRAW);8081glGenBuffers(1, indexBuffer);82glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *indexBuffer);83glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), indices.data(),84GL_STATIC_DRAW);85}8687#endif // SAMPLE_TORUS_LIGHTING_H_888990