Hi everybody

I defined an array pointer to hold a massive amount of data, (actually the RGB data of a screen with 1024*768 pixels). the data is assigned from another temporary array to the this array within a function that calculates the values of the temporary array and then assign them to the global one using FOR loops. the global array values should be kept saved to be used in an OpenGL function as a store of an image data and then display the image on the screen.

when I run the program, the program works fine until it reaches a specific row number of the screen rows (row number 16 of 128 rows) and crash giving the following message:

Unhandled exception at 0x7c812afb in OPENGL_.....exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fbb4..

when I used the delete function to delete the array it works until it reaches a different and specific row number (28) means works more but the array will be deleted before I use it which is unwanted. at any case, I need the program to work till the end of the rows (128).

any one can help me in this please?
how can I get rid of this error and make the program works? is there any problem about the array pointer, either the way of defining it or the way i am using it? if not what is the problem here, any idea?

many thanks in advance

here is parts of the OpenGL code in C++ environment:

#include <iostream>
#include <stdlib.h>
//#include "glext.h"
//#include "glew.h"
.
.
#include <GL/glut.h>
#endif
#include "stdafx.h"

#include <windows.h>




using namespace std;

.
.
int column_index = 0;
int row_index = 0;
.
.

unsigned char *pixels_index = new unsigned char[3*1024*768];

.
void update(int value) {
.
unsigned char *pixels_index_temp = new unsigned char[3*8*6];
.
.
int x_column = 8*column_index;	
int y_row = 6*row_index;

glReadPixels(x_column, y_row, 8, 6, GL_RGB, GL_UNSIGNED_BYTE, pixels_index_temp);		
for (int yi=0;yi<6;yi++)
		{
		for (int xi=0;xi<8;xi++)
			{
int local_index = 3*(yi*w_temp + xi);
int global_index = 3*((row_index*w*h_temp) + (yi*w) + (column_index*w_temp) + xi);
				
pixels_index[global_index] = pixels_index_temp[local_index];
pixels_index[global_index + 1] = pixels_index_temp[local_index + 1];
pixels_index[global_index + 2] = pixels_index_temp[local_index + 2];
			}
		}
	delete [] pixels_index_temp;
.
.
if (row_index < 128){glutTimerFunc(100, update, 0);}
	else {
..
..
.
.
delete [] pixels_index;
}

Sorry, you haven't given us enough code here...

We need to know what w, w_temp, h_temp get assigned to and how. If you can, post the
fragment of code that they are set in as well please.

I guess the you almost certainly have made a memory error with the indexes e.g. by overwriting global_index when it comes to the last value because of the +2 etc.

The other thing I am thinking of is that you have not initialized one of those variables?

Sorry, you haven't given us enough code here...

We need to know what w, w_temp, h_temp get assigned to and how. If you can, post the
fragment of code that they are set in as well please.

I guess the you almost certainly have made a memory error with the indexes e.g. by overwriting global_index when it comes to the last value because of the +2 etc.

The other thing I am thinking of is that you have not initialized one of those variables?

Thank you very much for reply
w is the width of the computer screen that displays the image, it is a global variable set to be 1024 pixels, each pixel has 3 colours RGB (red, green, blue) and so it needs 3 bytes (24 bit) in memory to be written. h is the height 768 pixels.
w_temp, and h_temp are temporary width and height set to be 8 and 6 respectively. The small window of 8*6 pixels is scanning the image from the lower left corner to the right and then again towards the top until it reaches the upper right corner, for that reason the number of bytes in memory will start with 0 and ends up with 3*1024*768.
in another file the global variable are defined as follows:
extern int column_index;
extern int row_index;
extern int w;
extern int h;
the OpenGL function reads the pixels values of the small screen in the same way (lower left to upper right indicated with the local index) and saved in an array of 3*8*6 elements and then in the FOR loop the values are saved in their corresponding locations in the main array pixel_index. after scanning the whole image the resulting array pixels_index that holds the pixels values will be displayed on the screen using opengl functions after row_index reaches 128 and gets out of the IF statement.
The image will be loaded from a file called tris0.md2

here is the code:

#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#endif
#include "stdafx.h"

#include <windows.h>


#include "image_loader.h"
#include "md2_format.h"
#include "lens_array.h"



using namespace std;

const float FLOOR_TEXTURE_SIZE = 15.0f; 
MD2Model* _model;
int _textureId;
int column_index = 0;
int row_index = 0;
int w = 1024;
int h = 768;

unsigned char *pixels_index = new unsigned char[3*1024*768];

void cleanup() {
	delete _model;
}

GLuint loadTexture(Image *image) {
	GLuint textureId;
	glGenTextures(1, &textureId);
	glBindTexture(GL_TEXTURE_2D, textureId);
	glTexImage2D(GL_TEXTURE_2D,
				 0,
				 GL_RGB,
				 image->width, image->height,
				 0,
				 GL_RGB,
				 GL_UNSIGNED_BYTE,
				 image->pixels);
	return textureId;
}

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);
	glShadeModel(GL_SMOOTH);
	
	_model = MD2Model::load("tris0.md2");
	
	Image* image = loadBMP("Brunel_logo.bmp");
	_textureId = loadTexture(image);
	delete image;
	}

