samples: Improve TorusLighting samples.

Adjust the code style of the ES1 and ES2 TorusLighting samples to match
the one of TorusBufferStorage.

Use the step function override to calculate rotation and move state
changes that are required only once out of the draw function.

Bug: angleproject:5751
Change-Id: Ie663c7ecfd306504b6f9e9390ec24b251194f970
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3178904
Reviewed-by: Cody Northrop <cnorthrop@google.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
This commit is contained in:
Lubosz Sarnecki 2021-09-22 11:54:42 +02:00 коммит произвёл Angle LUCI CQ
Родитель 3369f72057
Коммит da02c6d9a5
2 изменённых файлов: 87 добавлений и 78 удалений

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

@ -10,6 +10,8 @@
#include "SampleApplication.h"
#include "torus.h"
const float kDegreesPerSecond = 90.0f;
class GLES1TorusLightingSample : public SampleApplication
{
public:
@ -20,49 +22,18 @@ class GLES1TorusLightingSample : public SampleApplication
bool initialize() override
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
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);
GenerateTorus(&mVertexBuffer, &mIndexBuffer, &mIndexCount);
glEnableClientState(GL_VERTEX_ARRAY);
@ -74,20 +45,47 @@ class GLES1TorusLightingSample : public SampleApplication
reinterpret_cast<const void *>(3 * sizeof(GLfloat)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
float ratio = static_cast<float>(getWindow()->getWidth()) /
static_cast<float>(getWindow()->getHeight());
glMatrixMode(GL_PROJECTION);
glFrustumf(-ratio, ratio, -1, 1, 1.0f, 20.0f);
return true;
}
void destroy() override
{
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &mVertexBuffer);
glDeleteBuffers(1, &mIndexBuffer);
}
mAngle++;
void step(float dt, double totalTime) override { mAngle += kDegreesPerSecond * dt; }
void draw() override
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -5);
glRotatef(mAngle, 0, 1, 0);
glRotatef(mAngle * 0.25f, 1, 0, 0);
glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
}
private:
GLuint mVertexBuffer;
GLuint mIndexBuffer;
GLsizei mIndexCount;
float mAngle = 0;
GLuint mVertexBuffer = 0;
GLuint mIndexBuffer = 0;
GLsizei mIndexCount = 0;
float mAngle = 0;
};
int main(int argc, char **argv)

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

@ -13,6 +13,8 @@
#include "util/Matrix.h"
#include "util/shader_utils.h"
const float kDegreesPerSecond = 90.0f;
class GLES2TorusLightingSample : public SampleApplication
{
public:
@ -61,35 +63,8 @@ void main() {
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);
@ -99,27 +74,63 @@ void main() {
glEnableVertexAttribArray(mNormalLoc);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
float ratio = static_cast<float>(getWindow()->getWidth()) /
static_cast<float>(getWindow()->getHeight());
mPerspectiveMatrix = Matrix4::frustum(-ratio, ratio, -1, 1, 1.0f, 20.0f);
mTranslationMatrix = Matrix4::translate(angle::Vector3(0, 0, -5));
return true;
}
void destroy() override
{
glDisableVertexAttribArray(mPositionLoc);
glDisableVertexAttribArray(mNormalLoc);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteProgram(mProgram);
glDeleteBuffers(1, &mVertexBuffer);
glDeleteBuffers(1, &mIndexBuffer);
}
mAngle++;
void step(float dt, double totalTime) override { mAngle += kDegreesPerSecond * dt; }
void draw() override
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Matrix4 modelMatrix = mTranslationMatrix * Matrix4::rotate(mAngle, mYUnitVec) *
Matrix4::rotate(mAngle * 0.25f, mXUnitVec);
Matrix4 mvpMatrix = mPerspectiveMatrix * modelMatrix;
glUniformMatrix4fv(mMVMatrixLoc, 1, GL_FALSE, modelMatrix.data);
glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
}
private:
GLuint mProgram;
GLuint mProgram = 0;
GLint mPositionLoc;
GLint mNormalLoc;
GLint mPositionLoc = 0;
GLint mNormalLoc = 0;
GLuint mMVPMatrixLoc;
GLuint mMVMatrixLoc;
GLuint mMVPMatrixLoc = 0;
GLuint mMVMatrixLoc = 0;
GLuint mVertexBuffer;
GLuint mIndexBuffer;
GLsizei mIndexCount;
GLuint mVertexBuffer = 0;
GLuint mIndexBuffer = 0;
GLsizei mIndexCount = 0;
float mAngle = 0;
Matrix4 mPerspectiveMatrix;
Matrix4 mTranslationMatrix;
const angle::Vector3 mYUnitVec{0.0f, 1.0f, 0.0f};
const angle::Vector3 mXUnitVec{1.0f, 0.0f, 0.0f};
};
int main(int argc, char **argv)