OpenGL ES 绘制三角形(嵌入式HI3559AV平台实现 )

OpenGL ES 绘制三角形(嵌入式HI3559AV平台实现 )

一.实现源码

#include <iostream>
#include <string>
using namespace std;

#include <stdlib.h>
#include <unistd.h>
#include "EGL/egl.h"
#include "EGL/eglplatform.h"
#include "EGL/mali_fbdev_types.h"

#include "GLES3/gl3.h"

#define MALI_FPRINT(fmt, arg...) fprintf(stderr, fmt, ##arg)

#define ASSERT_EQUAL(value, expected) if(value!=expected) \
    fprintf(stderr, "func = %s, line = %d, value = %d\n", __FUNCTION__, __LINE__, (int)value);

#define ASSERT_NOT_EQUAL(value, expected) if(value==expected) \
    fprintf(stderr,"func = %s, line = %d, value = %d\n", __FUNCTION__, __LINE__, (int)value);

NativeDisplayType HI_COMMON_CreateNativeDisplay(void)
{
    return (NativeDisplayType)EGL_DEFAULT_DISPLAY;
}

void HI_COMMON_DestroyNativeDisplay(NativeDisplayType native_display)
{
    return;
}

NativeWindowType HI_COMMON_CreateNativeWindow(int width,int height)
{
    fbdev_window * fb_window = (fbdev_window *)malloc(sizeof(fbdev_window));

    fb_window->width = width;
    fb_window->height = height;

    return (NativeWindowType)fb_window;
}
void HI_COMMON_DestroyNativeWindow(NativeWindowType native_window)
{
    fbdev_window *window = (fbdev_window*)native_window;
    free(window);
}


char vShaderStr[] =
      "#version 300 es                          \n"
      "layout(location = 0) in vec4 vPosition;  \n"
      "void main()                              \n"
      "{                                        \n"
      "   gl_Position = vPosition;              \n"
      "}                                        \n";

char fShaderStr[] =
    "#version 300 es                              \n"
    "precision mediump float;                     \n"
    "out vec4 fragColor;                          \n"
    "void main()                                  \n"
    "{                                            \n"
    "   fragColor = vec4 ( 0.7, 0.4, 0.8, 1.0 );  \n"
    "}                                            \n";


