掌機 - Anbernic RG28XX - C/C++ - OpenGL ES 2.0 - Draw Texture



參考資訊:
https://docs.gl/es2/glTexSubImage2D
https://learnopengl.com/Getting-started/Textures
https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
 
#define LCD_W   480
#define LCD_H   640
 
const char *vShaderSrc =
    "attribute vec4 a_position;   \n"
    "attribute vec2 a_texCoord;   \n"
    "varying vec2 v_texCoord;     \n"
    "void main()                  \n"
    "{                            \n"
    "   gl_Position = a_position; \n"
    "   v_texCoord = a_texCoord;  \n"
    "}                            \n";
   
const char *fShaderSrc =
    "precision mediump float;                                  \n"
    "varying vec2 v_texCoord;                                  \n"
    "uniform sampler2D s_texture;                              \n"
    "void main()                                               \n"
    "{                                                         \n"
    "    vec3 tex = texture2D(s_texture, v_texCoord).rgb;      \n"
    "    gl_FragColor = vec4(tex, 1.0);                        \n"
    "}                                                         \n";
 
int main(int argc, char** argv)
{
    EGLint egl_major = 0;
    EGLint egl_minor = 0;
    EGLint num_configs = 0;
    EGLConfig configs = {0};
    EGLDisplay display = EGL_NO_DISPLAY;
    EGLSurface surface = EGL_NO_SURFACE;
    EGLContext context = EGL_NO_CONTEXT;
    EGLint config_attribs[] = {
        EGL_SURFACE_TYPE,    EGL_WINDOW_BIT,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_RED_SIZE,   8, 
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE,  8, 
        EGL_ALPHA_SIZE, 8,
        EGL_NONE
    };
    EGLint window_attributes[] = {
        EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
        EGL_NONE
    };
    EGLint const context_attributes[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE,
    };
    GLuint vShader = 0;
    GLuint fShader = 0;
    GLuint pObject = 0;
    GLint compiled = 0;
 
    int x = 0;
    int y = 0;
    uint32_t *p = NULL;
    GLuint texID = 0;
    GLubyte *buf = NULL;
 
    GLfloat vVertices[] = {
       -1.0f,  1.0f, 0.0f,      0.0f,  0.0f,
       -1.0f, -1.0f, 0.0f,      0.0f,  1.0f,
        1.0f, -1.0f, 0.0f,      1.0f,  1.0f,
        1.0f,  1.0f, 0.0f,      1.0f,  0.0f
    };
    GLushort indices[] = {0, 1, 2, 0, 2, 3};
 
    display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    eglInitialize(display, &egl_major, &egl_minor);
    eglChooseConfig(display, config_attribs, &configs, 1, &num_configs);
    surface = eglCreateWindowSurface(display, configs, 0, window_attributes);
    context = eglCreateContext(display, configs, EGL_NO_CONTEXT, context_attributes);
    eglMakeCurrent(display, surface, surface, context);
 
    vShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vShader, 1, &vShaderSrc, NULL);
    glCompileShader(vShader);
    glGetShaderiv(vShader, GL_COMPILE_STATUS, &compiled);
 
    fShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fShader, 1, &fShaderSrc, NULL);
    glCompileShader(fShader);
    glGetShaderiv(fShader, GL_COMPILE_STATUS, &compiled);
  
    pObject = glCreateProgram();
    glAttachShader(pObject, vShader);
    glAttachShader(pObject, fShader);
    glLinkProgram(pObject);
    glUseProgram(pObject);
 
    GLint posLoc = glGetAttribLocation(pObject, "a_position");
    GLint texLoc = glGetAttribLocation(pObject, "a_texCoord");
    GLint samLoc = glGetUniformLocation(pObject, "s_texture");
 
    glGenTextures(1, &texID);
    glViewport(0, 0, LCD_W, LCD_H);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
 
    uint32_t col[] = {0xffff0000, 0xff00ff00, 0xff0000ff};
 
    buf = malloc(LCD_W * LCD_H * 4);
    p = (uint32_t *)buf;
    for (y = 0; y < LCD_H; y++) {
        for (x = 0; x < LCD_W; x++) {
            *p++ = col[x / 160];
        }
    }
    glBindTexture(GL_TEXTURE_2D, texID);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, LCD_W, LCD_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
 
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texID);
 
    glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vVertices);
    glVertexAttribPointer(texLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3]);
    glEnableVertexAttribArray(posLoc);
    glEnableVertexAttribArray(texLoc);
    glUniform1i(samLoc, 0);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
 
    eglSwapBuffers(display, surface);
    sleep(3);
 
    free(buf);
    glDeleteTextures(1, &texID);
    eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglDestroyContext(display, context);
    eglDestroySurface(display, surface);
    eglTerminate(display);
    return 0;
}

編譯

$ /opt/a30/bin/arm-linux-gcc main.c -o test -lGLESv2 -lEGL -ldl

完成