Path: blob/main_old/samples/gles1/FlatShading.cpp
2578 views
//1// Copyright 2018 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 Hello_Triangle.c from7// Book: OpenGL(R) ES 2.0 Programming Guide8// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner9// ISBN-10: 032150279510// ISBN-13: 978032150279711// Publisher: Addison-Wesley Professional12// URLs: http://safari.informit.com/978032156383513// http://www.opengles-book.com1415#include "SampleApplication.h"1617#include <algorithm>1819#include "util/gles_loader_autogen.h"2021class FlatShadingSample : public SampleApplication22{23public:24FlatShadingSample(int argc, char **argv)25: SampleApplication("FlatShadingSample", argc, argv, 1, 0)26{}2728bool initialize() override29{30glClearColor(0.1f, 0.1f, 0.2f, 1.0f);31mRotDeg = 0.0f;3233return true;34}3536void draw() override37{38glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);3940GLfloat vertices[] = {41-0.5f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f,420.0f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f,43};4445GLfloat colors[] = {461.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,471.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,48};4950GLuint indices[] = {510, 1, 2, 2, 3, 0,52534, 1, 0, 4, 2, 1, 4, 3, 2, 4, 0, 3,54};5556glEnable(GL_DEPTH_TEST);5758glEnableClientState(GL_VERTEX_ARRAY);59glEnableClientState(GL_COLOR_ARRAY);60glVertexPointer(3, GL_FLOAT, 0, vertices);61glColorPointer(4, GL_FLOAT, 0, colors);6263for (int i = 0; i < 6; i++)64{65for (int j = 0; j < 6; j++)66{67if ((i + j * 6) % 2 == 0)68{69glShadeModel(GL_FLAT);70}71else72{73glShadeModel(GL_SMOOTH);74}7576glPushMatrix();7778glTranslatef(-0.7f + i * 0.3f, -0.7f + j * 0.3f, 0.0f);7980glRotatef(mRotDeg + (5.0f * (6.0f * i + j)), 0.0f, 1.0f, 0.0f);81glRotatef(20.0f + (10.0f * (6.0f * i + j)), 1.0f, 0.0f, 0.0f);82GLfloat scale = 0.2f;83glScalef(scale, scale, scale);84glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(GLuint), GL_UNSIGNED_INT,85indices);8687glPopMatrix();88}89}9091mRotDeg += 0.1f;92}9394private:95float mRotDeg = 0.0f;96};9798int main(int argc, char **argv)99{100FlatShadingSample app(argc, argv);101return app.run();102}103104105