void handleResize(int w1, int h1) {
	glViewport((column_index*8 ), (row_index*6), w1, h1);		glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();
	
float p = 0.015625;	
	if (column_index < 4)
	{
			if (row_index < 4)
			{
			glFrustum(-1.0, -1 + p*(4.5 + column_index), -1.0, -1 + p*(4.5 + row_index), 2.7, 200);
			}
			if (3 < row_index < 125){
			glFrustum(-1.0, -1 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), -1.0 + p*(4.5 + row_index), 2.7, 200);
			}
			else {
			glFrustum(-1.0, -1 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), 1.0, 2.7, 200);
			}
	} 
	if (3 < column_index < 125)
	{
			if (row_index < 4)
			{
			glFrustum(-1.0 + p*(column_index - 3.5), -1.0 + p*(4.5 + column_index), -1.0, -1 + p*(4.5 + row_index), 2.7, 200);			
			}
			if (3 < row_index < 125){
			glFrustum(-1.0 + p*(column_index - 3.5), -1.0 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), -1.0 + p*(4.5 + row_index), 2.7, 200);			
			}
			else {
			glFrustum(-1.0 + p*(column_index - 3.5), -1.0 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), 1.0, 2.7, 200);			
			}
				
	} else 
	{
			if (row_index < 4)
			{
			glFrustum(-1.0 + p*(column_index - 3.5), 1.0, -1.0, -1 + p*(4.5 + row_index), 2.7, 200);
			}
			if (3 < row_index < 125){
			glFrustum(-1.0 + p*(column_index - 3.5), 1.0, -1.0 + p*(row_index - 3.5), -1.0 + p*(4.5 + row_index), 2.7, 200);
			}
			else {
			glFrustum(-1.0 + p*(column_index - 3.5), 1.0, -1.0 + p*(row_index - 3.5), 1.0, 2.7, 200);
			}
		}
	
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glTranslatef(0.0f, 0.0f, -20.0f);	
	glRotatef(0.0f, 0.0f, 1.0f, 0.0f);		
	GLfloat ambientLight[] = {0.6f, 0.6f, 0.6f, 1.0f};		glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
	
	GLfloat lightColor[] = {0.8f, 0.8f, 0.8f, 1.0f};	
	GLfloat lightPos[] = {-4.2f, 0.0f, 0.0f, 0.0f};			glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

	
		if (_model != NULL) {
		glPushMatrix();
		glTranslatef(0.0f, 1.0f, 0.0f);	
		glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);					      glScalef(3.0f, 3.0f, 3.0f);				
		_model->draw();
		glPopMatrix();
		
	}
	
	
	glTranslatef(0.0f, -5.4f, -3.0f);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	glBegin(GL_QUADS);
	
	glNormal3f(0.0f, 1.0f, 0.0f);
	glTexCoord2f(2000 / FLOOR_TEXTURE_SIZE, 0 / FLOOR_TEXTURE_SIZE);		glVertex3f(1000.0f, 0.0f, 1000.0f);
	glTexCoord2f(2000 / FLOOR_TEXTURE_SIZE,(2000 + 0) / FLOOR_TEXTURE_SIZE);					
				 
	glVertex3f(1000.0f, 0.0f, -1000.0f);
	glTexCoord2f(0.0f, (2000 + 0) / FLOOR_TEXTURE_SIZE);		
	glVertex3f(-1000.0f, 0.0f, -1000.0f);
	glTexCoord2f(0.0f, 0 / FLOOR_TEXTURE_SIZE);	
	glVertex3f(-1000.0f, 0.0f, 1000.0f);
	
	glEnd();
	
	glutSwapBuffers();
	
}



