Lesson 19

Started by
4 comments, last by Pjotr Svetachov 22 years, 6 months ago
Hi there. I''m new to programming and just followed some tuts from this page. Then i grabbed the basecode and now i''m trying to implent some tutorials in to the basecode just for learning. Now for the problem: I''m trying to inplent the particle engine frol lesson 19 but i get this error when i run the program: Unhandled exception at 0x77d48c2c in GLtest.exe: 0xC0000005: Access violation reading location 0x4c6f6f70. Then i see a green error pointing to my call of the render_particle function when i move my mouse over it, it says: this code has called into another function. when that function is finished that function is executed. my code: particle.h #ifndef PARTICLES_ENGINE #define PARTICLES_ENGINE #include "extern.h" //extern fariable data and all the header includes are here typedef struct tagPARTICLES { bool active; // Active (Yes/No) float life; // Particle Life float fade; // Fade Speed float r; // Red Value float g; // Green Value float b; // Blue Value POINT3D position; // Positiob POINT3D direction; // Derection POINT3D gravity; // Gravity } PARTICLES; static GLfloat colors[12][3]= // Rainbow Of Colors { {1.0f,0.5f,0.5f},{1.0f,0.75f,0.5f},{1.0f,1.0f,0.5f},{0.75f,1.0f,0.5f}, {0.5f,1.0f,0.5f},{0.5f,1.0f,0.75f},{0.5f,1.0f,1.0f},{0.5f,0.75f,1.0f}, {0.5f,0.5f,1.0f},{0.75f,0.5f,1.0f},{1.0f,0.5f,1.0f},{1.0f,0.5f,0.75f} }; bool particles_init(int nummaxparticles,PARTICLES * particle); void particles_render(int nummaxparticles,PARTICLES * particle); #endif particle.ccp: #include "extern.h" bool particles_init(int nummaxparticles,PARTICLES * particle) { particle = new PARTICLES[nummaxparticles]; // Particle Array (Room For Particle Info) for (int loop=0;loop-1.5f)) particle[loop].yg-=0.01f; // If Number Pad 6 And X Gravity Is Less Than 1.5 Increase Pull Right if (keys[VK_NUMPAD6] && (particle[loop].xg<1.5f)) particle[loop].xg+=0.01f; // If Number Pad 4 And X Gravity Is Greater Than -1.5 Increase Pull Left if (keys[VK_NUMPAD4] && (particle[loop].xg>-1.5f)) particle[loop].xg-=0.01f; if (keys[VK_TAB]) // Tab Key Causes A Burst { particle[loop].position.x=0.0f; // Center On X Axis particle[loop].position.y=0.0f; // Center On Y Axis particle[loop].position.z=0.0f; // Center On Z Axis particle[loop].direction.x=float((rand()%50)-26.0f)*10.0f; // Random Speed On X Axis particle[loop].direction.y=float((rand()%50)-25.0f)*10.0f; // Random Speed On Y Axis particle[loop].direction.z=float((rand()%50)-25.0f)*10.0f; // Random Speed On Z Axis }*/ } } } code from main.ccp: PARTICLES particle1; BOOL Initialize (GL_Window* window, Keys* keys) // Any GL Init Code & User Initialiazation Goes Here { g_window = window; g_keys = keys; if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW ) { return FALSE; // If Texture Didn''t Load Return FALSE } if (!particles_init(1000,&particle1)) return FALSE; glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Blending Function For Translucency Based On Source Alpha Value // Start Of User Initialization angle = 0.0f; // Set Starting Angle To Zero BuildFont(); glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth (1.0f); // Depth Buffer Setup glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal) glEnable (GL_DEPTH_TEST); // Enable Depth Testing glShadeModel (GL_SMOOTH); // Select Smooth Shading glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate glEnable(GL_TEXTURE_2D); // Enable Texture Mapping SetupWorld(); return TRUE; // Return TRUE (Initialization Successful) } void Draw (void) { camera.x=-xpos; camera.z=-zpos; camera.y=-0.9f; GLfloat xtrans = -xpos; GLfloat ztrans = -zpos; GLfloat sceneroty = 360.0f - yrot; GLfloat x_m, y_m, z_m, u_m, v_m; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The Modelview Matrix glBindTexture(GL_TEXTURE_2D, texture[1].texID); // Select Our Second Texture glRotatef(sceneroty,0,1.0f,0); glTranslatef(camera.x, camera.y, camera.z); glDisable(GL_BLEND); // Disable Blending Before We Draw In 3D glColor3f(1.0f,1.0f,1.0f); // Bright White glBindTexture(GL_TEXTURE_2D, texture[2].texID); // Select Our Second Texture for (int loop_s = 0; loop_s < world1.numsectors; loop_s++) { for (int loop_m = 0; loop_m < world1.sector[loop_s].numtriangles; loop_m++) { glBindTexture(GL_TEXTURE_2D, texture[world1.sector[loop_s].triangle[loop_m].texture].texID); glBegin(GL_TRIANGLES); //if (IsPolyFisible(world1.sector[loop_s].triangle[loop_m],camera)==true) //{ for (int i=0;i<3;i++) { glNormal3f( 0.0f, 0.0f, 1.0f); x_m = world1.sector[loop_s].triangle[loop_m].vertex.x; y_m = world1.sector[loop_s].triangle[loop_m].vertex.y; z_m = world1.sector[loop_s].triangle[loop_m].vertex.z; u_m = world1.sector[loop_s].triangle[loop_m].vertex.u; v_m = world1.sector[loop_s].triangle[loop_m].vertex.v; glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m); //} } glEnd(); } } particles_render(100,&particle1); //HERE IS WERE THE GREEN ARROW POINTS GetClientRect (g_window->hWnd,&window); // Get Window Dimensions glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glPushMatrix(); // Store The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix glOrtho(0,window.right,0,window.bottom,-1,1); // Set Up An Ortho Screen glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix // Text glBindTexture(GL_TEXTURE_2D, texture[5].texID); // Select Our Font Texture glPrint(0,0,2,"FPS: %4.2d",fps); // Print Title glPrint(0,16,2,"X: %d200.0",camera.x); // Print Title glPrint(0,32,2,"Y: %d200.0",camera.y); // Print Title glPrint(0,48,2,"Z: %d200.0",camera.z); // Print Title glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glPopMatrix(); // Restore The Old Projection Matrix glMatrixMode(GL_MODELVIEW); glFlush (); // Flush The GL Rendering Pipeline } </i>
Advertisement
Just a guess..
Have you tried it without the messagebox function in render_particles?
It works now but the weird part is that i put the messagebox there because i got that error before i wanted to see if it got there

Thanks
I think it worked beceaus i rendered 100 particles instead of 1000 cus if i change the value to 1000 i get an error:
Unhandled exception at 0x77f61825 in GLtest.exe: 0xC0000005: Access violation writing location 0x00030ffc.

with again a green arrow in a file called crt0.c and line 270
whats this mean?
Single-step the application and see what goes wrong. This way you can examine all the variables as you go, and see whether they contain expected values etc...
  bool particles_init(int nummaxparticles,PARTICLES * particle){  particle = new PARTICLES[nummaxparticles];  ....}PARTICLES particle1;particles_init(1000,&particle1)  


Eek! Evil! Dangling pointer!
Try this instead
  bool particles_init(int nummaxparticles,PARTICLES **particle){  *particle = new PARTICLES[nummaxparticles];  ....}PARTICLES *particle1;particles_init(1000, &particle1);  

Kippesoep

This topic is closed to new replies.

Advertisement