samples: Add TorusLighting sample.

Add a vertex buffer example for GLES1 and 2.

Bug: angleproject:5751
Change-Id: If039451ff85dfffd8915497e9aaaab6e4ff71181
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2859827
Commit-Queue: Cody Northrop <cnorthrop@google.com>
Reviewed-by: Cody Northrop <cnorthrop@google.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
This commit is contained in:
Lubosz Sarnecki 2021-04-28 14:23:48 +02:00 коммит произвёл Commit Bot
Родитель bca7c40885
Коммит d1da88ee27
4 изменённых файлов: 330 добавлений и 0 удалений

Просмотреть файл

@ -179,6 +179,20 @@ angle_sample("gles1_draw_texture") {
sources = [ "gles1/DrawTexture.cpp" ]
}
angle_sample("gles1_torus_lighting") {
sources = [
"torus_lighting/TorusLightingES1.cpp",
"torus_lighting/torus.h",
]
}
angle_sample("gles2_torus_lighting") {
sources = [
"torus_lighting/TorusLightingES2.cpp",
"torus_lighting/torus.h",
]
}
if (angle_build_capture_replay_sample) {
# The capture_replay sample is set up to work with a single Context.
# To use the capture replay sample first move your capture sources into
@ -227,6 +241,8 @@ group("angle_samples") {
":gles1_hello_triangle",
":gles1_simple_lighting",
":gles1_simple_texture_2d",
":gles1_torus_lighting",
":gles2_torus_lighting",
":hello_triangle",
":mip_map_2d",
":multi_texture",

Просмотреть файл

@ -0,0 +1,97 @@
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Based on CubeMapActivity.java from The Android Open Source Project ApiDemos
// https://android.googlesource.com/platform/development/+/refs/heads/master/samples/ApiDemos/src/com/example/android/apis/graphics/CubeMapActivity.java
#include "SampleApplication.h"
#include "torus.h"
class GLES1TorusLightingSample : public SampleApplication
{
public:
GLES1TorusLightingSample(int argc, char **argv)
: SampleApplication("GLES1 Torus Lighting", argc, argv, 1, 0)
{}
bool initialize() override
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_SMOOTH);
GLfloat light_model_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GenerateTorus(&mVertexBuffer, &mIndexBuffer, &mIndexCount);
return true;
}
void destroy() override
{
glDeleteBuffers(1, &mVertexBuffer);
glDeleteBuffers(1, &mIndexBuffer);
}
void draw() override
{
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float ratio = (float)getWindow()->getWidth() / (float)getWindow()->getHeight();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-ratio, ratio, -1, 1, 1.0f, 20.0f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
GLfloat lightDir[] = {0.0f, 0.0f, 1.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lightDir);
glPopMatrix();
glTranslatef(0, 0, -5);
glRotatef(mAngle, 0, 1, 0);
glRotatef(mAngle * 0.25f, 1, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), nullptr);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 6 * sizeof(GLfloat),
reinterpret_cast<const void *>(3 * sizeof(GLfloat)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
mAngle++;
}
private:
GLuint mVertexBuffer;
GLuint mIndexBuffer;
GLsizei mIndexCount;
float mAngle = 0;
};
int main(int argc, char **argv)
{
GLES1TorusLightingSample app(argc, argv);
return app.run();
}

Просмотреть файл

