PDA

View Full Version : Draw Sphere



noobatopengl
10-18-2015, 03:40 PM
Hello There, This is my first on opengl coding. I have been stuck at thinking on about i can muli sphere at different location and zoom in until hit near plant and disapper and the next on go on until user hit esc. I was able to do muli sphere at same location. Can someone help me plz.

Here is my code for drawing a sphere




void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
srand((unsigned int)time(NULL));

// Rotate the scene so we can see the tops of the shapes.

glRotatef(xRotated, 0.0, 0.0, 0.0);
// rotation about Y axis
glRotatef(yRotated, 0.0, 0.0, 0.0);
// rotation about Z axis
glRotatef(zRotated, 0.0 , 0.0, 0.0);


glPushMatrix();
glTranslatef(0, 0.0, 0.0);
glutSolidSphere(1.0, 50, 50);

glPopMatrix();


glFlush();
}

If I want a muli sphere I just put a for loop on gl PushMatrix it work but not random loction

Carmine
10-19-2015, 10:13 AM
First of all, it is good that you posted your code using the [ code] tags,
and that you used indentation to make it more readable. This way we
can make more explicit corrections/suggestion. My first suggestion is that
you read the docs on glRotatef and glTranslatef, because your usage of
them implies that you do not understand the basics of how they work.
The 3 rotate commands and the translate command do nothing. Neither
does the srand command. I think you'll find that if you comment out
all of that stuff (as I've done below), your code will run the same way.
Rotations must be around an axis. To rotate 90 degs about the Y axis,
the command would be glRotatef (90, 0,1,0). The translate command
is not 'translate to', it is 'translate by'. So translating your sphere by
0,0,0 says don't move the sphere. My suggestion is to start simple.
Try displaying two spheres side-by-side with no rotations.


void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
/*
glPushMatrix();
srand((unsigned int)time(NULL));

glRotatef (xRotated, 0.0, 0.0, 0.0); // rotation about Y axis
glRotatef (yRotated, 0.0, 0.0, 0.0); // rotation about Z axis
glRotatef (zRotated, 0.0 , 0.0, 0.0);

glPushMatrix();
glTranslatef (0, 0.0, 0.0);
glutSolidSphere(1.0, 50, 50);
glPopMatrix();

glFlush();
*/
glutSolidSphere (1.0, 50, 50);
}

noobatopengl
10-19-2015, 11:02 AM
Now I fix it, It zoom but how can create more than one ball? and let it zoom one by one and if one ball hit near plant it disappear and next one come



GLfloat xRotated, yRotated, zRotated;

void redisplayFunc(void)
{


glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// clear the identity matrix.
glLoadIdentity();


glTranslatef(0.0, 0.0, -4.0);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated, 50.0, 0.0, 0.0);
// rotation about Y axis
glRotatef(yRotated, 0.0, 0.0, 0.0);
// rotation about Z axis
glRotatef(zRotated, 0.0, 0.0, 0.0);
// scaling transfomation
glScalef(1.0, 1.0, 1.0);
// built-in (glut library) function , draw you a sphere.
glutSolidSphere(2, 20, 50);
// Flush buffers to screen

glFlush();

}

void reshapeFunc(int x, int y)
{
//if (y == 0 || x == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(40.0, (GLdouble)x / (GLdouble)y, 0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, x, y); //Use the whole window for rendering
}

void idleFunc(void)
{

yRotated += 0.01;

redisplayFunc();
}


int main(int argc, char **argv)
{
//Initialize GLUT
glutInit(&argc, argv);
//double buffering used to avoid flickering problem in animation
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// window size
glutInitWindowSize(400, 350);
// create the window
glutCreateWindow("Sphere Rotating Animation");
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

xRotated = yRotated = zRotated = 30.0;
xRotated = 33;
yRotated = 40;
glClearColor(0.0, 0.0, 0.0, 0.0);
//Assign the function used in events
glutDisplayFunc(redisplayFunc);
glutReshapeFunc(reshapeFunc);
glutIdleFunc(idleFunc);
//init();

//Let start glut loop
glutMainLoop();
return 0;
}

