# Thread: views of a cube

1. ## views of a cube

hello;
in a program opengl, I drew a cube. when I input a letter the program displays a view of the cube, calculates and records (in a text file with glreadpixel ()) the number of pixels of red, green, and blue. (view = camera placement at a given position). my question is: is it possible to do this task directly, using a loop without using the keyboard before?
The code I use is as follows:
Code :
```#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>

typedef struct point{
float x; float y;float z;
};point pl;

point zero={-0.5, -0.5, 0.5}, un={0.5, -0.5, 0.5},deux={-0.5, 0.5 ,0.5},
trois={0.5, 0.5, 0.5},quatre={-0.5, 0.5 ,-0.5},cinq={0.5, 0.5, -0.5},six={-0.5, -0.5 ,-0.5},sept={0.5, -0.5, -0.5};
// variables du mouvement
static int scaling = 0;
static int translating = 0;
static int rotating = 0;
static float scale = 1.0;
static float center[3] = { 0.0, 0.0, 0.0 };
static float rotation[3] = { 0.0, 0.0, 0.0 };
static float translation[3] = { 0.0, 0.0, -4.0 };

//variables de la camera
float ex=1,ey=1,ez=1;

void face(point a,point b,point c,point d){
glBegin(GL_POLYGON);
glVertex3f(a.x,a.y,a.z);
glVertex3f(b.x,b.y,b.z);
glVertex3f(c.x,c.y,c.z);
glVertex3f(d.x,d.y,d.z);
glEnd();
}
void drawCube(){
// Setup viewing transformation
glLoadIdentity(); glScalef(scale, scale, scale); glTranslatef(translation[0], translation[1], 0.0);
// Set projection transformation
gluPerspective(45.0, 1, 0.1, 100.0);
// Set camera transformation
glTranslatef(translation[0], translation[1], translation[2]);
glScalef(scale, scale, scale);glRotatef(rotation[0], 1.0, 0.0, 0.0);
glRotatef(rotation[1], 0.0, 1.0, 0.0);glRotatef(rotation[2], 0.0, 0.0, 1.0);
glTranslatef(-center[0], -center[1], -center[2]);
gluLookAt((GLfloat)ex,(GLfloat)ey,(GLfloat)ez,0,0,0,0,1,0);

glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,.0,.0);face(zero,un,sept,six);
glColor3f(.0,1.0,.0);face(sept,cinq,quatre,six);
glColor3f(.0,.0,1.0);face(cinq,quatre,deux,trois);
glColor3f(1.0,1.0,1.0);face(deux,trois,un,zero);
glColor3f(1.0,1.0,.0);face(un,sept,cinq,trois);
glColor3f(.0,1.0,1.0);face(zero,six,quatre,deux);
glPopMatrix();
glFlush();
/********************
traitement de glrearpixel();
********************/
}
void init(){glClearColor(.0,.0,.0,.0);}

void clavier(unsigned char k,int x,int y){
switch(k){
case 'q':{ exit(0);break;}
case 'a': {ex =7;glutPostRedisplay();break;}
case 'z': {ex =-7;glutPostRedisplay();break;}
case 'e': {ey =5;glutPostRedisplay();break;}
case 'r': {ey =-5;glutPostRedisplay();break;}
case 't': {ez =6;glutPostRedisplay();break;}
case 'y': {ez =-6;glutPostRedisplay();break;}
//case 27: exit(0);break;
}
}
void main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(450,450);
glutInitWindowPosition(300,100);
glutCreateWindow("azer");
init();
glutDisplayFunc(drawCube);
glutKeyboardFunc(clavier);
glutMainLoop();
}```

cordially

2. glut has an idle function that it calls when it has nothing to do. You could put the code to modify "ex,ey,ez" in there and post a redisplay.

3. The idle function and its usage in one of my programs, I use it to rotate.
Code :
```static void idle(void)
{
spinAngle += 0.03125;  /* Add a small angle (in degrees). */
if (spinAngle > 360) {
spinAngle -= 360;
}
glutPostRedisplay();
}
glutIdleFunc(idle);```

Best Regards,

newbiecow

4. yes indeed it works well with idle.

static void idle(void)
{
ex +=2;cp=1;
if (ex > 6) {
ex=0;ey +=2;cp=3;
if (ey > 6) {
ey=0;ez +=2;cp=5;
if (ez > 6) {
fprintf(f,"%s\t",monObjet);fprintf(f,"\n");exit(0) ;
}
}
}
glutPostRedisplay();
}

thanks you all !

5. ## problem with idle function

hello, in a program I want to put the camera in certain places, for this I used the idle function. my problem is: when I write eyex + = r things do not work, but when I replace the variable r by its numerical value (eyex + = 1026) it works fine! what to do to fix this thing? (knowing that I have to use the variable, not its numerical value)

the code is as follows:
[CODE] double r;/ / radius of the object displayed
* static double eyex =-r,-r = eyey, eyez =-r;
GLUTRedraw void (void)
{
......
.......
gluLookAt ((GLfloat) eyex (GLfloat) eyey (GLfloat) eyez, gx, gy, gz, ux, uy, uz);
....
** r = sqrt ((square (gx)) + (square (gy)) + (square (gz)));
.....
}
static void idle (void)
* {
eyex + = r;
*** if (eyex> (2 * r)) {
eyex =-r = r + eyey;
if (eyey> (2 * r)) {
eyex =-r =-r eyey; eyez + = r;
if (eyez> (3 * r)) {
fprintf (f, "% s \ t", myObjet) fprintf (f, "\ n");
exit (0);
}
}
** }
** glutPostRedisplay ();
* } [/ CODE]

cordially

6. ## problem with idle function

hello, in a program I want to put the camera in certain places, for this I used the idle function. my problem is: when I write eyex + = r things do not work, but when I replace the variable r by its numerical value (eyex + = 1026) it works fine! what to do to fix this thing? (knowing that I have to use the variable, not its numerical value)

the code is as follows:
Code :
``` double r;/ / radius of the object displayed
* static double eyex =-r,-r = eyey, eyez =-r;
GLUTRedraw void (void)
{
......
.......
gluLookAt ((GLfloat) eyex (GLfloat) eyey (GLfloat) eyez, gx, gy, gz, ux, uy, uz);
....
** r = sqrt ((square (gx)) + (square (gy)) + (square (gz)));
.....
}
static void idle (void)
* {
eyex + = r;
*** if (eyex> (2 * r)) {
eyex =-r = r + eyey;
if (eyey> (2 * r)) {
eyex =-r =-r eyey; eyez + = r;
if (eyez> (3 * r)) {
fprintf (f, "% s \ t", myObjet) fprintf (f, "\ n");
exit (0);
}
}
** }
** glutPostRedisplay ();
* }```

cordially

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•