void update(int value) {
	
	initRendering();
	handleResize( 8, 6);
	drawScene();

	
	int w_temp = 8;
	int h_temp = 6;
	

    unsigned char *pixels_index_temp = new unsigned char[3*8*6];	
	glReadBuffer(GL_BACK_LEFT);	
	glPixelStorei(GL_PACK_ALIGNMENT, 1);

	int x_column = 8*column_index;	
	int y_row = 6*row_index;

	glReadPixels(x_column, y_row, 8, 6, GL_RGB, GL_UNSIGNED_BYTE, pixels_index_temp);	

	for (int yi=0;yi<6;yi++)
		{
		for (int xi=0;xi<8;xi++)
			{
				int global_index = 0;
				int local_index = 0;
				local_index = 3*(yi*w_temp + xi);
				global_index = 3*(row_index*w*h_temp + yi*w + column_index*w_temp + xi);
				
				pixels_index[global_index] = pixels_index_temp[local_index];
				pixels_index[global_index + 1] = pixels_index_temp[local_index + 1];
				pixels_index[global_index + 2] = pixels_index_temp[local_index + 2];
				
			}
		}

	delete [] pixels_index_temp;

	column_index = column_index + 1;
   
    if (column_index < 128){glutTimerFunc(100, update, 0);}
	else {row_index = row_index + 1;
	cout<<"row_index:   "<<row_index<<"\n";
	column_index = 0;}
		
	if (row_index < 128){glutTimerFunc(100, update, 0);}
	
else {
	
		
				
	glViewport(0, 0, w, h);		
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();
	
	glFrustum(-1.0, 1.0, -1.0, 1.0, 2.2, 200);

	GLuint ID=1;

	glGenTextures(1, &ID);
   
    glBindTexture(GL_TEXTURE_2D, ID);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, w, h,
                     GL_RGB, GL_UNSIGNED_BYTE, pixels_index);		   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 
                0, GL_RGB, GL_UNSIGNED_BYTE, pixels_index);

   

float VCorners[4][3] = {{ -7.7f, 0.0f, 10.0f },     // left bottom 

						{7.7f, 0.0f, 10.0f },    //  right bottom

						{ 7.7f, 11.0f, 10.0f },    //   right top

						{ -7.7f, 11.0f, 10.0f }};    //   left top

 
glEnable (GL_TEXTURE_2D);

glBegin (GL_QUADS);
	glTexCoord2f (0.0, 0.0);	glVertex3fv(VCorners[0]);
	glTexCoord2f (1.0, 0.0);	glVertex3fv(VCorners[1]);
	glTexCoord2f (1.0, 1.0);	glVertex3fv(VCorners[2]);
	glTexCoord2f (0.0, 1.0);	glVertex3fv(VCorners[3]);
glEnd ();


glDisable (GL_TEXTURE_2D);

	
	glutSwapBuffers();
refresh: glFlush();
goto refresh;
	
	}
   delete [] pixels_index;
	
}


int main(int argc, char** argv) {
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	
	glutInitWindowSize(1024, 768);
	


	glutCreateWindow("OPENGL_BRUNEL");
	
	initRendering();
	glutDisplayFunc(drawScene);
	
	
	glViewport(0, 0, 1024, 768);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0 , 1.0, -1.0, 1.0, 3, 200);


	glutTimerFunc(2500, update, 0);

	
	glutMainLoop();
	return 0;
}

