PDA

View Full Version : How to make textures mapping to sphere..



shellee
10-21-2004, 01:49 AM
Hello all!
I want to make a soccer ball.
At first I made a sphere.
and how can I take textures mapping to OpenGL ES? you know I can't find glTexGeni.
please teach me~ :confused:

KristofBeets
10-22-2004, 12:52 PM
You'll have to generate a sphere with texture coordinates yourself. Check this page for some hints:

Sphere with Texture Coordinates Code (http://astronomy.swin.edu.au/~pbourke/opengl/sphere/)

Another option is to use a program like 3DStudio and an exporter. You can then use the advanced texture mapping functionality of the app and extract the texture coordinates and other info using the exporter. One option is to parse the standard 3DS format. There are plenty of tutorials about loading 3D data from 3DS files, just do a google.

Hope this helps,

K-

shellee
10-24-2004, 03:19 PM
Thanks~ Kristof - PowerVR
I'll try it. :)
If I can't understand it, I will be asked again.

shellee
10-25-2004, 07:17 PM
Kristof - PowerVR

you know,
I generated a sphere with texture coordinates.
but I can not textures mapping to '*.tga image file'
sorry, I'm new OpenGL ES programmer :p

[ October 25, 2004: Message edited by: James Kay ]

KristofBeets
10-25-2004, 09:55 PM
<BLOCKQUOTE><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><HR>Originally posted by James Kay:
Kristof - PowerVR

you know,
I generated a sphere with texture coordinates.
but I can not textures mapping to '*.tga image file'
sorry, I'm new OpenGL ES programmer :p

[ October 25, 2004: Message edited by: James Kay ]<HR></BLOCKQUOTE>

Do you mean you have trouble loading a TGA file and use it as a texture? There are quite a few tutorials with sample code on how to load TGA data into a texture. Have a look at:

Link to Tutorial (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=24)

Hope this helps,

K-

shellee
10-26-2004, 04:11 PM
hi! I tried it to textures mapping.
But it is failed... :(

Now, I will show you my simple source code.
what's the matter about source code?

---------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <GLES/gl.h>

typedef struct // Create A Structure
{
GLubyte *imageData; // Image Data (Up To 32 Bits)
GLuint bpp; // Image Color Depth In Bits Per Pixel.
GLuint width; // Image Width
GLuint height; // Image Height
GLuint texID; // Texture ID Used To Select A Texture
} TextureImage; // Structure Name

TextureImage textures[1];

#define PI 3.1415926535897f
#define DTOR (PI / 180.0f)
typedef unsigned int DWORD;

typedef struct {
float x, y, z;
DWORD color;
float u, v;
} VERTEX;

VERTEX *Vertices;

void GenerateSphere(float radius, int theta, int phi, float hTile, float vTile)
{
int dtheta = 5;
int dphi = 5;
int n = 0;
int i;
float vx, vy, vz;
float mag;
int NumVertices = (int)((360/dtheta)*(360/dphi)*4);

Vertices = malloc (NumVertices * sizeof(VERTEX));

if (Vertices == NULL)
{
puts("Memory allocation error.");
exit(1);
}

for (phi=0; phi <= 360 - dphi; phi += (int)dphi) {
for (theta=0; theta <= 360 - dtheta; theta += (int)dtheta) {
Vertices[n].x = radius * sin(phi*DTOR) * cos(DTOR*theta);
Vertices[n].y = radius * sin(phi*DTOR) * sin(DTOR*theta);
Vertices[n].z = radius * cos(phi*DTOR);
n++;
Vertices[n].x = radius * sin((phi+dphi)*DTOR) * cos(theta*DTOR);
Vertices[n].y = radius * sin((phi+dphi)*DTOR) * sin(theta*DTOR);
Vertices[n].z = radius * cos((phi+dphi)*DTOR);
n++;
Vertices[n].x = radius * sin(DTOR*phi) * cos(DTOR*(theta+dtheta));
Vertices[n].y = radius * sin(DTOR*phi) * sin(DTOR*(theta+dtheta));
Vertices[n].z = radius * cos(DTOR*phi);
n++;
if (phi > 0 && phi < 360) {
Vertices[n].x = radius * sin((phi+dphi)*DTOR) * cos(DTOR*(theta+dtheta));
Vertices[n].y = radius * sin((phi+dphi)*DTOR) * sin(DTOR*(theta+dtheta));
Vertices[n].z = radius * cos((phi+dphi)*DTOR);
n++;
}
}
}
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt(sqrt(vx)+sqrt(vy)+sqrt(vz));

vx /= mag;
vy /= mag;
vz /= mag;

Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asin(vy) / PI) + 0.5f;

for (i=0; i < NumVertices-2; i++)
{
if (Vertices[i].u - Vertices[i+1].u > 0.9f)
Vertices[i+1].u += 1.0f;
if (Vertices[i+1].u - Vertices[i].u > 0.9f)
Vertices[i].u += 1.0f;
if (Vertices[i].u - Vertices[i+2].u > 0.9f)
Vertices[i+2].u += 1.0f;
if (Vertices[i+2].u - Vertices[i].u > 0.9f)
Vertices[i].u += 1.0f;
if (Vertices[i+1].u - Vertices[i+2].u > 0.9f)
Vertices[i+2].u += 1.0f;
if (Vertices[i+2].u - Vertices[i+1].u > 0.9f)
Vertices[i+1].u += 1.0f;
if (Vertices[i].v - Vertices[i+1].v > 0.8f)
Vertices[i+1].v += 1.0f;
if (Vertices[i+1].v - Vertices[i].v > 0.8f)
Vertices[i].v += 1.0f;
if (Vertices[i].v - Vertices[i+2].v > 0.8f)
Vertices[i+2].v += 1.0f;
if (Vertices[i+2].v - Vertices[i].v > 0.8f)
Vertices[i].v += 1.0f;
if (Vertices[i+1].v - Vertices[i+2].v > 0.8f)
Vertices[i+2].v += 1.0f;
if (Vertices[i+2].v - Vertices[i+1].v > 0.8f)
Vertices[i+1].v += 1.0f;
}

glBegin(GL_TRIANGLE_STRIP);
for (i=0; i < NumVertices; i++)
{
glTexCoord2f(Vertices[i].u, Vertices[i].v);
glVertex3f(Vertices[i].x, Vertices[i].y, Vertices[i].z);
}
glEnd();
free(Vertices);
}

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

glEnable(GL_TEXTURE_2D);
GenerateSphere(10.65f, 50, 50, 1, 1);
glDisable(GL_TEXTURE_2D);

}glPopMatrix();
glFlush();
}

