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
- why rendering is too much slow
- 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,&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(&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
");
if(!(pngfile = fopen("tile.png","rb")))
{
fprintf(stderr,"file not exist
");
}
fprintf(stdout,"checking PNG signatures
");
fread(sign,1,8,pngfile);
if(!(png_check_sig(sign,8)))
{fprintf(stderr,"not a valid signature
");
}
else fprintf(stdout,"signature valid
");
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
");
png_destroy_read_struct(&png_ptr,NULL,NULL);
}
if(setjmp(png_ptr->jmpbuf)){
png_destroy_read_struct(&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, &width, &height, &bit_depth,
&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 && 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
");
pChannels = (int*)png_get_channels(png_ptr, pinfo_ptr);
printf("channel per pixel %i
",pChannels) ;
fprintf(stdout, "before pRowbytes assignment
");
rowbytes = png_get_rowbytes(png_ptr, pinfo_ptr);
fprintf(stdout, "before image_data assignment
");
if ((image_data = (unsigned char *)malloc(rowbytes*height)) == NULL) {
png_destroy_read_struct(&png_ptr, &pinfo_ptr, NULL);
return NULL;
}
fprintf(stdout,"before loop
");
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