Carmine
10-19-2015, 12:34 PM
You simply call glutSolidSphere twice if you want to draw two balls.
You must be careful to position the balls so they aren't on top of each other.
The code below is an example.



float xRotated = 90.0, yRotated = 0.0, zRotated = 0.0;

//------------------------------ reshapeFunc ---------------------------------

void reshapeFunc (int x, int y)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();

gluPerspective (40.0, (GLdouble)x / (GLdouble)y, 0.5, 20.0);
glMatrixMode (GL_MODELVIEW);
glViewport (0, 0, x, y);
}

//------------------------------ Draw_Spheres -------------------------------

void Draw_Spheres (void)
{
glMatrixMode (GL_MODELVIEW);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -15.0);

glColor3f (0.8, 0.2, 0.1); // Red ball displaced to left.
glPushMatrix ();
glTranslatef (-1.5, 0.0, 0.0);
glRotatef (60.0, 1,0,0);
glRotatef (zRotated*2.0, 0,0,1); // Red ball rotates at twice the rate of blue ball.
glutSolidSphere (1.0, 20, 50);
glPopMatrix ();

glColor3f (0.1, 0.2, 0.8); // Blue ball displaced to right.
glPushMatrix ();
glTranslatef (1.5, 0.0, 0.0);
glRotatef (60.0, 1,0,0);
glRotatef (zRotated, 0,0,1);
glutSolidSphere (1.0, 20, 50);
glPopMatrix ();

glutSwapBuffers();
}

//-------------------------------- idleFunc ----------------------------------

void idleFunc (void)
{
zRotated += 0.3;
glutPostRedisplay();
}

//---------------------------------- main ------------------------------------

void main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize (800, 700);
glutInitWindowPosition (700, 200);
glutCreateWindow ("Sphere Rotating Animation");
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);

glClearColor (0.0, 0.0, 0.0, 0.0);

glutDisplayFunc (Draw_Spheres);
glutReshapeFunc (reshapeFunc);
glutIdleFunc (idleFunc);

glutMainLoop();
}

noobatopengl
10-19-2015, 02:04 PM
Thank you but When I try to use For Loop to random location. I dont want to copy and past 50 time of that. plus how can to pull ball to center and fly ball to near plan??



I edit the code you give. Now I got one sphere is flying to near plan (i hope it flying) When I go make a ~50 to 100 ball it fail.


Other question how do you pull the ball that is not on center to center and flying to near plan


#include <GL/glut.h> // Include the GLUT header file


bool movingUp = false; // Whether or not we are moving up or down
float yLocation = 0.0f; // Keep track of our position on the y axis.

float yRotationAngle = 0.0f; // The angle of rotation for our object


void display(void) {

glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations

glTranslatef(0.0f, 0.0f, -5.0f); // Push eveything 5 units back into the scene, otherwise we won't see the primitive

glTranslatef(0.0f, 0.0f, yLocation); // Translate our object along the y axis

glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate our object around the y axis

glutSolidSphere(1.0, 20, 50);

glFlush(); // Flush the OpenGL buffers to the window

if (movingUp) // If we are moving up
yLocation -= 0.005f; // Move up along our yLocation
else // Otherwise
yLocation += 0.005f; // Move down along our yLocation

if (yLocation < -3.0f) // If we have gone up too far
movingUp = false; // Reverse our direction so we are moving down
else if (yLocation > 3.0f) // Else if we have gone down too far
movingUp = true; // Reverse our direction so we are moving up

yRotationAngle += 0.005f; // Increment our rotation value

if (yRotationAngle > 360.0f) // If we have rotated beyond 360 degrees (a full rotation)
yRotationAngle -= 360.0f; // Subtract 360 degrees off of our rotation
}

void reshape(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)

gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes

glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}


