PDA

View Full Version : How to draw 3D rotating Cylinder



Ravikishore
11-29-2009, 11:04 PM
Hi,

i am new to openGL, i am not getting how to draw 3d Cylinder and that should be rotate in x-direction.

Please guide me, Your answer is valuable for me..

waiting for reply.

marshats
11-30-2009, 08:56 PM
Please be more specific -- what books or online articles have you looked at? What tools are you using C/C++? Posting your code always is a good start. It is quite difficult to answer such a broad question -- maybe looking at how to do an animation itself may help so see Post# 267635 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=267635#Post2676 35)

That post shows a rotating cube, to get a rotating cylinder the code is modifed to use GLUquadricObj as follows;


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

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

//put your specific idle code here
//... this code will run at desiredFPS
gAngle += dt*360./8.; //rotate 360 degrees every 8 seconds
//end your specific idle code here

glutPostRedisplay(); // initiate display() call at desiredFPS rate
}

void display() {
// Will be called at FPS rate, ok to use global values now to rener scene
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glTranslatef(0,0,-100);
glRotatef(gAngle,1.,1.,1.);
//glutWireCube(20.); //cube of size 20x20x20
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();

glutSwapBuffers();
}

void cleanupQuadric(void) // Properly Kill The Window
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}

void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);

IDquadric=gluNewQuadric(); // Create A Pointer To The Quadric Object ( NEW )
gluQuadricNormals(IDquadric, GLU_SMOOTH); // Create Smooth Normals ( NEW )
gluQuadricTexture(IDquadric, GL_TRUE); // Create Texture Coords ( NEW )
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // escape key
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("FPS test /w glutTimerFunc");

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

Ravikishore
11-30-2009, 10:28 PM
Hi,

Thank you so much.

i checked your code, it's displaying perfect cylinder. i made other code to Display cylinder and rotation of that is mouse control. if you want to check the code means i will send it.

my assignment is to place bmp format images on the side surfaces of the Cylinder. i am using C in Ubuntu.
if i rotate the cylinder around the x-axis, it should highlight the current image from my point of view.
Please guide me, any valuable answer is appreciated.

i am thankful for any kind of help.

marshats
11-30-2009, 11:49 PM
There is a nice image loading library called DevIL (http://openil.sourceforge.net/about.php) that is cross platform. Since you are on ubuntu it is a simple "sudo apt-get install libdevil-dev" command installation.

To use it see a previous post from earlier today at Post267979 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=267979#Post2679 79).

After you load the image like the post, you simply need to add two lines of code in the Display function before you start drawing the gluCylinder ...


struct TextureHandle {
... identical from post267979, cut-n-paste
};

TextureHandle logo;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
... identical to post267979, cut-n-paste
}

void init() {
... add following two lines
ilInit(); //initialize DevIL library required
LoadImageDevIL ("your.bmp", &amp;logo);
}

void display() {
...
//add two line to bind texture
glEnable (GL_TEXTURE_2D);
glBindTexture ( GL_TEXTURE_2D, logo.genID);

// your previous drawing of quadric cylinder
glPushMatrix();
glTranslatef(0,0,-100);
glRotatef(gAngle,1.,1.,1.);
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();
...
}

RigoN
12-01-2009, 12:27 AM
someone forgot that's about it:


Texture coordinate generation can be turned on and o with
gluQuadricTexture:

void gluQuadricTexture( GLUquadricObj *quadobj,GLboolean textureCoords );

GLU spec 6.3 page 21

Ravikishore
12-01-2009, 03:59 AM
Hi,

Thank You for your initiation.

I made the changes,what you sent above, but it's giving the error as "logo" is undefined, but it's already declared. i am not getting that.. placing the entire code below and there is two init() functions so i commented the below init()function :


Code :


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

#include <IL/ilut.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

struct TextureHandle {

ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;

};
TextureHandle wmap, logo, frac;


ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{

ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);


ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);


glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}

void init() {

ilInit();
LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
}

void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;


gAngle += dt*360./8.;

glutPostRedisplay();
}


void display() {

glEnable (GL_TEXTURE_2D);
glBindTexture ( GL_TEXTURE_2D, logo.genID);


glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glTranslatef(0,0,-100);
glRotatef(gAngle,1.,1.,1.);

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();

glutSwapBuffers();
}

void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}
/*
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);
}
*/
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("FPS test /w glutTimerFunc");

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}


and one more is it necessary to place this line
void gluQuadricTexture( GLUquadricObj *quadobj,GLboolean textureCoords );
if means, where i should i place ?
Compile: gcc cc1.c -o cc1 -I/usr/x11R6/include/ -L/usr/lib -lX11 -lXmu -lglut -lGL -lGLU

Is there any changes needed in above command, Guide me please.

i am thankful for any kind of help.

marshats
12-01-2009, 08:30 AM
You need to place gluQuadricTexture(IDquadric, GL_TRUE); after you called your gluNewQuadric. This is what gives you texture coordinates.

The compile command I use is for C++ (filename.cpp)


g++ main.cpp -lGL -lglut -lIL
but you can also use
gcc main.cpp -lGL -lglut -lIL

Now if you have a .c extension instead of .cpp, then the gcc command will assume Ansi-C. In which case you have to declare with a "struct" explicitly.


for .cpp: TextureHandle logo;
gcc main.cpp -lGL -lglut -lIL
for .c: struct TextureHandle logo;
gcc main.c -lGL -lglut -lIL

so your gcc compile command looks fine as is as long as you change to make the "logo" declaration ANSI-C instead of my ANSI-C++ as done in following code ...

Here's the code with the "struct" added for filename.c (ie c version)


// on linux with gcc: g++ main.c -lGL -lglut -lIL
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include <IL/ilut.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

struct TextureHandle
{

ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;

};

