Created
December 14, 2012 21:53
-
-
Save anonymous/4288977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <EGL/egl.h> | |
#include <EGL/eglext.h> | |
#include <EGL/eglext_brcm.h> | |
#include <stdio.h> | |
#include <assert.h> | |
#include "shared.h" | |
static const char *vShaderSrc = | |
"attribute vec4 a_position; \n" | |
"void main() \n" | |
"{ \n" | |
" gl_Position = a_position; \n" | |
"} \n"; | |
static const char *fShaderSrc = | |
"precision mediump float; \n" | |
"const vec4 color = vec4(1.0, 0.0, 0.0, 1.0); \n" | |
"void main() \n" | |
"{ \n" | |
" gl_FragColor = color; \n" | |
"} \n"; | |
#ifndef WINDOW | |
#define WINDOW 0 | |
#endif | |
struct simple_program_t | |
{ | |
GLint id; | |
GLint position_attr_location; | |
}; | |
struct simple_program_t create_simple_program() | |
{ | |
struct simple_program_t program; | |
program.id = create_gl_program(vShaderSrc, fShaderSrc); | |
program.position_attr_location = glGetAttribLocation(program.id, "a_position"); | |
return program; | |
} | |
struct vertex_t { | |
GLfloat position[3]; | |
}; | |
void draw(const struct simple_program_t p) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Drawing vectors | |
static const struct vertex_t vertex_data[] = { | |
{{-1.0f, -1.0f, 0.0f}}, | |
{{ 0.0f, 1.0f, 0.0f}}, | |
{{ 1.0f, -1.0f, 0.0f}}, | |
}; | |
glVertexAttribPointer(p.position_attr_location, 3, GL_FLOAT, GL_FALSE, sizeof(struct vertex_t), | |
&vertex_data[0].position); | |
glEnableVertexAttribArray(p.position_attr_location); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glFinish(); | |
} | |
int main(int argc, char** argv) | |
{ | |
bcm_host_init(); | |
EGLint contextAttribs[] = { | |
EGL_CONTEXT_CLIENT_VERSION, 2, | |
EGL_NONE | |
}; | |
EGLint attribList[] = | |
{ | |
EGL_RED_SIZE, 5, | |
EGL_GREEN_SIZE, 6, | |
EGL_BLUE_SIZE, 5, | |
EGL_ALPHA_SIZE, 8, | |
EGL_DEPTH_SIZE, EGL_DONT_CARE, | |
EGL_STENCIL_SIZE, EGL_DONT_CARE, | |
EGL_SAMPLE_BUFFERS, 0, | |
EGL_NONE | |
}; | |
Context ctx = create_context(EGL_NO_DISPLAY, attribList, contextAttribs); | |
EGLint width; | |
EGLint height; | |
#if (WINDOW) | |
ctx.surface = eglCreateWindowSurface(ctx.display, ctx.config, create_native_window(&width, &height), NULL); | |
#else | |
int global_image[5]; | |
width = 480; | |
height = 360; | |
ctx.surface = create_surface_from_shared_pixmap(ctx.display, ctx.config, global_image, width, height); | |
#endif | |
assert(ctx.surface != EGL_NO_SURFACE); | |
eglMakeCurrent(ctx.display, ctx.surface, ctx.surface, ctx.context); | |
// Creating program and cleaning up opengl | |
struct simple_program_t program = create_simple_program(); | |
glUseProgram(program.id); | |
glEnable(GL_BLEND); | |
glDisable(GL_DEPTH_TEST); | |
glClearColor(0.2f, 0.2f, 0.4f, 1.0f); | |
glViewport(0, 0, width, height); | |
glCullFace(GL_FRONT_AND_BACK); | |
int done = 0; | |
while (!done) { | |
draw(program); | |
EGLint attrib[] = | |
{ | |
EGL_MAP_PRESERVE_PIXELS_KHR, EGL_TRUE, | |
EGL_LOCK_USAGE_HINT_KHR, EGL_WRITE_SURFACE_BIT_KHR, // | EGL_READ_SURFACE_BIT_KHR, | |
EGL_NONE | |
}; | |
eglWaitClient(); | |
assert(eglLockSurfaceKHR(ctx.display, ctx.surface, attrib)); | |
unsigned char *data = 0; | |
int pitch = 0; | |
int pixsize = 0; | |
int origin = 0; | |
int r_offset = 0, g_offset = 0, b_offset = 0, a_offset = 0; | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_POINTER_KHR, (EGLint*)&data); | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_PITCH_KHR, &pitch); | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_PIXEL_SIZE_KHR, &pixsize); | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_ORIGIN_KHR, &origin); | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_PIXEL_RED_OFFSET_KHR, &r_offset); | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_PIXEL_GREEN_OFFSET_KHR, &g_offset); | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_PIXEL_BLUE_OFFSET_KHR, &b_offset); | |
eglQuerySurface(ctx.display, ctx.surface, EGL_BITMAP_PIXEL_ALPHA_OFFSET_KHR, &a_offset); | |
// assert(eglUnlockSurfaceKHR(ctx.display, ctx.surface)); | |
eglSwapBuffers(ctx.display, ctx.surface); | |
} | |
destroy_context(&ctx); | |
destroy_native_window(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC=gcc | |
LIBS = -lGLESv2 -lEGL -lbcm_host -L$(SDKSTAGE)/opt/vc/lib | |
INC_DIRS = -I$(SDKSTAGE)/opt/vc/include -I$(SDKSTAGE)/opt/vc/include/interface/vcos/pthreads | |
DEFINES = -D EGL_EGLEXT_PROTOTYPES -D GL_GLEXT_PROTOTYPES -DWINDOW=1 | |
lock: lock.o shared.o | |
$(CC) $(INC_DIRS) $(LIBS) $(DEFINES) $? -o $@ | |
%.o: %.c | |
$(CC) $(INC_DIRS) $(LIBS) $(DEFINES) -c $? -o $@ | |
clean: | |
rm -rf *.o | |
rm -rf lock | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment