Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Whitehair

Problem with displaying more than 2 .png files

This topic is 5651 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is my main code. I just can''t get it to display more than 2 images... I want to display all the images stored. And i am thinking of 100 images to be loaded in. anyone knows how to change this to make it display all the images in the const char * images [100] = {"opengl.png", "radial.png", "checker.png", "checker2.png", "nvlogo.png" }; next 2 post are the supporting .cpp files that execute the loading and settings. #ifdef WIN32 # include #endif #include <GL/gl.h> #include "glext.h" #ifdef WIN32 # include "wglext.h" #endif #include <string> #define GLH_EXT_SINGLE_FILE #include "glh_ext.h" #include "glh_obs.h" #include "glh_glut.h" #include "glh_convenience.h" #include "nv_png.h" #include "array_texture.h" using namespace std; using namespace glh; glut_callbacks cb; glut_simple_mouse_interactor camera, object; glut_perspective_reshaper reshaper; tex_object_2D textures[85]; display_list quad; int tx0 = 0; int tx1 = 1; int u; //string title_ll = "lower-left"; string title_lr = "lower-right"; //string title_ul = "upper-left"; //string title_ur = "upper-right"; bool b[256]; bool & translate_texture0 = b[''t'']; bool & rotate_texture1 = b[''r'']; float tx0rot = 90.f; float tx1rot = 0.f; // glut-ish callbacks void display(); void key(unsigned char k, int x, int y); void idle(); void menu(int entry) { key((unsigned char)entry, 0, 0); } // my functions void init_opengl(); int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(800, 800); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB ); glutCreateWindow("A Test"); init_opengl(); //title_ul = "texture 0"; //title_ur = "texture 1"; //title_ll = "REPLACE, MODULATE"; title_lr = "3D Model"; translate_texture0 = true; rotate_texture1 = true; b['' ''] = true; glut_helpers_initialize(); cb.keyboard_function = key; camera.configure_buttons(1); camera.set_camera_mode(true); object.configure_buttons(1); object.dolly.dolly[2] = -2; glut_add_interactor(&cb); glut_add_interactor(&object); glut_add_interactor(&reshaper); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return 0; } void init_opengl() { InitExtension("GL_ARB_multitexture"); glEnable(GL_DEPTH_TEST); const char * images [100] = {"opengl.png", "radial.png", "checker.png", "checker2.png", "nvlogo.png" }; array2 img; for(int i=0; i < 85; i++) { textures.bind(); textures[i].parameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); img = array2(); read_png_rgb(images[i], img); if(img.get_width() == 0) { cerr << "Unable to read image \"" << images[i] << "\"!" << endl; } make_rgb_texture(img); textures[i].unbind(); } quad.new_list(GL_COMPILE); glBegin(GL_QUADS); glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0); glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 0); glVertex3f(-1, -1, .5); glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1); glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 1); glVertex3f(-1, 1, .5); glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1); glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 1); glVertex3f( 1, 1, .5); glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0); glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 0); glVertex3f( 1, -1, .5); glEnd(); quad.end_list(); glActiveTextureARB(GL_TEXTURE0_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); } void key(unsigned char k, int x, int y) { b[k] = ! b[k]; if(k==27 || k==''q'') exit(0); if(k==''1'') tx0 = 0; if(k==''2'') tx0 = 1; if(k==''3'') tx0 = 2; if(k==''4'') tx0 = 3; if(k==''5'') tx0 = 4; if(k==''!'') tx1 = 0; if(k==''@'') tx1 = 1; if(k==''#'') tx1 = 2; if(k==''$'') tx1 = 3; if(k==''%'') tx1 = 4; if(k=='' '') if(b[k]) glutIdleFunc(idle); else glutIdleFunc(0); glutPostRedisplay(); } void idle() { if(translate_texture0) tx0rot = 100.0f; if(rotate_texture1) tx1rot = 100.0f; glutPostRedisplay(); } void display_lr() { glPushMatrix(); //int i=0; camera.apply_inverse_transform(); object.apply_transform(); //for (unsigned int i = 0; i < 100; i++) //{ glActiveTextureARB(GL_TEXTURE0_ARB); textures[u].bind(); textures[u].enable(); glActiveTextureARB(GL_TEXTURE1_ARB); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD); textures[u+1].bind(); textures[u+1].enable(); quad.call_list(); //} textures[u+1].unbind(); textures[u+1].disable(); glActiveTextureARB(GL_TEXTURE0_ARB); textures[u].unbind(); textures[u].disable(); glPopMatrix(); u++; } void render_string(const char * str) { glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glTranslatef(-.9f, .9f, 0); glScalef(.0006f, .0006f, 1); while(*str) { glutStrokeCharacter(GLUT_STROKE_ROMAN, *str); str++; } glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glEnable(GL_DEPTH_TEST); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLint vp[4]; glGetIntegerv(GL_VIEWPORT, vp); GLint w = vp[2]/2; GLint h = vp[3]/2; // Frivolous use of the texture matrix is not advised // if T&L performance is a bottleneck. Don''t use texgen // to compute texture coordinates if they don''t vary from // frame to frame. if(translate_texture0) { glActiveTextureARB(GL_TEXTURE0_ARB); glMatrixMode(GL_TEXTURE); glLoadIdentity(); float tranx(sin(to_radians(tx0rot))); float trany(cos(to_radians(tx0rot))); glTranslatef(1.0f * tranx, 1.0f * trany, 0); glMatrixMode(GL_MODELVIEW); } if(rotate_texture1) { glActiveTextureARB(GL_TEXTURE1_ARB); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glTranslatef( .5f, .5f, 0); glRotatef(tx1rot, 0, 0, 1); //glScalef(.7f, .7f,1); glTranslatef(-.5f,-.5f, 0); glMatrixMode(GL_MODELVIEW); glActiveTextureARB(GL_TEXTURE0_ARB); } glViewport(150,150, 512, 512); // for (unsigned int u = 0; u < 100; u++) // { display_lr(); // } render_string(title_lr.c_str()); glViewport(vp[0], vp[1], vp[2], vp[3]); glutSwapBuffers(); }