struct TextureHandle logo;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{

ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void display()
{

glEnable (GL_TEXTURE_2D);
glBindTexture ( GL_TEXTURE_2D, logo.genID);

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glTranslatef(0,0,-100);
glRotatef(gAngle,1.,1.,1.);

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();

glutSwapBuffers();
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
//LoadImageDevIL ("Logo.bmp", &amp;logo);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("FPS test /w glutTimerFunc");

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

Ravikishore
12-01-2009, 10:11 PM
Hi,
Thank You very much for your reply marshats,

The above code is working fine without any error messages.
i just changed width and height to get good clarity.
i dint get what exactly below line indicates, why cant we use directly value for IDquadric :

GLUquadricObj *IDquadric;

and i changed the line as
gluCylinder (30, 5.0f, 5.0f, 10.0f, 24, 8);

This shows segmentation fault. This means memory related are what.

one more thing requesting you is how to place many different images on the same cylinder ?

Please guide me.

i am thankful for any kind of help.

marshats
12-01-2009, 11:37 PM
This is a C question, whenever you see an argument of the form "type * X" then it expects an address, not the value, to be passed into the function ... see glCylinder definition (http://pyopengl.sourceforge.net/documentation/manual/gluCylinder.3G.html). Notice how the first argument it expects is a pointer to a GLUquadric. And reading further this pointer is defined by a very specific function, gluNewQuadric(). So you can't use the value of "30" for the first argument of your gluCylinder (30, 5.0f, 5.0f, 10.0f, 24, 8) -- the function is looking for the address not the value 30, this explains why you saw a segmentation fault.

The question about multiple textures on a single cylinder is more involved question. Do you have to do multiple textures or simply make a single texture in some drawing program that has the effect of multiple decals? The second approach easily maps to the code you already have. But if you have to do the first approach -- A quick google search lead to multitexturing tutorial (http://www.clockworkcoders.com/oglsl/tutorial8.htm) that may help you get started. Also see Post90211 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=90211#Post90211 ).

I believe with this you will have to start using the openGL extensions -- you can get setup to use them with the GLEW library. (http://glew.sourceforge.net/) On Ubuntu you can get it installed with a simple "sudo apt-get install libglew1.5-dev". To use it read Glew basic usage. (http://glew.sourceforge.net/basic.html)
Here's code to simply add glew functionality (NOT multitexturing) -- a single include is added with a call to glewInit(). And note the compile command is


gcc main.c -lGL -lglut -lIL -lGLEW



// on linux with gcc: gcc main.c -lGL -lglut -lIL -lGLEW
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include <IL/ilut.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

struct TextureHandle
{

ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;

};

struct TextureHandle logo;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{

ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void display()
{

glEnable (GL_TEXTURE_2D);
glBindTexture ( GL_TEXTURE_2D, logo.genID);

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0,0,-100);
glRotatef(gAngle,1.,1.,1.);

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();

glutSwapBuffers();
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
//LoadImageDevIL ("Logo.bmp", &amp;logo);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("FPS test /w glutTimerFunc");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

The changes of adding GLEW are just to help you have access to the commands like "glActiveTexture and glMultiTexCoord2f" that you will need for multi-texturing.

Ravikishore
12-02-2009, 12:31 AM
Hi,

i got the idea about segmentation fault. Thank you very much.

Regarding multitexturing, the code which is in above link is in shading language is it same for C. Sorry for asking simple question.

Guide me please.

marshats
12-02-2009, 09:09 AM
Here's a modified code example from a book well worth getting "OpenGL Programming Guide Fifth Edition, by Dave Shreiner, Mason Woo, Jackie Neider, Tom Davis" -- Chapter 9 has multitex.c (http://www.cosc.brocku.ca/Offerings/3P98/course/OpenGL/RedBookExamples/multitex.c). It shows how to do multitexturing in c (but on a QUAD instead of a cylinder). The images themselves are quite indistinct so the example isn't the best but it does show two images -- look at the fullscreen window when running. That will improve when you use your own images that aren't so dark.



/* multitex.c
from http://www.cosc.brocku.ca/Offerings/3P98/course/OpenGL/RedBookExamples/multitex.c
slightly modified to use GLEW for extensions

// on linux with gcc: gcc main.c -lGL -lglut -lIL -lGLEW
*/
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

static GLubyte texels0[32][32][4];
static GLubyte texels1[16][16][4];

void makeCheckImages(void)
{
int i, j;

for (i = 0; i < 32; i++) {
for (j = 0; j < 32; j++) {
texels0[i][j][0] = (GLubyte) i;
texels0[i][j][1] = (GLubyte) j;
texels0[i][j][2] = (GLubyte) (i*j)/255;
texels0[i][j][3] = (GLubyte) 255;
}
}

for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
texels1[i][j][0] = (GLubyte) 255;
texels1[i][j][1] = (GLubyte) i;
texels1[i][j][2] = (GLubyte) j;
texels1[i][j][3] = (GLubyte) 255;
}
}
}

void init(void)
{
GLuint texNames[2];

glClearColor (0.0, 0.0, 0.5, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

makeCheckImages();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(2, texNames);
glBindTexture(GL_TEXTURE_2D, texNames[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA,
GL_UNSIGNED_BYTE, texels0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glBindTexture(GL_TEXTURE_2D, texNames[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
GL_UNSIGNED_BYTE, texels1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
/* Use the two texture objects to define two texture units
* for use in multitexturing */
glActiveTexture (GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texNames[0]);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glMatrixMode (GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.5f, 0.5f, 0.0f);
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(-0.5f, -0.5f, 0.0f);
glMatrixMode (GL_MODELVIEW);
glActiveTexture (GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texNames[1]);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glMultiTexCoord2f (GL_TEXTURE0, 0.0, 0.0);
glMultiTexCoord2f (GL_TEXTURE1, 1.0, 0.0);
glVertex2f(0.0, 0.0);
glMultiTexCoord2f (GL_TEXTURE0, 0.5, 1.0);
glMultiTexCoord2f (GL_TEXTURE1, 0.5, 0.0);
glVertex2f(50.0, 100.0);
glMultiTexCoord2f (GL_TEXTURE0, 1.0, 0.0);
glMultiTexCoord2f (GL_TEXTURE1, 1.0, 1.0);
glVertex2f(100.0, 0.0);
glEnd();
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D(0.0, 100.0, 0.0, 100.0 * (GLdouble)h/(GLdouble)w);
else
gluOrtho2D(0.0, 100.0 * (GLdouble)w/(GLdouble)h, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
glewInit();
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}

marshats
12-02-2009, 08:57 PM
I realized my previous post could use the older blending capabilities instead ... see chapter06 in Redbook. (http://www.glprogramming.com/red/chapter06.html)

Heres some alternative code that reads two textures (Logo.bmp and Logo2.bmp) and combines them in two passes. The changes mostly occure in the Display() function.


// on linux with gcc: gcc main.c -lGL -lglut -lIL -lGLEW
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include <IL/ilut.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;

};

struct TextureHandle logo;
struct TextureHandle logo2;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,0.,0.);
glRotatef(90,0.,1.,0.);
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

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

glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
draw_cylinder();

glutSwapBuffers();
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
//LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
LoadImageDevIL ("Logo.bmp", &amp;logo);
LoadImageDevIL ("Logo2.gif", &amp;logo2);

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

Ravikishore
12-03-2009, 04:29 AM
Hi Thank you very much.

How to do multitexturing ?

i am using the code below :

AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;

if (!Filename)
{
return NULL;
}

File=fopen(Filename,"r");

if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}

return NULL;
}


int LoadGLTextures()
{
int Status=FALSE;

AUX_RGBImageRec *TextureImage[2];

memset(TextureImage,0,sizeof(void *)*2);

if ((TextureImage[0]=LoadBMP("Data/BG.bmp")) &amp;&amp;
(TextureImage[1]=LoadBMP("Data/Reflect.bmp")))
{
Status=TRUE;

glGenTextures(6, &amp;texture[0]);

for (int loop=0; loop<=1; loop++)
{

glBindTexture(GL_TEXTURE_2D, texture[loop]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);


glBindTexture(GL_TEXTURE_2D, texture[loop+2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);


glBindTexture(GL_TEXTURE_2D, texture[loop+4]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY,
GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
}
for (loop=0; loop<=1; loop++)
{
if (TextureImage[loop])
{
if (TextureImage[loop]->data)
{
free(TextureImage[loop]->data);
}
free(TextureImage[loop]);
}
}
}

return Status;
}



bool load_rgb_image(const char* file_name, int w, int h, RGBIMG* refimg)
{
GLuint sz;
FILE* file;
long fsize;
GLubyte* p;


refimg->w = (GLuint) w;
refimg->h = (GLuint) h;
sz = (((3*refimg->w+3)>>2)<<2)*refimg->h;
refimg->data = new GLubyte [sz];
if (refimg->data == NULL) return false;


file = fopen(file_name , "rb");
if (!file) return false;
fseek(file, 0L, SEEK_END);
fsize = ftell(file);
if (fsize != (long)sz) {
fclose(file);
return false;
}
fseek(file, 0L, SEEK_SET);
p = refimg->data;
while (fsize > 0) {
fread(p, 1, 1, file);
p++;
fsize--;
}
fclose(file);
return true;
}


bool setup_textures()
{
RGBIMG img;


glGenTextures(TEXTURES_NUM, g_texid);

if (!load_rgb_image("wall_64x64.raw", 64, 64, &amp;img)) return false;

glBindTexture(GL_TEXTURE_2D, g_texid[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, img.w, img.h, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data);

glBindTexture(GL_TEXTURE_2D, g_texid[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, img.w, img.h, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data);

glBindTexture(GL_TEXTURE_2D, g_texid[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img.w, img.h, GL_RGB, GL_UNSIGNED_BYTE, img.data);

delete img.data;
return true;
}


bool init(void)
{
if (!LoadGLTextures())
{
return FALSE;
}

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glLightfv(GL_LIGHT1, GL_AMBIENT, g_lightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, g_lightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,g_lightPosition);
glEnable(GL_LIGHT1);

g_quadratic = gluNewQuadric();
if (g_quadratic == 0) return false;
gluQuadricNormals(g_quadratic, GLU_SMOOTH);
gluQuadricTexture(g_quadratic, GL_TRUE);

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
return true;
}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
return TRUE; // Keep Going
}


void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0.0f,0.0f,g_z);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

glBindTexture(GL_TEXTURE_2D, texture[g_filter+(g_filter+1)]);
glPushMatrix();
glRotatef(g_xrot,1.0f,0.0f,0.0f);
glRotatef(g_yrot,0.0f,1.0f,0.0f);
switch(g_object)
{
case 0:
glDrawCube();
break;
case 1:
glTranslatef(0.0f,0.0f,-1.5f);
gluCylinder(g_quadratic,1.0f,1.0f,3.0f,32,32);
break;
case 2:
gluSphere(g_quadratic,1.3f,32,32);
break;
case 3:
glTranslatef(0.0f,0.0f,-1.5f);
gluCylinder(g_quadratic,1.0f,0.0f,3.0f,32,32);
break;
};

glPopMatrix();
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);

glBindTexture(GL_TEXTURE_2D, texture[g_filter*2]);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -24.0f);
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-13.3f, -10.0f, 10.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 13.3f, -10.0f, 10.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 13.3f, 10.0f, 10.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-13.3f, 10.0f, 10.0f);
glEnd();

glPopMatrix();

g_xrot+=g_xspeed;
g_yrot+=g_yspeed;



glutSwapBuffers ( );
}




The above part of code i used for multitexturing, but it's not working.

please i am new to openGL, help me out from this.

i am thankful for any kind of help.

Ravikishore
12-03-2009, 06:08 AM
Hi,


Yes as you told, it's displaying rectangle like object not a perfect Cylinder and two images on that are not so clear, they are compressed and shuffled..

Please help me from this.

i am thankful for Help.

marshats
12-03-2009, 10:23 AM
What code are you referring exactly? I have lost track. Also did you see the Post268130 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=268130#Post2681 30) and compile and run it -- that I think is a better example of multitexturing.

I have made a point to not use GLaux since it is over 15 years old, buggy, leaks memory, and deprecated -- see the section on "External Links:Image Loader"
Getting Started (http://www.opengl.org/wiki/Getting_started). I really can't help you debug that code with GLaux. I would suggest going back to using IL/il.h.

The GLaux code you posted is not a multitexturing example ie it does not apply two or more textures to the same geometric object. Instead it has one texture for each object and there are three objects which is not multitexturing. -- Maybe we have a semantics misunderstanding -- Do you want to apply two or more textures to the same object or simply have many objects each with their own texure decal?

As for why the image is compressed and distorted, understanding the Redbook Chapter 9 (http://www.glprogramming.com/red/chapter09.html) would be a better help there. But in essence it has to do with the specific numbers defined for S and T texture coordinates. Take a look at "Figure 9-6 : Texture-Map Distortion." This is one of the more difficult things to get straight when first learning texturing -- how the S and T coordinates and the original texture image correspond to one another visually.

Ravikishore
12-06-2009, 10:58 PM
Hi,
Thank you very much.

sorry for late reply. i am out.

I referred code which i placed above is from NeHe's production example, which uses GLaux library. ok leave, if it's old one.

I tried the code in Post268130 it's working without errors,This code generates a rectangle object not a cylinder and images placed on that are shuffled, no clear separation between those images. you are right i need to place more images on the single object.

please what to do Now ?

I am thankful for any kind of help.

marshats
12-07-2009, 07:03 PM
Here is a slightly modified code from post268130 -- to show it is a cylinder by changing axis of rotation and adding a global gState that toggles between 0 and 1 as you press a key.


// on linux with gcc: gcc main.c -lGL -lglut -lIL -lGLEW
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include <IL/ilut.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

GLuint gState = 0;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;

};

struct TextureHandle logo;
struct TextureHandle logo2;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,0.,0.);
glRotatef(90,1.,1.,0.);
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

if (gState) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);

glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
draw_cylinder();

glDisable(GL_BLEND);
} else {
glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();
}

glutSwapBuffers();
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
//LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
LoadImageDevIL ("Logo.bmp", &amp;logo);
LoadImageDevIL ("Logo2.gif", &amp;logo2);

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
gState++;
gState %= 2;
printf("%d\n",gState);
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

Supply any two textures that will be applied multi-textured onto a single cylinder (lines 155 and 156), compile and run this code.

Notice what happens when you press the spacebar key -- this will toggle between single texture and multi-texturing. Then look at the code that is relevant (see if-gState in Display). That's all there is to multi-texturing using blending. To understand this read the chapters in the Redbook that I referred you to in the previous posts.

Ravikishore
12-07-2009, 10:54 PM
Hi,

Thank You so much for your valuable answer.

The above code result's into cylinder,But while rotating, the back part of the cylinder is invisible.

i mean it shows semi-cylinder rotation. i gave two-textures on line 155 & 156. i changed the rotation parameters,but not displaying complete cylinder.

one more is i saw changes from single texture to multiple-texture
by pressing any key.

please help me out from above problem..

i am thankful for your help.

marshats
12-08-2009, 10:34 AM
Actually the code is showing a complete cylinder but culling the back faces -- to "fix" this I have added code to explicitly draw the top and bottom of the cylinder (see draw_cylinder() and the newly added gluDisk calls). Have you read Chapters 6 and 9 in the Redbook?


// on linux with gcc: gcc main.c -lGL -lglut -lIL -lGLEW
// on win+mingw: gcc main8.c -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -lglew32 -lIL -L/usr/local/lib -I/usr/local/include
//
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include <IL/il.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

GLuint gState = 0;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;
};

struct TextureHandle logo;
struct TextureHandle logo2;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,1.,0.);

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);

glPushMatrix(); // top of cylinder
glTranslatef(0.,0.,10.);
gluDisk(IDquadric,0,10,32,32);
glPopMatrix();

glPushMatrix(); // bottom of cylinder
glFrontFace(GL_CW);
gluDisk(IDquadric,0,10,32,32);
glFrontFace(GL_CCW);
glPopMatrix();

glPopMatrix();
}

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