@ -0,0 +1,129 @@
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Based on CubeMapActivity.java from The Android Open Source Project ApiDemos
// https://android.googlesource.com/platform/development/+/refs/heads/master/samples/ApiDemos/src/com/example/android/apis/graphics/CubeMapActivity.java
#include "SampleApplication.h"
#include "torus.h"
#include "util/Matrix.h"
#include "util/shader_utils.h"
class GLES2TorusLightingSample : public SampleApplication
{
public:
GLES2TorusLightingSample(int argc, char **argv)
: SampleApplication("GLES2 Torus Lighting", argc, argv, 2, 0)
{}
bool initialize() override
{
constexpr char kVS[] = R"(uniform mat4 mv;
uniform mat4 mvp;
attribute vec4 position;
attribute vec3 normal;
varying vec3 normal_view;
void main()
{
normal_view = vec3(mv * vec4(normal, 0.0));
gl_Position = mvp * position;
})";
constexpr char kFS[] = R"(precision mediump float;
varying vec3 normal_view;
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * dot(vec3(0.0, 0, 1.0), normalize(normal_view));
})";
mProgram = CompileProgram(kVS, kFS);
if (!mProgram)
{
return false;
}
mPositionLoc = glGetAttribLocation(mProgram, "position");
mNormalLoc = glGetAttribLocation(mProgram, "normal");
mMVPMatrixLoc = glGetUniformLocation(mProgram, "mvp");
mMVMatrixLoc = glGetUniformLocation(mProgram, "mv");
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
GenerateTorus(&mVertexBuffer, &mIndexBuffer, &mIndexCount);
return true;
}
void destroy() override
{
glDeleteProgram(mProgram);
glDeleteBuffers(1, &mVertexBuffer);
glDeleteBuffers(1, &mIndexBuffer);
}
void draw() override
{
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(mProgram);
float ratio = (float)getWindow()->getWidth() / (float)getWindow()->getHeight();
Matrix4 perspectiveMatrix = Matrix4::frustum(-ratio, ratio, -1, 1, 1.0f, 20.0f);
Matrix4 modelMatrix = Matrix4::translate(angle::Vector3(0, 0, -5)) *
Matrix4::rotate(mAngle, angle::Vector3(0.0f, 1.0f, 0.0f)) *
Matrix4::rotate(mAngle * 0.25f, angle::Vector3(1.0f, 0.0f, 0.0f));
Matrix4 mvpMatrix = perspectiveMatrix * modelMatrix;
glUniformMatrix4fv(mMVMatrixLoc, 1, GL_FALSE, modelMatrix.data);
glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
glEnableVertexAttribArray(mPositionLoc);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, false, 6 * sizeof(GLfloat), nullptr);
glVertexAttribPointer(mNormalLoc, 3, GL_FLOAT, false, 6 * sizeof(GLfloat),
reinterpret_cast<const void *>(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(mNormalLoc);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
mAngle++;
}
private:
GLuint mProgram;
GLint mPositionLoc;
GLint mNormalLoc;
GLuint mMVPMatrixLoc;
GLuint mMVMatrixLoc;
GLuint mVertexBuffer;
GLuint mIndexBuffer;
GLsizei mIndexCount;
float mAngle = 0;
};
int main(int argc, char **argv)
{
GLES2TorusLightingSample app(argc, argv);
return app.run();
}

Просмотреть файл

@ -0,0 +1,88 @@
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Based on CubeMapActivity.java from The Android Open Source Project ApiDemos
// https://android.googlesource.com/platform/development/+/refs/heads/master/samples/ApiDemos/src/com/example/android/apis/graphics/CubeMapActivity.java
#ifndef SAMPLE_TORUS_LIGHTING_H_
#define SAMPLE_TORUS_LIGHTING_H_
#include <cmath>
const float kPi = 3.1415926535897f;
const GLushort kSize = 60;
void GenerateTorus(GLuint *vertexBuffer, GLuint *indexBuffer, GLsizei *indexCount)
{
std::vector<GLushort> indices;
for (GLushort y = 0; y < kSize; y++)
{
for (GLushort x = 0; x < kSize; x++)
{
GLushort a = y * (kSize + 1) + x;
GLushort b = y * (kSize + 1) + x + 1;
GLushort c = (y + 1) * (kSize + 1) + x;
GLushort d = (y + 1) * (kSize + 1) + x + 1;
indices.push_back(a);
indices.push_back(c);
indices.push_back(b);
indices.push_back(b);
indices.push_back(c);
indices.push_back(d);
}
}
*indexCount = static_cast<GLsizei>(indices.size());
std::vector<GLfloat> vertices;
for (uint32_t j = 0; j <= kSize; j++)
{
float angleV = kPi * 2 * j / kSize;
float cosV = cosf(angleV);
float sinV = sinf(angleV);
for (uint32_t i = 0; i <= kSize; i++)
{
float angleU = kPi * 2 * i / kSize;
float cosU = cosf(angleU);
float sinU = sinf(angleU);
float d = 3.0f + 0.75f * cosU;
float x = d * cosV;
float y = d * (-sinV);
float z = 0.75f * sinU;
vertices.push_back(x);
vertices.push_back(y);
vertices.push_back(z);
float nx = cosV * cosU;
float ny = -sinV * cosU;
float nz = sinU;
float length = sqrtf(nx * nx + ny * ny + nz * nz);
nx /= length;
ny /= length;
nz /= length;
vertices.push_back(nx);
vertices.push_back(ny);
vertices.push_back(nz);
}
}
glGenBuffers(1, vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(),
GL_STATIC_DRAW);
glGenBuffers(1, indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), indices.data(),
GL_STATIC_DRAW);
}
#endif // SAMPLE_TORUS_LIGHTING_H_