Jump to content
  • Advertisement
Sign in to follow this  
GhostXoP

Sphere Creation in opengl

This topic is 2659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, i am new to the entire site and am hopeful to be here more to be aided in my journey into game development. At the moment i am reading the 3d game development math aid book, and going through the opengl tutorial, but stopped to read the book. (it is very much more important)

I came across spherical coordinates and had attempted to create a beginning sphere on my own, and with some help in the end.

I have converted the degrees to radians and plugged them into glVertex3f, but get a what looks like, pie slice.

I understand the naming convention is bad (degrees should be radians) please bear with me, but if requested a rename for better help, i will do so.

//GLRendering.h

void SetUp_OpenGL() {
glViewport(0, 0, 1024, 768);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1024.0f / 768.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
}

void RenderProgramScene() { // Where problem to be more then likely located
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -4.0f);
//glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
//glDisable (GL_CULL_FACE);

glBegin(GL_LINE_LOOP);
degrees_per_plot_YZ = 0.0f;
degrees_per_plot_YZ = (two *= 3.14) /= dots_per_axis_YZ;
two = 2.0f;
current_degrees_Z = 0.0f;

for (zcounter = 0.0f; zcounter < dots_per_axis_YZ; zcounter++) { //Begin drawing of Z axis, within the for loop
if (firstrun_Z == 1) { //if running the first time, set to 0, and do not add anything to the current degrees
firstrun_Z = 0;
} else { //other wise
current_degrees_Z += degrees_per_plot_YZ; //(virtually) rotate axis of drawing y axis by adding degrees per plot, to current degress to get angle to next plot
firstrun_Y = 1; //in this part, we assume the circle from the last loop has run once, and we can set it again
current_degrees_Y = 0.0f; //reset degrees
}


for(ycounter = 0.0f; ycounter < dots_per_axis_YZ; ycounter++) { //draw loop for y axis
if (firstrun_Y == 1) { //if first run, set all to zero, and print
firstrun_Y = 0; //then set to 0 because we have run this once already.

//glvertex prints using the spherical to cartesian convertion. The book says y is r * sin(angle z) cos (angle z) then the degree to radian convertion. was corrected from help to be sin sin, and not sin cos
glVertex3f((radian*cos(current_degrees_Y*((3.14f) / 180.0f))*sin(current_degrees_Z*((3.14f) / 180.0f))),(radian*sin(current_degrees_Y*((3.14f) / 180.0f))*sin(current_degrees_Z*((3.14f) / 180.0f))),(radian*cos(current_degrees_Z*((3.14f) / 180.0f))));
} else {
current_degrees_Y += degrees_per_plot_YZ; //ran the second time, add degrees to next plot
glVertex3f((radian*cos(current_degrees_Y*((3.14f) / 180.0f))*sin(current_degrees_Z*((3.14f) / 180.0f))),(radian*sin(current_degrees_Y*((3.14f) / 180.0f))*sin(current_degrees_Z*((3.14f) / 180.0f))),(radian*cos(current_degrees_Z*((3.14f) / 180.0f))));
}
}
}

//object is done be drawn, now draw it to screen
glEnd();
SwapBuffers(hDC);
//the number you see at the top right of the screen
sprintf(titlebuf, "%i", frame++);
wintitle = (LPCSTR)titlebuf;
SetWindowText(hwnd, wintitle);
}


void End_OpenGL() {
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
}






The image of the sphere at the moment with the code above compiled:

http://i55.tinypic.com/35c24ad.png

To the top right is the frames drawn since execution. Can anyone please tell me what my problem may be?

Edit: i hope this is better, if still a little difficult or messy, please let me know and i will fix it

Share this post


Link to post
Share on other sites
Advertisement
I only glanced at your code, but here's a couple of suggestions. First, maybe just post the code to the thread, as that'll make it a bit easier for people to check it out. Second, just from glancing at it it's clear that the code could be cleaned up a lot, which would then make it easier to find the cause of the problem you're seeing. (If you can post the code here, I imagine I or someone else will be able to make some suggestions.)

Share this post


Link to post
Share on other sites

I only glanced at your code, but here's a couple of suggestions. First, maybe just post the code to the thread, as that'll make it a bit easier for people to check it out. Second, just from glancing at it it's clear that the code could be cleaned up a lot, which would then make it easier to find the cause of the problem you're seeing. (If you can post the code here, I imagine I or someone else will be able to make some suggestions.)


Will do, Thank you for your suggestion

Share this post


Link to post
Share on other sites
There's probably like two people on this entire forum that know how to procedurally generate a sphere. The rest use game engines. ;o

Share this post


Link to post
Share on other sites

There's probably like two people on this entire forum that know how to procedurally generate a sphere. The rest use game engines. ;o


This i understand, many who have made very good engines havent made a sphere (self made) with C++, but from the opengl Glut library.

This would be a great step up for me (A big accomplishment for someone who has been in this for only a couple months now) It wouldnt prove me better then them, but better knowledge
for the long run, and for my advantage (creating spherical objects in my own way) I am not planning on using a game engine but one i would like to construct after i have completed learning the math and opengl basic information. It is a very hard task, but i am willing to spend a very long time on it.

For now, i need to accomplish this, with much needed help.

Share this post


Link to post
Share on other sites
You should really consider using better naming conventions, and much more domain specific naming, as it is really hard to decipher what your code is doing when reading it. Angles have names, such as 'theta' and 'phi' that are much more meaningful, and spheres have lines of 'latitude' and 'longitude' rather than dots on axes.

The simple act of properly naming your variables to coincide with algebraic and geometric concepts makes your algorithm self-descriptive. This will help you and others spot your own mistakes much better. With what you have, I would not have known what you are trying to do if you had not said so in your post.

Share this post


Link to post
Share on other sites
Im sorry about the naming convention, it is good practice, but as a side small project, i didnt mind it. I will improve on it more in the next lesson.

owl: Thank you for the link, i will read it

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!