glMatrixMode(GL_MODELVIEW);

if (gState) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);

glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
draw_cylinder();

glDisable(GL_BLEND);
} else {
glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();
}

glutSwapBuffers();
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
//LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
LoadImageDevIL ("Logo.bmp", &amp;logo);
LoadImageDevIL ("Logo2.bmp", &amp;logo2);

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
gState++;
gState %= 2;
printf("%d\n",gState);
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

Ravikishore
12-09-2009, 03:39 AM
Hi,

thank You So much for replying.

The above code is generating complete cylinder with top face.
Is there any function to display complete cylinder without top face. if i comment gluDisk()function it is not showing complete cylinder.

Yes as you specified in the previous post #268169, i studied Chapter-9. one more is as a fresher it is getting quite difficult for understanding. i will pickup shortly..

Please help me..

i am thankful for your help

marshats
12-10-2009, 08:26 PM
The problem is that glBlend allows backside polygons to be seen so to stop the problem of seeing a ghost image of the back surface thru the cylinder backface culling had to be enabled -- causing you to see half the cylinder. One way around that is to generate the mixed texture on a flat rectangle FIRST then copy that texture to be mapped on the cylinder later.

Step 1 generate mixed texture logoM (see new function CreateMultiTexture())

Step 2 use logoM without blending mapped onto the cylinder



