diff options
author | Rob Clark <robdclark@gmail.com> | 2017-02-21 12:39:54 -0500 |
---|---|---|
committer | Rob Clark <robdclark@gmail.com> | 2017-02-21 14:34:26 -0500 |
commit | f42ba05ef0fb3a5aea77b3cdd788d21ed92f677e (patch) | |
tree | 1b90838a2b709a5bdedcdc7be8148a9c4320e581 | |
parent | 2ec69c153a5456758c924e14e70d6544e4d2a866 (diff) |
split out smooth-shaded cube
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | common.c | 79 | ||||
-rw-r--r-- | common.h | 22 | ||||
-rw-r--r-- | cube-smooth.c | 273 | ||||
-rw-r--r-- | kmscube.c | 324 |
5 files changed, 382 insertions, 317 deletions
diff --git a/Makefile.am b/Makefile.am index 602fbf4..33f0c98 100644 --- a/Makefile.am +++ b/Makefile.am @@ -42,5 +42,6 @@ kmscube_CFLAGS = \ kmscube_SOURCES = \ common.c \ common.h \ + cube-smooth.c \ kmscube.c \ esTransform.c @@ -40,10 +40,89 @@ const struct gbm * init_gbm(int drm_fd, int w, int h) return NULL; } + gbm.width = w; + gbm.height = h; + return &gbm; } +int init_egl(struct egl *egl, const struct gbm *gbm) +{ + EGLint major, minor, n; + + static const EGLint context_attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + + static const EGLint config_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_ALPHA_SIZE, 0, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_NONE + }; + +#define get_proc(name) do { \ + egl->name = (void *)eglGetProcAddress(#name); \ + } while (0) + + get_proc(eglGetPlatformDisplayEXT); + + if (egl->eglGetPlatformDisplayEXT) { + egl->display = egl->eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_KHR, + gbm->dev, NULL); + } else { + egl->display = eglGetDisplay((void *)gbm->dev); + } + + if (!eglInitialize(egl->display, &major, &minor)) { + printf("failed to initialize\n"); + return -1; + } + + printf("Using display %p with EGL version %d.%d\n", + egl->display, major, minor); + + printf("EGL Version \"%s\"\n", eglQueryString(egl->display, EGL_VERSION)); + printf("EGL Vendor \"%s\"\n", eglQueryString(egl->display, EGL_VENDOR)); + printf("EGL Extensions \"%s\"\n", eglQueryString(egl->display, EGL_EXTENSIONS)); + + if (!eglBindAPI(EGL_OPENGL_ES_API)) { + printf("failed to bind api EGL_OPENGL_ES_API\n"); + return -1; + } + + if (!eglChooseConfig(egl->display, config_attribs, &egl->config, 1, &n) || n != 1) { + printf("failed to choose config: %d\n", n); + return -1; + } + + egl->context = eglCreateContext(egl->display, egl->config, + EGL_NO_CONTEXT, context_attribs); + if (egl->context == NULL) { + printf("failed to create context\n"); + return -1; + } + + egl->surface = eglCreateWindowSurface(egl->display, egl->config, + (EGLNativeWindowType)gbm->surface, NULL); + if (egl->surface == EGL_NO_SURFACE) { + printf("failed to create egl surface\n"); + return -1; + } + + /* connect the context to the surface */ + eglMakeCurrent(egl->display, egl->surface, egl->surface, egl->context); + + printf("GL Extensions: \"%s\"\n", glGetString(GL_EXTENSIONS)); + + return 0; +} + int create_program(const char *vs_src, const char *fs_src) { GLuint vertex_shader, fragment_shader, program; @@ -24,6 +24,12 @@ #ifndef _COMMON_H #define _COMMON_H +#define GL_GLEXT_PROTOTYPES 1 +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> +#include <EGL/egl.h> +#include <EGL/eglext.h> + #include <gbm.h> #define GL_GLEXT_PROTOTYPES 1 @@ -35,12 +41,28 @@ struct gbm { struct gbm_device *dev; struct gbm_surface *surface; + int width, height; }; const struct gbm * init_gbm(int drm_fd, int w, int h); +struct egl { + EGLDisplay display; + EGLConfig config; + EGLContext context; + EGLSurface surface; + + PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; + + void (*draw)(unsigned i); +}; + +int init_egl(struct egl *egl, const struct gbm *gbm); int create_program(const char *vs_src, const char *fs_src); int link_program(unsigned program); + +const struct egl * init_cube_smooth(const struct gbm *gbm); + #endif /* _COMMON_H */ diff --git a/cube-smooth.c b/cube-smooth.c new file mode 100644 index 0000000..f29face --- /dev/null +++ b/cube-smooth.c @@ -0,0 +1,273 @@ +/* + * Copyright (c) 2017 Rob Clark <rclark@redhat.com> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sub license, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" +#include "esUtil.h" + + +struct { + struct egl egl; + + GLfloat aspect; + + GLuint program; + GLint modelviewmatrix, modelviewprojectionmatrix, normalmatrix; + GLuint vbo; + GLuint positionsoffset, colorsoffset, normalsoffset; +} gl; + +static const GLfloat vVertices[] = { + // front + -1.0f, -1.0f, +1.0f, + +1.0f, -1.0f, +1.0f, + -1.0f, +1.0f, +1.0f, + +1.0f, +1.0f, +1.0f, + // back + +1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + +1.0f, +1.0f, -1.0f, + -1.0f, +1.0f, -1.0f, + // right + +1.0f, -1.0f, +1.0f, + +1.0f, -1.0f, -1.0f, + +1.0f, +1.0f, +1.0f, + +1.0f, +1.0f, -1.0f, + // left + -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, +1.0f, + -1.0f, +1.0f, -1.0f, + -1.0f, +1.0f, +1.0f, + // top + -1.0f, +1.0f, +1.0f, + +1.0f, +1.0f, +1.0f, + -1.0f, +1.0f, -1.0f, + +1.0f, +1.0f, -1.0f, + // bottom + -1.0f, -1.0f, -1.0f, + +1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, +1.0f, + +1.0f, -1.0f, +1.0f, +}; + +static const GLfloat vColors[] = { + // front + 0.0f, 0.0f, 1.0f, // blue + 1.0f, 0.0f, 1.0f, // magenta + 0.0f, 1.0f, 1.0f, // cyan + 1.0f, 1.0f, 1.0f, // white + // back + 1.0f, 0.0f, 0.0f, // red + 0.0f, 0.0f, 0.0f, // black + 1.0f, 1.0f, 0.0f, // yellow + 0.0f, 1.0f, 0.0f, // green + // right + 1.0f, 0.0f, 1.0f, // magenta + 1.0f, 0.0f, 0.0f, // red + 1.0f, 1.0f, 1.0f, // white + 1.0f, 1.0f, 0.0f, // yellow + // left + 0.0f, 0.0f, 0.0f, // black + 0.0f, 0.0f, 1.0f, // blue + 0.0f, 1.0f, 0.0f, // green + 0.0f, 1.0f, 1.0f, // cyan + // top + 0.0f, 1.0f, 1.0f, // cyan + 1.0f, 1.0f, 1.0f, // white + 0.0f, 1.0f, 0.0f, // green + 1.0f, 1.0f, 0.0f, // yellow + // bottom + 0.0f, 0.0f, 0.0f, // black + 1.0f, 0.0f, 0.0f, // red + 0.0f, 0.0f, 1.0f, // blue + 1.0f, 0.0f, 1.0f // magenta +}; + +static const GLfloat vNormals[] = { + // front + +0.0f, +0.0f, +1.0f, // forward + +0.0f, +0.0f, +1.0f, // forward + +0.0f, +0.0f, +1.0f, // forward + +0.0f, +0.0f, +1.0f, // forward + // back + +0.0f, +0.0f, -1.0f, // backward + +0.0f, +0.0f, -1.0f, // backward + +0.0f, +0.0f, -1.0f, // backward + +0.0f, +0.0f, -1.0f, // backward + // right + +1.0f, +0.0f, +0.0f, // right + +1.0f, +0.0f, +0.0f, // right + +1.0f, +0.0f, +0.0f, // right + +1.0f, +0.0f, +0.0f, // right + // left + -1.0f, +0.0f, +0.0f, // left + -1.0f, +0.0f, +0.0f, // left + -1.0f, +0.0f, +0.0f, // left + -1.0f, +0.0f, +0.0f, // left + // top + +0.0f, +1.0f, +0.0f, // up + +0.0f, +1.0f, +0.0f, // up + +0.0f, +1.0f, +0.0f, // up + +0.0f, +1.0f, +0.0f, // up + // bottom + +0.0f, -1.0f, +0.0f, // down + +0.0f, -1.0f, +0.0f, // down + +0.0f, -1.0f, +0.0f, // down + +0.0f, -1.0f, +0.0f // down +}; + +static const char *vertex_shader_source = + "uniform mat4 modelviewMatrix; \n" + "uniform mat4 modelviewprojectionMatrix;\n" + "uniform mat3 normalMatrix; \n" + " \n" + "attribute vec4 in_position; \n" + "attribute vec3 in_normal; \n" + "attribute vec4 in_color; \n" + "\n" + "vec4 lightSource = vec4(2.0, 2.0, 20.0, 0.0);\n" + " \n" + "varying vec4 vVaryingColor; \n" + " \n" + "void main() \n" + "{ \n" + " gl_Position = modelviewprojectionMatrix * in_position;\n" + " vec3 vEyeNormal = normalMatrix * in_normal;\n" + " vec4 vPosition4 = modelviewMatrix * in_position;\n" + " vec3 vPosition3 = vPosition4.xyz / vPosition4.w;\n" + " vec3 vLightDir = normalize(lightSource.xyz - vPosition3);\n" + " float diff = max(0.0, dot(vEyeNormal, vLightDir));\n" + " vVaryingColor = vec4(diff * in_color.rgb, 1.0);\n" + "} \n"; + +static const char *fragment_shader_source = + "precision mediump float; \n" + " \n" + "varying vec4 vVaryingColor; \n" + " \n" + "void main() \n" + "{ \n" + " gl_FragColor = vVaryingColor; \n" + "} \n"; + + +static void draw_cube_smooth(unsigned i) +{ + ESMatrix modelview; + + /* clear the color buffer */ + glClearColor(0.5, 0.5, 0.5, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + esMatrixLoadIdentity(&modelview); + esTranslate(&modelview, 0.0f, 0.0f, -8.0f); + esRotate(&modelview, 45.0f + (0.25f * i), 1.0f, 0.0f, 0.0f); + esRotate(&modelview, 45.0f - (0.5f * i), 0.0f, 1.0f, 0.0f); + esRotate(&modelview, 10.0f + (0.15f * i), 0.0f, 0.0f, 1.0f); + + ESMatrix projection; + esMatrixLoadIdentity(&projection); + esFrustum(&projection, -2.8f, +2.8f, -2.8f * gl.aspect, +2.8f * gl.aspect, 6.0f, 10.0f); + + ESMatrix modelviewprojection; + esMatrixLoadIdentity(&modelviewprojection); + esMatrixMultiply(&modelviewprojection, &modelview, &projection); + + float normal[9]; + normal[0] = modelview.m[0][0]; + normal[1] = modelview.m[0][1]; + normal[2] = modelview.m[0][2]; + normal[3] = modelview.m[1][0]; + normal[4] = modelview.m[1][1]; + normal[5] = modelview.m[1][2]; + normal[6] = modelview.m[2][0]; + normal[7] = modelview.m[2][1]; + normal[8] = modelview.m[2][2]; + + glUniformMatrix4fv(gl.modelviewmatrix, 1, GL_FALSE, &modelview.m[0][0]); + glUniformMatrix4fv(gl.modelviewprojectionmatrix, 1, GL_FALSE, &modelviewprojection.m[0][0]); + glUniformMatrix3fv(gl.normalmatrix, 1, GL_FALSE, normal); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glDrawArrays(GL_TRIANGLE_STRIP, 4, 4); + glDrawArrays(GL_TRIANGLE_STRIP, 8, 4); + glDrawArrays(GL_TRIANGLE_STRIP, 12, 4); + glDrawArrays(GL_TRIANGLE_STRIP, 16, 4); + glDrawArrays(GL_TRIANGLE_STRIP, 20, 4); +} + +const struct egl * init_cube_smooth(const struct gbm *gbm) +{ + int ret; + + ret = init_egl(&gl.egl, gbm); + if (ret) + return NULL; + + gl.aspect = (GLfloat)(gbm->height) / (GLfloat)(gbm->width); + + ret = create_program(vertex_shader_source, fragment_shader_source); + if (ret < 0) + return NULL; + + gl.program = ret; + + glBindAttribLocation(gl.program, 0, "in_position"); + glBindAttribLocation(gl.program, 1, "in_normal"); + glBindAttribLocation(gl.program, 2, "in_color"); + + ret = link_program(gl.program); + if (ret) + return NULL; + + glUseProgram(gl.program); + + gl.modelviewmatrix = glGetUniformLocation(gl.program, "modelviewMatrix"); + gl.modelviewprojectionmatrix = glGetUniformLocation(gl.program, "modelviewprojectionMatrix"); + gl.normalmatrix = glGetUniformLocation(gl.program, "normalMatrix"); + + glViewport(0, 0, gbm->width, gbm->height); + glEnable(GL_CULL_FACE); + + gl.positionsoffset = 0; + gl.colorsoffset = sizeof(vVertices); + gl.normalsoffset = sizeof(vVertices) + sizeof(vColors); + glGenBuffers(1, &gl.vbo); + glBindBuffer(GL_ARRAY_BUFFER, gl.vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices) + sizeof(vColors) + sizeof(vNormals), 0, GL_STATIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, gl.positionsoffset, sizeof(vVertices), &vVertices[0]); + glBufferSubData(GL_ARRAY_BUFFER, gl.colorsoffset, sizeof(vColors), &vColors[0]); + glBufferSubData(GL_ARRAY_BUFFER, gl.normalsoffset, sizeof(vNormals), &vNormals[0]); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(intptr_t)gl.positionsoffset); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(intptr_t)gl.normalsoffset); + glEnableVertexAttribArray(1); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(intptr_t)gl.colorsoffset); + glEnableVertexAttribArray(2); + + gl.egl.draw = draw_cube_smooth; + + return &gl.egl; +} @@ -39,25 +39,11 @@ #include <assert.h> #include "common.h" -#include "esUtil.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) - -static struct { - EGLDisplay display; - EGLConfig config; - EGLContext context; - EGLSurface surface; - GLuint program; - GLint modelviewmatrix, modelviewprojectionmatrix, normalmatrix; - GLuint vbo; - GLuint positionsoffset, colorsoffset, normalsoffset; - - PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; -} gl; - +static const struct egl *egl; static const struct gbm *gbm; static struct { @@ -198,302 +184,6 @@ static int init_drm(const char *dev) return 0; } -static int init_gl(void) -{ - EGLint major, minor, n; - int ret; - - static const GLfloat vVertices[] = { - // front - -1.0f, -1.0f, +1.0f, - +1.0f, -1.0f, +1.0f, - -1.0f, +1.0f, +1.0f, - +1.0f, +1.0f, +1.0f, - // back - +1.0f, -1.0f, -1.0f, - -1.0f, -1.0f, -1.0f, - +1.0f, +1.0f, -1.0f, - -1.0f, +1.0f, -1.0f, - // right - +1.0f, -1.0f, +1.0f, - +1.0f, -1.0f, -1.0f, - +1.0f, +1.0f, +1.0f, - +1.0f, +1.0f, -1.0f, - // left - -1.0f, -1.0f, -1.0f, - -1.0f, -1.0f, +1.0f, - -1.0f, +1.0f, -1.0f, - -1.0f, +1.0f, +1.0f, - // top - -1.0f, +1.0f, +1.0f, - +1.0f, +1.0f, +1.0f, - -1.0f, +1.0f, -1.0f, - +1.0f, +1.0f, -1.0f, - // bottom - -1.0f, -1.0f, -1.0f, - +1.0f, -1.0f, -1.0f, - -1.0f, -1.0f, +1.0f, - +1.0f, -1.0f, +1.0f, - }; - - static const GLfloat vColors[] = { - // front - 0.0f, 0.0f, 1.0f, // blue - 1.0f, 0.0f, 1.0f, // magenta - 0.0f, 1.0f, 1.0f, // cyan - 1.0f, 1.0f, 1.0f, // white - // back - 1.0f, 0.0f, 0.0f, // red - 0.0f, 0.0f, 0.0f, // black - 1.0f, 1.0f, 0.0f, // yellow - 0.0f, 1.0f, 0.0f, // green - // right - 1.0f, 0.0f, 1.0f, // magenta - 1.0f, 0.0f, 0.0f, // red - 1.0f, 1.0f, 1.0f, // white - 1.0f, 1.0f, 0.0f, // yellow - // left - 0.0f, 0.0f, 0.0f, // black - 0.0f, 0.0f, 1.0f, // blue - 0.0f, 1.0f, 0.0f, // green - 0.0f, 1.0f, 1.0f, // cyan - // top - 0.0f, 1.0f, 1.0f, // cyan - 1.0f, 1.0f, 1.0f, // white - 0.0f, 1.0f, 0.0f, // green - 1.0f, 1.0f, 0.0f, // yellow - // bottom - 0.0f, 0.0f, 0.0f, // black - 1.0f, 0.0f, 0.0f, // red - 0.0f, 0.0f, 1.0f, // blue - 1.0f, 0.0f, 1.0f // magenta - }; - - static const GLfloat vNormals[] = { - // front - +0.0f, +0.0f, +1.0f, // forward - +0.0f, +0.0f, +1.0f, // forward - +0.0f, +0.0f, +1.0f, // forward - +0.0f, +0.0f, +1.0f, // forward - // back - +0.0f, +0.0f, -1.0f, // backward - +0.0f, +0.0f, -1.0f, // backward - +0.0f, +0.0f, -1.0f, // backward - +0.0f, +0.0f, -1.0f, // backward - // right - +1.0f, +0.0f, +0.0f, // right - +1.0f, +0.0f, +0.0f, // right - +1.0f, +0.0f, +0.0f, // right - +1.0f, +0.0f, +0.0f, // right - // left - -1.0f, +0.0f, +0.0f, // left - -1.0f, +0.0f, +0.0f, // left - -1.0f, +0.0f, +0.0f, // left - -1.0f, +0.0f, +0.0f, // left - // top - +0.0f, +1.0f, +0.0f, // up - +0.0f, +1.0f, +0.0f, // up - +0.0f, +1.0f, +0.0f, // up - +0.0f, +1.0f, +0.0f, // up - // bottom - +0.0f, -1.0f, +0.0f, // down - +0.0f, -1.0f, +0.0f, // down - +0.0f, -1.0f, +0.0f, // down - +0.0f, -1.0f, +0.0f // down - }; - - static const EGLint context_attribs[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - static const EGLint config_attribs[] = { - EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_RED_SIZE, 1, - EGL_GREEN_SIZE, 1, - EGL_BLUE_SIZE, 1, - EGL_ALPHA_SIZE, 0, - EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, - EGL_NONE - }; - - static const char *vertex_shader_source = - "uniform mat4 modelviewMatrix; \n" - "uniform mat4 modelviewprojectionMatrix;\n" - "uniform mat3 normalMatrix; \n" - " \n" - "attribute vec4 in_position; \n" - "attribute vec3 in_normal; \n" - "attribute vec4 in_color; \n" - "\n" - "vec4 lightSource = vec4(2.0, 2.0, 20.0, 0.0);\n" - " \n" - "varying vec4 vVaryingColor; \n" - " \n" - "void main() \n" - "{ \n" - " gl_Position = modelviewprojectionMatrix * in_position;\n" - " vec3 vEyeNormal = normalMatrix * in_normal;\n" - " vec4 vPosition4 = modelviewMatrix * in_position;\n" - " vec3 vPosition3 = vPosition4.xyz / vPosition4.w;\n" - " vec3 vLightDir = normalize(lightSource.xyz - vPosition3);\n" - " float diff = max(0.0, dot(vEyeNormal, vLightDir));\n" - " vVaryingColor = vec4(diff * in_color.rgb, 1.0);\n" - "} \n"; - - static const char *fragment_shader_source = - "precision mediump float; \n" - " \n" - "varying vec4 vVaryingColor; \n" - " \n" - "void main() \n" - "{ \n" - " gl_FragColor = vVaryingColor; \n" - "} \n"; - -#define get_proc(name) do { \ - gl.name = (void *)eglGetProcAddress(#name); \ - } while (0) - - get_proc(eglGetPlatformDisplayEXT); - - if (gl.eglGetPlatformDisplayEXT) { - gl.display = gl.eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_KHR, - gbm->dev, NULL); - } else { - gl.display = eglGetDisplay((void *)gbm->dev); - } - - if (!eglInitialize(gl.display, &major, &minor)) { - printf("failed to initialize\n"); - return -1; - } - - printf("Using display %p with EGL version %d.%d\n", - gl.display, major, minor); - - printf("EGL Version \"%s\"\n", eglQueryString(gl.display, EGL_VERSION)); - printf("EGL Vendor \"%s\"\n", eglQueryString(gl.display, EGL_VENDOR)); - printf("EGL Extensions \"%s\"\n", eglQueryString(gl.display, EGL_EXTENSIONS)); - - if (!eglBindAPI(EGL_OPENGL_ES_API)) { - printf("failed to bind api EGL_OPENGL_ES_API\n"); - return -1; - } - - if (!eglChooseConfig(gl.display, config_attribs, &gl.config, 1, &n) || n != 1) { - printf("failed to choose config: %d\n", n); - return -1; - } - - gl.context = eglCreateContext(gl.display, gl.config, - EGL_NO_CONTEXT, context_attribs); - if (gl.context == NULL) { - printf("failed to create context\n"); - return -1; - } - - gl.surface = eglCreateWindowSurface(gl.display, gl.config, - (EGLNativeWindowType)gbm->surface, NULL); - if (gl.surface == EGL_NO_SURFACE) { - printf("failed to create egl surface\n"); - return -1; - } - - /* connect the context to the surface */ - eglMakeCurrent(gl.display, gl.surface, gl.surface, gl.context); - - printf("GL Extensions: \"%s\"\n", glGetString(GL_EXTENSIONS)); - - ret = create_program(vertex_shader_source, fragment_shader_source); - if (ret < 0) - return ret; - - gl.program = ret; - - glBindAttribLocation(gl.program, 0, "in_position"); - glBindAttribLocation(gl.program, 1, "in_normal"); - glBindAttribLocation(gl.program, 2, "in_color"); - - ret = link_program(gl.program); - if (ret < 0) - return ret; - - glUseProgram(gl.program); - - gl.modelviewmatrix = glGetUniformLocation(gl.program, "modelviewMatrix"); - gl.modelviewprojectionmatrix = glGetUniformLocation(gl.program, "modelviewprojectionMatrix"); - gl.normalmatrix = glGetUniformLocation(gl.program, "normalMatrix"); - - glViewport(0, 0, drm.mode->hdisplay, drm.mode->vdisplay); - glEnable(GL_CULL_FACE); - - gl.positionsoffset = 0; - gl.colorsoffset = sizeof(vVertices); - gl.normalsoffset = sizeof(vVertices) + sizeof(vColors); - glGenBuffers(1, &gl.vbo); - glBindBuffer(GL_ARRAY_BUFFER, gl.vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices) + sizeof(vColors) + sizeof(vNormals), 0, GL_STATIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, gl.positionsoffset, sizeof(vVertices), &vVertices[0]); - glBufferSubData(GL_ARRAY_BUFFER, gl.colorsoffset, sizeof(vColors), &vColors[0]); - glBufferSubData(GL_ARRAY_BUFFER, gl.normalsoffset, sizeof(vNormals), &vNormals[0]); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(intptr_t)gl.positionsoffset); - glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(intptr_t)gl.normalsoffset); - glEnableVertexAttribArray(1); - glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(intptr_t)gl.colorsoffset); - glEnableVertexAttribArray(2); - - return 0; -} - -static void draw(uint32_t i) -{ - ESMatrix modelview; - - /* clear the color buffer */ - glClearColor(0.5, 0.5, 0.5, 1.0); - glClear(GL_COLOR_BUFFER_BIT); - - esMatrixLoadIdentity(&modelview); - esTranslate(&modelview, 0.0f, 0.0f, -8.0f); - esRotate(&modelview, 45.0f + (0.25f * i), 1.0f, 0.0f, 0.0f); - esRotate(&modelview, 45.0f - (0.5f * i), 0.0f, 1.0f, 0.0f); - esRotate(&modelview, 10.0f + (0.15f * i), 0.0f, 0.0f, 1.0f); - - GLfloat aspect = (GLfloat)(drm.mode->vdisplay) / (GLfloat)(drm.mode->hdisplay); - - ESMatrix projection; - esMatrixLoadIdentity(&projection); - esFrustum(&projection, -2.8f, +2.8f, -2.8f * aspect, +2.8f * aspect, 6.0f, 10.0f); - - ESMatrix modelviewprojection; - esMatrixLoadIdentity(&modelviewprojection); - esMatrixMultiply(&modelviewprojection, &modelview, &projection); - - float normal[9]; - normal[0] = modelview.m[0][0]; - normal[1] = modelview.m[0][1]; - normal[2] = modelview.m[0][2]; - normal[3] = modelview.m[1][0]; - normal[4] = modelview.m[1][1]; - normal[5] = modelview.m[1][2]; - normal[6] = modelview.m[2][0]; - normal[7] = modelview.m[2][1]; - normal[8] = modelview.m[2][2]; - - glUniformMatrix4fv(gl.modelviewmatrix, 1, GL_FALSE, &modelview.m[0][0]); - glUniformMatrix4fv(gl.modelviewprojectionmatrix, 1, GL_FALSE, &modelviewprojection.m[0][0]); - glUniformMatrix3fv(gl.normalmatrix, 1, GL_FALSE, normal); - - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glDrawArrays(GL_TRIANGLE_STRIP, 4, 4); - glDrawArrays(GL_TRIANGLE_STRIP, 8, 4); - glDrawArrays(GL_TRIANGLE_STRIP, 12, 4); - glDrawArrays(GL_TRIANGLE_STRIP, 16, 4); - glDrawArrays(GL_TRIANGLE_STRIP, 20, 4); -} - static void drm_fb_destroy_callback(struct gbm_bo *bo, void *data) { @@ -599,16 +289,16 @@ int main(int argc, char *argv[]) return -1; } - ret = init_gl(); - if (ret) { + egl = init_cube_smooth(gbm); + if (!egl) { printf("failed to initialize EGL\n"); - return ret; + return -1; } /* clear the color buffer */ glClearColor(0.5, 0.5, 0.5, 1.0); glClear(GL_COLOR_BUFFER_BIT); - eglSwapBuffers(gl.display, gl.surface); + eglSwapBuffers(egl->display, egl->surface); bo = gbm_surface_lock_front_buffer(gbm->surface); fb = drm_fb_get_from_bo(bo); @@ -624,9 +314,9 @@ int main(int argc, char *argv[]) struct gbm_bo *next_bo; int waiting_for_flip = 1; - draw(i++); + egl->draw(i++); - eglSwapBuffers(gl.display, gl.surface); + eglSwapBuffers(egl->display, egl->surface); next_bo = gbm_surface_lock_front_buffer(gbm->surface); fb = drm_fb_get_from_bo(next_bo); |