int LoadTGA(TextureImage *texture, char *filename) // Loads A TGA File Into Memory
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
GLubyte TGAcompare[12]; // Used To Compare TGA Header
GLubyte header[6]; // First 6 Useful Bytes From The Header
GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File
GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram
GLuint temp; // Temporary Variable
GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)
GLuint i;

FILE *file = fopen(filename, "rb"); // Open The TGA File

if( file==NULL || // Does File Even Exist?
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeo f(TGAcompare) || // Are There 12 Bytes To Read?
memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want?
fread(header,1,sizeof(header),file)!=sizeof(header )) // If So Read Next 6 Header Bytes
{
if (file == NULL) // Did The File Even Exist? *Added Jim Strong*
return 0; // Return False
else
{
fclose(file); // If Anything Failed, Close The File
return 0; // Return False
}
}

texture->width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture->height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)

if( texture->width <=0 || // Is The Width Less Than Or Equal To Zero
texture->height <=0 || // Is The Height Less Than Or Equal To Zero
(header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?
{
fclose(file); // If Anything Failed, Close The File
return 0; // Return False
}

texture->bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = texture->width*texture->height*bytesPerPixel; // Calculate The Memory Required For The TGA Data

texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data

if( texture->imageData==NULL || // Does The Storage Memory Exist?
fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved?
{
if(texture->imageData!=NULL) // Was Image Data Loaded
free(texture->imageData); // If So, Release The Image Data

fclose(file); // Close The File
return 0; // Return False
}

for(i=0; i<imageSize; i+=bytesPerPixel) // Loop Through The Image Data
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
temp=texture->imageData[i]; // Temporarily Store The Value At Image Data 'i'
texture->imageData[i] = texture->imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}

fclose (file); // Close The File

// Build A Texture From The Data
glGenTextures(1, &texture[0].texID); // Generate OpenGL texture IDs

glBindTexture(GL_TEXTURE_2D, texture[0].texID); // Bind Our Texture
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered

if (texture[0].bpp==32) // Was The TGA 24 Bits
{
type=GL_RGB; // If So Set The 'type' To GL_RGB
}

glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);

return 1; // Texture Building Went Ok, Return True
}

