Linux教程

Linux之SDL2+OpenGL+EGL绘制(十六)

本文主要是介绍Linux之SDL2+OpenGL+EGL绘制(十六),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.概述

 SDL:窗口系统(基于X11或WayLand协议)

OpenGL(与硬件无关):通过法命令给GPU完成绘制工作.

EGL(与硬件相关):是窗口系统(SDL)和OpenGL媒介

# emacs opengles_for_linux.c
// # gcc gles_linux.c -lm -lSDL2 -lEGL -lGLESv1_CM -o gles_linux
// GLES
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
// EGL
#include <EGL/egl.h>

// SDL2
#include <SDL2/SDL.h> // For Events
#include <SDL2/SDL_syswm.h>

EGLDisplay  glDisplay;
EGLConfig   glConfig;
EGLContext  glContext;
EGLSurface  glSurface;

const char  *gl_vendor, *gl_renderer, *gl_version, *gl_extensions;
SDL_Window *glesWindow = NULL;
float mCubeRotation = 0.0f;

float vertices[] = {
	-1.0f, -1.0f, -1.0f,
	1.0f, -1.0f, -1.0f,
	1.0f,  1.0f, -1.0f,
	-1.0f,  1.0f, -1.0f,
	-1.0f, -1.0f,  1.0f,
	1.0f, -1.0f,  1.0f,
	1.0f,  1.0f,  1.0f,
	-1.0f,  1.0f,  1.0f
};

float colors[] = {
	0.0f,  1.0f,  0.0f,  1.0f,
	0.0f,  1.0f,  0.0f,  1.0f,
	1.0f,  0.5f,  0.0f,  1.0f,
	1.0f,  0.5f,  0.0f,  1.0f,
	1.0f,  0.0f,  0.0f,  1.0f,
	1.0f,  0.0f,  0.0f,  1.0f,
	0.0f,  0.0f,  1.0f,  1.0f,
	1.0f,  0.0f,  1.0f,  1.0f
};

char indices[] = {
	0, 4, 5, 0, 5, 1,
	1, 5, 6, 1, 6, 2,
	2, 6, 7, 2, 7, 3,
	3, 7, 4, 3, 4, 0,
	4, 7, 6, 4, 6, 5,
	3, 0, 1, 3, 1, 2
};

void emulateGLUperspective(GLfloat fovY, GLfloat aspect, GLfloat zNear,GLfloat zFar){
	GLfloat fW, fH;
	fH = tan(fovY / 180 * M_PI) * zNear / 2;
	fW = fH * aspect;
	glFrustumf(-fW, fW, -fH, fH, zNear, zFar);
}

void init_GLES(void){
	int screenwidth  = 800;
	int screenheight = 480;
	int screenbpp    =  16;
	int fullscreen   =   0;

	EGLint egl_config_attr[] = {
		EGL_BUFFER_SIZE,    16,
		EGL_DEPTH_SIZE,     16,
		EGL_STENCIL_SIZE,   0,
		EGL_SURFACE_TYPE,
		EGL_WINDOW_BIT,
		EGL_NONE
	};

	EGLint numConfigs, majorVersion, minorVersion;
	glesWindow = SDL_CreateWindow("LOR_GLES_DEMO", 0, 0, screenwidth, screenheight,fullscreen ? (SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN) : SDL_WINDOW_OPENGL);
	glDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
	eglInitialize(glDisplay, &majorVersion, &minorVersion);
	eglChooseConfig(glDisplay, egl_config_attr, &glConfig, 1, &numConfigs);
	SDL_SysWMinfo sysInfo;
	SDL_VERSION(&sysInfo.version); // Set SDL version
	SDL_GetWindowWMInfo(glesWindow, &sysInfo);
	glContext = eglCreateContext(glDisplay, glConfig, EGL_NO_CONTEXT, NULL);
	glSurface = eglCreateWindowSurface(glDisplay, glConfig,
																		 (EGLNativeWindowType)sysInfo.info.x11.window, 0); // X11?
	eglMakeCurrent(glDisplay, glSurface, glSurface, glContext);
	eglSwapInterval(glDisplay, 1);
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	glClearDepthf(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_RELATIVE_MOVE_TO_NV, GL_NICEST);
	glViewport(0, 0, screenwidth, screenheight);
	glMatrixMode(GL_RECT_NV);
	glLoadIdentity();
	emulateGLUperspective(45.0f, (float) screenwidth / (float) screenheight, 0.1f,
												100.0f);
	glViewport(0, 0, screenwidth, screenheight);
	glMatrixMode(GL_NEVER);
	glLoadIdentity();
}

void draw_frame(){
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -10.0f);
	glRotatef(mCubeRotation, 1.0f, 1.0f, 1.0f);
	glFrontFace(GL_CW);
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glColorPointer(4, GL_FLOAT, 0, colors);
	glEnableClientState(GL_VERTEX_ARRAY_KHR);
	glEnableClientState(GL_COLOR_EXT);
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
	glDisableClientState(GL_VERTEX_ARRAY_KHR);
	glDisableClientState(GL_COLOR_EXT);
	glLoadIdentity();
	mCubeRotation -= 0.50f;
}

int main(void){
	int loop = 1;
	SDL_Event event;
	init_GLES();

	while (loop) {
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
			case SDL_QUIT: {
				loop = 0;
				break;
			}
			}
		}

		draw_frame();
		eglSwapBuffers(glDisplay, glSurface);
	}

	// Cleaning
	eglDestroySurface(glDisplay, glSurface);
	eglDestroyContext(glDisplay, glContext);
	eglTerminate(glDisplay);
	SDL_DestroyWindow(glesWindow);
	return 0;
}

# gcc opengl_for_linux.c -lm -lSDL2 -lEGL -lGLESv1_CM -o gles_linux


这篇关于Linux之SDL2+OpenGL+EGL绘制(十六)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!