Hiya,
Im trying to use the subdivided icosahedron from the redbook, and use the gluLookAt() function to look at each of the vertices that are plotted and then record there pixel value to a text file, I think this all works - however (there always is a however isnt there!)
Basically I know that I need to “re-display” after ive used gluLookAt, otherwise nothing moves and I just record the same pixel value again and again (Thats whats happening),
Im really stuck with this, ive tried numerous things for the past day or 2 now, and I really can’t figure it out, posting here was a last resort…
Any advice would be greatly recieved by this village idiot…(code below, there are 2 files in the project “main.c” and “scan.c” think my problem is in scan.c which is at the bottom)
thanks!
Dave
///////////////////////////////////main.c\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//main.c
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>
#define A 0.5257311 //more precision needed?
#define B 0.8506508 //more precision needed?
FILE *fp;
static GLfloat vdata[12][3]=
{
{-A, 0, B}, {A, 0, B}, {-A, 0, -B}, {A, 0, -B},
{0, B, A}, {0, B, -A}, {0, -B, A}, {0, -B, -A},
{B, A, 0}, {-B, A, 0}, {B, -A, 0}, {-B, -A, 0}
};
static GLuint index[20][3] =
{
{1, 4, 0}, {4, 9, 0}, {4, 9, 5}, {8, 5, 4}, {1, 8, 4},
{1, 10, 8}, {10, 3, 8}, {8, 3, 5}, {3, 2, 5}, {3, 7, 2},
{3, 10, 7}, {10, 6, 7}, {6, 11, 7}, {6, 0, 11}, {6, 1, 0},
{10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}
};
int NumberofDivisions = 2; //5 seems to provide a nice resolution
void scan(void);
void DrawTriangle(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3])
{
fprintf(fp,"%f, %f, %f
", v1[1], v1[2], v1[3]);
fprintf(fp,"%f, %f, %f
", v2[1], v2[2], v2[3]);
fprintf(fp,"%f, %f, %f
", v3[1], v3[2], v3[3]);
glBegin(GL_POLYGON);
glNormal3fv(v1);
glVertex3fv(v1);
glNormal3fv(v2);
glVertex3fv(v2);
glNormal3fv(v3);
glVertex3fv(v3);
glEnd();
}
void Normalize(float v[3])
{
GLfloat d;
d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
if(d == 0)
{
exit(0);
}
v[0] /= d;
v[1] /= d;
v[2] /= d;
}
void NormCrosProd(float u[3], float v[3], float out[3])
{
out[0] = u[1] * v[2] - u[2] * v[1];
out[1] = u[2] * v[0] - u[0] * v[2];
out[2] = u[0] * v[1] - u[1] * v[0];
Normalize(out);
}
void Subdivide(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3], int depth)
{
GLfloat v12[3], v23[3], v31[3];
int i;
if(depth == 0)
{
DrawTriangle(v1, v2, v3);
return;
}
for(i = 0; i < 3; i++)
{
v12[i] = (v1[i] + v2[i]) / 2.0;
v23[i] = (v2[i] + v3[i]) / 2.0;
v31[i] = (v3[i] + v1[i]) / 2.0;
}
Normalize(v12);
Normalize(v23);
Normalize(v31);
Subdivide(v1, v12, v31, depth-1);
Subdivide(v2, v23, v12, depth-1);
Subdivide(v3, v31, v23, depth-1);
Subdivide(v12, v23, v31, depth-1);
}
void init(void)
{
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glClearColor(0.0, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_SHADE_MODEL,GL_NICEST);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void reshape (int w, int h)
{
if(h == 0)
{
h = 1;
}
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, w/h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display(void)
{
int i, j;
GLfloat d1[3], d2[3], norm[3];
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glPolygonMode ( GL_FRONT_AND_BACK, GL_FILL );
for(i = 0; i <= 20; i++)
{
Subdivide(vdata[index[i][0]], vdata[index[i][1]], vdata[index[i][2]], NumberofDivisions);
//subdivide and draw sphere
}
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: //bug out.
exit(0);
break;
case 'a':
case 'A':
scan();
break;
}
}
int main(int argc, char** argv)
{
fp = fopen("c:/realdata.txt", "a");
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Recursive subdivision of a sphere");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
fclose(fp);
return 0;
}
///////////////////////////////////scan.c\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//scan.c
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
struct Vectors{
float X;
float Y;
float Z;
};
struct Vectors *vData;
void scan(void)
{
printf("we have go
");
int i = 1;
int j = 1;
FILE *fpa;
FILE *fpb;
char filename[256];
vData = (struct Vectors *) malloc(240 + 1 * sizeof(struct Vectors));
if((fpa = fopen("c:/realdata.txt", "r")) == NULL)
{
printf("ut-oh spageteo! vector file couldnt be opened
");
exit ( 0 );
}
while(i < 240 && fscanf(fpa, "%f,%f,%f", &vData[i].X, &vData[i].Y, &vData[i].Z) !=EOF)
{
++i;
}
fclose(fpa);
fpb = fopen("c:/intensityreal.txt", "a");
GLubyte pixel[3];
for(j = 1; j <=240; j++)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(vData[j].X + 1, vData[j].Y + 1, vData[j].Z + 1, 0, 0, 0, 0, 1, 0);
glReadBuffer(GL_BACK);
glReadPixels(250,250,1,1,GL_RGB,GL_UNSIGNED_BYTE,(void *)pixel);
fprintf(fpb, "%d
", pixel[0]);
}
fclose(fpb);
}