PNG Textures dont work anymore

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

PNG Textures dont work anymore

Post by dcTom »

Hi

my Notebook makes strange noise so i installed Kos on a New PC

Everything is working fine, just some parts of my code is not working any more.
the main problem is that the "PNG Textures code" doesnt work any more

short for if the code

Code: Select all

#include <png/png.h>

GLuint textures[20],startbildschirm,cube,logo,font,cursor;

void quard(int x1,int x2, int y1, int y2, int add){ // rechteck mit ganyer textur
		glBegin(GL_QUADS);
		glColor4f(1.0,1.0,1.0,1.0f);
		glTexCoord2f(0,0);
		glVertex3f(x1,y1,add);  // durch add in der hoehe verschiebbar
		glTexCoord2f(1,0);
		glVertex3f(x2,y1,add);
		glTexCoord2f(1,1);
		glVertex3f(x2,y2,add);
		glTexCoord2f(0,1);
		glVertex3f(x1,y2,add);
		glEnd();
		}

void loadtxr_png(const char *fn, GLuint *txr) {
	kos_img_t img;
	pvr_ptr_t txaddr;

	GLuint mask =  PNG_MASK_ALPHA;
	GLuint frmt =  GL_ARGB1555_TWID;

	if (png_to_img(fn,mask, &img) < 0) {
		printf("can't load %s\n", fn);
		return;
	}

	txaddr = pvr_mem_malloc(img.w * img.h * 2);
	pvr_txr_load_kimg(&img, txaddr, PVR_TXRLOAD_16BPP);
	kos_img_free(&img, 0);

	glGenTextures(1, txr);
	glBindTexture(GL_TEXTURE_2D, *txr);
	glKosTex2D(frmt, img.w, img.h, txaddr); //<--------------------- the Problem

}

int main(int argc, char **argv) {

loadtxr_png("/rd/logo.png", &logo );

while(1){


glBindTexture(GL_TEXTURE_2D, logo);  // fighter oben
quard(120,520, 480, 160, 4);



}


}

the compile dont like this line

glKosTex2D(frmt, img.w, img.h, txaddr);


compiler msg

test.o: In Funktion `loadtxr_png':
/opt/toolchains/dc/test/test.c:33: Nicht definierter Verweis auf `_glKosTex2D'
collect2: Fehler: ld gab 1 als Ende-Status zurück
make: *** [test.elf] Fehler 1

Nicht definierter Verweis auf = undefined reference to
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

Make sure you sourced the environ file

Code: Select all

source /opt/toolchains/dc/kos/environ.sh
Then make sure -lGL is linked in your Makefile.

EDIT: actually I think this function was deprecated and removed in the newer OpenGL implementation for KOS. I think you should just use glTexImage2D instead like you would in normal OpenGL.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

still doesnt work

my makefile

Code: Select all

TARGET = test.elf
OBJS = test.o

all: rm-elf $(TARGET)

include $(KOS_BASE)/Makefile.rules

clean:
	-rm -f $(TARGET) $(OBJS) romdisk.*

rm-elf:
	-rm -f $(TARGET) romdisk.*

$(TARGET): $(OBJS) romdisk.o
	$(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \
		$(OBJS) romdisk.o  $(OBJEXTRA) -lGL -lpng -lkmg -lpcx -loggvorbisplay -lm -lkallisti -lgcc  -lz -ltga $(KOS_LIBS)   -lkmg -lkosutils

romdisk.img:
	 $(KOS_GENROMFS) -f romdisk.img -d romdisk -v

romdisk.o: romdisk.img
	$(KOS_BASE)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o




run: $(TARGET)
	$(KOS_LOADER) $(TARGET)

dist:
	rm -f $(OBJS) romdisk.o romdisk.img
	$(KOS_STRIP) $(TARGET)

User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

Please look at my edit, I think you missed it.
I think that function is part of the old OpenGL implementation, but it was taken out in the implementation by Ph3nom, which is standard now. (no such function exists in standard OpenGL)
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

i think we are coming closer


i replaced

glKosTex2D(frmt, img.w, img.h, txaddr);

with

glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, img.w, img.h, 0,GL_RGB, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);

bit i think some of the parameter are not ok, because i only get a black square
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

so changed the complete code, think it was from 2005


new code for the textures

Code: Select all


pvr_ptr_t logo;       
png_load_texture("/rd/logo.png", &logo,PNG_MASK_ALPHA,512,512 );  
glBindTexture(GL_TEXTURE_2D, logo1);
  





