GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Dec-2005, 09:11
pl39xh pl39xh is offline
New Member
 
Join Date: Jul 2005
Location: Reutlingen, Germany
Posts: 11
pl39xh is on a distinguished road

problem with casting


hi,
i've got a problem with the opengl funtion glTexImage2D();

CPP / C++ / C Code:
GLubyte ***texture; //this is a dynamically allocated 3d array containing pixel data of a .png image

//now i want to assign texture to glTexImage2D 
glTexImage2D(..., const GLvoid *texels); //which takes a const GLvoid * as a parameter

i've tried a lot but i couldn't figure out how to do this .. maybe it isn't even possible. I know that it does work with an array that isn't dynamically allocated but i need a solution with dynamically allocated arrays.

i'm greatful for help
  #2  
Old 04-Dec-2005, 15:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: problem with casting


Quote:
Originally Posted by pl39xh
hi,
i've got a problem with the opengl funtion glTexImage2D();

CPP / C++ / C Code:
GLubyte ***texture; //this is a dynamically allocated 3d array containing pixel data of a .png image

//now i want to assign texture to glTexImage2D 
glTexImage2D(..., const GLvoid *texels); //which takes a const GLvoid * as a parameter

i've tried a lot but i couldn't figure out how to do this .. maybe it isn't even possible. I know that it does work with an array that isn't dynamically allocated but i need a solution with dynamically allocated arrays.

i'm greatful for help

No, and it has nothing to do with casting. It has to do with how arrays are stored in memory.

The function will read pixel values sequentially from memory starting at the adress given by the parameter "texels". (You always get a contiguous block of memory when you declare an array or a multi-dimensional array; it's guaranteed.)

Generally, dynamic allocation of multi-dimensional arrays is performed by separate calls to some allocation function (malloc, calloc, new, or some such thing), and the individual elements will not all be in a single contiguous block of memory (since successive calls to malloc, for example, will not result in the blocks being contiguous). You could allocate a single large array for the pixels, but then you would not be able to access the pixels with multi-dimensional array index notation.

If you have something more specific in mind (or if I have misunderstood your intentions), please tell us a little more about what you are working on.

Regards,

Dave
Last edited by davekw7x : 04-Dec-2005 at 16:19.
  #3  
Old 05-Dec-2005, 08:30
pl39xh pl39xh is offline
New Member
 
Join Date: Jul 2005
Location: Reutlingen, Germany
Posts: 11
pl39xh is on a distinguished road

Re: problem with casting


thank you
that was exactly what i was looking for, so you didn't misunderstand me.
i did not come to the idea that this was the problem all the time.
i have rewritten my code and now its working ..

if someone is interested in how to load .png files and make them opengl textures (on linux) i'll post my solution.
  #4  
Old 05-Dec-2005, 14:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: problem with casting


Quote:
Originally Posted by pl39xh
thank you
that was exactly what i was looking for, so you didn't misunderstand me.
i did not come to the idea that this was the problem all the time.
i have rewritten my code and now its working ..

if someone is interested in how to load .png files and make them opengl textures (on linux) i'll post my solution.

I am always interested in things that work. If it's not really, really (really) long, I would like to see it.

Regards,

Dave
  #5  
Old 05-Dec-2005, 14:10
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: problem with casting


Quote:
Originally Posted by pl39xh
if someone is interested in how to load .png files and make them opengl textures (on linux) i'll post my solution.

I am curious enough to look at your solution. OpenGL stuff intruigues me but I have not had a chance to play with it in FLTK yet.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #6  
Old 06-Dec-2005, 06:04
pl39xh pl39xh is offline
New Member
 
Join Date: Jul 2005
Location: Reutlingen, Germany
Posts: 11
pl39xh is on a distinguished road

Re: problem with casting


k... so here it is.
it's no really professional code and a bit large too, but its working and i'm happy about that


first the main.c file...
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

#include <GL/gl.h> //you have to install the glut and opengl libraries
#include <GL/glut.h>

#include "texture.h"

Texture *texture[2]; //is defined in the texture.h file below
static GLuint texnames[2]; 

void init(void); 
void reshape(int w, int h);
void keyboard(unsigned char key, int x, int y);
void display(void);
int init_game(void); 

int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 
	glutInitWindowSize(1024, 768);
	glutCreateWindow("Test");
	glutFullScreen();
	init();
	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);
	glutReshapeFunc(reshape);
	init_game();
	glutMainLoop();
	return 0; 
}