I had to mention that when it crashes the break point arrow points at the line 189
pixels_index[global_index] = pixels_index_temp[local_index];

thanks

I tried to reduce the screen size to be only 28 in width and run it several times. it crashed at the value 16 of row_index which is the same value the program crash at when the delete functioon is used (e.g. delete [] pixels_index)
when I used delete fucntion with the reduced height image the progarm crashed directly and gave me error pointing at the file ostream.cpp .

I need your help please, it is very important for me. your help will be really appreciated

many thanks

There are several very worring things to think about.

The first is the delete [] pixels_index that happens on line 58 of your original code and line 261 of the update. That line gets called a lot of times if I am not mistaken, but you only create pixels_index once. To test it, put a std::cout<<"this is called only once "<<std::endl; and make sure I am wrong.

Second, the goto refresh command, that is going to produces a huge stack, at each time, you are getting an extra loop. You should loop over the flush ONLY while there are events on the queue, then you should stop.

Other than that I can't see anything, hopefully, that is the problem or someone else can see what is wrong...

There are several very worring things to think about.

The first is the delete [] pixels_index that happens on line 58 of your original code and line 261 of the update. That line gets called a lot of times if I am not mistaken, but you only create pixels_index once. To test it, put a std::cout<<"this is called only once "<<std::endl; and make sure I am wrong.

Second, the goto refresh command, that is going to produces a huge stack, at each time, you are getting an extra loop. You should loop over the flush ONLY while there are events on the queue, then you should stop.

Other than that I can't see anything, hopefully, that is the problem or someone else can see what is wrong...

Thank you very much

I made sure that you are right about the delete function, it is called several times but I changed the location of this function in the code but did not change the result so much, even when it is removed the result is the same. if it is necce3ssary where in this code it should be?
when I added delete function in line 204, the program crashed, that looks as if the program crashes when the array is deleted in one way or another, so is it usual that the array is deleted on its own after calling it several times? what is the bets to do please? using an additional array?

the goto loop is not reached because it is inside the else statement (line 207) which is the case when the whole job is done, but it is not.

finally, is there any problem of calling a function from inside the same function? this is the case here with update function

waiting for an urgent reply

many thanks again

You MUST make sure that each call to a new , is matched by one and only one call to a delete.

So looking at your code, you put the delete after 289 before 290.

I really don't like the goto. It is called AFTER the program has finished, but, before you have exited the mainloop. Its effect is to prevent normal exiting of the program.

Calling a function from itself, is ok IF and ONLY IF, you have a way to get out.
This is wrong for example:

void function()
{
   // Stuff...
   function();
}

Were as this would be ok:

void function(count)
{
   if (count<0) return; 
   // stuff
   function(count-1);
}

Note the early termination in the second version.
What you have created is an endless loop, which allocates a small amount of extra memory each time through. Therefore I am unhappy with it. It is perfectly good, if you call it a set number of times, then exit. But make sure you have an exit.

You MUST make sure that each call to a new , is matched by one and only one call to a delete.

So looking at your code, you put the delete after 289 before 290.

I really don't like the goto. It is called AFTER the program has finished, but, before you have exited the mainloop. Its effect is to prevent normal exiting of the program.

Calling a function from itself, is ok IF and ONLY IF, you have a way to get out.
This is wrong for example:

void function()
{
   // Stuff...
   function();
}

Were as this would be ok:

void function(count)
{
   if (count<0) return; 
   // stuff
   function(count-1);
}

Note the early termination in the second version.
What you have created is an endless loop, which allocates a small amount of extra memory each time through. Therefore I am unhappy with it. It is perfectly good, if you call it a set number of times, then exit. But make sure you have an exit.

This is the code after the above suggestions, delete was added in the correct place, goto was removed and return was added to exit from the function update. even so the same problem occurs in the same way and at the same value (row_index = 16) it crashes and gives the following message:

Unhandled exception at 0x7c812afb in OPENGL_BRUNEL.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fbb8..

I hope to hear new suggestions and thank you very much for the help so far

#include <stdlib.h>
#include <GL/glut.h>
#endif
#include "stdafx.h"

