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:
#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
glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluPerspective(45.0, 1, 0.1, 100.0);
// Set camera transformation
glMatrixMode(GL_MODELVIEW);glLoadIdentity();
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