Path: blob/main_old/samples/torus_lighting/TorusLightingES1.cpp
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#include "SampleApplication.h"10#include "torus.h"1112class GLES1TorusLightingSample : public SampleApplication13{14public:15GLES1TorusLightingSample(int argc, char **argv)16: SampleApplication("GLES1 Torus Lighting", argc, argv, 1, 0)17{}1819bool initialize() override20{21glClearColor(0.0f, 0.0f, 0.0f, 0.0f);2223glShadeModel(GL_SMOOTH);2425GLfloat light_model_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};26glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);27glEnable(GL_LIGHTING);28glEnable(GL_LIGHT0);2930GenerateTorus(&mVertexBuffer, &mIndexBuffer, &mIndexCount);3132return true;33}3435void destroy() override36{37glDeleteBuffers(1, &mVertexBuffer);38glDeleteBuffers(1, &mIndexBuffer);39}4041void draw() override42{43glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());44glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);4546float ratio = (float)getWindow()->getWidth() / (float)getWindow()->getHeight();47glMatrixMode(GL_PROJECTION);48glLoadIdentity();49glFrustumf(-ratio, ratio, -1, 1, 1.0f, 20.0f);5051glEnable(GL_DEPTH_TEST);52glMatrixMode(GL_MODELVIEW);53glLoadIdentity();5455glPushMatrix();5657GLfloat lightDir[] = {0.0f, 0.0f, 1.0f, 0.0f};58glLightfv(GL_LIGHT0, GL_POSITION, lightDir);59glPopMatrix();6061glTranslatef(0, 0, -5);6263glRotatef(mAngle, 0, 1, 0);64glRotatef(mAngle * 0.25f, 1, 0, 0);6566glEnableClientState(GL_VERTEX_ARRAY);6768glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);69glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), nullptr);7071glEnableClientState(GL_NORMAL_ARRAY);72glNormalPointer(GL_FLOAT, 6 * sizeof(GLfloat),73reinterpret_cast<const void *>(3 * sizeof(GLfloat)));7475glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);76glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);77glDisableClientState(GL_VERTEX_ARRAY);78glDisableClientState(GL_NORMAL_ARRAY);79glBindBuffer(GL_ARRAY_BUFFER, 0);80glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);8182mAngle++;83}8485private:86GLuint mVertexBuffer;87GLuint mIndexBuffer;88GLsizei mIndexCount;89float mAngle = 0;90};9192int main(int argc, char **argv)93{94GLES1TorusLightingSample app(argc, argv);95return app.run();96}979899