'Translate a 3d pyramid on openGl?
I have this code right here which makes a pyramid 3d. Also it displays a scale and rotation on x,y,z. My main problem is the translation, like it walking in y from -180 to 180 coordinates and doing rotation together. So fair just move from (0,0,0) to (-180,0,0) and it stays in that point. I want to start from -180 and to going on 180 (to run the pyramid ).
|
|
|
/_\ - - - - - - - - | - - - - - - - - - > /_\
---|-------------------|----------------------|-------
-180 | 180
|
|
|
#include <GL/glut.h>
#include <cmath>
struct point3D { double x, y, z; };
struct matrix3x3
{
double a, b, c,
d, e, f,
g, h, i;
};
point3D C[5];
double ANGLEY = 0, ANGLEZ = 0;
void set_identity_matrix(matrix3x3 &m)
{
m.a = 1; m.b = 0; m.c = 0;
m.d = 0; m.e = 1; m.f = 0;
m.g = 0; m.h = 0; m.i = 1;
}
void mult(matrix3x3 m, point3D &p)
{
point3D temp;
temp.x = m.a * p.x + m.b * p.y + m.c * p.z;
temp.y = m.d * p.x + m.e * p.y + m.f * p.z;
temp.z = m.g * p.x + m.h * p.y + m.i * p.z;
p = temp;
}
void rotateY(point3D &p, double degrees)
{
matrix3x3 temp;
set_identity_matrix(temp);
double sinTheta = sin(degrees * M_PI / 180);
double cosTheta = cos(degrees * M_PI / 180);
temp.a = cosTheta;
temp.c = sinTheta;
temp.g = -sinTheta;
temp.i = cosTheta;
mult(temp, p);
}
void rotateZ(point3D &p, double degrees)
{
matrix3x3 temp;
set_identity_matrix(temp);
double sinTheta = sin(degrees * M_PI / 180);
double cosTheta = cos(degrees * M_PI / 180);
temp.a = cosTheta;
temp.b = -sinTheta;
temp.d = sinTheta;
temp.e = cosTheta;
mult(temp, p);
}
void draw_line(double x1, double y1, double x2, double y2)
{
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd();
}
void setup_pyramid()
{
C[0].x = -0.5; C[0].y = -0.5; C[0].z = -0.5;
C[1].x = -0.5; C[1].y = -0.5; C[1].z = 0.5;
C[2].x = 0.5; C[2].y = -0.5; C[2].z = 0.5;
C[3].x = 0.5; C[3].y = -0.5; C[3].z = -0.5;
C[4].x = 0.0; C[4].y = 0.5; C[4].z = 0.0;
}
void scale_pyramid(double s)
{
int i;
for( i = 0; i < 5; i++ )
{
C[i].x *= s;
C[i].y *= s;
C[i].z *= s;
}
}
void draw_pyramid()
{
int i;
for( i = 0; i < 4; i++ )
{
draw_line(C[i].x, C[i].y, C[(i + 1) % 4].x, C[(i + 1) % 4]. y);
draw_line(C[i].x, C[i].y, C[4].x, C[4]. y);
}
}
void draw_axes()
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3ub(0, 0, 0);
glBegin(GL_LINES);
glVertex2d(-240, 0);
glVertex2d( 240, 0);
glVertex2d(0, -180);
glVertex2d(0, 180);
glEnd();
glPopAttrib();
}
void display()
{
int i;
float k;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
draw_axes();
for( k=-180.0; k < 180; k++)
glTranslated ( k, 0.0 ,0.0 );
for( i = 0; i < 5; i++ )
rotateZ(C[i], -ANGLEZ);
for( i = 0; i < 5; i++ )
rotateY(C[i], -ANGLEY);
glColor3ub(0, 0, 255);
draw_pyramid();
for( i = 0; i < 5; i++ )
rotateY(C[i], ANGLEY);
for( i = 0; i < 5; i++ )
rotateZ(C[i], ANGLEZ);
glTranslated ( k+1, -0.0 ,0.0 );
ANGLEY += 1;
ANGLEZ += 1;
glFlush();
}
void timer(int value)
{
display();
glutTimerFunc(1000.0 / 90.0, timer, 0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("OpenGL Lab");
gluOrtho2D(-240, 240, -180, 180);
glutDisplayFunc(display);
timer(0);
setup_pyramid();
scale_pyramid(100);
glutMainLoop();
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
