PDA

View Full Version : How to do Content browsing on the objects



Sindhu
02-23-2010, 06:46 AM
Hi anyone,

As i am new to OpenGL, please tell me how to do content browsing using openGL on the object like Cylinder.

I know how to place images on the surface of the cylinder, but i need to move the images, like the image which is on viewer side should look bigger one .

For example the images are with names of the different persons.

The name which comes towards the viewer side should look bigger one.

Please help me from this .


Advance Thanks for reply.

Sindhu
02-24-2010, 06:20 AM
Hi,

Please help me from this problem.

I know how to do single texturing, but giving segmentation fault while doing multi-texturing.

The code is HERE (http://www.2shared.com/file/11634633/9315f90e/rar.html)

and the output showing is segmentation fault as below :


user@lxdevenv:~/Temp$ gdb rar
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) info s
No stack.
(gdb) run
Starting program: /localhome/user/Temp/rar
[Thread debugging using libthread_db enabled]
freeglut (/localhome/user/Temp/rar): Unable to create direct context rendering for window 'Cylinder'
This may hurt performance.
Width of p1.bmp: 300
Height of p1.bmp: 300
Width of pa1.bmp: 320
Height of pa1.bmp: 300
Width of pa3.bmp: 528
Height of pa3.bmp: 396
[New Thread 0xb7aa16c0 (LWP 6371)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7aa16c0 (LWP 6371)]
0xb7c9e9bc in memcpy () from /lib/tls/i686/cmov/libc.so.6
(gdb) info s
#0 0xb7c9e9bc in memcpy () from /lib/tls/i686/cmov/libc.so.6
#1 0xb7ed3989 in _XRead () from /usr/lib/libX11.so.6
#2 0xb7e278c1 in ?? () from /usr/lib/libGL.so.1
#3 0xb7e297b3 in ?? () from /usr/lib/libGL.so.1
#4 0x080492d9 in loadimage () at rar.c:182
#5 0x080495b8 in init () at rar.c:225
#6 0x080499d1 in main (argc=Cannot access memory at address 0x3
) at rar.c:427
(gdb)



please help me from, how to do multi-texturing ?

I am thankful for the answer.

ZbuffeR
02-24-2010, 08:28 AM
(can't download your code)
go to loadimage () at rar.c:182
You will probably see something like glTexImage2d(...,datapointer);

And the error is that datatpointer hold to few bytes for this image, compared to what says the parameters such as width height type and GL_RGB or GL_RGBA components.

Sindhu
02-24-2010, 10:53 PM
Hi,

Thank you So much for reply.

file is down-loadable , in that link below there is a line i,e
Save file to your PC: click here so there you click,
it will download.

From the information in above post,
i need to increase the memory size of the image ?

I am thankful for the answer.

ZbuffeR
02-25-2010, 01:37 AM
Save file to your PC: click here so there you click
Yes of course, that is what I did.

it will download.
No, it got stuck forever, at least twice.


i need to increase the memory size of the image ?
Probably, but is more important, is that the datapointer points a memory block having exactly the correct size.

Be sure to set data alignement to 1 byte (no alignement) :
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

Read this old page for details about why it is important :
http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

Sindhu
02-26-2010, 12:05 AM
Hi
Sir,

Thank you so much for replying.

i placed the above two lines in loadimage() function.but it is still showing the fault which is as below :

Fault :

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7a2c6c0 (LWP 14098)]
0x080492f6 in loadimage () at rar.c:182
182 glGenTextures(3,image->genID);
(gdb) info s
#0 0x080492f6 in loadimage () at rar.c:182
#1 0x08049610 in init () at rar.c:231
#2 0x08049a29 in main (argc=1645339, argv=0x0) at rar.c:433
(gdb)


So please help me from this Sir,

ZbuffeR
02-26-2010, 05:23 AM
Just post the loadImage() code already, as there is a special "code" block this forum to preserve formating.


glGenTextures(3,image->genID);
A segfault here means that genID is pointer to a memory block that is too small (or wrong) to hold the 3 needed texture ids.

Sindhu
03-03-2010, 11:41 PM
Hi Sir,

Thanks for your replying.

I am posting the code below :

kindly see the code :


#include<GL/glut.h>
#include<GL/glu.h>
#include<GL/gl.h>
#include<time.h>
#include<math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ESCAPE 27
int window,click=0,limit=0;
float ytheta=0;

GLint texture[4];
int nval = 16,i;
GLfloat base[90][3], top[90][3];

struct timespec t;

struct Image
{
unsigned long sizeX;
unsigned long sizeY;
char *data;
GLuint *genID;

};
struct Image image1;
struct Image image2;
struct Image image3;

struct Image *image;

int ImageLoad(char *filename, struct Image *image)
{
FILE *file;
unsigned long size;
unsigned long i;
unsigned short int planes;
unsigned short int bpp;
char temp;
if ((file = fopen(filename, "rb"))==NULL)
{
printf("File Not Found : %s\n",filename);
return 0;
}
fseek(file, 18, SEEK_CUR);

if ((i = fread(&amp;image->sizeX, 4, 1, file)) != 1)
{
printf("Error reading width from %s.\n", filename);
return 0;
}

printf("Width of %s: %lu\n", filename, image->sizeX);
if ((i = fread(&amp;image->sizeY, 4, 1, file)) != 1)
{
printf("Error reading height from %s.\n", filename);
return 0;
}

printf("Height of %s: %lu\n", filename, image->sizeY);
size = image->sizeX * image->sizeY * 3;

if ((fread(&amp;planes, 2, 1, file)) != 1)
{
printf("Error reading planes from %s.\n", filename);
return 0;
}
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}