#include <windows.h>


#include "image_loader.h"
#include "md2_format.h"
#include "lens_array.h"



using namespace std;

const float FLOOR_TEXTURE_SIZE = 15.0f; 
MD2Model* _model;
int _textureId;
int column_index = 0;
int row_index = 0;
int w = 1024;
int h = 768;

unsigned char *pixels_index = new unsigned char[3*1024*768];

void cleanup() {
	delete _model;
}

GLuint loadTexture(Image *image) {
	GLuint textureId;
	glGenTextures(1, &textureId);
	glBindTexture(GL_TEXTURE_2D, textureId);
	glTexImage2D(GL_TEXTURE_2D,
				 0,
				 GL_RGB,
				 image->width, image->height,
				 0,
				 GL_RGB,
				 GL_UNSIGNED_BYTE,
				 image->pixels);
	return textureId;
}

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);
	glShadeModel(GL_SMOOTH);
	
	_model = MD2Model::load("tris0.md2");
	
	Image* image = loadBMP("Brunel_logo.bmp");
	_textureId = loadTexture(image);
	delete image;
	}

void handleResize(int w1, int h1) {
	glViewport((column_index*8 ), (row_index*6), w1, h1);		glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();
	
float p = 0.015625;	
	if (column_index < 4)
	{
			if (row_index < 4)
			{
			glFrustum(-1.0, -1 + p*(4.5 + column_index), -1.0, -1 + p*(4.5 + row_index), 2.7, 200);
			}
			if (3 < row_index < 125){
			glFrustum(-1.0, -1 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), -1.0 + p*(4.5 + row_index), 2.7, 200);
			}
			else {
			glFrustum(-1.0, -1 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), 1.0, 2.7, 200);
			}
	} 
	if (3 < column_index < 125)
	{
			if (row_index < 4)
			{
			glFrustum(-1.0 + p*(column_index - 3.5), -1.0 + p*(4.5 + column_index), -1.0, -1 + p*(4.5 + row_index), 2.7, 200);			
			}
			if (3 < row_index < 125){
			glFrustum(-1.0 + p*(column_index - 3.5), -1.0 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), -1.0 + p*(4.5 + row_index), 2.7, 200);			
			}
			else {
			glFrustum(-1.0 + p*(column_index - 3.5), -1.0 + p*(4.5 + column_index), -1.0 + p*(row_index - 3.5), 1.0, 2.7, 200);			
			}
				
	} else 
	{
			if (row_index < 4)
			{
			glFrustum(-1.0 + p*(column_index - 3.5), 1.0, -1.0, -1 + p*(4.5 + row_index), 2.7, 200);
			}
			if (3 < row_index < 125){
			glFrustum(-1.0 + p*(column_index - 3.5), 1.0, -1.0 + p*(row_index - 3.5), -1.0 + p*(4.5 + row_index), 2.7, 200);
			}
			else {
			glFrustum(-1.0 + p*(column_index - 3.5), 1.0, -1.0 + p*(row_index - 3.5), 1.0, 2.7, 200);
			}
		}
	
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glTranslatef(0.0f, 0.0f, -20.0f);	
	glRotatef(0.0f, 0.0f, 1.0f, 0.0f);		
	GLfloat ambientLight[] = {0.6f, 0.6f, 0.6f, 1.0f};		glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
	
	GLfloat lightColor[] = {0.8f, 0.8f, 0.8f, 1.0f};	
	GLfloat lightPos[] = {-4.2f, 0.0f, 0.0f, 0.0f};			glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

	
		if (_model != NULL) {
		glPushMatrix();
		glTranslatef(0.0f, 1.0f, 0.0f);	
		glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);					      glScalef(3.0f, 3.0f, 3.0f);				
		_model->draw();
		glPopMatrix();
		
	}
	
	
	glTranslatef(0.0f, -5.4f, -3.0f);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	glBegin(GL_QUADS);
	
	glNormal3f(0.0f, 1.0f, 0.0f);
	glTexCoord2f(2000 / FLOOR_TEXTURE_SIZE, 0 / FLOOR_TEXTURE_SIZE);		glVertex3f(1000.0f, 0.0f, 1000.0f);
	glTexCoord2f(2000 / FLOOR_TEXTURE_SIZE,(2000 + 0) / FLOOR_TEXTURE_SIZE);					
				 
	glVertex3f(1000.0f, 0.0f, -1000.0f);
	glTexCoord2f(0.0f, (2000 + 0) / FLOOR_TEXTURE_SIZE);		
	glVertex3f(-1000.0f, 0.0f, -1000.0f);
	glTexCoord2f(0.0f, 0 / FLOOR_TEXTURE_SIZE);	
	glVertex3f(-1000.0f, 0.0f, 1000.0f);
	
	glEnd();
	
	glutSwapBuffers();
	
}