int main(int argc, char *argv[])
{
    cout<<"c++ env start_____________________________"<<endl;
    NativeDisplayType native_display;
    NativeWindowType native_window;
    EGLDisplay egl_display;
    EGLSurface egl_surface;
    EGLConfig egl_config;
    EGLContext egl_context;
    int ret;
    int matching_configs;
    int window_width = 640, window_height = 360;

    int configAttribs[] =
    {
        EGL_SAMPLES,      0,
        EGL_RED_SIZE,     8,
        EGL_GREEN_SIZE,   8,
        EGL_BLUE_SIZE,    8,
        EGL_ALPHA_SIZE,   8,
        EGL_DEPTH_SIZE,   0,
        EGL_STENCIL_SIZE, 0,
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_NONE,
    };

    int ctxAttribs[] =
    { 
        EGL_CONTEXT_CLIENT_VERSION, 2, //old=2
        EGL_NONE
    };

    native_display = HI_COMMON_CreateNativeDisplay();
    native_window = HI_COMMON_CreateNativeWindow(window_width, window_height);

    /* >>>>>>>>>>>>>>>>>>>>>>>>> EGL process >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
    egl_display = eglGetDisplay(native_display);

    ret = eglBindAPI(EGL_OPENGL_ES_API);
    ASSERT_EQUAL(ret, EGL_TRUE);

    ret = eglInitialize(egl_display, NULL, NULL);
    ASSERT_EQUAL(ret, EGL_TRUE);

    ret = eglChooseConfig(egl_display, configAttribs, &egl_config, 1, &matching_configs);
    ASSERT_EQUAL(ret, EGL_TRUE);

    egl_surface = eglCreateWindowSurface(egl_display, egl_config, native_window, NULL);
    ASSERT_NOT_EQUAL((unsigned long)egl_surface, (unsigned long)EGL_NO_SURFACE);

    egl_context = eglCreateContext(egl_display, egl_config, NULL, ctxAttribs);
    ASSERT_EQUAL(ret, EGL_TRUE);

    ret = eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
    ASSERT_EQUAL(ret, EGL_TRUE);

    unsigned int vshader_id, fshader_id;
    unsigned int program_id;
    int compiled1, compiled2;

    const char *ptr1 = vShaderStr;
    const char *ptr2 = fShaderStr;
    vshader_id = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vshader_id, 1, &ptr1, nullptr);
    glCompileShader(vshader_id);
    glGetShaderiv(vshader_id, GL_COMPILE_STATUS, &compiled1);
    if(!compiled1)
        printf("vshader compiled failed\n");
    else
        printf("vshader compiled1=%d\n",compiled1);

    fshader_id = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fshader_id, 1, &ptr2, nullptr);
    glCompileShader(fshader_id);
    glGetShaderiv(fshader_id, GL_COMPILE_STATUS, &compiled2);
    if(!compiled2)
        printf("fshader compiled failed\n");
    else
        printf("fshader compiled1=%d\n",compiled2);

    printf("GL version : %s\n", glGetString(GL_VERSION));
    printf("GL vendor : %s\n", glGetString(GL_VENDOR));
    printf("GL renderer : %s\n", glGetString(GL_RENDERER));
    fflush(stdout);

    program_id = glCreateProgram();
    glAttachShader(program_id, vshader_id);
    glAttachShader(program_id, fshader_id);
    glLinkProgram(program_id);

    glGetProgramiv(program_id, GL_LINK_STATUS, &ret);
    if(!ret)
        printf("gl programiv link shader failed\n");
    else
        printf("gl programiv link=%d\n",ret);
    glUseProgram(program_id);


    GLfloat vVertices[] = {  0.0f,  0.5f, 0.0f,
                            -0.5f, -0.5f, 0.0f,
                            0.5f, -0.5f, 0.0f
                         };

    // Set the viewport
    glViewport ( 0, 0, 400, 400);

    // Clear the color buffer
    glClear (GL_COLOR_BUFFER_BIT);

    // Load the vertex data
   
    glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
    glEnableVertexAttribArray ( 0 );
    glDrawArrays (GL_TRIANGLES, 0, 3 );
    glFinish();

    glDeleteProgram(program_id);
    glDeleteShader(vshader_id);
    glDeleteShader(fshader_id);

    ret = eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    ASSERT_EQUAL(ret,EGL_TRUE);

    ret = eglDestroyContext(egl_display, egl_context);
    ASSERT_EQUAL(ret,EGL_TRUE);

    ret = eglDestroySurface(egl_display, egl_surface);
    ASSERT_EQUAL(ret,EGL_TRUE);

    ret = eglTerminate(egl_display);
    ASSERT_EQUAL(ret,EGL_TRUE);

    HI_COMMON_DestroyNativeWindow(native_window);
    HI_COMMON_DestroyNativeDisplay(native_display);    

    cout<<"c++ env end_______________________________"<<endl;
    return 0;
}

Makefile: make生成可执行程序,需要连接海思59A gpu库头文件

CC:=aarch64-himix100-linux-gcc
CXX:= aarch64-himix100-linux-g++
TARGET:= opengl_es_test
CPPFLAGS +=-I/home/sdk/59a/Hi3559AV100_SDK_V2.0.3.1/mpp/component/gpu/release/include
CPPFLAGS +=-L/home/sdk/59a/Hi3559AV100_SDK_V2.0.3.1/mpp/component/gpu/release/lib -lmali
main: 
	$(CXX) $(CPPFLAGS) main.cpp -o $(TARGET) && cp $(TARGET) /home/sdk/share

二.实现效果

1.在主板上加载海思gpu ko文件, 然后主板运行./sample_hifb 0 0 0 程序(海思fb机制, hdmi口连接显示器刷新显示)
2.运行绘制三角形程序: ./opengl_es_test
3.图片效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值