void init(void)
{ 
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	glEnable(GL_TEXTURE_2D);
}

int init_game()
{
	if((texture[0] = load_tex_img("<your image>.png")) == NULL){
		fprintf(stderr, "init_game: failed to load texture image\n");
		return 0;
	}
	
	if((texture[1] = load_tex_img("<your image>.png")) == NULL){
		fprintf(stderr, "init_game: failed to load texture image\n");
	}

	glGenTextures(2, texnames);
	
	texture[0]->x = 0.0f; //texture coordinates ... not really necessary ..
	texture[0]->y = 0.0f;
	texture[0]->w = 1.0f;
	texture[0]->h = 1.0f; 
	
	texture[1]->x = 0.0f; 
	texture[1]->y = 0.0f; 
	texture[1]->w = 1.0f; 
	texture[1]->h = 1.0f;
	
	glBindTexture(GL_TEXTURE_2D, texnames[0]);   //texture binds
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture[0]->height, texture[0]->width, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture[0]->img); 
	
	glBindTexture(GL_TEXTURE_2D, texnames[1]);   
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture[1]->height, texture[1]->width, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture[1]->img); 
	
	return 1;
}

void display()
{
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
	
	glBindTexture(GL_TEXTURE_2D, texnames[0]);
	glBegin(GL_QUADS);
		glTexCoord2f(texture[0]->w, texture[0]->h); glVertex3f(0.25f, -0.25f, 0.0f);
		glTexCoord2f(texture[0]->w, texture[0]->y); glVertex3f(0.0f, -0.25f, 0.0f);
		glTexCoord2f(texture[0]->x, texture[0]->y); glVertex3f(0.0f, 0.0f, 0.0f);
		glTexCoord2f(texture[0]->x, texture[0]->h); glVertex3f(0.25f, 0.0f, 0.0f);
	glEnd();
	glBindTexture(GL_TEXTURE_2D, texnames[1]);	
	glBegin(GL_QUADS);
		glTexCoord2f(texture[1]->w, texture[1]->h); glVertex3f(0.0f, 0.0f, 0.0f);
		glTexCoord2f(texture[1]->w, texture[1]->y); glVertex3f(-1.0f, 0.0f, 0.0f);
		glTexCoord2f(texture[1]->x, texture[1]->y); glVertex3f(-1.0f, 1.0f, 0.0f);
		glTexCoord2f(texture[1]->x, texture[1]->h); glVertex3f(0.0f, 1.0f, 0.0f);
	glEnd();
	
	glutSwapBuffers(); 	//should show one image in the upper left corner and one in the center of the screen ... changing the vertex coordinates makes sense.
}

void reshape(int w, int h) //ordinary reshape function
{
	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 30.0);	
	glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0.0, 0.0, -1.0);
}

void keyboard(unsigned char key, int x, int y) //ESC quits and return forces redraw.
{
	switch(key){
		case 0x20:
			glutPostRedisplay(); 
			break;
		case 0x1b:
			exit(3); //3 because of atexit for a function that frees the allocated memory.. not included yet .. :oops: 
			break; 
		default:
			break;
	}
}

now the texture.c and .h file followed by the makefile

CPP / C++ / C Code:
//texture.c
#include <stdio.h>
#include <stdlib.h>

#include <GL/gl.h>
#include <GL/glut.h>

#include <png.h> //you need to install libpng before compiling.  

#include "texture.h" 