void update(int value) {
	
	initRendering();
	handleResize( 8, 6);
	drawScene();

	
	int w_temp = 8;
	int h_temp = 6;
	

    unsigned char *pixels_index_temp = new unsigned char[3*8*6];	
	glReadBuffer(GL_BACK_LEFT);	
	glPixelStorei(GL_PACK_ALIGNMENT, 1);

	int x_column = 8*column_index;	
	int y_row = 6*row_index;

	glReadPixels(x_column, y_row, 8, 6, GL_RGB, GL_UNSIGNED_BYTE, pixels_index_temp);	

	for (int yi=0;yi<6;yi++)
		{
		for (int xi=0;xi<8;xi++)
			{
				int global_index = 0;
				int local_index = 0;
				local_index = 3*(yi*w_temp + xi);
				global_index = 3*(row_index*w*h_temp + yi*w + column_index*w_temp + xi);
				
				pixels_index[global_index] = pixels_index_temp[local_index];
				pixels_index[global_index + 1] = pixels_index_temp[local_index + 1];
				pixels_index[global_index + 2] = pixels_index_temp[local_index + 2];
				
			}
		}

	delete [] pixels_index_temp;

	column_index = column_index + 1;
   
    if (column_index < 128){glutTimerFunc(100, update, 0);}
	else {row_index = row_index + 1;
	cout<<"row_index:   "<<row_index<<"\n";
	column_index = 0;}
		
	if (row_index < 128){glutTimerFunc(100, update, 0);}
	
else {
	
		
				
	glViewport(0, 0, w, h);		
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();
	
	glFrustum(-1.0, 1.0, -1.0, 1.0, 2.2, 200);

	GLuint ID=1;

	glGenTextures(1, &ID);
   
    glBindTexture(GL_TEXTURE_2D, ID);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, w, h,
                     GL_RGB, GL_UNSIGNED_BYTE, pixels_index);		   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 
                0, GL_RGB, GL_UNSIGNED_BYTE, pixels_index);

   

float VCorners[4][3] = {{ -7.7f, 0.0f, 10.0f },     // left bottom 

						{7.7f, 0.0f, 10.0f },    //  right bottom

						{ 7.7f, 11.0f, 10.0f },    //   right top

						{ -7.7f, 11.0f, 10.0f }};    //   left top

 
glEnable (GL_TEXTURE_2D);

glBegin (GL_QUADS);
	glTexCoord2f (0.0, 0.0);	glVertex3fv(VCorners[0]);
	glTexCoord2f (1.0, 0.0);	glVertex3fv(VCorners[1]);
	glTexCoord2f (1.0, 1.0);	glVertex3fv(VCorners[2]);
	glTexCoord2f (0.0, 1.0);	glVertex3fv(VCorners[3]);
glEnd ();


glDisable (GL_TEXTURE_2D);

	
	glutSwapBuffers();
         glFlush();
         return;
	
	}
   
return;	
}


int main(int argc, char** argv) {
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	
	glutInitWindowSize(1024, 768);
	


	glutCreateWindow("OPENGL_BRUNEL");
	
	initRendering();
	glutDisplayFunc(drawScene);
	
	
	glViewport(0, 0, 1024, 768);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1.0 , 1.0, -1.0, 1.0, 3, 200);


	glutTimerFunc(2500, update, 0);

	
	glutMainLoop();
         delete [] pixels_index;
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.