*** ASSERTION FAILURE ***
Assertion "0" failed at pvr_prim.c:98 in `pvr_poly_compile': Invalid texture U size

the size of the png file is 512x512
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

Code: Select all

 int png_load_texture(const char *filename, pvr_ptr_t *tex, uint32 alpha, uint32 *w, uint32 *h);
Those are pointers to the dimensions, not immediate values.
The function stores the dimensions of the image there, you're not supposed to use them as input.
The compiler should have told you about this. Did you disable warnings?

And it seems you never actually loaded the texture into OpenGL.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

so if i call the funktion what do i write for *w and *h what pointer do i need?
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

Code: Select all

uint32 w, h;
png_load_texture("/rd/logo.png", &logo,PNG_MASK_ALPHA,&w,&h);
// w and h now contain the dimensions of logo.png
glTexImage2D(...w, h, ...)
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

Hello,
i already tried this

Code: Select all

uint32 w, h;
png_load_texture("/rd/logo.png", &logo,PNG_MASK_ALPHA,&w,&h);
but still the same probelm

first, for what do i need
glTexImage2D(...w, h, ...)
after
png_load_texture("/rd/logo.png", &logo,PNG_MASK_ALPHA,&w,&h); ?


i also found a probelm with the datatype of logo

the png_load_texture funktion expect as argument 2 the type pvr_ptr_t

glBindTexture(GL_TEXTURE_2D, logo);

expect GLuint

so if i write

Code: Select all

GLuint logo; 
i get an warning that png_load_textur expect argument of type pvr_ptr_t

for

Code: Select all

pvr_ptr_t logo;
i get a warning that glBindTexture expect argument of type GLuint
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

Loading a texture in OpenGL consists of two phases.

1. Loading the image file into memory.
You have the compressed PNG image and you want to parse information like its dimensions, color format and get an array of pixel values.
So you fopen the file and parse it according to PNG file specifications.

2. Passing the dimensions, color format and array of pixel values to the graphics chip.
Although you have loaded the texture, it's not ready for drawing yet. You need to first transfer it from main memory to texture memory (on the graphics chip).
Depending on what graphics driver you use this is Direct3D (in the official WindowsCE development kit, not in KOS), OpenGL (Ph3nom's OpenGL implementation for KOS) or the PVR API (also in KOS, more info about the differences here: http://dcemulation.org/?title=Graphics_APIs).

When you look at the declaration of the function you're using, you see this.

Code: Select all

int png_load_texture(const char *filename, pvr_ptr_t *tex, uint32 alpha, uint32 *w, uint32 *h)
pvr_ptr_t is a pointer to texture memory (on the graphics chip).
You can tell from the data type that you're using a loading function here that is supposed to be used with the PVR API, not OpenGL.

Now look at the declaration of glTexImage2D (https://www.khronos.org/registry/OpenGL ... mage2D.xml):

Code: Select all

void glTexImage2D(GLenum target,  GLint level,  GLint internalformat,  GLsizei width,  GLsizei height,  GLint border,  GLenum format,  GLenum type,  const GLvoid * data)
You see that it doesn't take a pvr_ptr_t data, but a GLvoid* data (which is just a void*, an array of color values of any type).

I think what you really want to be using is:

Code: Select all

/* Load a PNG to a KOS Platform Independent Image */
int png_to_img(const char * filename, uint32 mask, kos_img_t *rv);
From the includes you can tell that kos_img_t is defined in kos/img.h (kos/addons/include/kos/img.h).
You can also look it up in the online documentation at http://gamedev.allusion.net/docs/kos-cu ... __img.html (I suggest you bookmark this page).

So in summary:
1. Use png_to_img() to load the meta data and get an array of color values
2. Pass that into OpenGL using glTexImage2D() using the information stored in kos_img_t.

Does that help you?
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

I've written a tutorial for you here: http://dcemulation.org/?title=Loading_P ... L_textures
It makes direct use of libpng.
Hope that makes stuff easier for you.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

thanks a lot, for the tutorial, i will report after testing all



You made my day, or week.

textures came back :)

only thing that is left, the transparency of the png pictures is not working any more.
i had sprites with alphachannel, but now the also the alpha part is over an oter texture.


so 2 questions, is your code automaticly recognize that the png file has an alpha-part
and if yes with what funktion do i activate it
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

You probably want:

Code: Select all

  glEnable(GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
This mixes the image you draw with the background by calculating

Code: Select all

new_color * alpha + background_color * (1-alpha)
So when your new color has alpha of 1 it is fully visible. If its alpha is 0 the background is fully visible, 0.5 is a mix of both.

There's also glEnable(GL_ALPHA_TEST) if all you're doing is "don't display pixels with alpha value less than 1" instead of blending, but I think that's not implemented in KOS OpenGL.
Attachments
2017-02-26-021826_642x570_scrot.png
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

working fine now, just forgot the
glEnable(GL_BLEND);


but im wondering why, if i load more then 1 png

png_to_gl_texture(&logo,"/rd/logo.png);
png_to_gl_texture(&map,"/rd/map.png);


i allway have the same texture t
he last one i loaded,


it looks like
glGenTextures generates always the same ID

and if i define
struct texture tex_test;


glBindTexture(GL_Texture_2D, &tex_test.id);

it binds also the map.png texture
but i have not loaded it into tex_test
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

I cannot reproduce this problem..

Please type the following to confirm the ids are different (they should be)

Code: Select all

printf("logo id: %d\nmap id: %d\n", logo.id, map.id);
Then when you draw make sure you call glBindTexture with the corresponding id before you draw. Otherwise it will constantly use the same id.

For example this works for me

Code: Select all

draw_textured_quad(map, 0, 0); // calls glBindTexture map.id
draw_textured_quad(logo, 50, 50); // calls glBindTexture logo.id
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

Problem fixed
glBindTexture(GL_TEXTURE_2D, tex_logo.id); works
glBindTexture(GL_TEXTURE_2D, &tex_logo.id); not good


Thanks for all your time
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by bogglez »

You had a problem with pointers earlier as well.
Your compiler should notice that you needed a GLuint but passed in a GLuint*.
Do you have warnings disabled? They can be turned on with "KOS_CFLAGS += -Wall -Wextra".

And glad it's working now.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
dcTom
DCEmu Junior
DCEmu Junior
Posts: 43
Joined: Mon Feb 20, 2017 7:49 pm
Has thanked: 0
Been thanked: 0

Re: PNG Textures dont work anymore

Post by dcTom »

You had a problem with pointers earlier as well.
Your compiler should notice that you needed a GLuint but passed in a GLuint*.
Do you have warnings disabled? They can be turned on with "KOS_CFLAGS += -Wall -Wextra".

And glad it's working now.

yes i'm on war a little bit with Pointer

Warnings are enabeled but the list of Files that are loaded are so long - so havent seen them :)
Post Reply