int check_texture_size(Texture *tex)
{
	int n;
	int w = 0, h = 0;
	
	for(n = MAXTEXTURESIZE; n >= 1; n /= 2){
		if(tex->width == n)
			w = n;	
		if(tex->height == n)
			h = n;
	}

	if(w && h)
		return 1; 
	else 
		return 0;
}

Texture *load_tex_img(char *filename) //returns a Texture with png pixel data
{
	Texture *tex; 
	FILE *png;
	png_bytep *row_pointers; //png functions will store the pixel data in row_pointers.

	int x, y, n = 0;
	int color_type;	
	
	tex = malloc(sizeof(Texture));

	png = fopen(filename, "rb"); //open the png file.
	if(!png){
		fprintf(stderr, "load_tex_img: failed to open %s\n", filename); 
		return NULL; 
	}	

	png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);    
	if (!png_ptr){
		fprintf(stderr, "load_tex_img: failed to initialize png_ptr\n");
		return NULL;
	} //init png_ptr. 

    	png_infop info_ptr = png_create_info_struct(png_ptr); //init info_ptr
    	if (!info_ptr)
    	{
        	png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
        	fprintf(stderr, "load_tex_img: failed to initialize info_ptr\n");
    		return NULL;
	}

    	png_infop end_info = png_create_info_struct(png_ptr);
    	if (!end_info)
    	{
        	png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
        	fprintf(stderr, "load_tex_img: failed to initialze end_info\n");
        	return NULL; 
	}
	png_init_io(png_ptr, png);
	
  	png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING, NULL);
	
	row_pointers = png_get_rows(png_ptr, info_ptr);  //read pixel data into row_pointers .. to understand this part you may have a look at the libpng manual.
	 
	tex->width = info_ptr->width; //dimensions of the png file
	tex->height = info_ptr->height; 
	
	color_type = info_ptr->color_type;
	if(color_type != PNG_COLOR_TYPE_RGB_ALPHA){
		fprintf(stderr, "has no non alpha image support yet ^^\n");
	        return NULL; //all your images need to have an alpha channel .. you can write code that supports images without alpha channel. for more informations on how to do this visit the libpng homepage and look for the manual.  
        } //
	
	if(!check_texture_size(tex)){ 
		fprintf(stderr, "load_tex_img: dimensions of texture image must be powers of 2\n");
		return NULL;
	} //opengl only takes file dimensions as powers of two ... 


	tex->img = (GLubyte*)malloc(tex->width * tex->height * 4 * sizeof(GLubyte)); //malloc space for the pixel data .. thanks dave. :) 
	
	for(x = 0; x < tex->width * 4; x += 4){
		for(y = 0; y < tex->height; y++){
			tex->img[n]   = row_pointers[y][x];
			tex->img[n+1] = row_pointers[y][x+1];
		        tex->img[n+2] = row_pointers[y][x+2];
		        tex->img[n+3] = row_pointers[y][x+3];
		        n += 4;
		}
	} //copy the data from row_pointers to tex. row pointers stores its data in this order: [y][x*4]; (in lines)  

	fclose(png); 	//closes the png file and frees the data
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); //frees the read_struct(png_ptr) 
	
	return tex; //returns the texture.  
}


now texture.h ..
CPP / C++ / C Code:
#ifndef TEXTURE
#define TEXTURE

#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>

#include <png.h>

#include "texture.h"

#define MAXTEXTURESIZE 1024 //don't now if this is needed .. if not just leave it away...
 
typedef struct { //the Texture struct
	GLubyte *img; 
	float x, y; //texture coords  
	float w, h;
	int width, height; //file dimensions
} Texture;

Texture *load_tex_img(char *filename); 
int check_texture_size(Texture *tex); 

#endif


