PDA

View Full Version : Drawing with a txt file



Hellcowboy
09-11-2011, 07:33 PM
ok here is my code so far I know the problem is in my display function
as I need a loop with a nested loop as the outer loop n has the number of polylines and the inner loop has lines as the array that has number of segments with an X and Y. My code says it has no errors but when I run it it says it has error.

code:
#include <windows.h>
#include <gl/glut.h>
#include <stdio.h>


struct polyLine {
int n;
float* x;
float* y;

};

struct sketch {
int n;
struct polyLine* lines;
};



struct sketch face;

FILE* openFile() {
FILE* fp = fopen("C:\\floatface.txt", "r");
if (fp == NULL) {
printf("file did not open");
}
return fp;
}

struct polyLine readPolyLine(FILE* fp) {
struct polyLine line;
int i;
int n = fscanf(fp, "%d", &amp;line.n);
if (n==0 || n==EOF) {
line.n = 0;
return line;
}
line.x = (float*)malloc(sizeof(float)*line.n);
line.y = (float*)malloc(sizeof(float)*line.n);
for (i=0; i<line.n; i++) {
fscanf(fp, "%f %f", line.x+i, line.y+i);
}
return line;
}

struct sketch readFile() {
FILE* fp = openFile();
struct sketch s;
int i;
fscanf(fp, "%d", &amp;s.n);
s.lines = (struct polyLine*)malloc(sizeof(struct polyLine)*s.n);
for(i=0; i<s.n; i++) {
s.lines[i] = readPolyLine(fp);
}
return s;
}


void display(int n,struct polyLine* lines,struct polyLine line) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
while(n<0) {
while(lines<0) {
glVertex2f( line.x[1], line.y[1]);
lines++;}
n++; }
glEnd();
}

void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}


int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("hello");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

txt file
37
5 0.5234375 0.44921875 0.5 0.4375 0.51953125 0.41796875 0.546875 0.4375 0.52734375 0.44921875
5 0.7421875 0.44921875 0.71875 0.4375 0.73828125 0.41796875 0.765625 0.4375 0.7421875 0.44921875
3 0.46484375 0.42578125 0.51953125 0.453125 0.57421875 0.4296875
3 0.69140625 0.42578125 0.7421875 0.44921875 0.79296875 0.42578125
3 0.47265625 0.42578125 0.51953125 0.4140625 0.57421875 0.4296875
3 0.69140625 0.42578125 0.74609375 0.4140625 0.78515625 0.421875
3 0.4609375 0.44140625 0.515625 0.46484375 0.578125 0.4453125
3 0.6875 0.44140625 0.74609375 0.46484375 0.796875 0.44140625
3 0.49609375 0.3984375 0.52734375 0.40234375 0.5625 0.4140625
3 0.6953125 0.41015625 0.73046875 0.3984375 0.765625 0.3984375
6 0.609375 0.453125 0.609375 0.40234375 0.59765625 0.35546875 0.6015625 0.328125 0.609375 0.30078125 0.62890625 0.2890625
6 0.6484375 0.453125 0.6484375 0.40234375 0.6484375 0.3515625 0.65625 0.328125 0.65234375 0.30078125 0.62890625 0.2890625
6 0.5859375 0.33984375 0.57421875 0.32421875 0.5703125 0.3046875 0.578125 0.2890625 0.59765625 0.30078125 0.62890625 0.2890625
6 0.67578125 0.33984375 0.6875 0.328125 0.69140625 0.3046875 0.6796875 0.2890625 0.6640625 0.30078125 0.63671875 0.2890625
6 0.4375 0.46484375 0.44140625 0.484375 0.48828125 0.50390625 0.54296875 0.5 0.5859375 0.48828125 0.59375 0.46875
6 0.66796875 0.46875 0.67578125 0.484375 0.7265625 0.49609375 0.77734375 0.5 0.8125 0.484375 0.82421875 0.46484375
4 0.4375 0.4609375 0.484375 0.484375 0.5390625 0.4765625 0.59375 0.46875
4 0.66796875 0.46875 0.73046875 0.4765625 0.78125 0.484375 0.8203125 0.46484375
7 0.53515625 0.20703125 0.58203125 0.22265625 0.609375 0.234375 0.6328125 0.22265625 0.65625 0.23046875 0.69140625 0.22265625 0.73046875 0.2109375
7 0.5390625 0.20703125 0.578125 0.20703125 0.609375 0.2109375 0.63671875 0.20703125 0.6640625 0.2109375 0.6953125 0.20703125 0.7265625 0.2109375
7 0.5390625 0.20703125 0.58203125 0.20703125 0.609375 0.2109375 0.63671875 0.20703125 0.6640625 0.2109375 0.69140625 0.2109375 0.7265625 0.20703125
7 0.55078125 0.203125 0.578125 0.19140625 0.60546875 0.1796875 0.63671875 0.17578125 0.66796875 0.1796875 0.69921875 0.19140625 0.72265625 0.20703125
3 0.40234375 0.44921875 0.39453125 0.375 0.40625 0.29296875
3 0.85546875 0.453125 0.8671875 0.37890625 0.8515625 0.30078125
7 0.38671875 0.4140625 0.359375 0.4375 0.34375 0.41796875 0.3515625 0.375 0.3671875 0.3203125 0.38671875 0.26953125 0.40625 0.28125
7 0.875 0.41796875 0.90234375 0.4375 0.9140625 0.41015625 0.90625 0.375 0.8984375 0.32421875 0.875 0.27734375 0.85546875 0.28125
11 0.40625 0.29296875 0.421875 0.22265625 0.44921875 0.1640625 0.50390625 0.109375 0.57421875 0.0625 0.6328125 0.05078125 0.703125 0.06640625 0.765625 0.109375 0.80859375 0.16015625 0.83984375 0.22265625 0.85546875 0.3046875
13 0.39453125 0.4375 0.41796875 0.49609375 0.4453125 0.5546875 0.46875 0.59375 0.51171875 0.62890625 0.5703125 0.640625 0.625 0.63671875 0.6796875 0.62890625 0.734375 0.625 0.78515625 0.59765625 0.8203125 0.5546875 0.84765625 0.5078125 0.8671875 0.44140625
13 0.36328125 0.203125 0.3046875 0.32421875 0.296875 0.4453125 0.3203125 0.60546875 0.38671875 0.7265625 0.50390625 0.8203125 0.6171875 0.828125 0.734375 0.82421875 0.84765625 0.75 0.921875 0.6328125 0.95703125 0.4765625 0.9765625 0.34375 0.91015625 0.21875
3 0.56640625 0.31640625 0.54296875 0.2890625 0.52734375 0.2578125
3 0.6953125 0.3125 0.72265625 0.28515625 0.7421875 0.25390625
3 0.41015625 0.3046875 0.42578125 0.28125 0.4375 0.2578125
3 0.8515625 0.3046875 0.8359375 0.28515625 0.82421875 0.26171875
2 0.62109375 0.2734375 0.62109375 0.24609375
2 0.64453125 0.2734375 0.64453125 0.24609375
2 0.6328125 0.09375 0.6328125 0.0703125
3 0.59765625 0.1484375 0.63671875 0.15625 0.67578125 0.14453125

nakedman
09-11-2011, 07:39 PM
Can you be a little more specific about the errors it gives you? Looks like a fun idea to read it like this :)