PDA

View Full Version : Png image rendering slower in my OGL App



vivek vyas
08-07-2009, 06:53 AM
I know Not posting much of code just part which i think ia an error
here my request
please create file as they are below
test them with valid png file tile.png
and
answer me to
1. why rendering is too much slow
2. how i feel them with colors

thanks in advance(i m beginner dont take that in a big fun)
i hope you not take hard work in fun(huh just pChannel is replaced which is not hat hard)
thats it




makefile


LIB = -I/usr/lib
INC = -I/usr/include
LINK = -lglut -lGL -lpng
OBJ = main.o png.o
COMP = gcc
pngs:main.o png.o
$(COMP) -o 1pntes $(OBJ) $(LIB) $(INC) $(LINK)

main.cpp


#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#define Width 800
#define Height 600
unsigned char *loadpng(const char *filename);
GLuint tex;
GLfloat theta = 0.0f;
void LoadTexture()
{unsigned char *data;
glGenTextures(0,&amp;tex);
glBindTexture(GL_TEXTURE_2D,tex);
data = loadpng("tile.png");

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void idle()
{

// theta +=1.0f;
glutPostRedisplay();

}
void Rend()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glPushMatrix();
glBindTexture(GL_TEXTURE_2D,tex);

glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);glVertex2f(-1.0f,1.0f);
glTexCoord2f(-1.0f,0.0f);glVertex2f(1.0f,1.0f);
glTexCoord2f(-1.0f,-1.0f);glVertex2f(1.0f,-1.0f);
glTexCoord2f(0.0f,-1.0f);glVertex2f(-1.0f,-1.0f);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
}
void Reshape(int w,int h)
{
GLfloat rat = (GLfloat)w/(GLfloat)h;
if(h==0)h=1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0f,rat,1.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w,h);
}

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

glutInitDisplayMode(GLUT_RGBA |GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(Width,Height);
glutCreateWindow("Png test");
LoadTexture();


glutDisplayFunc(Rend);
glutReshapeFunc(Reshape);
glutIdleFunc(idle);
glutMainLoop();
}


png.cpp

#include <stdlib.h>
#include <stdio.h>
#include <png.h>
#include <GL/glu.h>
unsigned char *loadpng(const char *filename)
{
png_uint_32 width,height;int color_type,bit_depth;

png_structp png_ptr;
png_infop pinfo_ptr;
unsigned char sign[8];
FILE *pngfile;
printf("Png test\n");
if(!(pngfile = fopen("tile.png","rb")))
{
fprintf(stderr,"file not exist\n");
}
fprintf(stdout,"checking PNG signatures\n");

fread(sign,1,8,pngfile);

if(!(png_check_sig(sign,8)))
{fprintf(stderr,"not a valid signature\n");
}
else fprintf(stdout,"signature valid\n");

png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL, NULL,NULL);
pinfo_ptr = png_create_info_struct(png_ptr);
if(!pinfo_ptr)
{
fprintf(stderr,"info poiter not alloc\n");
png_destroy_read_struct(&amp;png_ptr,NULL,NULL);
}
if(setjmp(png_ptr->jmpbuf)){
png_destroy_read_struct(&amp;png_ptr,NULL,NULL);
}
png_init_io(png_ptr,pngfile);
png_set_sig_bytes(png_ptr,8);
png_read_info(png_ptr,pinfo_ptr);
png_get_IHDR(png_ptr, pinfo_ptr, &amp;width, &amp;height, &amp;bit_depth,
&amp;color_type, NULL, NULL, NULL);
int *pChannels;
unsigned long *pRowbytes;
unsigned char *image_data;
png_uint_32 i, rowbytes;
png_bytep row_pointers[height];

if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY &amp;&amp; bit_depth < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, pinfo_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
png_read_update_info(png_ptr, pinfo_ptr);
fprintf(stdout, "before pChannels assignment\n");
pChannels = (int*)png_get_channels(png_ptr, pinfo_ptr);
printf("channel per pixel %i\n",pChannels) ;
fprintf(stdout, "before pRowbytes assignment\n ");
rowbytes = png_get_rowbytes(png_ptr, pinfo_ptr);


fprintf(stdout, "before image_data assignment\n ");
if ((image_data = (unsigned char *)malloc(rowbytes*height)) == NULL) {
png_destroy_read_struct(&amp;png_ptr, &amp;pinfo_ptr, NULL);
return NULL;
}
fprintf(stdout,"before loop\n");
for (i = 0; i < height; ++i)
row_pointers[i] = image_data + i*rowbytes;
png_read_image(png_ptr,row_pointers);
gluBuild2DMipmaps( GL_TEXTURE_2D,
GL_INTENSITY,
width,height,
GL_RGB,
GL_UNSIGNED_BYTE,
image_data );

}

what thing i add to get colors in my life

vivek vyas
08-07-2009, 08:40 AM
OH the fault is my own
GL_INTENSITY->GL_RGBA
now rendering slower(on moving that window)

vivek vyas
08-07-2009, 08:49 AM
please reply me if any one had tried my code
:sorrow:(i am feeling helpless) :sorrow:
coloring and orienting is done but performance is slower(OpenGL rendering smooth but on moving window. window moves slower)
is my code have some issue bug
please reply me
have any suggestion PM(Private Message) to me

todayman
08-07-2009, 09:43 AM
You're calling:

glGenTextures(0, &amp;tex);

The first argument should be the number of new textures, in this case 1. I'm not sure if that's the problem, but you should fix it.

vivek vyas
08-07-2009, 11:33 AM
I think it is issue with my hardware
please reply if window moves faster by mouse dragging..

vivek vyas
08-08-2009, 12:16 PM
Is this buged like programme is only run slower in my Suse 11.0 or it not run other then my system??
I test some in devil example(by fidding roation in Quad. May be there is my fault but someday i will rectify it that i hopes) the motion of window also slow as the code was doing