Hi I am trying to write a code to draw a sphere centered within a cube using openGL. Yesterday I had written it in such a way that the values of the side length of cube and dia of sphere are give in the code itself.That worked perfectly. Now I modified the code so that it takes these values from the user. Is it possible to do so? This is my attempt:
#include <GL/glut.h>
#include <iostream>
using namespace std;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void value(void)//This is the function I am using to take input
{
float edge,dia,scale,d;
cout<<"Please enter side length of square: "<<flush;
cin>>float edge;
cout<<"Please enter scale factor: "<<flush;
cin>>float scale;
d=edge*scale;
if (d<1.732*edge)
{
dia=d;
}
else
{
cout<<"Please enter smaller scale value: "<<flush;
}
}
using namespace std;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity(); /*clear the matrix */
/*viewing transformation*/
gluLookAt(4.0,2.0,2.5,0.0,0.0,0.0,0.0,1.0,0.0);
glBegin(GL_LINES);
glColor3f(1.0f,0.0f,0.0f); //cube is red
glutWireCube(edge);
glColor3f(0.0f,0.0f,1.0f); //sphere is blue
glutWireSphere(dia,50,50);
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
gluPerspective(45.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
The purpose of the function "value" is two-fold:to take the side and dia values as input and also to limit the scale factor between the side length and the dia.
Please help me.Am working on this for hours.Thanks in advance.