// on linux with gcc: gcc main.c -lGL -lglut -lIL -lGLEW
// on win+mingw: gcc main8.c -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -lglew32 -lIL -L/usr/local/lib -I/usr/local/include
//
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include <IL/il.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

GLuint gState = 0;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;
};

struct TextureHandle logo;
struct TextureHandle logo2;
struct TextureHandle logoM;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,1.,0.);
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();
}

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

glMatrixMode(GL_MODELVIEW);

if (gState) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
//glBlendFunc (GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
glBlendColor(.5,.5,.5,.5); // set blend constants to 0.5

glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
draw_cylinder();

glDisable(GL_BLEND);
} else {
glBindTexture ( GL_TEXTURE_2D, logoM.genID);
draw_cylinder();
}

glutSwapBuffers();
}

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

// store values to return to when done
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

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

// Temporarily set Viewport to Match Texture Size
glViewport(0,0,logoM.w,logoM.h);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
//glBlendFunc (GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
glBlendColor(.5,.5,.5,.5);

glBindTexture ( GL_TEXTURE_2D, logo.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();

glBindTexture ( GL_TEXTURE_2D, logo2.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);

glBindTexture(GL_TEXTURE_2D,logoM.genID); // Bind To The Mixed Texture

// Copy mixed texture to logoM
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, logoM.w, logoM.h, 0);
// Finally, logoM is now 0.5*logo + 0.5*logo2

//return to state before generating mixed texture
glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

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


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

//glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
//LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
LoadImageDevIL ("logo.jpg", &amp;logo);
LoadImageDevIL ("logo.jpg", &amp;logoM); // make room for Multi-texture
LoadImageDevIL ("logo2.jpg", &amp;logo2);

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
gState++;
gState %= 2;
printf("%d\n",gState);
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(CreateMultiTexture);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

Ravikishore
12-14-2009, 04:05 AM
Hi,

Thank you so much, This is one of the good idea for creating Multiple-texture using CreateMultiTexture() function.

The output of the above program is not clear separation of images, i think this is because of using the below function :

glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, logoM.w, logoM.h, 0);

one more is here it is placing both the images i,e logo & logo2 in logoM. i think because of this the output is not clear.

My question is :
Is it possible to divide the cylinder area into Rectangles, and placing images on that Rectangular places ?

I think this will be easy.

How to do this please ?

Please guide me.


I am thankful for any kind of reply .

marshats
12-14-2009, 08:40 PM
glCopyTexImage2D is what actually copies the final pixels to the new texture ... It is actually the blend operations that are the culprit. Try compiling the code with the glutDisplayFunc(display) commented out


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

with that done now you can see what CreateMultiTexture() is really doing. Study the effect and so you should now see hoy one can put multiple textures at _different_ locations as follows (ie remove the BLEND functions and set the glTexCoord2f's)


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

// store values to return to when done
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

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

// Temporarily set Viewport to Match Texture Size
glViewport(0,0,logoM.w,logoM.h);

glBindTexture ( GL_TEXTURE_2D, logo.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-1);
glTexCoord2f(1,0); glVertex2f( 0,-1);
glTexCoord2f(1,1); glVertex2f( 0, 0);
glTexCoord2f(0,1); glVertex2f(-1, 0);
glEnd();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-0,-0);
glTexCoord2f(1,0); glVertex2f( 1,-0);
glTexCoord2f(1,1); glVertex2f( 1, 1);
glTexCoord2f(0,1); glVertex2f(-0, 1);
glEnd();

glBindTexture(GL_TEXTURE_2D,logoM.genID); // Bind To The Mixed Texture

// Copy mixed texture to logoM
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, logoM.w, logoM.h, 0);
// Finally, logoM is now 0.5*logo + 0.5*logo2

//return to state before generating mixed texture
glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

glutDisplayFunc(display); // now that you have mixed texture, do real display
}
with this approach you can tile images but they cannot overlap.

Ravikishore
12-14-2009, 10:22 PM
Hi,

Thank you so much for replying.

I changed the code as you told in the above post :


CODE :

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

#include <IL/il.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

GLuint gState = 0;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;
};

struct TextureHandle logo;
struct TextureHandle logo2;
struct TextureHandle logoM;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,1.,0.);
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();
}

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

glMatrixMode(GL_MODELVIEW);
if (gState) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);

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

glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
draw_cylinder();

glDisable(GL_BLEND);
} else {
glBindTexture ( GL_TEXTURE_2D, logoM.genID);
draw_cylinder();
}

glutSwapBuffers();
}

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


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

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

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


glViewport(0,0,logoM.w,logoM.h);

glBindTexture ( GL_TEXTURE_2D, logo.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-1);
glTexCoord2f(1,0); glVertex2f( 0,-1);
glTexCoord2f(1,1); glVertex2f( 0, 0);
glTexCoord2f(0,1); glVertex2f(-1, 0);
glEnd();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-0,-0);
glTexCoord2f(1,0); glVertex2f( 1,-0);
glTexCoord2f(1,1); glVertex2f( 1, 1);
glTexCoord2f(0,1); glVertex2f(-0, 1);
glEnd();

glBindTexture(GL_TEXTURE_2D,logoM.genID);


glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, logoM.w, logoM.h, 0);



glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

glutDisplayFunc(display);
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}

void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);


glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();

