1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // This file provides implementation for the sample app HelloTriangle.
16 // The main executable source is HelloTriangle.cpp.
17 #include "HelloTriangle.h"
18
19 #include "Standalone.h"
20
21 #include <functional>
22
23 #include <glm/gtc/matrix_transform.hpp>
24 #include <glm/gtc/type_ptr.hpp>
25
26 namespace gfxstream {
27
initialize()28 void HelloTriangle::initialize() {
29 constexpr char vshaderSrc[] = R"(#version 300 es
30 precision highp float;
31
32 layout (location = 0) in vec2 pos;
33 layout (location = 1) in vec3 color;
34
35 uniform mat4 transform;
36
37 out vec3 color_varying;
38
39 void main() {
40 gl_Position = transform * vec4(pos, 0.0, 1.0);
41 color_varying = (transform * vec4(color, 1.0)).xyz;
42 }
43 )";
44 constexpr char fshaderSrc[] = R"(#version 300 es
45 precision highp float;
46
47 in vec3 color_varying;
48
49 out vec4 fragColor;
50
51 void main() {
52 fragColor = vec4(color_varying, 1.0);
53 }
54 )";
55
56 GLint program = compileAndLinkShaderProgram(vshaderSrc, fshaderSrc);
57
58 auto gl = getGlDispatch();
59
60 mTransformLoc = gl->glGetUniformLocation(program, "transform");
61
62 gl->glEnableVertexAttribArray(0);
63 gl->glEnableVertexAttribArray(1);
64
65 const VertexAttributes vertexAttrs[] = {
66 { { -0.5f, -0.5f,}, { 0.2, 0.1, 0.9, }, },
67 { { 0.5f, -0.5f,}, { 0.8, 0.3, 0.1,}, },
68 { { 0.0f, 0.5f,}, { 0.1, 0.9, 0.6,}, },
69 };
70
71 gl->glGenBuffers(1, &mBuffer);
72 gl->glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
73 gl->glBufferData(GL_ARRAY_BUFFER, sizeof(vertexAttrs), vertexAttrs,
74 GL_STATIC_DRAW);
75
76 gl->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
77 sizeof(VertexAttributes), 0);
78 gl->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
79 sizeof(VertexAttributes),
80 (GLvoid*)offsetof(VertexAttributes, color));
81
82 gl->glUseProgram(program);
83
84 gl->glClearColor(0.2f, 0.2f, 0.3f, 0.0f);
85 }
86
draw()87 void HelloTriangle::draw() {
88 glm::mat4 rot =
89 glm::rotate(glm::mat4(), mTime, glm::vec3(0.0f, 0.0f, 1.0f));
90
91 auto gl = getGlDispatch();
92
93 gl->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
94 gl->glUniformMatrix4fv(mTransformLoc, 1, GL_FALSE, glm::value_ptr(rot));
95 gl->glDrawArrays(GL_TRIANGLES, 0, 3);
96
97 mTime += 0.05f;
98 }
99
100 } // namespace gfxstream
101