Thread: How to draw 3D rotating Cylinder

1. Re: MultiTexture a gluCylinder

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.

2. Re: MultiTexture a gluCylinder

Hi,

Happy Vacation.

3. Re: MultiTexture a gluCylinder

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.

Happy Vacation.

4. Re: MultiTexture a gluCylinder

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 .

5. Re: MultiTexture a gluCylinder

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)

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

#include <IL/il.h>

GLfloat gTexOffset = 0.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);
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);
// you need to set dt,ds to move the texture origin
glTranslatef(0.,gTexOffset/100.,0);

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);
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();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
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!

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 init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);

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

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);
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);
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();

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;
}```

6. Re: MultiTexture a gluCylinder

Hi,

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

But the code showing the run time error as below :

Code :
```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

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 ?

7. Re: MultiTexture a gluCylinder

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 to see how to use Dropbox 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 section "What UBBCode can I use in my posts?" to explain what you can format in these openGL forums.

8. Re: MultiTexture a gluCylinder

Hi,

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

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

9. Re: MultiTexture a gluCylinder

Code :
`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"?

Code :
```> 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```

10. Re: MultiTexture a gluCylinder

Hi,

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.