Click here to Skip to main content
16,015,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ); // using it to stop the window because after compiling it goes off.
		
	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
Posted
Updated 11-Jul-11 16:52pm
v3

That's a bit too much code to analyze, but I've noticed this line:
GLuint  uiVBO[surftotal];

Does that actually compile?? surftotal is defined as a global, non-constant int. The compiler therefore should throw an error. If it doesn't, it has no way of knowing what size this array should be, but since surftotal is initialized to 0 it might create a zer0-sized array here, no matter what value surftotal has at runtime. Obviously, any later attempt to access elements in this array will lead to undefined behaviour.
 
Share this answer
 
v2
Comments
sudhanshu2511 11-Jul-11 6:18am    
actually surftotal is initialized as zero but it gets uts value after reading the input file by the function void readfile , like in this case its value goes to 2, I have figured out the blank screen error actually I was not giving it projection when I give it projection the solution I wanted comes , but the window disappears , thats still a question to me .
Stefan_Lang 11-Jul-11 6:30am    
My point was that the code for allcoating arrays on the stack gets generated at compile time, and at compile time the compiler does not know what value surftotal will contain, so the array size will be undefined, or 0!

As I said, this should be a compile time error, I wonder why your compiler accepts the code.
Stefan_Lang 11-Jul-11 7:20am    
FYI, please follow this link:
http://en.wikipedia.org/wiki/C_%28programming_language%29#Arrays

The first sentence says it all: 'a fixed, static size specified at compile time'
sudhanshu2511 11-Jul-11 22:45pm    
OK I understand you mean that surftotal is changing its value inside readfile but it is not passed to main file , so when main file try to obtain the value of surftotal it takes its global declared value that is zero. But when I try to print the value of surftotal at runtime its value is not zero its 2 . I don't know how but its getting its value .
Now I have changed the question because what I want I am getting but still disappearance of screen is a question to me , any technique to stop the screen or reason for this disappearance
Stefan_Lang 12-Jul-11 5:37am    
No, not at all!

The point is that whatever you do with surftotal at runtime does not matter at all to the line I quoted above! In fact, since you declared it a global, it will have the expected value at the time your program reaches that line.

Again: the size of the array uiVBO WILL BE DETERMINED AT COMPILE TIME! The compiler has no way of knowing what the value of surftotal will be at runtime, so about the only things it can do is either assume 0 or use the initializer you provided, which is also 0. Since an array of size 0 is illegal, the compiler should throw an error - I really do not get why your compiler does not! What compiler are you using?

If you don't believe me, add in a statement after that array declaration:
printf("size of array: %d\n", (int)(sizeof(uiVBO)/sizeof(GLuint))));
Looks like you have no render loop. You draw once and then exit the program!
 
Share this answer
 
Comments
sudhanshu2511 13-Jul-11 21:20pm    
Yeah , I actually done it by introducing render loop for 800 frames of triangles, but thanks for your reply .
enhzflep 13-Jul-11 21:59pm    
On my lowly core-i3 laptop (with gpu in same package as cpu) I would expect to get about 700 frames/second with the above code. Take that over to my desktop with GTS250 and I would get some 10,000 frames/sec or more. Hope 800 frames is a large enough loop.
Harrison H 14-Jul-11 13:55pm    
Seriously. Have an infinite loop render the damn thing and then force close it. Then you can worry about adding code that will exit the loop gracefully.
Harrison H 14-Jul-11 13:57pm    
I might add that putting some of this stuff into real classes with member functions, etc... would really help us help you. It was damn hard trying to determine if you have a render loop at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900