Unify GLES1 and GLES2 (libraries are compatible and can be co-linked)

git-svn-id: https://svn.code.sf.net/p/freeglut/code/trunk@1687 7f0cb862-5218-0410-a997-914c9d46530a
This commit is contained in:
beuc 2014-06-01 09:24:47 +00:00
parent 693b4b8de8
commit 25be478938
9 changed files with 79 additions and 129 deletions

3
.gitattributes vendored
View File

@ -15,9 +15,8 @@ freeglut/freeglut/README.mac -text
freeglut/freeglut/README.mingw_cross svn_keywords=Author+Date+Id+Revision
freeglut/freeglut/README.win32 svn_keywords=Author+Date+Id+Revision
freeglut/freeglut/TODO svn_keywords=Author+Date+Id+Revision
freeglut/freeglut/android/Android.mk -text
freeglut/freeglut/android/README -text
freeglut/freeglut/android/gles1/Android.mk -text
freeglut/freeglut/android/gles2/Android.mk -text
freeglut/freeglut/android_toolchain.cmake -text
freeglut/freeglut/config.h.in svn_keywords=Author+Date+Id+Revision
freeglut/freeglut/doc/download.html svn_keywords=Author+Date+Id+Revision

View File

@ -40,9 +40,7 @@ ELSE()
ENDIF()
# OpenGL ES support
OPTION(FREEGLUT_GLES1 "Use OpenGL ES 1.x (requires EGL)" OFF)
OPTION(FREEGLUT_GLES2 "Use OpenGL ES 2.x (requires EGL) (overrides BUILD_GLES1)" OFF)
OPTION(FREEGLUT_GLES "Use OpenGL ES (requires EGL)" OFF)
SET(FREEGLUT_HEADERS
@ -81,7 +79,7 @@ SET(FREEGLUT_SRCS
src/fg_window.c
)
# TODO: OpenGL ES requires a compatible version of these files:
IF(NOT FREEGLUT_GLES2 AND NOT FREEGLUT_GLES1)
IF(NOT FREEGLUT_GLES)
LIST(APPEND FREEGLUT_SRCS
src/fg_font.c
src/fg_menu.c
@ -172,7 +170,7 @@ ELSE()
src/x11/fg_window_x11.c
src/x11/fg_xinput_x11.c
)
IF(NOT(FREEGLUT_GLES2 OR FREEGLUT_GLES1))
IF(NOT(FREEGLUT_GLES))
LIST(APPEND FREEGLUT_SRCS
src/x11/fg_internal_x11_glx.h
src/x11/fg_display_x11_glx.c
@ -183,7 +181,7 @@ ELSE()
)
ENDIF()
ENDIF()
IF(FREEGLUT_GLES2 OR FREEGLUT_GLES1)
IF(FREEGLUT_GLES)
LIST(APPEND FREEGLUT_SRCS
src/egl/fg_internal_egl.h
src/egl/fg_display_egl.c
@ -199,18 +197,14 @@ IF(FREEGLUT_GLES2 OR FREEGLUT_GLES1)
)
ENDIF()
# For OpenGL ES (GLES):
# - compile with -DFREEGLUT_GLES1 and -DFREEGLUT_GLES2 to cleanly
# bootstrap headers inclusion in freeglut_std.h; these constants
# also need to be defined in client applications (e.g. through
# pkg-config), but use GLES constants directly for all other needs
# - define GLES version-specific library
IF(FREEGLUT_GLES2)
ADD_DEFINITIONS(-DFREEGLUT_GLES2)
LIST(APPEND LIBS GLESv2 EGL)
ELSEIF(FREEGLUT_GLES1)
ADD_DEFINITIONS(-DFREEGLUT_GLES1)
LIST(APPEND LIBS GLESv1_CM EGL)
# For OpenGL ES (GLES): compile with -DFREEGLUT_GLES to cleanly
# bootstrap headers inclusion in freeglut_std.h; this constant also
# need to be defined in client applications (e.g. through pkg-config),
# but do use GLES constants directly for all other needs
# GLES1 and GLES2 libraries are compatible and can be co-linked.
IF(FREEGLUT_GLES)
ADD_DEFINITIONS(-DFREEGLUT_GLES)
LIST(APPEND LIBS GLESv2 GLESv1_CM EGL)
ELSE()
FIND_PACKAGE(OpenGL REQUIRED)
LIST(APPEND LIBS ${OPENGL_gl_LIBRARY})
@ -369,10 +363,8 @@ ELSE()
# - the shared library should link to the dependency libraries so that the user
# won't have to link them explicitly (they shouldn't have to know that we depend
# on Xrandr or Xxf86vm)
IF(FREEGLUT_GLES2)
SET(LIBNAME freeglut-gles2)
ELSEIF(FREEGLUT_GLES1)
SET(LIBNAME freeglut-gles1)
IF(FREEGLUT_GLES)
SET(LIBNAME freeglut-gles)
ELSE()
SET(LIBNAME glut)
ENDIF()
@ -489,29 +481,17 @@ ADD_DEMO(timer progs/demos/timer/timer.c)
# Define static build dependencies
IF(WIN32)
SET(PC_LIBS_PRIVATE "-lopengl32 -lwinmm -lgdi32 -lm")
ELSEIF(FREEGLUT_GLES2)
ELSEIF(FREEGLUT_GLES)
IF(ANDROID)
SET(PC_LIBS_PRIVATE "-llog -landroid -lGLESv2 -lEGL -lm")
SET(PC_LIBS_PRIVATE "-llog -landroid -lGLESv2 -lGLESv1_CM -lEGL -lm")
ELSEIF(BLACKBERRY)
IF(PLAYBOOK)
SET(PC_LIBS_PRIVATE "-lbps -lscreen -lGLESv2 -lEGL -lm")
SET(PC_LIBS_PRIVATE "-lbps -lscreen -lGLESv2 -lGLESv1_CM -lEGL -lm")
ELSE()
SET(PC_LIBS_PRIVATE "-lbps -lslog2 -lscreen -lGLESv2 -lEGL -lm")
SET(PC_LIBS_PRIVATE "-lbps -lslog2 -lscreen -lGLESv2 -lGLESv1_CM -lEGL -lm")
ENDIF()
ELSE()
SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGLESv2 -lEGL -lm")
ENDIF()
ELSEIF(FREEGLUT_GLES1)
IF(ANDROID)
SET(PC_LIBS_PRIVATE "-llog -landroid -lGLESv1_CM -lEGL -lm")
ELSEIF(BLACKBERRY)
IF(PLAYBOOK)
SET(PC_LIBS_PRIVATE "-lbps -lscreen -lGLESv1_CM -lEGL -lm")
ELSE()
SET(PC_LIBS_PRIVATE "-lbps -lslog2 -lscreen -lGLESv1_CM -lEGL -lm")
ENDIF()
ELSE()
SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGLESv1_CM -lEGL -lm")
SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGLESv2 -lGLESv1_CM -lEGL -lm")
ENDIF()
ELSE()
SET(PC_LIBS_PRIVATE "-lX11 -lXxf86vm -lXrandr -lGL -lm")
@ -520,16 +500,12 @@ ENDIF()
# bootstrap headers inclusion in freeglut_std.h:
SET(PC_LIBNAME "glut")
SET(PC_FILENAME "freeglut.pc")
IF(FREEGLUT_GLES2)
SET(PC_CFLAGS "-DFREEGLUT_GLES2")
SET(PC_LIBNAME "freeglut-gles2")
SET(PC_FILENAME "freeglut-gles2.pc")
ELSEIF(FREEGLUT_GLES1)
SET(PC_CFLAGS "-DFREEGLUT_GLES1")
SET(PC_LIBNAME "freeglut-gles1")
SET(PC_FILENAME "freeglut-gles1.pc")
IF(FREEGLUT_GLES)
SET(PC_CFLAGS "-DFREEGLUT_GLES")
SET(PC_LIBNAME "freeglut-gles")
SET(PC_FILENAME "freeglut-gles.pc")
ENDIF()
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/freeglut.pc.in ${CMAKE_BINARY_DIR}/freeglut.pc @ONLY)
INSTALL(FILES ${CMAKE_BINARY_DIR}/freeglut.pc DESTINATION share/pkgconfig/ RENAME ${PC_FILENAME})
# TODO: change the library and .pc name when building for GLES,
# e.g. -lglut-GLES2
# e.g. -lglut-GLES

