I have written a code but I am getting run time error :
I have introduced all the error functions , but no error comes .
Debug it with gdb which says
Starting program: /home/sudhanshu/OpenGL_ES/example/cube/cube
[Thread debugging using libthread_db enabled]
Program exited normally.
The output I am getting :
I am getting my output what I want but screen disappears after the output comes.
My code of OpenGL-ES 2.0 :
#include <stdio.h>
#include "X11/Xlib.h"
#include "X11/Xutil.h"
#include <EGL/egl.h>
#include <malloc.h>
#include <GLES2/gl2.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define VERTEX_ARRAY 0
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 500
bool TestEGLError(const char* pszLocation)
{
EGLint iErr = eglGetError();
if (iErr != EGL_SUCCESS)
{
printf("%s failed (%d).\n", pszLocation, iErr);
return false;
}
return true;
}
#define MAXTRIANGLES 10000
#define MAXSURF 5
struct coltriangle {
float normpt1[3];
float normpt2[3];
float normpt3[3];
float pt1[3];
float pt2[3];
float pt3[3];
};
struct coltriangle triArray[MAXSURF][MAXTRIANGLES];
float maxVal=0.0;
int triNum[MAXSURF];
int surftotal = 0;
void readFile(FILE* infile)
{
int jend=1,kend=1,lend=1;
int e, j, f, k, g, l;
float coord;
while ((!(feof(infile)))&&(surftotal<MAXSURF))
{
do
{
jend=1;kend=1;lend=1;
triNum[surftotal]++;
for (e=0;e<3;e++)
{
fscanf(infile,"%f",&coord);
triArray[surftotal][triNum[surftotal]].normpt1[e]=coord;
}
for (j=0;j<3;j++)
{
fscanf(infile,"%f",&coord);
if (feof(infile)) break;
if ((jend == 1) && ((int)(coord) == -1))
jend = 1;
else jend = 0;
if (coord > maxVal) maxVal=coord;
triArray[surftotal][triNum[surftotal]].pt1[j]=coord;
}
for (f=0;f<3;f++)
{
fscanf(infile,"%f",&coord);
triArray[surftotal][triNum[surftotal]].normpt2[f]=coord;
}
for (k=0;k<3;k++)
{
fscanf(infile,"%f",&coord);
if (feof(infile)) break;
if ((jend == 1)&&(kend == 1)&&((int)(coord) == -1))
kend = 1;
else kend = 0;
if (coord > maxVal) maxVal=coord;
triArray[surftotal][triNum[surftotal]].pt2[k]=coord;
}
for (g=0;g<3;g++)
{
fscanf(infile,"%f",&coord);
triArray[surftotal][triNum[surftotal]].normpt3[g]=coord;
}
for (l=0;l<3;l++)
{
fscanf(infile,"%f",&coord);
if (feof(infile)) break;
if ((kend == 1)&&(lend == 1)&&((int)(coord) == -1))
lend = 1;
else lend = 0;
if (coord > maxVal) maxVal=coord;
triArray[surftotal][triNum[surftotal]].pt3[l]=coord;
}
} while ((!(feof(infile)))&&(triNum[surftotal]<MAXTRIANGLES)&&(lend == 0));
surftotal++;
triNum[surftotal]=0;
}
}
int main(int argc, char **argv)
{
int surfnum, i;
GLfloat *Vertices[3];
FILE *infile;
infile = fopen("temp.dat","r");
if (!infile)
{
fprintf(stderr,"\nCan't open source file %s\n",(argv[1]));
exit(1);
}
readFile(infile);
Window x11Window = 0;
Display* x11Display = 0;
long x11Screen = 0;
XVisualInfo* x11Visual = 0;
Colormap x11Colormap = 0;
EGLDisplay eglDisplay = 0;
EGLConfig eglConfig = 0;
EGLSurface eglSurface = 0;
EGLContext eglContext = 0;
GLuint uiVBO[surftotal];
EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
GLfloat right=8;
GLfloat left=-8;
GLfloat far=10;
GLfloat near=-8;
GLfloat top=8;
GLfloat bottom=-8;
GLfloat r_l = right-left;
GLfloat t_b = top-bottom;
GLfloat f_n = far-near;
GLfloat tx = -(right+left)/(right-left);
GLfloat ty = -(top+bottom)/(top-bottom);
GLfloat tz = -(far+near)/(far-near);
float projmatrix[] =
{
2/r_l, 0, 0, tx,
0, 2/t_b, 0, ty,
0, 0, (-2)/f_n, tz,
0, 0, 0, 1
};
const char* pszFragShader = "\
void main (void)\
{\
gl_FragColor = vec4(1,1,0,1);\
gl_FragColor = vec4(1.0, 0.0,0.0 ,1.0);\
gl_FragColor = vec4(0.0, 0.0,1.0 ,1.0);\
gl_FragColor = vec4(0.0, 1.0,1.0 ,1.0);\
}";
const char* pszVertShader = "\
attribute highp vec4 myVertex;\
uniform mediump mat4 projmatrix;\
invariant gl_Position;\
void main(void)\
{\
gl_Position = projmatrix * myVertex;\
}";
Window sRootWindow;
XSetWindowAttributes sWA;
unsigned int ui32Mask;
int i32Depth;
int i32Width, i32Height;
x11Display = XOpenDisplay( 0 );
if (!x11Display)
{
printf("Error: Unable to open X display\n");
goto cleanup;
}
x11Screen = XDefaultScreen( x11Display );
sRootWindow = RootWindow(x11Display, x11Screen);
i32Depth = DefaultDepth(x11Display, x11Screen);
x11Visual = new XVisualInfo;
XMatchVisualInfo( x11Display, x11Screen, i32Depth, TrueColor, x11Visual);
if (!x11Visual)
{
printf("Error: Unable to acquire visual\n");
goto cleanup;
}
x11Colormap = XCreateColormap( x11Display, sRootWindow, x11Visual->visual, AllocNone );
sWA.colormap = x11Colormap;
sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
i32Width = WINDOW_WIDTH < XDisplayWidth(x11Display, x11Screen) ? WINDOW_WIDTH : XDisplayWidth(x11Display, x11Screen);
i32Height = WINDOW_HEIGHT < XDisplayHeight(x11Display,x11Screen) ? WINDOW_HEIGHT: XDisplayHeight(x11Display,x11Screen);
x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, i32Width, i32Height,
0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);
XMapWindow(x11Display, x11Window);
XFlush(x11Display);
eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);
EGLint iMajorVersion, iMinorVersion;
if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion))
{
printf("Error: eglInitialize() failed.\n");
goto cleanup;
}
eglBindAPI(EGL_OPENGL_ES_API);
if (!TestEGLError("eglBindAPI"))
{
goto cleanup;
}
EGLint pi32ConfigAttribs[5];
pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
pi32ConfigAttribs[4] = EGL_NONE;
EGLint iConfigs;
if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))
{
printf("Error: eglChooseConfig() failed.\n");
goto cleanup;
}
eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);
if (!TestEGLError("eglCreateWindowSurface"))
{
goto cleanup;
}
eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
if (!TestEGLError("eglCreateContext"))
{
goto cleanup;
}
eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
if (!TestEGLError("eglMakeCurrent"))
{
goto cleanup;
}
GLuint uiFragShader, uiVertShader;
GLuint uiProgramObject;
uiFragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(uiFragShader, 1, (const char**)&pszFragShader, NULL);
glCompileShader(uiFragShader);
GLint bShaderCompiled;
glGetShaderiv(uiFragShader, GL_COMPILE_STATUS, &bShaderCompiled);
if (!bShaderCompiled)
{
int i32InfoLogLength, i32CharsWritten;
glGetShaderiv(uiFragShader, GL_INFO_LOG_LENGTH, &i32InfoLogLength);
char* pszInfoLog = new char[i32InfoLogLength];
glGetShaderInfoLog(uiFragShader, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
printf("Failed to compile fragment shader: %s\n", pszInfoLog);
delete [] pszInfoLog;
goto cleanup;
}
uiVertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(uiVertShader, 1, (const char**)&pszVertShader, NULL);
glCompileShader(uiVertShader);
glGetShaderiv(uiVertShader, GL_COMPILE_STATUS, &bShaderCompiled);
if (!bShaderCompiled)
{
int i32InfoLogLength, i32CharsWritten;
glGetShaderiv(uiVertShader, GL_INFO_LOG_LENGTH, &i32InfoLogLength);
char* pszInfoLog = new char[i32InfoLogLength];
glGetShaderInfoLog(uiVertShader, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
printf("Failed to compile vertex shader: %s\n", pszInfoLog);
delete [] pszInfoLog;
goto cleanup;
}
uiProgramObject = glCreateProgram();
glAttachShader(uiProgramObject, uiFragShader);
glAttachShader(uiProgramObject, uiVertShader);
glBindAttribLocation(uiProgramObject, VERTEX_ARRAY, "myVertex");
glLinkProgram(uiProgramObject);
GLint bLinked;
glGetProgramiv(uiProgramObject, GL_LINK_STATUS, &bLinked);
if (!bLinked)
{
int ui32InfoLogLength, ui32CharsWritten;
glGetProgramiv(uiProgramObject, GL_INFO_LOG_LENGTH, &ui32InfoLogLength);
char* pszInfoLog = new char[ui32InfoLogLength];
glGetProgramInfoLog(uiProgramObject, ui32InfoLogLength, &ui32CharsWritten, pszInfoLog);
printf("Failed to link program: %s\n", pszInfoLog);
delete [] pszInfoLog;
goto cleanup;
}
glUseProgram(uiProgramObject);
glGenBuffers(surftotal, uiVBO);
{
for(surfnum=0; surfnum<surftotal; ++surfnum)
{
glBindBuffer(GL_ARRAY_BUFFER, uiVBO[surfnum]);
size_t buf_size = 9*sizeof(GLfloat)*triNum[surfnum];
GLfloat* const pData = (GLfloat*)malloc(buf_size);
for(i=0; i<triNum[surfnum]; ++i){
memcpy(pData+i*9, triArray[surfnum][i].pt1, 3*sizeof(GLfloat));
memcpy(pData+i*9+3, triArray[surfnum][i].pt2, 3*sizeof(GLfloat));
memcpy(pData+i*9+6, triArray[surfnum][i].pt3, 3*sizeof(GLfloat));
}
glBufferData(GL_ARRAY_BUFFER, buf_size, pData, GL_STATIC_DRAW);
free(pData);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT);
if (!TestEGLError("glClear"))
{
goto cleanup;
}
int i32Location = glGetUniformLocation(uiProgramObject, "projmatrix");
glUniformMatrix4fv( i32Location, 1, GL_FALSE, projmatrix);
glEnableVertexAttribArray(VERTEX_ARRAY);
for(surfnum=0; surfnum<surftotal; ++surfnum)
{
glBindBuffer(GL_ARRAY_BUFFER, uiVBO[surfnum]);
glVertexAttribPointer(VERTEX_ARRAY, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3*triNum[surfnum]);
if (!TestEGLError("glDrawArrays"))
{
goto cleanup;
}
if (surfnum==0){
glClearColor(1,1,0,0);
}
else if (surfnum==1){
glClearColor(1,0,0,1);
}
else if (surfnum==2){
glClearColor(0,0,1,1);
}
else if (surfnum==3){
glClearColor(0,1,1,1);
}
else if (surfnum==4){
glClearColor(1,0,1,1);}
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
eglSwapBuffers(eglDisplay, eglSurface);
if (!TestEGLError("eglSwapBuffers"))
{
goto cleanup;
}
int i32NumMessages = XPending( x11Display );
for( int i = 0; i < i32NumMessages; i++ )
{
XEvent event;
XNextEvent( x11Display, &event );
}
}
glDeleteBuffers(surftotal,uiVBO);
glDeleteProgram(uiProgramObject);
glDeleteShader(uiFragShader);
glDeleteShader(uiVertShader);
int x;
scanf("%i...", &x );
cleanup:
eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) ;
eglTerminate(eglDisplay);
if (x11Window) XDestroyWindow(x11Display, x11Window);
if (x11Colormap) XFreeColormap( x11Display, x11Colormap );
if (x11Display) XCloseDisplay(x11Display);
return 0;
}
The text file "temp.dat " looks like this :
0.206582 0.890803 0.404716 -1.000000 -0.563636 -0.563636
0.156972 0.896985 0.413254 -1.000000 -0.775000 0.000000
0.112780 0.890965 0.439843 -0.100000 -1.000000 0.000000
0.112780 0.890965 0.439843 -0.100000 -1.000000 0.000000
0.116167 0.889299 0.442326 -0.040000 -1.000000 -0.040000
0.206582 0.890803 0.404716 -1.000000 -0.563636 -0.563636
0.116167 0.889299 0.442326 -0.040000 -1.000000 -0.040000
0.218218 0.872872 0.436436 0.000000 -0.525000 -1.000000
0.220453 0.881813 0.416901 -0.466667 -0.466667 -1.000000
0.116167 0.889299 0.442326 -0.040000 -1.000000 -0.040000
0.116593 0.888327 0.444164 0.000000 -1.000000 -0.050000
0.218218 0.872872 0.436436 0.000000 -0.525000 -1.000000
0.220453 0.881813 0.416901 -0.466667 -0.466667 -1.000000
0.221425 0.885699 0.408054 -1.000000 -0.314286 -1.000000
0.206582 0.890803 0.404716 -1.000000 -0.563636 -0.563636
0.116167 0.889299 0.442326 -0.040000 -1.000000 -0.040000
0.220453 0.881813 0.416901 -0.466667 -0.466667 -1.000000
0.206582 0.890803 0.404716 -1.000000 -0.563636 -0.563636
-1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1