I need to draw cube by taking input from user.
Can anybody help me with the algorithm.
Thanx.
I need to draw cube by taking input from user.
Can anybody help me with the algorithm.
Thanx.
This is a general question. Are you familiar with the transformations?Which API do you want to use to interact with the mouse?–GLUT, DirectInput, WM_MOUSE ?
Not by glut. Mouse input is also not needed. I just have to take input from user like how many points or triangles there will be or length of cube etc and draw the cube, cone, cylinder etc.
a cube:
get width W = w;
get position P(x,y,z)
use one color C or multiple colors
draw the cube … simple way use quads
glBegin(GL_QUADS)
//draw Front/Back/Left/Right/Top/Bottom Face
//front
glVertex3f(0, 0, 0)
glVertex3f(w, 0, 0)
glVertex3f(w, w, 0)
glVertex3f(0, w, 0)
//back
glVertex3f(0, 0, w)
glVertex3f(w, 0, w)
glVertex3f(w, w, w)
glVertex3f(0, w, w)
//left
glVertex3f(0, 0, 0)
glVertex3f(0, 0, w)
glVertex3f(0, w, w)
glVertex3f(0, w, 0)
//right
glVertex3f(w, 0, 0)
glVertex3f(w, 0, w)
glVertex3f(w, w, w)
glVertex3f(w, w, 0)
//top
glVertex3f(0, w, 0)
glVertex3f(w, w, 0)
glVertex3f(w, w, w)
glVertex3f(0, w, w)
//bottom
glVertex3f(0, 0, 0)
glVertex3f(w, 0, 0)
glVertex3f(w, 0, w)
glVertex3f(0, 0, w)
glEnd()
glTranslatef(Px, Py, Pz)
specify color … (pay attention to lighting disabled glDisable(GL_LIGHTING))
if one color for the whole cube then
place glColor3f(Cx, Cy, Cz) before glBegin
else specify a color for each face
this will draw a cube with 6 faces If you want smaller quads (e.g. improve lighting) then you have to divide w by NumberOfQuadsPerFace and
draw NumberOfQuadsPerFace per Face each of which has a width/height or depth of NumberOfQuadsPerFace^-1
Thanx powerpad. Do you have little complex than this one? I mean to find the co-ordiante by angle and drawing by triangular mesh.
I am sorry but I don’t understand your question.
What do you mean by angle and by mesh …
The user inputs an angle and you want to calculate a cube or what …
given a point p(x,y,z) and a length l than you can
calculate a point p’(x’, y’, z’) by using the laws of sine and cosine to get a p’ that has angle alpha difference to p
e.g. a circle is defined by
x = r * cos(alpha)
y = r * sin(alpha)
where r is the sphere’s radius and x,y defines the center of the sphere. If you want to define a quad(a, b, b’, a’) by alpha you probably mean
a = (ax,ay,az)
b = (bx,by,bz) - bx = ax, by = ay + r, bz
b’ = (bx + r * cos(alpha), by + rsin(alpha), bz)
a’ = (ax + r * cos(alpha), ay + rsin(alpha), az)
this leads to something like this
-b' -- | - |
b |
-a’ -alpha a------------->
you can define rotated points for all the planes you want assuming a radius r or getting it from the user.
I am sorry but I don’t understand your question.
What do you mean by angle and by mesh …
The user inputs an angle and you want to calculate a cube or what …
given a point p(x,y,z) and a length l than you can
calculate a point p’(x’, y’, z’) by using the laws of sine and cosine to get a p’ that has angle alpha difference to p
e.g. a circle is defined by
x = r * cos(alpha)
y = r * sin(alpha)
where r is the sphere’s radius and x,y defines the center of the sphere. If you want to define a quad(a, b, b’, a’) by alpha you probably mean
a = (ax,ay,az)
b = (bx,by,bz) - bx = ax, by = ay + r, bz
b’ = (bx + r * cos(alpha), by + rsin(alpha), bz)
a’ = (ax + r * cos(alpha), ay + rsin(alpha), az)
this leads to something like this
-b'
-- |
- |
b |
| -a'
| -- |
| -alpha|
a------------->
you can define rotated points for all the planes you want assuming a radius r or getting it from the user.
I am sorry but I don’t understand your question.
What do you mean by angle and by mesh …
The user inputs an angle and you want to calculate a cube or what …
given a point p(x,y,z) and a length l than you can
calculate a point p’(x’, y’, z’) by using the laws of sine and cosine to get a p’ that has angle alpha difference to p
e.g. a circle is defined by
x = r * cos(alpha)
y = r * sin(alpha)
where r is the sphere’s radius and x,y define the points on the sphere for alpha 0…2PI. If you want to define a quad(a, b, b’, a’) by alpha you probably mean
a = (ax,ay,az)
b = (bx,by,bz) - bx = ax, by = ay + r, bz
b’ = (bx + r * cos(alpha), by + rsin(alpha), bz)
a’ = (ax + r * cos(alpha), ay + rsin(alpha), az)
this leads to something like this
-b'
-- |
- |
b |
| -a'
| -- |
| -alpha|
a------------->
you can define rotated points for all the planes you want assuming a radius r or getting it from the user.
yeah something like that for cube how can i draw by just taking input length(l) from user. or for cylinder what can i do.
I hope that someone can delete my 3 posts (don’t know how this could happen )
well the case with taking only length is the cube
that I have described above.
A cylinder takes 2 inputs the radius r for the base surface - a circle and the length l of the outer surface (the coat or how one calls it)
I would draw the cylinder in a brute force way by
approximating the sphere e.g. (this places the sphere at 0,0 and generates for one point on the
base sphere all points on a line
from x,y to x, y + l
the thing left is to connect those points to proper triangles … and recall it is brute force
(but it should work although I did not test it)
for(i = 0; i < 360°; i += 360 / Steps)
{
x = radius * cos(i)
y = radius * sin(i)
for(j = 0; j < l; j += l / steps)
y' = y + j;
}
thanx again.
Well can you clarify the cube eqn. and how can i get ‘z’ plane.
i tried like below but the program is crashing.
for(int i=0; i < 360; i += 360 / steps)
{
x = r* cos(i);
y = r* sin(i);
glBegin(GL_POINTS);
for(int j = 0; j < 2; j += 2 / steps)
{
y = y + j;
glVertex2d(x,y);
}
glEnd();
}
i’m confused with steps variable isnt it for to getting no of circles.
here is how you could draw the base circle
float PI2 = 6.283185307179586476925286766559f;
float d = 0.0f;
float steps = 180;
float s = PI2 / (float)steps;
float r = 10.0f;
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
int iI = 0;
Gl.glBegin(Gl.GL_TRIANGLE_FAN);
Gl.glVertex3f(0.0f, 0.0f, 0.0f);
for(iI = 0; iI <= steps; iI++)
{
d += s;
x = r * (float)Math.Cos(d);
y = r * (float)Math.Sin(d);
Gl.glVertex3f(x, y, 0.0f);
}
Gl.glEnd();
and this is a brute force way to draw a cylinder with radius r and height h
float PI2 = 6.283185307179586476925286766559f;
float d = 0.0f;
float steps = 45;
float s = PI2 / steps;
float r = 20.0f;
float x0 = 0.0f;
float y0 = 0.0f;
float z0 = 0.0f;
float x1, z1;
int iI = 0;
float h = 5.0f;
Gl.glBegin(Gl.GL_TRIANGLES);
for(iI = 0; iI <= steps; iI++)
{
d += s;
x0 = r * (float)Math.Cos(d);
x1 = r * (float)Math.Cos(d + s);
z0 = r * (float)Math.Sin(d);
z1 = r * (float)Math.Sin(d + s);
//bottom circle
Gl.glColor3f(1.0f, 0.0f, 0.0f);
Gl.glVertex3f(0.0f, y0, 0.0f);
Gl.glVertex3f(x0, y0, z0);
Gl.glVertex3f(x1, y0, z1);
//coat
Gl.glColor3f(0.0f, 1.0f, 0.0f);
Gl.glVertex3f(x0, y0, z0);
Gl.glVertex3f(x1, y0, z1);
Gl.glVertex3f(x1, y0 + h, z1);
Gl.glVertex3f(x1, y0 + h, z1);
Gl.glVertex3f(x0, y0 + h, z0);
Gl.glVertex3f(x0, y0, z0);
//top circle
Gl.glColor3f(0.0f, 0.0f, 1.0f);
Gl.glVertex3f(0.0f, y0 +h, 0.0f);
Gl.glVertex3f(x0, y0 + h, z0);
Gl.glVertex3f(x1, y0 + h, z1);
}
Gl.glEnd();