Share this post


Link to post
Share on other sites
Advertisement
nv_png.cpp

#include <stdio.h>
#include "nv_png.h"
#include "png.h"

using namespace glh;

void read_png_rgb(const char * filename, glh::array2 & image)
{
FILE * fp;
png_byte sig[8];
int bit_depth, color_type;
double gamma;
png_uint_32 channels, row_bytes;
png_byte **row_pointers = 0;
png_structp png_ptr = 0;
png_infop info_ptr = 0;


// open the PNG input file
if (!filename) return;

if (!(fp = fopen(filename, "rb"))) return;

// first check the eight byte PNG signature
fread(sig, 1, 8, fp);
if (!png_check_sig(sig, 8)) { fclose(fp); return; }


// start back here!!!!

// create the two png(-info) structures

png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (!png_ptr) { fclose(fp); return; }

info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, 0, 0);
fclose(fp);
}

// initialize the png structure
png_init_io(png_ptr, fp);

png_set_sig_bytes(png_ptr, 8);

// read all PNG info up to image data
png_read_info(png_ptr, info_ptr);

// get width, height, bit-depth and color-type
unsigned long w, h;
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);

// expand images of all color-type and bit-depth to 3x8 bit RGB images
// let the library process things like alpha, transparency, background

if (bit_depth == 16) png_set_strip_16(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
if (bit_depth < 8) png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);

// if required set gamma conversion
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);

// after the transformations have been registered update info_ptr data
png_read_update_info(png_ptr, info_ptr);

// get again width, height and the new bit-depth and color-type
png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);


// row_bytes is the width x number of channels
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
channels = png_get_channels(png_ptr, info_ptr);

// now we can allocate memory to store the image

png_byte * img = new png_byte[row_bytes * h];

// and allocate memory for an array of row-pointers

png_byte ** row = new png_byte * [h];


// set the individual row-pointers to point at the correct offsets

for (unsigned int i = 0; i < h; i++)
row = img + i * row_bytes;

// now we can go ahead and just read the whole image

png_read_image(png_ptr, row);

// read the additional chunks in the PNG file (not really needed)

png_read_end(png_ptr, NULL);

image = array2(w, h);

{
for(unsigned int i=0; i < w; i++)
for(unsigned int j=0; j < h; j++)
{ image(i,j) = vec3ub(img + ((h-j-1)*row_bytes + i * 3)); }
}

delete [] row;
delete [] img;

png_destroy_read_struct(&png_ptr, 0, 0);

fclose (fp);
}

void read_png_grey(const char * filename, glh::array2 & image)
{
}


[edited by - Whitehair on December 4, 2002 7:23:33 AM]

Share this post


Link to post
Share on other sites
array_texture.cpp

#include "array_texture.h"