LoadImageDevIL ("/localhome/user/Temp/sample/rro1.jpg", &amp;logo);
LoadImageDevIL ("/localhome/user/Temp/sample/bird.jpg", &amp;logoM);
LoadImageDevIL ("/localhome/user/Temp/sample/out.jpg", &amp;logo2);

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
gState++;
gState %= 2;
printf("%d\n",gState);
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(CreateMultiTexture);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}


if i made a comment on below line :

glutDisplayFunc(display)

it is displaying only two images i,e rro1.jpg & out.jpg,These two images are in diagonal axis, but the image bird.jpg is not displaying.

if i remove the comment on above line i am getting the cylinder with only two images and those are also not so clear images.

Is it necessary to Resize the images to visible clearly on the cylinder surface.


Please Help me.

I am thankful for your help.

marshats
12-14-2009, 11:29 PM
if i made a comment on below line :

glutDisplayFunc(display)

it is displaying only two images i,e rro1.jpg & out.jpg,These two images are in diagonal axis, but the image bird.jpg is not displaying.

That is correct -- what you are seeing is the logoM texture that you created by adding the two logo + logo2 textures. The position of those textures (diagonals) is set by the four call pairs "glTexCoord2f(*); glVertex2f(*);" Try plotting those points on paper and see how they correspond to the diagonals.



if i remove the comment on above line i am getting the cylinder with only two images and those are also not so clear images.

You misunderstand what CreateMultiTexture() is doing -- it is OVEWRITING the texture logoM whatever you used in the line "LoadImageDevIL ("/localhome/user/Temp/sample/bird.jpg", &amp;logoM);" This line is a trick to get a block of memory for writing to with a "good" pixel size. Just create a blank image file with a size; width=width of logo + width of logo2 and height = height of logo + height of logo2. Notice that by calling "glutDisplayFunc(display);" its then only when the multitextured logoM is applied to the cylinder!



Is it necessary to Resize the images to visible clearly on the cylinder surface.

glViewport(0,0,logoM.w,logoM.h);
is setting the size of the viewport in pixels to be match the "blank" image you first loaded "LoadImageDevIL ("/localhome/user/Temp/sample/bird.jpg", &amp;logoM);" -- the idea is to make a blank image with a reasonable large size. A better name is "blank.jpg" instead of "bird.jpg". If you want a third image on the cylinder, add a third texture to the drawing like


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


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

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

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


glViewport(0,0,logoM.w,logoM.h);

//lower left
glBindTexture ( GL_TEXTURE_2D, logo.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-1);
glTexCoord2f(1,0); glVertex2f( 0,-1);
glTexCoord2f(1,1); glVertex2f( 0, 0);
glTexCoord2f(0,1); glVertex2f(-1, 0);
glEnd();

//upper right
glBindTexture ( GL_TEXTURE_2D, logo2.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-0,-0);
glTexCoord2f(1,0); glVertex2f( 1,-0);
glTexCoord2f(1,1); glVertex2f( 1, 1);
glTexCoord2f(0,1); glVertex2f(-0, 1);
glEnd();

//upper left
glBindTexture ( GL_TEXTURE_2D, logo3.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-0);
glTexCoord2f(1,0); glVertex2f( 0,-0);
glTexCoord2f(1,1); glVertex2f( 0, 1);
glTexCoord2f(0,1); glVertex2f(-1, 1);
glEnd();

glBindTexture(GL_TEXTURE_2D,logoM.genID);


glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, logoM.w, logoM.h, 0);



glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

glutDisplayFunc(display);
}

Ravikishore
12-15-2009, 04:20 AM
Hi,

I am really happy to see 4 images on cylinder with little bit good clarity.You Helped me a lot, Thank you so much.

I placed one more image by placing lower right coordinates as below :

//lower Right

glBindTexture ( GL_TEXTURE_2D, logo4.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-0,-0);
glTexCoord2f(1,0); glVertex2f( 1,0);
glTexCoord2f(1,1); glVertex2f( 1,-1);
glTexCoord2f(0,1); glVertex2f(0,-1);
glEnd();

so total 4 different images can placed on the cylinder using above code.

Is it possible to place more than 4 images ?

and one more is color of images are quite poor.

Now we are placing images one below the other.
Is it possible to place images one after the other on the cylinder ?

i think it is possible to place images one after the other only for two images with coordinates which you gave in post 268610

i need to place more images one after the other.

Please guide me.

I am thankful for your reply.

marshats
12-15-2009, 09:18 PM
Thats great you added a fourth image.

Yes, you can have as many images as you like -- try changing the X and Y values for the the glVertex2f(X,Y);. For each image you want you add a block of


glBindTexture ( GL_TEXTURE_2D, logoN.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(X1,Y1);
glTexCoord2f(1,0); glVertex2f(X2,Y2);
glTexCoord2f(1,1); glVertex2f(X3,Y3);
glTexCoord2f(0,1); glVertex2f(X4,Y4);
glEnd();

And set X,Y pairs to position and size it within a square with origin at 0,0 and the upper corner at 1,1.

Can you post an image somehow of the poor colors you are seeing?



Now we are placing images one below the other.
Is it possible to place images one after the other on the cylinder ?

YOu have control again just set the X,Y values as you please in each glVertex* call.

Ravikishore
12-15-2009, 10:39 PM
Hi,
Thank you so much for helping me,

Is it possible to give values for (X1,Y1)(X2,Y2)(X3,Y3)(X4,Y4) as (1,1)(-1,1)(-1,-1)(1,-1) for first image and (2,1)(1,1)(1,-1)(2,-1)for second image placing right to first image using below code which you gave :


glBindTexture ( GL_TEXTURE_2D, logoN.genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(X1,Y1);
glTexCoord2f(1,0); glVertex2f(X2,Y2);
glTexCoord2f(1,1); glVertex2f(X3,Y3);
glTexCoord2f(0,1); glVertex2f(X4,Y4);
glEnd();

How to fix the center-point on the cylinder ?

How it possible to post images on the forum, i dont know Please help me, i will show you the output .

marshats
12-16-2009, 07:03 PM
About adding figures to this forum see upper right corner of this page which is FAQ (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=faq). About the tenth item down labelled "How do I add an image to my message?" is the one to read. While there you may want to also read "What UBBCode can I use in my posts?" especially the part for inserting "code".



Is it possible to give values for (X1,Y1)(X2,Y2)(X3,Y3)(X4,Y4) as (1,1)(-1,1)(-1,-1)(1,-1) for first image and (2,1)(1,1)(1,-1)(2,-1)for second image placing right to first image using below code which you gave :


Yes, but you have to use the MODELVIEW matrix ie add a "glOrtho(-2,2,-2,2, -1,1 )" in CreateMultiTexture()



void CreateMultiTexture()
{
...
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glOrtho(-2,2,-2,2, -1,1 );
...




How to fix the center-point on the cylinder ?

This one I am not certain the best way to do this. One option is to use TEXTURE MATRIX stack when its time to draw your cylinder


void draw_cylinder()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,1.,0.);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(ds,dt,0); // you need to set dt,ds to move the texture origin

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

This is one question that someone else may have a definitive answer to -- think of my answer above as a hint! Read up on TEXTURE matrix in Redbook or possibly start another thread post to get others opinions on moving a texture on a gluQuadric geometry. Here's an example of a NON-rotating cylinder but the origin of the texture is being moved with each new frame (ds is changing)



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

#include <IL/il.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

GLuint gState = 0;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;
};

struct TextureHandle logo;
struct TextureHandle logo2;
struct TextureHandle logoM;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(45,1.,1.,0.);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.,gAngle/100.,0); // you need to set dt,ds to move the texture origin

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

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

