• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
krywen

Cylindrical Texture Mapping

1 post in this topic

Here is an example program of cylindrical texture mapping.
I was unable to find it on the web, so I've made my one.

OpenGL does not generate cylindrical mapping automatically;
you can create spherical mapping with
[source lang="cpp"]
glTexGend(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
[/source]
but not cylindrical, so we must do it by hand, coupling each vertex with corrisponding texture coordinate, using
[source lang="cpp"]glTexCoord2d(s, t); glVertex3d( x, y, z );[/source]


First think about parametrical rappresentation of a cylinder:

cylinder(u, v):
x = r * cos(u * 2*pi)
y = r * sin(u * 2*pi)
z = v
in our case u and v are the texture coordinates
u=s
v=t
so the idea is to iterate on texture coordinates, and generating corresponding vertices.
Suppose we want the cylinder to have 360 side faces; then we select 360 point in the texture (u coordinate) and calculate corresponding (x,y,z) vertex coordinate.
Coordinate v will be only 0.0 or 1.0;

In our reference system, Y is the height, so we modify formulas:
cylinder(u, v):
x = r * cos(u * 2*pi)
z = r * sin(u * 2*pi)
y = v

We use a GL_QUAD_STRIP fot the cylinder side.
Here we generate a cylinder with the height parallel at Y axis.
[CODE]
/* radius */
double r=5.0;
double height=4.0;
/* number of side faces */
int faces=360;
/* Choose neutral color (white)*/
glColor3d(1,1,1);
/* Enable 2D Texture*/
glEnable(GL_TEXTURE_2D);
/* set current working texture */
glBindTexture(GL_TEXTURE_2D, texture[0]);

/* Disabling these is not necessary in this example,
* BUT if you have previously enabled GL_TEXTURE_GEN_
* for other textures,then you need these lines
*/
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);

glBegin(GL_QUAD_STRIP);
double x, y, z;
y=height;
for (int i =0; i <= faces; i++) {
double u = i/ (double) faces;
x = r*cos(2*M_PI*u);
z = r*sin(2*M_PI*u);
/* Bottom vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, 0, z );
/* Top vertex*/
glTexCoord2d(u, 0.0); glVertex3d( x, y, z );
}
glEnd();
[/CODE]

[unverified]
Issue:
I have loaded images with SDL, but if you loaded the image with OpenGL probably you will have to modify code in this way:

[CODE]
glTexCoord2d(u, 0.0); glVertex3d( x, 0, z );
/* Top vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, y, z );
}
glEnd();
[/CODE]

Attachment is a working code using OpenGL, glut, and SDL.
The attached code alse let you rotate the view with mouse to verify the result.

I really hope my hours spent to create this example will be usefull to someone.

---

[b]Update 21 August 2012[/b] [b]- Face Orientation[/b]

The code above will create cylinder with faces where the front is toward the center of cylinder (this because the OpenGL default is glFrontFace(CCW)= faces have front orientation where its vertex are declared in counterclockwise order ). Keep an eye on this especially if you are using back face culling
[CODE]glEnable(GL_CULL_FACE); glCullFace(GL_BACK);[/CODE]

If you want your cylinder to have front face towards the extern, just reorder the vertexes declaration in this way:
[CODE]glBegin(GL_QUAD_STRIP);
double x, y, z;
y=height;
for (int i =0; i <= faces; i++) {
double u = i/ (double) faces;
x = r*cos(2*M_PI*u);
z = r*sin(2*M_PI*u);
/* Top vertex*/
glTexCoord2d(u, 0.0); glVertex3d( x, y, z );
/* Bottom vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, 0, z );
}[/CODE]

or, instead of doing thath, you can put glFrontFace(GL_CW); before glBegin
[CODE]
glFrontFace(GL_CW);
glBegin(GL_QUAD_STRIP);
double x, y, z;
y=height;
for (int i =0; i <= faces; i++) {
double u = i/ (double) faces;
x = r*cos(2*M_PI*u);
z = r*sin(2*M_PI*u);
/* Bottom vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, 0, z );
/* Top vertex*/
glTexCoord2d(u, 0.0); glVertex3d( x, y, z );
}[/CODE]
this second method have an issue: from now every polygon you will draw will have front face if vertex are declared CounterClockWise, which is not OpenGL default. Edited by krywen
0

Share this post


Link to post
Share on other sites
This is as my own cylinder texture mapping but i did't use GL_QUAD_STRIP, used GL_QUADS. This topic can help in need of someone.
Thanks.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0