void init()
{
glClearColor(1.0f ,1.0f, 1.0f, 1.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);

if (LoadTGA(&textures[0],"image/planet03.tga") == 0) // Load The Font Texture
{
return; // If Loading Failed, Return False
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, texName);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
}

[ October 26, 2004: Message edited by: James Kay ]

KristofBeets
10-26-2004, 09:41 PM
<BLOCKQUOTE><font size="1" face="Arial, Verdana, Helvetica, sans-serif">quote:</font><HR>glBegin(GL_TRIANGLE_STRIP);
for (i=0; i < NumVertices; i++)
{
glTexCoord2f(Vertices[i].u, Vertices[i].v);
glVertex3f(Vertices[i].x, Vertices[i].y, Vertices[i].z);
}
glEnd();
free(Vertices);
}<HR></BLOCKQUOTE>

glBegin and glEnd calls do not exist in OGLES. You'll have to use glDrawArrays or glDrawElements.

I might have some code around to draw a sphere in OGLES, I'll post it if I can find it.

K-

DaveR
10-27-2004, 04:05 AM
Check the Vincent source files. There's an 'ugshape.c' file that contains implementations of several simple objects.

shellee
10-27-2004, 07:38 PM
Thank you for all~
I did it to textures mapping to sphere.
but you know, the textures mapping of sphere have seam. so I added source code for fixing the seam. but I can't fix... :confused:
I was helpful this Document.
http://www.flipcode.com/articles/article_skydomes.zip

this is the source code for fixing the seam.
-------------------------------------------
for (i=0; i < NumVertices-2; i++)
{
float u_x = 0.9f, u_y = 1.0f;
float v_x = 0.8f, v_y = 1.0f;

if (Vertices[i].u - Vertices[i+1].u > u_x)
Vertices[i+1].u += u_y;
if (Vertices[i+1].u - Vertices[i].u > u_x)
Vertices[i].u += u_y;
if (Vertices[i].u - Vertices[i+2].u > u_x)
Vertices[i+2].u += u_y;
if (Vertices[i+2].u - Vertices[i].u > u_x)
Vertices[i].u += u_y;
if (Vertices[i+1].u - Vertices[i+2].u > u_x)
Vertices[i+2].u += u_y;
if (Vertices[i+2].u - Vertices[i+1].u > u_x)
Vertices[i+1].u += u_y;
if (Vertices[i].v - Vertices[i+1].v > v_x)
Vertices[i+1].v += v_y;
if (Vertices[i+1].v - Vertices[i].v > v_x)
Vertices[i].v += v_y;
if (Vertices[i].v - Vertices[i+2].v > v_x)
Vertices[i+2].v += v_y;
if (Vertices[i+2].v - Vertices[i].v > v_x)
Vertices[i].v += v_y;
if (Vertices[i+1].v - Vertices[i+2].v > v_x)
Vertices[i+2].v += v_y;
if (Vertices[i+2].v - Vertices[i+1].v > v_x)
Vertices[i+1].v += v_y;
}