on my university I got an assignment to build a program on OpenGl that read from a given txt file a coordinates of some lines and draw them to the screen.
i thank everyone that might help me, I know a little bit programming on C but I'm a little confused when it connects with OpenGl.
Thanks a lot.

Dani AI

Generated

As described, the task splits into two clear parts: (1) read and parse the text file into a list of line segments, and (2) render those segments with an OpenGL context. As pointed out, having a small, working example makes debugging much easier. Below is a compact, practical approach that avoids OpenGL trickiness and keeps the C-side simple.

A robust parsing pattern (assumes each text line is "x1 y1 x2 y2"):

typedef struct { float x1,y1,x2,y2; } Line;

Line *lines = NULL;
size_t nlines = 0;

FILE *f = fopen("lines.txt","r");
if (!f) { perror("lines.txt"); exit(1); }

char buf[256];
while (fgets(buf, sizeof(buf), f)) {
    float a,b,c,d;
    if (sscanf(buf, "%f %f %f %f", &a,&b,&c,&d) == 4) {
        lines = realloc(lines, (nlines+1)*sizeof *lines);
        lines[nlines++] = (Line){a,b,c,d};
    }
}
fclose(f);

After loading, compute the bounding box (min/max of all coordinates), add a small margin, and set an orthographic projection so the file coordinates map directly to the view. For simple assignments the legacy pipeline is easiest:

glMatrixMode(GL_PROJECTION); glLoadIdentity();
glOrtho(minx-pad, maxx+pad, miny-pad, maxy+pad, -1, 1);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();

glBegin(GL_LINES);
for (i=0; i<nlines; ++i) {
    glVertex2f(lines[i].x1, lines[i].y1);
    glVertex2f(lines[i].x2, lines[i].y2);
}
glEnd();

Troubleshooting notes: always check fopen/sscanf return values; watch for file format variations (commas, extra columns, header lines); remember OpenGL’s default Y axis is upwards (flip Y if the input uses screen coordinates). For larger datasets or modern learning goals, replace immediate mode with VBOs+shaders; for a quick university project the above is simpler and widely accepted.

Sorry, but we don't do your school work for you. Make an effort. Post your code and errors/problems you are having here and we may be able to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.