OLD | NEW |
---|---|
(Empty) | |
1 // 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 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <string.h> | |
8 #include <time.h> | |
9 | |
10 #include "main.h" | |
11 #include "utils.h" | |
12 | |
13 | |
14 GLuint GenerateAndBindTexture() { | |
15 GLuint name = ~0; | |
16 glGenTextures(1, &name); | |
17 glBindTexture(GL_TEXTURE_2D, name); | |
18 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
19 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
20 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); | |
21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); | |
22 return name; | |
23 } | |
24 | |
25 | |
26 char *CreateBitmap(int w, int h) { | |
27 char *bitmap = new char[w * h]; | |
28 memset(bitmap, 255, w * h); | |
29 memset(bitmap + w * (1 + h / 2), 0, w); | |
30 memset(bitmap + w * (5 + h / 2), 0, w); | |
31 return bitmap; | |
32 } | |
33 | |
34 | |
35 const char *vertex_shader = | |
36 "attribute vec4 c;" | |
37 "uniform float shift;" | |
38 "void main() {" | |
39 " gl_Position = c;" | |
40 " gl_TexCoord[0] = vec4(c.y, c.x - shift, 0.0, 0.0);" | |
41 "}"; | |
42 | |
43 const char *fragment_shader = | |
44 "uniform sampler2D tex;" | |
45 "void main() {" | |
46 " gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);" | |
47 "}"; | |
48 | |
49 | |
50 // If refresh is set to zero, we enable vsync. Otherwise we redraw that many | |
51 // times a second. | |
52 struct timespec* g_sleep_duration = NULL; | |
53 static void ParseArgs(int argc, char* argv[]) { | |
54 bool refresh_arg = false; | |
55 for (int i = 0; i < argc; i++) { | |
56 if (refresh_arg) { | |
57 refresh_arg = false; | |
58 | |
59 int refresh = atoi(argv[i]); | |
60 if (refresh > 1) { | |
61 delete g_sleep_duration; | |
62 g_sleep_duration = new struct timespec; | |
63 g_sleep_duration->tv_sec = 0; | |
64 g_sleep_duration->tv_nsec = static_cast<long>(1.e9 / refresh); | |
65 } else { | |
66 printf("-r requires integer greater than one.\n"); | |
67 } | |
68 } else if (strcmp("-o", argv[i]) == 0) { | |
69 g_override_redirect = false; | |
70 printf("blah\n"); | |
piman
2010/03/12 22:52:11
bleh.
| |
71 } else if (strcmp("-r", argv[i]) == 0) { | |
72 refresh_arg = true; | |
73 } | |
74 } | |
75 } | |
76 | |
77 | |
78 int main(int argc, char* argv[]) { | |
79 ParseArgs(argc, argv); | |
80 g_height = -1; | |
81 if (!Init()) { | |
82 printf("# Failed to initialize.\n"); | |
83 return 1; | |
84 } | |
85 | |
86 InitContext(); | |
87 glViewport(-g_width, -g_height, g_width*2, g_height*2); | |
88 | |
89 char *bitmap = CreateBitmap(g_height, g_width*2); | |
90 GLuint texture = GenerateAndBindTexture(); | |
91 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, g_height, g_width, 0, | |
92 GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap + g_height * g_width); | |
93 | |
94 GLfloat vertices[8] = { | |
95 0.f, 0.f, | |
96 1.f, 0.f, | |
97 0.f, 1.f, | |
98 1.f, 1.f, | |
99 }; | |
100 | |
101 GLuint program = InitShaderProgram(vertex_shader, fragment_shader); | |
102 int attribute_index = glGetAttribLocation(program, "c"); | |
103 glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, vertices); | |
104 glEnableVertexAttribArray(attribute_index); | |
105 | |
106 int texture_sampler = glGetUniformLocation(program, "tex"); | |
107 glUniform1f(texture_sampler, 0); | |
108 | |
109 int shift_uniform = glGetUniformLocation(program, "shift"); | |
110 int i = 0; | |
111 // TODO: do something clever to set use_vsync. | |
piman
2010/03/12 22:52:11
You can remove the TODO
| |
112 SwapInterval(g_sleep_duration ? 0 : 1); | |
113 for (;;) { | |
114 glClear(GL_COLOR_BUFFER_BIT); | |
115 glUniform1f(shift_uniform, 1.f / g_width * | |
116 (i < g_width ? i : 2 * g_width - i)); | |
117 i = (i + 4) % (2 * g_width); | |
118 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
119 glFlush(); | |
120 | |
121 if (g_sleep_duration) | |
122 nanosleep(g_sleep_duration, NULL); | |
123 | |
124 SwapBuffers(); | |
125 } | |
126 | |
127 glDeleteTextures(1, &texture); | |
128 DestroyContext(); | |
129 return 0; | |
130 } | |
OLD | NEW |