if ((i = fread(&amp;bpp, 2, 1, file)) != 1) {
printf("Error reading bpp from %s.\n", filename);
return 0;
}
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}


fseek(file, 24, SEEK_CUR);


image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}

if ((i = fread(image->data, size, 1, file)) != 1)
{
printf("Error reading image data from %s.\n", filename);
return 0;
}

for (i=0;i<size;i+=3) {
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}


return 1;
}
void spin()
{
if(click==1)
{
ytheta+=5.0;
if(ytheta>=360.0)
ytheta=0;
limit++;
if(limit==9)
{
click=0;
limit=0;
}

}
glutSwapBuffers();
glutPostRedisplay();
}
void mouse (GLint btn, GLint state, GLint x, GLint y)
{
if (btn==GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)
{
click = 1;
}
else if (btn==GLUT_RIGHT_BUTTON &amp;&amp; state == GLUT_DOWN)
{
click = 1 ;
}
}
void loadimage()
{
int wrap;



ImageLoad("p1.bmp", &amp;image1);

ImageLoad("pa1.bmp", &amp;image2);

ImageLoad("pa3.bmp", &amp;image3);



glGenTextures(3,&amp;image->genID);
glBindTexture(GL_TEXTURE_2D, &amp;image->genID); // 2d texture (x and y size)
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR); // scale linearly when image smalled than texture
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,wrap ? GL_REPEAT : GL_CLAMP_TO_BORDER );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,wrap ? GL_REPEAT : GL_CLAMP_TO_BORDER );

glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data);


glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);


}






void load (GLfloat a[][3], int n, GLfloat r, GLfloat offset, GLfloat zval)
{
GLfloat delta=2*M_PI/(float)n, theta=0.0;
GLint i;
for (i=0;i<n;i++)
{
a[i][0]=r*cos(theta+offset);
a[i][1]=r*sin(theta+offset);
a[i][2]=zval;
theta += delta;
}
}
void init()
{
glEnable(GL_TEXTURE_2D);
glClearColor(1.0,0.0,0.0,0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0,100.0,-100.0,100.0,-100.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0,0.0,-20.0) ;

loadimage ();
}
void cylinder ()
{
GLfloat base[90][3], top[90][3];
int nval = 16,i;
load (base,nval,40.0,90.0,0.0) ;
load (top,nval,40.0,90.0,90.0) ;
//LoadGLTextures(image1,0);
//glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin (GL_QUADS) ;
for (i=0;i<nval;i+=2)
{
// glColor3f (0.01*i,0.5,0.5) ;
glColor3f(1.0,0.0,1.0);
glVertex3fv(base[i]);
glVertex3fv(base[(i+1)%nval]);
glVertex3fv(top[(i+1)%nval]);
glVertex3fv(top[i]);
}
glEnd () ;

glBegin (GL_QUADS) ;
for (i=1;i<nval;i+=2)
{

glTexCoord2f(0.0f, 0.0f);
glVertex3fv(base[i]);
glTexCoord2f(1.0f, 0.0f);
glVertex3fv(base[(i+1)%nval]);
glTexCoord2f(1.0f, 1.0f);
glVertex3fv(top[(i+1)%nval]);
glTexCoord2f(0.0f, 1.0f);
glVertex3fv(top[i]);
}
glEnd ();

glBegin(GL_POLYGON);
for(i=0;i<100;i++)
glVertex3f(top[i][0],top[i][1],90);
glEnd();
}




void display()
{


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);


glLoadIdentity();
gluLookAt (0.0,-80.0,30.0,0.0,0.0,0.0,0.0,-80.0,31.0) ;
glRotatef(ytheta,0,0,1);


glBindTexture ( GL_TEXTURE_2D, image1.genID);
cylinder();



glBindTexture ( GL_TEXTURE_2D, image3.genID);
cylinder();

glDisable(GL_BLEND);

glutPostRedisplay();
glutSwapBuffers();



}





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


glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glOrtho(-2,2,-2,2, -1,1 );



glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);

glBlendColor(.5,.5,.5,.5);

for(i=1;i<nval;i+=2)

{
if(i==1||i==13)
{
glBindTexture ( GL_TEXTURE_2D, image1.genID);


glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3fv(base[i]);
glTexCoord2f(1,0);
glVertex3fv(base[(i+1)%nval]);
glTexCoord2f(1,1);
glVertex3fv(top[(i+1)%nval]);


glTexCoord2f(0,1);
glVertex3fv(top[i]);
glEnd();

}


if(i==5||i==17)
{
glBindTexture ( GL_TEXTURE_2D, image3.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-1);
glTexCoord2f(1,0); glVertex2f( 1,-1);
glTexCoord2f(1,1); glVertex2f( 1, 1);
glTexCoord2f(0,1); glVertex2f(-1, 1);
glEnd();
}

}

glDisable(GL_BLEND);


glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

// glutDisplayFunc(display); // now that you have mixed texture, do real display
}




int main(int argc,char **argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,0);
glutCreateWindow("Cylinder");
glutDisplayFunc(display);
glutIdleFunc(spin);
glutMouseFunc(mouse);
init();
glutMainLoop();
}



This is giving segmentation fault.

How to eliminate that fault ?

Kindly requesting You to check the output.



i am Thankful for your reply.