Learning to write openGL ES2 but pretty much failed at the first hurdle, I am pretty certain its a shader error but the code and output is listed below!
Error
Created Vertex Shader!
Created Fragment Shader!
Segmentation fault
Code: Select all
#include <stdio.h>
#include "bcm_host.h"
#include "egl.h"
#include "gl2.h"
EGLDisplay egldisplay;
EGLConfig eglconfig;
EGLSurface eglsurface;
EGLContext eglcontext;
GLuint programObject;
GLuint LoadShader(const char *shaderSrc, GLenum type)
{
GLuint shader;
GLint compiled;
// Create the shader object
shader = glCreateShader(type);
if(shader == 0)
printf("Unable to create shader object!n");
return 0;
// Load the shader source
glShaderSource(shader, 1, &shaderSrc, NULL); // Error happens here!!!
// Compile the shader
glCompileShader(shader);
// Check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled)
{
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
glDeleteShader(shader);
printf("Unable to compile shader!n");
return 0;
}
return shader;
}
//
// Initialize the shader and program object
//
int Init()
{
const char vShaderStr[] =
"attribute vec4 vPosition; n"
"void main() n"
"{ n"
" gl_Position = vPosition; n"
"} n";
const char fShaderStr[] =
"precision mediump float; n"
"void main() n"
"{ n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); n"
"} n";
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;
//Create the vertex shader
vShader = glCreateShader(GL_VERTEX_SHADER);
if(vShader == 0) {
printf("Unable to create vertex shader object!n");
return 1;
}
// Load the shader source
glShaderSource(vShader, 1, &vShaderStr, NULL);
// Compile the shader
glCompileShader(vShader);
// Check the compile status
glGetShaderiv(vShader, GL_COMPILE_STATUS, &compiled);
if(!compiled)
{
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
glDeleteShader(shader);
printf("Unable to compile shader!n");
return 0;
}
// Load the vertex/fragment shaders
vertexShader = LoadShader(vShaderStr, GL_VERTEX_SHADER);
fragmentShader = LoadShader(fShaderStr, GL_FRAGMENT_SHADER);
// Create the program object
programObject = glCreateProgram();
if(programObject == 0) {
printf("Unable to create Program Objectn");
return 1;
}
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
// Bind vPosition to attribute 0
glBindAttribLocation(programObject, 0, "vPosition");
// Link the program
glLinkProgram(programObject);
// Check the link status
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if(!linked)
{
glDeleteProgram(programObject);
printf("Unable to link program");
return 1;
}
// Store the program object
proObj = programObject;
return 0;
}
void DrawTriangle()
{
GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f};
// Use the program object
glUseProgram(proObj);
// Load the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void render(int w, int h)
{
//clear the buffer
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//draw triangle
DrawTriangle();
//actually draw the stuff to the screen
eglSwapBuffers(egldisplay, eglsurface);
}
void init(NativeWindowType window)
{
static const EGLint s_configAttribs[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_LUMINANCE_SIZE, EGL_DONT_CARE,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_SAMPLES, 1,
EGL_NONE
};
EGLint numconfigs;
egldisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(egldisplay, NULL, NULL);
eglBindAPI(EGL_OPENGL_ES_API);
eglChooseConfig(egldisplay, s_configAttribs, &eglconfig, 1, &numconfigs);
eglsurface = eglCreateWindowSurface(egldisplay, eglconfig, window, NULL);
eglcontext = eglCreateContext(egldisplay, eglconfig, NULL, NULL);
eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext);
}
void deinit(void)
{
eglMakeCurrent(egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglTerminate(egldisplay);
eglReleaseThread();
}
int main(void)
{
uint32_t width, height;
bcm_host_init();
int s;
static EGL_DISPMANX_WINDOW_T nativewindow;
DISPMANX_ELEMENT_HANDLE_T dispman_element;
DISPMANX_DISPLAY_HANDLE_T dispman_display;
DISPMANX_UPDATE_HANDLE_T dispman_update;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
s = graphics_get_display_size(0 /* LCD */, &width, &height);
dst_rect.x = 0;
dst_rect.y = 0;
dst_rect.width = width;
dst_rect.height = height;
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = width << 16;
src_rect.height = height << 16;
dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
dispman_update = vc_dispmanx_update_start( 0 );
dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display,
1/*layer*/, &dst_rect, 0/*src*/,
&src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/);
nativewindow.element = dispman_element;
nativewindow.width = width;
nativewindow.height = height;
vc_dispmanx_update_submit_sync( dispman_update );
init(&nativewindow);
// Set the viewport
if(Init() != 0) {
printf("Unable to INIT!n");
return 1;
}
glViewport(0, 0, width, height);
//60 frames per second at 600 = 10 seconds
int count = 0;
while (count < 600) {
render(width, height);
count++;
}
deinit();
return 0;
}