Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 06 June 2009 - 05:44 PM
Posted 06 June 2009 - 07:09 PM
Posted 07 June 2009 - 06:54 PM
void drawsphere()
{
#define PI 3.14159265358979323846f
int width = 32;
int height = 16;
float theta, phi, *dat;
int i, j, t, ntri, nvec, *idx;
nvec = (height-2)* width+2;
ntri = (height-2)*(width-1)*2;
dat = (float*) malloc( nvec * 3*sizeof(float) );
idx = (int*) malloc( ntri * 3*sizeof(int) );
for( t=0, j=1; j<height-1; j++ )
for( i=0; i<width; i++ )
{
theta = float(j)/(height-1) * PI;
phi = float(i)/(width-1 ) * PI*2;
dat[t++] = sinf(theta) * cosf(phi);
dat[t++] = cosf(theta);
dat[t++] = -sinf(theta) * sinf(phi);
}
dat[t++]=0; dat[t++]= 1; dat[t++]=0;
dat[t++]=0; dat[t++]=-1; dat[t++]=0;
for( t=0, j=0; j<height-3; j++ )
for( i=0; i<width-1; i++ )
{
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i+1;
idx[t++] = (j )*width + i+1;
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i ;
idx[t++] = (j+1)*width + i+1;
}
for( i=0; i<width-1; i++ )
{
idx[t++] = (height-2)*width;
idx[t++] = i;
idx[t++] = i+1;
idx[t++] = (height-2)*width+1;
idx[t++] = (height-3)*width + i+1;
idx[t++] = (height-3)*width + i;
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3,GL_FLOAT,0,dat);
glNormalPointer(GL_FLOAT,0,dat);
glDrawElements(GL_TRIANGLES, ntri*3, GL_UNSIGNED_INT, idx );
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
free(idx);
free(dat);
}
Posted 07 June 2009 - 08:52 PM
Posted 08 June 2009 - 06:28 PM
Quote:
Original post by jyk
Could you edit your post and make the links click-able using HTML tags? That'll make it easier for us to check them out (and to understand what you're having trouble with).
The two most common methods for generating a sphere mesh (IMX at least) are:
1. Generate the vertices using spherical coordinate conversions.
2. Generate the vertices by recursively subdividing a Platonic solid and then normalizing the vertices of the resulting mesh.
The former is quite a bit easier to implement than the latter, but the distribution of triangles is noticeably uneven. However, in many cases this won't be a problem (in fact, I think the GLU library uses this method to generate sphere meshes). The second method produces a more even distribution of triangles, but is less straightforward to implement.
In addition to making those links click-able, perhaps you could clarify which methods you've tried, and what sort of troubles you've run into.
Quote:
Original post by gltiich
R = 1.0
For J = 0, PI
For I = 0, 2*PI
X = R * Cos(I) * Sin(J)
Y = R * Sin(I) * Sin(J)
Z = R * Cos(J)
PLOT(X, Y, Z)
Quote:
Original post by ma_hty
"... over the years ..."
Arr... I don't think it can be a problem in a scale of years. Anyway, it is just an easy task. Let me show you a simple way of doing it.
*** Source Snippet Removed ***
The source code above, drawsphere(), draws an unit sphere. It prepares the necessary vertices first. And then, it prepares the indexes of the triangles. Lastly, it draws them using some OpenGL commands.
Quote:
Original post by phresnel
Hurp:
I try to be simplicisstic.
Look at a sine wave.
Look at a cosine wave.
See how they differ.
Posted 08 June 2009 - 08:19 PM
Quote:
Original post by Hurp
While that sort of makes sense, I do not see how triangles could be formed from that.
const float radius = ...;
for (angle=0.0f; angle<=2pi-step_size; angle+=step_size) {
const float a[2] = { radius*cosf(alpha), radius*sinf(alpha) };
const float b[2] = { radius*cosf(alpha+step_size), radius*sinf(alpha+step_size) };
drawLine (a,b);
}
Posted 08 June 2009 - 08:27 PM
Posted 10 June 2009 - 05:53 PM
Quote:
Original post by ma_hty
Arr... the reasoning...
That's obvious (to me). You can count the number of vertices and the number of triangles precisely. There are only two cases. They are the triangles touching the poles and the triangles not touching the poles. The source code is there already. I grantee you it is 100% functional. So, please spend some time to read.
The only less obvious issue about the triangles is their winding. You have to make sure the winding of the triangles consistence with each others. Such that, the 3D model can work with culling correctly.
unsigned int m_unNumberOfVerticies;
unsigned int m_unNumberOfTriangles;
unsigned int m_unNumberOfIndicies;
D3DVERTEXELEMENT9 dvVertexElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
D3DDECL_END()
};
lpD3Device->CreateVertexDeclaration(dvVertexElements, &m_VD);
#define PI 3.14159265358979323846f
int width = 32;
int height = 16;
float theta, phi, *dat;
int i, j, t, ntri, nvec, *idx;
nvec = (height-2)* width+2;
ntri = (height-2)*(width-1)*2;
dat = (float*) malloc( nvec * 3*sizeof(float) );
idx = (int*) malloc( ntri * 3*sizeof(int) );
m_unNumberOfVerticies = nvec;
m_unNumberOfIndicies = ntri * 3;
m_unNumberOfTriangles = ntri;
for( t=0, j=1; j<height-1; j++ )
for( i=0; i<width; i++ )
{
theta = float(j)/(height-1) * PI;
phi = float(i)/(width-1 ) * PI*2;
dat[t++] = sinf(theta) * cosf(phi);
dat[t++] = cosf(theta);
dat[t++] = -sinf(theta) * sinf(phi);
}
dat[t++]=0; dat[t++]= 1; dat[t++]=0;
dat[t++]=0; dat[t++]=-1; dat[t++]=0;
for( t=0, j=0; j<height-3; j++ )
for( i=0; i<width-1; i++ )
{
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i+1;
idx[t++] = (j )*width + i+1;
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i ;
idx[t++] = (j+1)*width + i+1;
}
for( i=0; i<width-1; i++ )
{
idx[t++] = (height-2)*width;
idx[t++] = i;
idx[t++] = i+1;
idx[t++] = (height-2)*width+1;
idx[t++] = (height-3)*width + i+1;
idx[t++] = (height-3)*width + i;
}
lpD3Device->CreateVertexBuffer(nvec*sizeof(float), 0, 0, D3DPOOL_DEFAULT, &m_VB, 0);
lpD3Device->CreateIndexBuffer(m_unNumberOfIndicies*sizeof(int),0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_IB, 0);
void *buffer;
m_VB->Lock(0,0, &buffer, 0);
memcpy(buffer, dat, nvec* (sizeof(float) * 3));
m_VB->Unlock();
m_IB->Lock(0,0, &buffer, 0);
memcpy(buffer, idx, m_unNumberOfIndicies*sizeof(int));
m_IB->Unlock();
lpD3DDevice->SetStreamSource(0, m_VB, 0, m_sizeof(float)*3);
lpD3DDevice->SetIndices(m_IB);
lpD3DDevice->SetVertexDeclaration(m_VD);
lpD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_unNumberOfVerticies, 0, m_unNumberOfTriangles);
Posted 11 June 2009 - 06:09 PM
Posted 11 June 2009 - 11:47 PM
for( t=0, j=0; j<height-3; j++ )
for( i=0; i<width-1; i++ )
{
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i+1; // switch this line with the following line
idx[t++] = (j )*width + i+1; // <<
idx[t++] = (j )*width + i ;
idx[t++] = (j+1)*width + i ; // switch this line with the following line
idx[t++] = (j+1)*width + i+1; // <<
}
for( i=0; i<width-1; i++ )
{
idx[t++] = (height-2)*width;
idx[t++] = i; // switch this line with the following line
idx[t++] = i+1; // <<
idx[t++] = (height-2)*width+1;
idx[t++] = (height-3)*width + i+1; // switch this line with the following line
idx[t++] = (height-3)*width + i; // <<
}
Posted 12 June 2009 - 12:12 AM
Posted 12 June 2009 - 05:31 PM
Quote:
Original post by ma_hty
lpD3Device->CreateIndexBuffer(m_unNumberOfIndicies*sizeof(int),0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_IB, 0);
This line is probably incorrect. D3DFMT_INDEX16 is probably two bytes integer. However, you copy to the buffer a block of 4 bytes integer later. You must be very unlucky. Otherwise, you should have noticed it because the program crashed during execution.
Posted 15 June 2009 - 05:38 PM
Quote:
Original post by ma_hty
lpD3Device->CreateVertexBuffer(nvec*sizeof(float), 0, 0, D3DPOOL_DEFAULT, &m_VB, 0); //<< wrong
lpD3Device->CreateVertexBuffer(nvec*3*sizeof(float), 0, 0, D3DPOOL_DEFAULT, &m_VB, 0);
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.