Do not use on any
shared computer
July 24, 2008 07:47pm pdt
null
[x]
Attachment Details

VS & OpenGL program: Access violation reading location 0x00000000.

Tags: OpenGL, C, C++, GLSL, Unhandled exception at 0x00000000 in basic.exe: 0xC0000005: Access violation reading location 0x00000000.
Hey guys,

I'm trying to run an OpenGL 2.0 program that is supposed to apply GLSL shaders on the scene. The code runs fine on a Mac, but running it in Visual Studio 2005 yields a pop-up dialog containing:

Unhandled exception at 0x00000000 in basic.exe: 0xC0000005: Access violation reading location 0x00000000.

This occurs when attempting to load a shader, done as such:
glUseProgram( setUpAShader("C:\Users\Korax\Desktop\Expert_Problem\plastic.vert",
                                                  "C:\Users\Korax\Desktop\Expert_Problem\plastic.frag") );

The .vert and .frag files are just plain text files, for practical purposes.

For clarity, I have uploaded a snapshot of Visual Studio. It should be on this thread and also at:
http://74.220.207.186/~rodrigou/img/VS_snapshot.jpg

Please help!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
//
// ShaderSetup.cpp
//
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
//#include <OPENGL/gl.h>
#include <GL/glew.h>
#include <GL/glut.h>
 
char *textFileRead(char *fn) {
 
 
	FILE *fp;
	char *content = NULL;
 
	int count=0;
 
	if (fn != NULL) {
		fp = fopen(fn,"rt");
 
		if (fp != NULL) {
      
      fseek(fp, 0, SEEK_END);
      count = ftell(fp);
      rewind(fp);
 
			if (count > 0) {
				content = (char *)malloc(sizeof(char) * (count+1));
				count = fread(content,sizeof(char),count,fp);
				content[count] = '\0';
			}
			fclose(fp);
		}
	}
	else {
		fprintf (stderr, "Error reading %s\n", fn);
	}
	return content;
}
 
int textFileWrite(char *fn, char *s) {
 
	FILE *fp;
	int status = 0;
 
	if (fn != NULL) {
		fp = fopen(fn,"w");
 
		if (fp != NULL) {
			
			if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
				status = 1;
			fclose(fp);
		}
	}
	return(status);
}
 
 
 
 
void printShaderInfoLog(GLuint obj)
{
    GLint infologLength = 0;
    GLsizei charsWritten  = 0;
    char *infoLog;
 
	glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
 
    if (infologLength > 0)
    {
        infoLog = (char *)malloc(infologLength);
        glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
		printf("%s\n",infoLog);
        free(infoLog);
    }
}
 
void printProgramInfoLog(GLuint obj)
{
    GLint infologLength = 0;
    GLsizei charsWritten  = 0;
    char *infoLog;
 
	glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
 
    if (infologLength > 0)
    {
        infoLog = (char *)malloc(infologLength);
        glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
		printf("%s\n",infoLog);
        free(infoLog);
    }
}
 
 
/**
 * Auxillary function to set up a GLSL shader program.  requires the name of a vertex program and a fragment
 * program.  Returns a handle to the created GLSL program
 *
 * vert - Name of source file for vertex program
 * frag - Name of source file for fragment program
 */
 
GLuint setUpAShader (char *vert, char *frag)
{
	// Read in shader source
	char *vs = "";
	char *fs = "";
	
	// Create the shader 
	GLuint the_vert = glCreateShader(GL_VERTEX_SHADER);
	GLuint the_frag = glCreateShader(GL_FRAGMENT_SHADER);
	
	// Read in shader source
	vs = textFileRead(vert);
	if (!vs) {
		fprintf (stderr, "Error reading vertex shader file %s\n", vert);
		return 0;
	}
	fs = textFileRead(frag);
	if (!fs) {
		fprintf (stderr, "Error reading fragment shader file %s\n", frag);
		return 0;
	}
 
	const char * vv = vs;
	const char * ff = fs;
 
	glShaderSource(the_vert, 1, &vv,NULL);
	glShaderSource(the_frag, 1, &ff,NULL);
 
	free(vs);free(fs);
	
	// Compile the shader
	glCompileShader(the_vert);
	glCompileShader(the_frag);
	printShaderInfoLog(the_vert);
	printShaderInfoLog(the_frag);
	
	// Create the program -- attaching your shaders
	GLuint the_program = glCreateProgram();
	glAttachShader(the_program, the_vert);
	glAttachShader(the_program, the_frag);
	printProgramInfoLog(the_program);
	
	// Link the program
	glLinkProgram(the_program);
	printProgramInfoLog(the_program);
	
	return the_program;
 
}
 
 
//
// lab1.cpp
//
 
#include <math.h>
#include <assert.h>
 
//#include <GLUT/glut.h>
#include <GL/glew.h>
#include <GL/glut.h>
 
#include "ShaderSetup.h"
 
#include <stdio.h>
 
#define bool int
#define false 0
#define true 1
 
int currentX;		// Current location of X
int	currentY;		// Current location of Y
int startX;			// Starting location of X
int	startY;			// Starting location of Y
int windowHeight;		// Current height of window
int windowWidth;		// Current width of window
 
float 	angle 		= 0.0; 
float	axis[3];
float 	lastPosition[3]	= { 0.0F, 0.0F, 0.0F };
float	trans[3];
 
bool 	redrawContinue 	= false;
bool    trackballMove 	= false;
bool 	trackingMouse 	= false;
 