glMatrixMode(GL_MODELVIEW);

if (gState) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
//glBlendFunc (GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
glBlendColor(.5,.5,.5,.5); // set blend constants to 0.5

glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cylinder();

glBindTexture ( GL_TEXTURE_2D, logo2.genID);
draw_cylinder();

glDisable(GL_BLEND);
} else {
glBindTexture ( GL_TEXTURE_2D, logoM.genID);
draw_cylinder();
}

glutSwapBuffers();
}

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

// store values to return to when done
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

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


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

// Temporarily set Viewport to Match Texture Size
glViewport(0,0,logoM.w,logoM.h);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
//glBlendFunc (GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
glBlendColor(.5,.5,.5,.5);

glBindTexture ( GL_TEXTURE_2D, logo.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();

glBindTexture ( GL_TEXTURE_2D, logo2.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);

glBindTexture(GL_TEXTURE_2D,logoM.genID); // Bind To The Mixed Texture

// Copy mixed texture to logoM
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, logoM.w, logoM.h, 0);
// Finally, logoM is now 0.5*logo + 0.5*logo2

//return to state before generating mixed texture
glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

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


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

//glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
//LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
LoadImageDevIL ("logo.jpg", &amp;logo);
LoadImageDevIL ("logo.jpg", &amp;logoM); // make room for Multi-texture
LoadImageDevIL ("logo2.jpg", &amp;logo2);

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
gState++;
gState %= 2;
printf("%d\n",gState);
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(CreateMultiTexture);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}

Ravikishore
12-17-2009, 02:28 AM
Hi,

Thank you so much.i got the idea how to place images one after the other.

The above code is placing the images one after the other. But the thing is those two images are overlapping while displaying because of same parameter in function glVertex2f() for both logo & logo2 i think.

Different types of displaying images which you told :

1)Cylinder with images which are placed one below the other. Code is in post 268799.

2)we have seen cylinder with images which are placed one after the other. Code is in post 268893.

One more, Is it possible to place images side by side(Like pages in the book) on the Non-rotating Cylinder, but images should rotate to display next image which is side to currently displaying image ?




Please guide me.

i am thankful for your reply.

marshats
12-17-2009, 10:53 AM
The above code is placing the images one after the other. But the thing is those two images are overlapping while displaying because of same parameter in function glVertex2f() for both logo & logo2 i think.


Yes, you have to choose values/numbers that make sense to what you want in all the glVertex2f() calls

Points 1 an 2: You have a code now that shows you how to position as many textures as you like on the cylinder and shows you how to move the texture around on that cylinder. That is complete flexibility to do what you describe (place as many images on the cylinder as you like, place images in any order left or right - top-to-bottom).

In terms of pages in a book -- you can do this in openGL yes. But that is beyond the scope of this post. Once you understand this code above a new post would be a good place to start to understand a more 3D book like structure.

ps I will be out of the office for the next two weeks on vacation. You have a working code. Try to read through it and understand especially the GL_MODELVIEW and GL_TEXTURE matrix stack.

Ravikishore
12-18-2009, 05:04 AM
Hi,

Thank you very very much for your ideas. i got more information from you.

Happy Vacation.

Ravikishore
12-23-2009, 11:29 PM
Hi,

Thank you so much for your help.

i need to scale texture images which are there on the cylinder surface.i,e the image which is on the viewer side should be look like larger and the remaining images on the other side should be smaller in size compare to one which i am watching currently.

please reply after finishing your vacation. from now i am on leave for 10 days.

Happy Vacation.

i am thankful for your reply.

Ravikishore
01-04-2010, 07:03 AM
Hi,

I happy for completing vacation.. i pasted the above problem in advance forums, but i dint the replies . so please help me from this problem .


I am thankful for your reply.

marshats
01-04-2010, 10:20 PM
Happy New Year :)

The following code shows how to place a texture in a particular location in an MxN grid of textures ... study function "placeTexture" function starting at line 101.

Try commenting out different lines between 147 and 152 to see how to choose a particular grid location where the texture is located. Notice that the 0,1 slot is commented out initially hence nothing is drawn at the leftmost middle slot.

Once you are comfortable with placing an arbritrary texture anywhere in the grid then you can texture the cylinder by removing the comment on line 170 (glutDisplayFunc(display);)



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

#include <IL/il.h>

GLfloat gTexOffset = 0.0;
GLUquadricObj *IDquadric;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;
};

struct TextureHandle logo;
struct TextureHandle logo2;
struct TextureHandle logoM;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

//gTexOffset += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(45,1.,1.,0.);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
// you need to set dt,ds to move the texture origin
glTranslatef(0.,gTexOffset/100.,0);

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


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

glBindTexture ( GL_TEXTURE_2D, logoM.genID);
draw_cylinder();

glutSwapBuffers();
}


void placeTexture(GLuint M, GLuint N, GLuint i, GLuint j, GLuint genID)
{
// M is number of slots in horizonatal direction
// choose i = 0,1, ... M-1
// N is number of slots in vertical direction
// choose j = 0,1, ... N-1
// genID is texture ID

GLfloat X1 = (GLfloat) i / (GLfloat) M;
GLfloat X2 = X1 + (GLfloat) 1 / (GLfloat) M;
GLfloat Y1 = (GLfloat) j / (GLfloat) N;
GLfloat Y2 = Y1 + (GLfloat) 1 / (GLfloat) N;

glBindTexture ( GL_TEXTURE_2D, genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(X1,Y1);
glTexCoord2f(1,0); glVertex2f(X2,Y1);
glTexCoord2f(1,1); glVertex2f(X2,Y2);
glTexCoord2f(0,1); glVertex2f(X1,Y2);
glEnd();
}


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

// store values to return to when done
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

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

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

// Temporarily set Viewport to Match Texture Size
glViewport(0,0,logoM.w,logoM.h);

// setup grid of 2x3 textures
GLuint M = 2;
GLuint N = 3;
placeTexture(M,N, 0,0, logo.genID); // place texture in 0,0 slot
placeTexture(M,N, 1,0, logo2.genID); // place texture in 1,0 slot
//placeTexture(M,N, 0,1, logo.genID); // place texture in 0,1 slot
placeTexture(M,N, 1,1, logo2.genID); // place texture in 1,1 slot
placeTexture(M,N, 0,2, logo.genID); // place texture in 0,2 slot
placeTexture(M,N, 1,2, logo2.genID); // place texture in 1,2 slot

// Bind To The Mixed Texture
glBindTexture(GL_TEXTURE_2D,logoM.genID);

// Copy mixed texture to logoM
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, logoM.w, logoM.h, 0);
// Finally, logoM is now grid of placed textures!

//return to state before generating mixed texture
glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

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


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

//glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
//LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);
LoadImageDevIL ("logo.jpg", &amp;logo);
LoadImageDevIL ("logo2.jpg", &amp;logo2);

LoadImageDevIL ("logoM.jpg", &amp;logoM); // make room for Multi-texture grid

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(CreateMultiTexture);
glutKeyboardFunc(keyboard);

init();
glutReshapeWindow(logoM.w,logoM.h);

glutMainLoop();
return 0;
}

Ravikishore
01-05-2010, 06:10 AM
Hi,

Thank you so much for your reply.

I executed the above code by removing all the commands except the below :

//LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);


But the code showing the run time error as below :



user@lxdevenv:~/Desktop$ ./rect5
freeglut (./rect5): Unable to create direct context rendering for window 'Multipass texturing Demo'
This may hurt performance.
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 12 (X_ConfigureWindow)
Value in failed request: 0x0
Serial number of failed request: 38
Current serial number in output stream: 39
cleanupQuadric completed





if i made comment which you made in the above code. giving the same error but changing the numbers in the place of 38 and 39.

what will be the problem ?


I am thankful for your reply

marshats
01-05-2010, 07:08 AM
I suspect this error is due to the size of your texture. What are the exact pixel sizes of all the textures you are using?

I really need a way to debug with your actual textures -- Take a look at Post269192 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=269192#Post2691 92) to see how to use Dropbox (http://www.dropbox.com/pricing) to share files. Once you have your account and uploaded your textures tell me which links using the notation without the quotes "[CODE]"your url link"[/code"]" and I will be able to downlaod your images. See forum FAQ (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=faq) section "What UBBCode can I use in my posts?" to explain what you can format in these openGL forums.

Ravikishore
01-05-2010, 11:31 PM
Hi,

Thank you so much for your reply.

I am sending the link where i uploaded my textures as below :

http://img40.imageshack.us/gal.php?g=aptp.jpg

I am thankful for your reply ..

marshats
01-06-2010, 07:39 AM
When I download from your link I get the files


aptp.jpg logo2sk.jpg logofni.jpg logomn.jpg


Why are the names so different from "logo.jpg, logo2.jpg, logoM.jpg" that the C-code references? Also you mentiojned that you had a problem with a "LoadImageDevIL ("/localhome/user/Temp/sample/bird.bmp", &amp;logo);" in a previous post -- where is the "bird.bmp" file for download? Do you mean for me to use "aptp.jpg" instead of "bird.bmp"?

do the pixel dimensions make sense for the following downloaded files?


