blob: 934fb10b12b0cfc15455c2eeff215165d591be95 [file] [log] [blame]
Vincent Scheibc5114c12010-04-08 08:49:50 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Application that displays graphics using OpenGL [ES] with the intent
6// of being used in functional tests.
7
8#include <gflags/gflags.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13
14#include <cmath>
15
16#include "main.h"
17#include "utils.h"
18
19
20GLuint GenerateAndBindTexture() {
21 GLuint name = ~0;
22 glGenTextures(1, &name);
23 glBindTexture(GL_TEXTURE_2D, name);
24 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
25 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
26 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
27 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
28 return name;
29}
30
31
32unsigned char* CreateBitmap(int w, int h) {
33 unsigned char* bitmap = new unsigned char[w * h];
34 unsigned char* pixel = bitmap;
35 float w2 = w/2.0f;
36 float h2 = h/2.0f;
37 for (int y = 0; y < h; y++) {
38 for (int x = 0; x < w; x++) {
39 // Fill with soft ellipse
40 float dx = fabs((x - w2) / w2);
41 float dy = fabs((y - h2) / h2);
42 float dist2 = dx*dx + dy*dy;
43 if (dist2 > 1.f)
44 dist2 = 1.f;
45 *pixel = (1.f-dist2) * 255.f;
46 pixel++;
47 }
48 }
49 return bitmap;
50}
51
52
53const char kVertexShader[] =
54 "attribute vec4 c;"
55 "void main() {"
56 " gl_Position = c;"
57 " gl_TexCoord[0] = vec4(c.y, c.x, 0.0, 0.0);"
58 "}";
59
60const char kFragmentShader[] =
61 "uniform sampler2D tex;"
62 "void main() {"
63 " gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);"
64 "}";
65
66
67// If refresh is set to zero, we enable vsync. Otherwise we redraw that many
68// times a second.
69DEFINE_int32(seconds_to_run, 10, "seconds to run application for");
70
71int main(int argc, char* argv[]) {
72 g_width = -1;
73 g_height = -1;
74 google::ParseCommandLineFlags(&argc, &argv, true);
75
76 if (!Init()) {
77 printf("# Failed to initialize.\n");
78 return 1;
79 }
80
81 InitContext();
82 glViewport(-g_width, -g_height, g_width*2, g_height*2);
83
84 unsigned char* bitmap = CreateBitmap(g_height, g_width);
85 GLuint texture = GenerateAndBindTexture();
86 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, g_height, g_width, 0,
87 GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap);
88
89 GLfloat vertices[8] = {
90 0.f, 0.f,
91 1.f, 0.f,
92 0.f, 1.f,
93 1.f, 1.f,
94 };
95
96 GLuint program = InitShaderProgram(kVertexShader, kFragmentShader);
97 int attribute_index = glGetAttribLocation(program, "c");
98 glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, vertices);
99 glEnableVertexAttribArray(attribute_index);
100
101 int texture_sampler = glGetUniformLocation(program, "tex");
102 glUniform1f(texture_sampler, 0);
103
104 uint64_t wait_until_done = GetUTime() + 1000000ULL * FLAGS_seconds_to_run;
105 do {
106 glClear(GL_COLOR_BUFFER_BIT);
107 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
108 SwapBuffers();
109 } while (GetUTime() < wait_until_done);
110
111 glDeleteTextures(1, &texture);
112 DestroyContext();
113 return 0;
114}