View File

@ -2,8 +2,8 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := freeglut-gles1
LOCAL_SRC_FILES := lib/libfreeglut-gles1.a
LOCAL_MODULE := freeglut-gles
LOCAL_SRC_FILES := lib/libfreeglut-gles.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

View File

@ -1,9 +0,0 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := freeglut-gles2
LOCAL_SRC_FILES := lib/libfreeglut-gles2.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

View File

@ -129,15 +129,13 @@
/*
* Always include OpenGL and GLU headers
*/
/* Note: FREEGLUT_GLES1 and FREEGLUT_GLES2 are only used to cleanly
bootstrap headers inclusion here; use GLES constants directly
/* Note: FREEGLUT_GLES is only used to cleanly bootstrap headers
inclusion here; use GLES constants directly
(e.g. GL_ES_VERSION_2_0) for all other needs */
#ifdef FREEGLUT_GLES2
# include <EGL/egl.h>
# include <GLES2/gl2.h>
#elif FREEGLUT_GLES1
#ifdef FREEGLUT_GLES
# include <EGL/egl.h>
# include <GLES/gl.h>
# include <GLES2/gl2.h>
#elif __APPLE__
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>

View File

@ -11,7 +11,7 @@ ENDIF(CMAKE_COMPILER_IS_GNUCC)
# FreeGLUT
include(FindPkgConfig)
pkg_check_modules(freeglut REQUIRED freeglut-gles1>=3.0.0)
pkg_check_modules(freeglut REQUIRED freeglut-gles>=3.0.0)
if(freeglut_FOUND)
include_directories(${freeglut_STATIC_INCLUDE_DIRS})
link_directories(${freeglut_STATIC_LIBRARY_DIRS})