// Scene will mimic the basic scene used in RenderMan exercises
//  Vertices and colors for drawing the objects in the scene
GLfloat WallColors[][3] = {
    { 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 }, 
    { 1.0, 0.0, 0.0 }
};
 
GLfloat WallVertices[][3] = {
    { -5.0,  5.0,  0.0 }, {  5.0, 5.0,  0.0 }, { 5.0,  -5.0,  0.0 }, 
    { -5.0,  -5.0,  0.0 }
};
 
GLfloat FloorColors[][3] = {
    { 1.0, 1.0, 0.5 }, { 1.0, 1.0, 0.5 }, { 1.0, 1.0, 0.5 }, 
    { 1.0, 1.0, 0.5 }
};
 
GLfloat FloorVertices[][3] = {
    { -5.0, -5.0,  5.0 }, {  5.0, -5.0,  5.0 }, { 5.0,  -5.0,  -5.0 }, 
    { -5.0, -5.0, -5.0 }
};
 
GLUquadricObj *sphere = NULL;
 
 
/*
 *  The function draws your scene.  It will also apply the shader and attach it to
 *  correct primitive.  For sake of the example, the shader will be applied to the
 *  wall.
 */
 
void drawscene( void ) {
 
	// draw your floor
	/*
	GLuint brickProgram = setUpAShader("brick2.vert","brick2.frag");
		glUseProgram( brickProgram );	
	GLint brickLocation = glGetUniformLocation(brickProgram, "BrickColor");
	glUniform3f(brickLocation, 1.0, 1.0, 0.0);
	*/
 
	glBegin( GL_POLYGON );
      glColor3fv(  FloorColors[0]   );
      glVertex3fv( FloorVertices[0] );
 
      glColor3fv(  FloorColors[1]   );
      glVertex3fv( FloorVertices[1] );
 
      glColor3fv(  FloorColors[2]   );
      glVertex3fv( FloorVertices[2] );
 
      glColor3fv(  FloorColors[3]   );
      glVertex3fv( FloorVertices[3] );
	glEnd( );
	
 
 
	// draw the wall -- don't forget to assign your texture coords
	/*
	glUseProgram( setUpAShader("wood2.vert","wood2.frag") );	
	*/
	glBegin( GL_POLYGON );
      glColor3fv(  WallColors[0]   );
	  glTexCoord2f (0.0, 0.0);
      glVertex3fv( WallVertices[0] );
 
      glColor3fv(  WallColors[1]   );
	  glTexCoord2f (0.0, 1.0);
      glVertex3fv( WallVertices[1] );
 
      glColor3fv(  WallColors[2]   );
	  glTexCoord2f (1.0, 1.0);
      glVertex3fv( WallVertices[2] );
 
      glColor3fv(  WallColors[3]   );
	  glTexCoord2f (1.0, 0.0);
      glVertex3fv( WallVertices[3] );
	glEnd( );
	
	// draw the sphere	
	glUseProgram( setUpAShader("C:\Users\Korax\Desktop\Expert_Problem\plastic.vert","C:\Users\Korax\Desktop\Expert_Problem\plastic.frag") );	
	
	if (!sphere) {
		sphere = gluNewQuadric();
		gluQuadricDrawStyle(sphere, GLU_FILL);
	}
	glPushMatrix();
		glTranslatef (0.0, 0.0, -2.0);
		glColor3f (0.0, 0.0, 1.0);
		gluSphere (sphere,2,100, 100);
	glPopMatrix();
	
	
}
 
 
 
/*
 * Display callback function - used for redisplay as well
 */
 
void display( void ) {
 
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
    // View transform 
    if ( trackballMove ) {
 
        glRotatef( angle, axis[0], axis[1], axis[2] );
 
    }
    drawscene();
 
    glutSwapBuffers();
}
 
 
 
/*
 * Callback function for screen window resizing/reshaping
 */
 
void myReshape( int width, int height ) {
 
    glViewport( 0, 0, width, height );
    windowWidth = width;
    windowHeight = height;
 
}
 
 
/*
 * Main routine - GLUT setup and initialization
 */
 
int main( int argc, char **argv ) {
 
	// Initialize GLUT
    glutInit( &argc, argv );
 
    // Enable double buffering and depth buffering
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
	glutInitWindowSize( 500, 500 );
	glutCreateWindow( argv[0] );
 
	// Callback functions are specified 
	glutReshapeFunc( myReshape );
	glutDisplayFunc( display );
 
	// enable depth testing
	glEnable( GL_DEPTH_TEST ); 
	glEnable (GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
 
	// Camera stuff - matrix initialization
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );
	gluPerspective (45, 1.0, 1.0, 100.0);
 
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity( );
	gluLookAt (0.0, 15.0, -15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
	
	// enter your display loop.
	glutMainLoop();
	
	return 0;
}
Attachments:
null
Snapshot of Visual Studio.
Snapshot of Visual Studio.
 
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: Koraxen
Solution Provided By: josgood
Participating Experts: 2
Solution Grade: A
Views: 63
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by josgood

Rank: Master

Expert Comment by josgood:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by Koraxen
Author Comment by Koraxen:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by josgood

Rank: Master

Expert Comment by josgood:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by josgood

Rank: Master

Accepted Solution by josgood:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by ikework

Rank: Guru

Assisted Solution by ikework:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by josgood

Rank: Master

Expert Comment by josgood:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by ikework

Rank: Guru

Expert Comment by ikework:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by Koraxen
Author Comment by Koraxen:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by Koraxen
Author Comment by Koraxen:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628