]> 4ch.mooo.com Git - plz.git/blob - plzpart-gl/opengl.c
wwww
[plz.git] / plzpart-gl / opengl.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <GLES2/gl2.h>
6 #include "u2gl.h"
7 #include "opengl.h"
8
9 static int view_width;
10 static int view_height;
11
12 static struct u2gl_program bg_program;
13
14 float obj[9];
15
16 float bg_obj[12] = {
17         0.0f, 0.0f, 0.0f,
18         320.0f, 0.0f, 0.0f,
19         0.0f, 200.0f, 0.0f,
20         320.0f, 200.0f, 0.0f
21 };
22
23 static const char vertex_shader_texture[] =
24 "uniform mat4 pMatrix;\n"
25 "uniform mat4 uMatrix;\n"
26 "attribute vec4 aPosition;\n"
27 "varying vec3 vPosition;\n"
28 "attribute vec2 aTexPosition;\n"
29 "varying vec2 vTexPosition;\n"
30 "\n"
31 "void main() {\n"
32 "    mat4 Matrix = pMatrix * uMatrix;\n"
33 "    vec4 position = Matrix * aPosition;\n"
34 "    gl_Position = position;\n"
35 "    vTexPosition = aTexPosition;\n"
36 "    vPosition = vec3(position);\n"
37 "}\n";
38
39 static char fragment_shader_texture[20000];
40
41 int uRes_location;
42 int uTime_location;
43
44 static float tex_coords[] = {
45         0.0f, 1.0f,
46         1.0f, 1.0f,
47         0.0f, 0.0f,
48         1.0f, 0.0f
49 };
50
51 void draw_bg(float t)
52 {
53         glUniform1f(uTime_location, t);
54
55         //glActiveTexture(GL_TEXTURE0);
56         //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 320, 400, GL_RGB,
57         //                              GL_UNSIGNED_BYTE, image);
58         glUseProgram(bg_program.program);
59         u2gl_draw_textured_triangle_strip(&bg_program, bg_obj, 4);
60 }
61
62 #if 0
63 static void init_texture()
64 {
65         GLuint tex[3];
66
67         u2gl_set_tex_coords(tex_coords);
68
69         glGenTextures(1, tex);
70         glActiveTexture(GL_TEXTURE0);
71         glBindTexture(GL_TEXTURE_2D, tex[0]);
72         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
73         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
74         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
75         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76
77         image = calloc(3, 320 * 400);
78
79         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 320, 400, 0, GL_RGB,
80                                 GL_UNSIGNED_BYTE, image);
81         glGenerateMipmap(GL_TEXTURE_2D);
82         u2gl_check_error("init_texture 1");
83 }
84 #endif
85
86 extern int window_width;
87 extern int window_height;
88
89 int init_opengl()
90 {
91         GLuint v, f;
92         FILE *in;
93
94         view_width = 320;
95         view_height = 200;
96
97         in = fopen("plz.glsl", "r");
98         if (in != NULL) {
99                 fread(fragment_shader_texture, 20000, 1, in);
100                 fclose(in);
101         }
102
103         v = u2gl_compile_vertex_shader(vertex_shader_texture);
104         f = u2gl_compile_fragment_shader(fragment_shader_texture);
105         u2gl_create_program(&bg_program, f, v);
106         u2gl_check_error("create program bg");
107
108         uRes_location = glGetUniformLocation(bg_program.program, "iResolution");
109         uTime_location = glGetUniformLocation(bg_program.program, "iGlobalTime");
110         u2gl_check_error("get uniform locations");
111
112         glUniform3f(uRes_location, (float)window_width, (float)window_height, 0.0f);
113         u2gl_check_error("set resolution");
114
115         glDisable(GL_DEPTH_TEST);
116         glEnable(GL_BLEND);
117
118         //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
119         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
120
121         glClearColor(.0, .0, .0, 0);
122         u2gl_check_error("init_opengl");
123
124         //init_texture();
125         //u2gl_check_error("init_texture");
126
127         u2gl_projection(0, view_width, 0, view_height, &bg_program);
128
129         return 0;
130 }
131
132 void clear_screen()
133 {
134         glClear(GL_COLOR_BUFFER_BIT);
135 }