View File

@ -27,39 +27,49 @@
#include "fg_internal.h"
int fghChooseConfig(EGLConfig* config) {
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
#ifdef GL_ES_VERSION_2_0
EGLint attribs[32];
int i = 0;
attribs[i++] = EGL_SURFACE_TYPE;
attribs[i++] = EGL_WINDOW_BIT;
if (fgState.MajorVersion >= 2) {
/*
* Khronos does not specify a EGL_OPENGL_ES3_BIT outside of the OpenGL extension "EGL_KHR_create_context". There are numerous references on the internet that
* say to use EGL_OPENGL_ES3_BIT, followed by many saying they can't find it in any headers. In fact, the offical updated specification for EGL does not have
* any references to OpenGL ES 3.0. Tests have shown that EGL_OPENGL_ES2_BIT will work with ES 3.0.
*/
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
#elif GL_VERSION_ES_CM_1_0 || GL_VERSION_ES_CL_1_0 || GL_VERSION_ES_CM_1_1 || GL_VERSION_ES_CL_1_1
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
#else
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
#endif
attribs[i++] = EGL_RENDERABLE_TYPE;
attribs[i++] = EGL_OPENGL_ES2_BIT;
} else {
attribs[i++] = EGL_RENDERABLE_TYPE;
attribs[i++] = EGL_OPENGL_ES_BIT;
}
/* Technically it's possible to request a standard OpenGL (non-ES)
context, but currently our build system assumes EGL => GLES */
/* attribs[i++] = EGL_RENDERABLE_TYPE; */
/* attribs[i++] = EGL_OPENGL_BIT; */
#ifdef TARGET_HOST_BLACKBERRY
/* Only 888 and 565 seem to work. Based on
http://qt.gitorious.org/qt/qtbase/source/893deb1a93021cdfabe038cdf1869de33a60cbc9:src/plugins/platforms/qnx/qqnxglcontext.cpp and
https://twitter.com/BlackBerryDev/status/380720927475912706 */
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
attribs[i++] = EGL_BLUE_SIZE; attribs[i++] = 8;
attribs[i++] = EGL_GREEN_SIZE; attribs[i++] = 8;
attribs[i++] = EGL_RED_SIZE; attribs[i++] = 8;
#else
EGL_BLUE_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_RED_SIZE, 1,
attribs[i++] = EGL_BLUE_SIZE; attribs[i++] = 1;
attribs[i++] = EGL_GREEN_SIZE; attribs[i++] = 1;
attribs[i++] = EGL_RED_SIZE; attribs[i++] = 1;
#endif
EGL_ALPHA_SIZE, (fgState.DisplayMode & GLUT_ALPHA) ? 1 : 0,
EGL_DEPTH_SIZE, (fgState.DisplayMode & GLUT_DEPTH) ? 1 : 0,
EGL_STENCIL_SIZE, (fgState.DisplayMode & GLUT_STENCIL) ? 1 : 0,
EGL_SAMPLE_BUFFERS, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? 1 : 0,
EGL_SAMPLES, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? fgState.SampleNumber : 0,
EGL_NONE
};
attribs[i++] = EGL_ALPHA_SIZE;
attribs[i++] = (fgState.DisplayMode & GLUT_ALPHA) ? 1 : 0;
attribs[i++] = EGL_DEPTH_SIZE;
attribs[i++] = (fgState.DisplayMode & GLUT_DEPTH) ? 1 : 0;
attribs[i++] = EGL_STENCIL_SIZE;
attribs[i++] = (fgState.DisplayMode & GLUT_STENCIL) ? 1 : 0;
attribs[i++] = EGL_SAMPLE_BUFFERS;
attribs[i++] = (fgState.DisplayMode & GLUT_MULTISAMPLE) ? 1 : 0;
attribs[i++] = EGL_SAMPLES;
attribs[i++] = (fgState.DisplayMode & GLUT_MULTISAMPLE) ? fgState.SampleNumber : 0;
attribs[i++] = EGL_NONE;
EGLint num_config;
if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
@ -76,39 +86,26 @@ int fghChooseConfig(EGLConfig* config) {
*/
EGLContext fghCreateNewContextEGL( SFG_Window* window ) {
EGLContext context;
EGLint ver = -1;
EGLDisplay eglDisplay = fgDisplay.pDisplay.egl.Display;
EGLConfig eglConfig = window->Window.pContext.egl.Config;
/* Ensure OpenGLES 2.0 context */
static EGLint ctx_attribs[] = {
#ifdef GL_ES_VERSION_2_0
EGL_CONTEXT_CLIENT_VERSION, 2,
#elif GL_VERSION_ES_CM_1_0 || GL_VERSION_ES_CL_1_0 || GL_VERSION_ES_CM_1_1 || GL_VERSION_ES_CL_1_1
EGL_CONTEXT_CLIENT_VERSION, 1,
#endif
EGL_NONE
};
#ifdef GL_ES_VERSION_2_0
/*
* As GLES 3.0 is backwards compatible with GLES 2.0, we set 2.0 as default unless the user states a different version.
* This updates the context attributes and lets us check that the correct version was set when we query it after creation.
*/
int gles2Ver = fgState.MajorVersion <= 2 ? 2 : fgState.MajorVersion;
ctx_attribs[1] = gles2Ver;
#endif
/* On GLES, user specifies the target version with glutInitContextVersion */
EGLint ctx_attribs[32];
int i = 0;
ctx_attribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
ctx_attribs[i++] = fgState.MajorVersion;
ctx_attribs[i++] = EGL_NONE;
context = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, ctx_attribs);
if (context == EGL_NO_CONTEXT) {
fgWarning("Cannot initialize EGL context, err=%x\n", eglGetError());
fghContextCreationError();
}
EGLint ver = -1;
eglQueryContext(fgDisplay.pDisplay.egl.Display, context, EGL_CONTEXT_CLIENT_VERSION, &ver);
#ifdef GL_ES_VERSION_2_0
if (ver != gles2Ver)
#else
if (ver != 1)
#endif
if (ver != fgState.MajorVersion)
fgError("Wrong GLES major version: %d\n", ver);
return context;

View File

@ -44,14 +44,12 @@
/* declare for drawing using the different OpenGL versions here so we can
have a nice code order below */
#ifndef GL_ES_VERSION_2_0
static void fghDrawGeometryWire11(GLfloat *vertices, GLfloat *normals,
GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2
);
static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLfloat *textcs, GLsizei numVertices,
GLushort *vertIdxs, GLsizei numParts, GLsizei numVertIdxsPerPart);
#endif
static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei numVertices,
GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2,
@ -62,9 +60,7 @@ static void fghDrawGeometrySolid20(GLfloat *vertices, GLfloat *normals, GLfloat
GLint attribute_v_coord, GLint attribute_v_normal, GLint attribute_v_texture);
/* declare function for generating visualization of normals */
static void fghGenerateNormalVisualization(GLfloat *vertices, GLfloat *normals, GLsizei numVertices);
#ifndef GL_ES_VERSION_2_0
static void fghDrawNormalVisualization11();
#endif
static void fghDrawNormalVisualization20(GLint attribute_v_coord);
/* Drawing geometry:
@ -147,12 +143,10 @@ void fghDrawGeometryWire(GLfloat *vertices, GLfloat *normals, GLsizei numVertice
vertIdxs, numParts, numVertPerPart, vertexMode,
vertIdxs2, numParts2, numVertPerPart2,
attribute_v_coord, attribute_v_normal);
#ifndef GL_ES_VERSION_2_0
else
fghDrawGeometryWire11(vertices, normals,
vertIdxs, numParts, numVertPerPart, vertexMode,
vertIdxs2, numParts2, numVertPerPart2);
#endif
}
/* Draw the geometric shape with filled triangles
@ -203,7 +197,6 @@ void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLfloat *textcs,
/* draw normals for each vertex as well */
fghDrawNormalVisualization20(attribute_v_coord);
}
#ifndef GL_ES_VERSION_2_0
else
{
fghDrawGeometrySolid11(vertices, normals, textcs, numVertices,
@ -213,13 +206,11 @@ void fghDrawGeometrySolid(GLfloat *vertices, GLfloat *normals, GLfloat *textcs,
/* draw normals for each vertex as well */
fghDrawNormalVisualization11();
}
#endif
}
/* Version for OpenGL (ES) 1.1 */
#ifndef GL_ES_VERSION_2_0
static void fghDrawGeometryWire11(GLfloat *vertices, GLfloat *normals,
GLushort *vertIdxs, GLsizei numParts, GLsizei numVertPerPart, GLenum vertexMode,
GLushort *vertIdxs2, GLsizei numParts2, GLsizei numVertPerPart2
@ -282,7 +273,6 @@ static void fghDrawGeometrySolid11(GLfloat *vertices, GLfloat *normals, GLfloat
if (textcs)
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
#endif
/* Version for OpenGL (ES) >= 2.0 */
static void fghDrawGeometryWire20(GLfloat *vertices, GLfloat *normals, GLsizei numVertices,
@ -540,7 +530,6 @@ static void fghGenerateNormalVisualization(GLfloat *vertices, GLfloat *normals,
}
/* Version for OpenGL (ES) 1.1 */
#ifndef GL_ES_VERSION_2_0
static void fghDrawNormalVisualization11()
{
GLfloat currentColor[4];
@ -559,7 +548,6 @@ static void fghDrawNormalVisualization11()
free(verticesForNormalVisualization);
glColor4f(currentColor[0],currentColor[1],currentColor[2],currentColor[3]);
}
#endif
/* Version for OpenGL (ES) >= 2.0 */
static void fghDrawNormalVisualization20(GLint attribute_v_coord)

View File

@ -43,8 +43,9 @@ void FGAPIENTRY glutSetVertexAttribTexCoord2(GLint attrib) {
}
void fgInitGL2() {
#ifndef GL_ES_VERSION_2_0
fgState.HasOpenGL20 = 0;
#ifdef GL_ES_VERSION_2_0
fgState.HasOpenGL20 = (fgState.MajorVersion >= 2);
#else
/* TODO: Mesa returns a valid stub function, rather than NULL,
when we request a non-existent function */
#define CHECK(func, a) if ((a) == NULL) { fgWarning("fgInitGL2: " func " is NULL"); return; }
@ -56,6 +57,6 @@ void fgInitGL2() {
CHECK("fghEnableVertexAttribArray", fghEnableVertexAttribArray = (FGH_PFNGLENABLEVERTEXATTRIBARRAYPROC)glutGetProcAddress("glEnableVertexAttribArray"));
CHECK("fghDisableVertexAttribArray", fghDisableVertexAttribArray = (FGH_PFNGLDISABLEVERTEXATTRIBARRAYPROC)glutGetProcAddress("glDisableVertexAttribArray"));
#undef CHECK
#endif
fgState.HasOpenGL20 = 1;
#endif
}