]> 4ch.mooo.com Git - plz.git/blob - _plzpart/opengl.c
wwww
[plz.git] / _plzpart / 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 const char fragment_shader_texture[] =
40 "precision mediump float;\n"
41 "uniform sampler2D uTexture;\n"
42 "uniform vec4 uColor;\n"
43 "varying vec3 vPosition;\n"
44 "varying vec2 vTexPosition;\n"
45 "\n"
46 "void main() {\n"
47 "    gl_FragColor = texture2D(uTexture, vTexPosition);\n"
48 "}\n";
49
50
51 int uTime_location;
52 int uPos_location;
53 int uTexPos_location;
54 int uRadius_location;
55 int uLensTex_location;
56 int uRotTex_location;
57
58 static float tex_coords[] = {
59         0.0f, 1.0f,
60         1.0f, 1.0f,
61         0.0f, 0.0f,
62         1.0f, 0.0f
63 };
64
65 static float color[256][4];
66
67 unsigned char *image;
68
69
70 #define CC 32
71
72 void setrgb(int c, int r, int g, int b)
73 {
74         float alpha = 0.5f;
75
76         color[c][0] = (float)r / CC;
77         color[c][1] = (float)g / CC;
78         color[c][2] = (float)b / CC;
79         color[c][3] = alpha;
80 }
81
82 void getrgb(int c, char *p)
83 {
84         p[0] = color[c][0] * CC;
85         p[1] = color[c][1] * CC;
86         p[2] = color[c][2] * CC;
87 }
88
89 void draw_bg()
90 {
91         //glActiveTexture(GL_TEXTURE0);
92         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 320, 400, GL_RGB,
93                                         GL_UNSIGNED_BYTE, image);
94         glUseProgram(bg_program.program);
95         u2gl_draw_textured_triangle_strip(&bg_program, bg_obj, 4);
96 }
97
98 static void init_texture()
99 {
100         GLuint tex[3];
101
102         u2gl_set_tex_coords(tex_coords);
103
104         glGenTextures(1, tex);
105         glActiveTexture(GL_TEXTURE0);
106         glBindTexture(GL_TEXTURE_2D, tex[0]);
107         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
108         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
109         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
110         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
111
112         image = calloc(3, 320 * 400);
113
114         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 320, 400, 0, GL_RGB,
115                                 GL_UNSIGNED_BYTE, image);
116         glGenerateMipmap(GL_TEXTURE_2D);
117         u2gl_check_error("init_texture 1");
118 }
119
120
121 int init_opengl()
122 {
123         GLuint v, f;
124
125         view_width = 320;
126         view_height = 200;
127
128         v = u2gl_compile_vertex_shader(vertex_shader_texture);
129         f = u2gl_compile_fragment_shader(fragment_shader_texture);
130         u2gl_create_program(&bg_program, f, v);
131         u2gl_check_error("create program bg");
132
133         glDisable(GL_DEPTH_TEST);
134         glEnable(GL_BLEND);
135
136         //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
137         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138
139         glClearColor(.0, .0, .0, 0);
140         u2gl_check_error("init_opengl");
141
142         init_texture();
143         u2gl_check_error("init_texture");
144
145         u2gl_projection(0, view_width, 0, view_height, &bg_program);
146
147         return 0;
148 }
149
150 void clear_screen()
151 {
152         glClear(GL_COLOR_BUFFER_BIT);
153 }