and finally the makefile for linux. (i am using ubuntu and it also worked with gentoo ... don't now if this is working on other operating systems)
Code:
INCLUDES= -I/usr/include/GL FLAGS= -Wall LIBS= -L/usr/lib/ -lpng -lGL -lGLU -lglut -lX11 -lm -lXmu //the libs you need... HEADERS= texture.o main.o default: main clean: rm *.o main: includes gcc -c $(INCLUDES) main.c -o main.o gcc $(HEADERS) -o test $(LIBS) make clean includes: gcc -c $(FLAGS) $(INCLUDES) texture.c -o texture.o //its obviously that you need gcc..

i have modified the code a little bit for posting .. if something isn't working ill post the fixed part again..

alex.

btw.. what is FLTK?
  #7  
Old 06-Dec-2005, 07:24
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: problem with casting


Quote:
Originally Posted by pl39xh
k... so here it is.
it's no really professional code and a bit large too, but its working and i'm happy about that
Thanks Alex, I am snowbound today so I'm going to give your example a go this morning.

Quote:
Originally Posted by pl39xh
btw.. what is FLTK?

The Fast Light Tool Kit, can be found at FLTK.org is an open source UI library that can be used under a number of OS'es. We also have a forum here but since I do most of the responding the quality may be a bit suspect.

It can work with OpenGL so I am going to see if I can force your example into it or not. Most likely not since I have never given it a go but what the heck, it's worth a shot.

Thanks for sharing your example with us.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #8  
Old 06-Dec-2005, 08:21
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: problem with casting


Ok, with a few changes I can get it to use the OpenGL stuff that I have with FLTK.

Namely,
CPP / C++ / C Code:
#include <GL/gl.h>
#include <GL/glut.h>

becomes,
CPP / C++ / C Code:
#include <FL/gl.h>
#include <FL/glut.H>

I had a problem with the malloc in load_tex_img and had to cast it to a Texture*. I suppose at that point tex should be checked to make sure it isn't NULL.

Now to read the docs and put it in an OpenGL window to see if it works. Right now, running the exe just crashes miserably when attempting to open the TEST window.

I'm going to go through the FLTK example program and convert your main code into something FLTK understands and set up an OpenGL container in an FLTK window.

Oh, I had one question, what type of image should the png be? Can it be anything or should it be some sort of greyscale imagery? Just curious about that is all. If you could attach the one you used in your testing that would be cool.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #9  
Old 06-Dec-2005, 08:38
pl39xh pl39xh is offline
New Member
 
Join Date: Jul 2005
Location: Reutlingen, Germany
Posts: 11
pl39xh is on a distinguished road

Re: problem with casting


Quote:
Oh, I had one question, what type of image should the png be? Can it be anything or should it be some sort of greyscale imagery? Just curious about that is all. If you could attach the one you used in your testing that would be cool.

No, it has to be an image with alpha channel and it's dimensions must be powers of 2. If you have an image that hasn't dimensions of powers of two you can resize it or put it into a bigger one, but then you have to use other texture coordinates.
I have attached the image I used.
Attached Images
File Type: png freya.png (93.3 KB, 6 views)
  #10  
Old 06-Dec-2005, 09:06
pl39xh pl39xh is offline
New Member
 
Join Date: Jul 2005
Location: Reutlingen, Germany
Posts: 11
pl39xh is on a distinguished road

Re: problem with casting


I have compiled the posted programm myself to see if I've accomplished to put some mistakes in it and it compiled and worked without errors. The only mistake are the C-style comments in the makefile .. ^^.

Quote:
I had a problem with the malloc in load_tex_img and had to cast it to a Texture*. I suppose at that point tex should be checked to make sure it isn't NULL.

Oh.. thx


Quote:
Now to read the docs and put it in an OpenGL window to see if it works. Right now, running the exe just crashes miserably when attempting to open the TEST window.

This could have happened because of the use of a wrong image.. the programm is still in test phase so it sadly doesn't support all types of png images yet.

Quote:
The Fast Light Tool Kit, can be found at FLTK.org is an open source UI library that can be used under a number of OS'es. We also have a forum here but since I do most of the responding the quality may be a bit suspect
.

hm this sounds interesting
I'll deal with that at some time.

Thanks for your interest in this

Alex
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
String problem vaha C Programming Language 3 24-May-2005 18:21
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:53.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.