> identify ravikishore/*
ravikishore/aptp.jpg JPEG 154x100 154x100+0+0 8-bit DirectClass 2.26kb
ravikishore/logo2sk.jpg[1] JPEG 154x100 154x100+0+0 8-bit DirectClass 2.3kb
ravikishore/logofni.jpg[2] JPEG 150x100 150x100+0+0 8-bit DirectClass 2.23kb
ravikishore/logomn.jpg[3] JPEG 400x500 400x500+0+0 8-bit DirectClass 3.76kb

Ravikishore
01-06-2010, 09:58 PM
Hi,

Thank you for replying.

I uploaded the images as logo.jpg, logo2.jpg, logoM.jpg , but after uploading to imageShack, the names of the files are getting changed while downloading. i checked this by once again uploading the images and checked by downloading them.

But the image names i included in the program and image names in my folder are same .

and all the dimensions in the above mentioned images are same, they dint get changed.

Yes your are right instead of bird.bmp use apt.jpg.

other then image names is there any other problem ?

I am thankful for your reply.

marshats
01-07-2010, 08:19 PM
I was able to replicate your error if the image file was not loaded ie some error like it being an invalid image file. The error your reported was not due to my original suspicion of the image file sizes.

Your images are reporting _incorrectly_ that they are RGB format but they are actually RGBA ... To fix this change the line in ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)



ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
to
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

Ravikishore
01-08-2010, 12:19 AM
Hi,

Thank you so much for the reply.

Now i am getting the cylinder(Like an arc) with image rotating on that.



In that arc like cylinder only logo.jpg is displaying and logo2.jpg is missing on the surface of the cylinder.

You are getting complete Cylinder with both images ?

I am thankful for your reply..

marshats
01-09-2010, 01:21 PM
Yes, all the images show up fine with the GL_RGBA change.

Can you post your code?

Ravikishore
01-10-2010, 09:07 PM
Hi,

Thanks for the replying.

The code which is executing in my system is posted below :



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

#include <IL/il.h>

GLfloat gTexOffset = 0.0;
GLUquadricObj *IDquadric;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;
};

struct TextureHandle logo;
struct TextureHandle logo2;
struct TextureHandle logoM;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;


ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);


T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gTexOffset += dt*360./8.;

glutPostRedisplay();
}


void draw_cylinder()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(45,1.,1.,0.);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();

glTranslatef(0.,gTexOffset/100.,0);

gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


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

glBindTexture ( GL_TEXTURE_2D, logoM.genID);
draw_cylinder();

glutSwapBuffers();
}




void placeTexture(GLuint M, GLuint N, GLuint i, GLuint j, GLuint genID)
{

GLfloat X1 = (GLfloat) i / (GLfloat) M;
GLfloat X2 = X1 + (GLfloat) 1 / (GLfloat) M;
GLfloat Y1 = (GLfloat) j / (GLfloat) N;
GLfloat Y2 = Y1 + (GLfloat) 1 / (GLfloat) N;

glBindTexture ( GL_TEXTURE_2D, genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(X1,Y1);
glTexCoord2f(1,0); glVertex2f(X2,Y1);
glTexCoord2f(1,1); glVertex2f(X2,Y2);
glTexCoord2f(0,1); glVertex2f(X1,Y2);
glEnd();
}


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


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

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

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


glViewport(0,0,logoM.w,logoM.h);


GLuint M = 2;
GLuint N = 3;
placeTexture(M,N, 0,0, logo.genID);
placeTexture(M,N, 1,0, logo2.genID);

placeTexture(M,N, 1,1, logo2.genID);
placeTexture(M,N, 0,2, logo.genID);
placeTexture(M,N, 1,2, logo2.genID);


glBindTexture(GL_TEXTURE_2D,logoM.genID);


glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, logoM.w, logoM.h, 0);


glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glutSwapBuffers();

glutDisplayFunc(display);
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();

LoadImageDevIL ("/localhome/user/Temp/sample/logo.jpg", &amp;logo);
LoadImageDevIL ("/localhome/user/Temp/sample/logo2.jpg", &amp;logo2);

LoadImageDevIL ("/localhome/user/Temp/sample/logoM.jpg", &amp;logoM);
glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(CreateMultiTexture);
glutKeyboardFunc(keyboard);

init();
glutReshapeWindow(logoM.w,logoM.h);

glutMainLoop();
return 0;
}



i am thankful for your reply.

pavanb2v
01-10-2010, 10:40 PM
hi i've a question....how do i draw a cylinder using basic polygons a not glucylinder...i should be able to colour my polygons on the surface of the cylinder as well...kindly send me the code...thankyou......

marshats
01-11-2010, 09:09 PM
I am at a loss to explain your problem. The code with your jpg's works fine on my machine.

What is your video card? what is your OS? what version of the libraries are you using: DevIL? freeglut? GL drivers?

marshats
01-11-2010, 09:21 PM
hi i've a question....how do i draw a cylinder using basic polygons a not glucylinder...i should be able to colour my polygons on the surface of the cylinder as well...kindly send me the code...thankyou......

This post is getting a little long and it is using gluQuadric -- it would probably be best if you start a new thread for your question. If you are trying to simply texture you could try looking at the Redbook (http://www.opengl.org/documentation/red_book/) especially Chapter 9 and the Sample Code (http://www.opengl.org/resources/code/samples/redbook/) -- checker.c is a good start. To turn that into a cylinder you would just have to change the " glTexCoord2f(...); glVertex3f(...);" in display().

Ravikishore
01-13-2010, 01:15 AM
Hi,

Thank You for replying.

I am currently working on virtual machine(Ubuntu) on my window system.
The version of VM is : 2.5.3 build-185404

and i installed Devil, GL libraries as you told the command
below:
sudo apt-get install libdevil-dev" command installation.

and i had a graphics controller : Intel(R)82845G/GL/GE/PE/GV Graphics controller. and my machine as 2GB RAM

is it possible to paste the output which is displaying on your machine ?
and could you send the versions of your Libraries ?

I am thankful for your replying.

marshats
01-13-2010, 09:22 PM
I have never been able to get full openGL support thru a VM. That is a likely reason for the problem. I am using the default versions of libraries with NON-VM Ubuntu 9.10 (reading from synaptic):



libIL ... 1.7.8-2
freeglut 2.4-0-6ubuntu1
libglew ... 1.5.1-4ubuntu1


This code (http://24.130.61.216/temporary/Ravikishore_step1.c) with your pics generates this picture:
http://24.130.61.216/temporary/Ravikishore_step1.png

This code (http://24.130.61.216/temporary/Ravikishore_step2.c) with your pics generates this picture:
http://24.130.61.216/temporary/Ravikishore_step2.png

Post the results of running glewinfo form the command line from within your VM/Ubuntu machine:


glewinfo > glewinfo.txt

This may help spot some reasons for the problem. For comparison, running this command on my machine yields the following glewinfo.txt (http://24.130.61.216/temporary/glewinfo.txt)

Ravikishore
01-14-2010, 11:44 PM
Hi,

Thanks for replying.

The first output code which you pasted above is giving the Runtime message as below :

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">
user@lxdevenv:~/Desktop$ ./forcode
freeglut (./forcode): Unable to create direct context rendering for window 'Multipass texturing Demo'
This may hurt performance.
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 12 (X_ConfigureWindow)
Value in failed request: 0x0
Serial number of failed request: 38
Current serial number in output stream: 39
cleanupQuadric completed
user@lxdevenv:~/Desktop$

[/QUOTE]</div>

and the second output is displayed on my System by making little changes in your code.

the above cylinder in output-2 should be placed like below figure :

http://img10.imageshack.us/i/untitu.png/

What to do for it ?

i am thankful for the reply.

marshats
01-19-2010, 07:09 PM
Can you please run "glewinfo" and post the result here to see what your capabilities are? I still suspect you are running into a virtual machine issue not a GL/GLUT/libIL problem.

Ravikishore
01-24-2010, 11:55 PM
Hi,

Sorry for Late reply and Thank you for replying.

The information of glew is pasted in the below link, Please check it.

http://www.2shared.com/file/10885944/1a7d81e7/infoglee.html

i am Thankful for your reply.

marshats
01-25-2010, 12:20 AM
The beginning of your link to glewinfo file was missing ... it started with


glGetInvariantIntegervEXT: OK
glGetLocalConstantBooleanvEXT: OK
glGetLocalConstantFloatvEXT: OK

I was looking for something more like


---------------------------
GLEW Extension Info
---------------------------

GLEW version 1.5.1
Reporting capabilities of display :0.0, visual 0x2b
Running on a GeForce 9600 GT/PCI/SSE2/3DNOW! from NVIDIA Corporation
OpenGL version 3.2.0 NVIDIA 195.17 is supported

GL_VERSION_1_1: OK
...

Ravikishore
01-27-2010, 03:47 AM
Hi,

I am pleased for replying.

I tried twice, using "glewinfo" command but it is displaying the same information which is there in previous post.

I am thankful for your reply.

marshats
01-27-2010, 07:23 PM
Try on the commandline the following


glewinfo | less

and


glewinfo > glewinfo.txt
gedit glewinfo.txt

The post the resultant file glewinfo.txt.

Ravikishore
01-27-2010, 10:18 PM
Hi,

Thank you So much for replying.
I uploaded the glewinfo.txt file. please download from the below link.

http://www.2shared.com/file/10949945/2900306d/glewinfo.html

What will be the problem ?

I am thankful for your reply.

marshats
01-28-2010, 09:53 PM
That is an older version of Mesa. I am at a loss as to explain what the problem is.

However, one last thing to try is related to the images themselves. In older versions of openGL there was a requirement that the images have sizes that are powers of 2 ie length or width is 2,4,8,16, ... 2^N. So I have resized your images to be 64x64. And in the code I added an "assert" to check if loadImage fails or not to give a more graceful failure if the image format has anything to do with the problem.

So download these images and try the code with added assert to check for image loading failure:
logo.jpg (http://24.130.61.216/temporary/openglPost271132/logo.jpg)
logo2.jpg (http://24.130.61.216/temporary/openglPost271132/logo2.jpg)
logoM.jpg (http://24.130.61.216/temporary/openglPost271132/logoM.jpg)
code (http://24.130.61.216/temporary/openglPost271132/R_rev2.c)

If this does not work I do not know what else to try. It runs fine on my machine -- NVIDIA GL drivers (not Mesa GL software drivers) and UBUNTU 9.10 with all latest updates (not in a virtual machine either).

Ravikishore
01-29-2010, 03:45 AM
Hi,

I am pleased for your replying.

I downloaded the images and executed the code which is there in above post.

The output i am getting is posted in below Link please check it.

http://img693.imageshack.us/i/rrriv.png/

Is this output your are expecting ?


I am Thankful for your reply.

marshats
02-02-2010, 09:22 AM
Yes, that is what you should see.

Now try changing those image files and see if the errors come back but DO NOT recompile the code, just copy new jpg files into the same folder. I suspect your images have to be even powers of 2.

Ravikishore
02-18-2010, 02:32 AM
Hi,

Sorry for Late reply.

Thank you very much for your replying.

I am trying to do multitexturing on the cylinder(which is made up of polygons)surface.
i am not able to place multiple images on it.
I am not getting what is went wrong ?

i am loading three images, but it is displaying the 1st image on the surface.

The code is Herercms.c (http://www.2shared.com/file/11474077/4cd1836a/rcms.html)

output is Here output (http://img246.imageshack.us/img246/7656/53259189.png)

Please Help me..

I am Thankful for your reply.

Ravikishore
02-19-2010, 11:49 PM
Hi,

please Help me Regarding Multi-Texturing.

I sent the code which i did for single image texture on the Cylinder surface.

I have to place more than one image on the Cylinder.SO please Help me from this.

I am Thankful for your reply please.