PDA

View Full Version : Display function



Hellcowboy
09-09-2011, 12:03 PM
Ok got most of the code but I don't know how to start a display function

Code:
#include <windows.h>
#include <gl/glut.h>

// new code starts here
#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(void) {
glClear(GL_COLOR_BUFFER_BIT);
sketch readFile();
glEnd();
glFlush();
glutSwapBuffers();
}
}

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;
}

Text File thats imported:
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

xiphos
09-12-2011, 12:11 AM
update display function with
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();

for(int t = 0; t < face.n; t++)
{
glBegin(GL_LINES);
for(int i = 0; i < face.lines[t].n; i ++)
{
glVertex2f(face.lines[t].x[i] , face.lines[t].y[i]);
}
glEnd();

}

glFlush();
glutSwapBuffers();
}
and call
face = readFile(); in init() function.

See if it works.

Just curiosity, how did you generated that text file?

BionicBytes
09-12-2011, 06:46 AM
Now you have two threads open relating to the exact same problem.
Please refrain from doing this. If people aren't responding then perhaps you should take note of what you have posted and write it again more clearly giving a more exact description of the actual problem.