OpenGL-ES Ubuntu 环境搭建
此的方法在 ubuntu 和 deepin 上验证都可以成功搭建
软件包安装
sudo apt install libx11-dev
sudo apt install libglfw3 libglfw3-dev
sudo apt-get install libgles2-mesa
sudo apt-get install libgles2-mesa-dev
检查环境是否安装成功:
/usr/include 下是否有 EGL GL GLES2 GLES3 的目录
Note: 上面的环境中同时安装了 x11 和 glfw,实际上只需要安装一个自己需要的即可, x11 和 glfw 都是为 OES 环境对接到窗口系统中,
个人觉得 x11 的API 对 egl 的封装比较标准话一些,可以用于学习 egl 的api
第一个三角形
基于 glfw 实现
#include <stdio.h>
#include <time.h>
#include <GLES2/gl2.h>
#include <GLFW/glfw3.h>
// Vertex Shader source code
// Vertex Shader source code
const GLchar* vertexSource =
"#version 300 es\n"
"layout(location = 0) in vec4 position;\n"
"void main() {\n"
" gl_Position = position;\n"
"}\n";
// Fragment Shader source code
const GLchar* fragmentSource =
"#version 300 es\n"
"precision mediump float;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
int main() {
printf("main testsuites enter n");
// Initialize GLFW
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
// Create a windowed mode window and its OpenGL context
GLFWwindow* window = glfwCreateWindow(640, 480, "opengles-glfw", NULL, NULL);
if (!window) {
fprintf(stderr, "Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
// Load the OpenGL ES functions
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Create and compile the vertex shader
GLint status;
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
// Check for compilation errors
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
fprintf(stderr, "Vertex Shader Compile Error: %s\n", buffer);
}
// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(fragmentShader, 512, NULL, buffer);
fprintf(stderr, "Fragment Shader Compile Error: %s\n", buffer);
}
// Link the vertex and