using namespace glh;
void make_rgba_texture(const array2 & rgb,
const array2 & a)
{
int w = rgb.get_width();
int h = rgb.get_height();

if(a.get_width() != w || a.get_height() != h) return; // !?

GLubyte * img = new GLubyte[w*h*4];
GLubyte * ip = img;

for(int j=0; j < h; j++)
for(int i=0; i < w; i++)
{
const vec3ub & v = rgb(i,j);
*ip++ = v[0];
*ip++ = v[1];
*ip++ = v[2];
*ip++ = a(i,j);
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, img);


delete [] img;
}

void make_rgb_texture(const array2 & rgb)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rgb.get_width(), rgb.get_height(), 0,
GL_RGB, GL_UNSIGNED_BYTE, (const void *)rgb.get_pointer());
}

void make_scalar_texture(const array2 & s,
GLenum internal_format, GLenum format)
{
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, s.get_width(), s.get_height(), 0,
format, GL_UNSIGNED_BYTE, (const void *)s.get_pointer());
}

Share this post


Link to post
Share on other sites
and what do you think is the problem?

are the images loaded correctly?
how many images is this code trying to render right now?

try to localize the problem and only post a few relevant source lines. don''t post entire program and say "it doesn''t work".

Share this post


Link to post
Share on other sites
quote:
Original post by niyaw
and what do you think is the problem?

are the images loaded correctly?
how many images is this code trying to render right now?

try to localize the problem and only post a few relevant source lines. don''t post entire program and say "it doesn''t work".


void display_lr()
{

glPushMatrix();

//int i=0;
camera.apply_inverse_transform();
object.apply_transform();
//for (unsigned int i = 0; i < 100; i++)
//{
glActiveTextureARB(GL_TEXTURE0_ARB);
textures[u].bind();
textures[u].enable();

glActiveTextureARB(GL_TEXTURE1_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
textures[u+1].bind();
textures[u+1].enable();

quad.call_list();
//}
textures[u+1].unbind();
textures[u+1].disable();

glActiveTextureARB(GL_TEXTURE0_ARB);
textures[u].unbind();
textures[u].disable();

glPopMatrix();
u++;

}

Can''t seem to be able to display images that are stored in memory location textures[3] and beyond.

Share this post


Link to post
Share on other sites
I do beleive it''s your use of your texture class. You are calling bind and unbind, but never giving them a Unique texture ID (normally gotten from a glGenTextures call). First you have to assign it a unique id, and use that .

Billy - BillyB@mrsnj.com

Share this post


Link to post
Share on other sites
Thanks.

I have solved and is able to display all the images loaded into the memory.

Now the problem is that the first image and the second will merge together and being displayed. And at a distance away, the next two (3rd and 4th images) are merged and displayed. Followed by the next 2 at a distance...

When i rotate the display i can see that they are place all over the display area and not in linear order...

I need to place them in order and they are supposed to be projected and place in numerical order with linear rotation and translated possible...

I figured it''s in the display void...

Even after i commented out all the rotate and translate function, there do not seem to be any difference.


void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLint vp[4];
glGetIntegerv(GL_VIEWPORT, vp);

GLint w = vp[2]/2;
GLint h = vp[3]/2;


// Frivolous use of the texture matrix is not advised
// if T&L performance is a bottleneck. Don''t use texgen
// to compute texture coordinates if they don''t vary from
// frame to frame.
if(translate_texture0)
{
glActiveTextureARB(GL_TEXTURE0_ARB);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
//float tranx(sin(to_radians(tx0rot)));
// float trany(cos(to_radians(tx0rot)));
// glTranslatef(1.0f * tranx, 1.0f * trany, 0);
glMatrixMode(GL_MODELVIEW);
}

if(rotate_texture1)
{
glActiveTextureARB(GL_TEXTURE1_ARB);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
// glTranslatef( 1.0f, 1.0f, 0);
// glRotatef(tx1rot, 0, 0, 1);
// glScalef(1.0f, 1.0f,1);
// glTranslatef(-1.0f,-1.0f, 0);
glMatrixMode(GL_MODELVIEW);
glActiveTextureARB(GL_TEXTURE0_ARB);
}

glViewport(150,150, 512, 512);

//for (unsigned int u = 0; u < 100; u++)
// {
//if (u <= 1)
//{
display_lr();

//display_lr();
// }
render_string(title_lr.c_str());


glViewport(vp[0], vp[1], vp[2], vp[3]);

glutSwapBuffers();


}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!