int main(int argc, char **argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize(500, 500); // Set the width and height of the window
glutInitWindowPosition(100, 100); // Set the position of the window
glutCreateWindow("Your first OpenGL Window"); // Set the title for the window

glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering

glutIdleFunc(display); // Tell GLUT to use the method "display" as our idle method as well

glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping


glutMainLoop(); // Enter GLUT's main loop
}

Carmine
10-20-2015, 11:42 AM
Thank you but When I try to use For Loop to random location.
I dont want to copy and past 50 time of that.
plus how can to pull ball to center and fly ball to near plan??
Now I got one sphere is flying to near plan (i hope it flying) When I go make a ~50 to 100 ball it fail.
Other question how do you pull the ball that is not on center to center and flying to near plan
I have received your code, compiled, and run it.
It shows one, white, ball, moving towards and away from camera.
Next, I'll try to address some of your questions.
Not sure I understand the questions, but I'll take a guess.

Carmine
10-20-2015, 02:25 PM
This small extension of your code answers some of your questions.
The Y rotation currently in your code does nothing.
Perhaps that is for some future capability?
If you comment it out, the code runs just the same.
BTW - it is recommended that your Display function should not be used as your idle function.
Not sure why. But I always avoid it. Perhaps someone else could explain that to us?



#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glu.h>
#include <gl.h>
#include <glut.h>


bool movingUp = false; // Whether or not we are moving up or down
float yLocation = 0.0f; // Keep track of our position on the y axis.
float yRotationAngle = 0.0f; // The angle of rotation for our object

// --------------------------------------------------------------------------------------------

void Draw_Spheres (float yloc)
{
int b;
static float balls[100][3];
static short first = TRUE;

if (first) { // Initialize 100 spheres with random x,y coordinates.
first = FALSE;
for (b = 0; b < 100; b++) {
balls[b][0] = float(rand() % 50 - 25) / 10.0;
balls[b][1] = float(rand() % 50 - 25) / 10.0;
balls[b][2] = 0.0;
}
}

for (b = 0; b < 100; b++) { // Translate balls towards and away from front plane.
glPushMatrix ();
glTranslatef (balls[b][0], balls[b][1], yloc);
glutSolidSphere (0.2, 20, 50);
glPopMatrix();
}
}

// --------------------------------------------------------------------------------------------

void display (void) {

glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations

glTranslatef(0.0f, 0.0f, -5.0f); // Push eveything 5 units back into the scene, otherwise we won't see the primitive

// glTranslatef(0.0f, 0.0f, yLocation); // Translate our object along the y axis
Draw_Spheres (yLocation);

glRotatef (yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate our object around the y axis

glFlush(); // Flush the OpenGL buffers to the window

if (movingUp) // If we are moving up
yLocation -= 0.005f; // Move up along our yLocation
else // Otherwise
yLocation += 0.005f; // Move down along our yLocation

if (yLocation < -3.0f) // If we have gone up too far
movingUp = false; // Reverse our direction so we are moving down
else if (yLocation > 3.0f) // Else if we have gone down too far
movingUp = true; // Reverse our direction so we are moving up

yRotationAngle += 0.005f; // Increment our rotation value
if (yRotationAngle > 360.0f) // If we have rotated beyond 360 degrees (a full rotation)
yRotationAngle -= 360.0f; // Subtract 360 degrees off of our rotation
}

// --------------------------------------------------------------------------------------------

void reshape(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}

// --------------------------------------------------------------------------------------------

int main(int argc, char **argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize(500, 500); // Set the width and height of the window
glutInitWindowPosition(800, 200); // Set the position of the window
glutCreateWindow("Your first OpenGL Window"); // Set the title for the window

glutDisplayFunc (display); // Tell GLUT to use the method "display" for rendering
glutIdleFunc (display); // Tell GLUT to use the method "display" as our idle method as well
glutReshapeFunc (reshape); // Tell GLUT to use the method "reshape" for reshaping

glutMainLoop(); // Enter GLUT's main loop
}


2162