View Full Version : Reading x and y coordinate from a file

03-20-2014, 12:04 AM
I am a beginner in OpenGL with glut library. I have a file with two colums of data that represent x and y coordinates.
i want to read this data and draw point on the frame. the file is a text file and the data are like this
2 6
5 8
7 5
3 8

First column is x while second is y coordinate, single space between them. In my program if i use console application (C++) i can read the file but if i use glut OpenGL
it cannot read the file.

Agent D
03-20-2014, 12:43 AM
How do you try to read the file? What doesn't work, the reading or the drawing? My glass ball is cloudy, could you please be a bit more specific and perhaps show us your code?

Reading a text file of a format that simple is a very basic operation in most programming lanugages (except maybe brainfuck and similar), so if you have a basic understanding of the fundamentals of the programming lanuage you use, reading that file is a really primitive task, no mater what fancy libraries and framework your program uses. If you don't understand the fundamentals of the programing language you are using, it is adviseable to getting an in-depth understanding of the language at first, before learning how to use OpenGL(R) through that language.

03-25-2014, 06:34 AM
here is the code

#include <time.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

void init(void)
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(0.0, 100.0, 0.0, 150.0);

void figure(void)
glPointSize (5.0);

int i, x, y, c = 0;
//i used 2-dim array to store points
int point[100][2];
ifstream infile;


while (infile >> i)
cout <<" how many times is the while lop running" <<endl;
if (c % 2 != 0) //if i is the first integer in a row
point[c / 2][0] = i; //set as x-cord.

else //else if i is the second integer in a row
point[c / 2][1] = i; //set as y-cord.

// if a row's integers are all read, then draw the point using x/y -cord.
if(point[c / 2][0]&&point[c / 2][1])
glBegin (GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f (point[c / 2][0], point[c / 2][1]);

//cout <<i;

//cout <<c;


int main(int argc, char *argv[])
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

glutCreateWindow("Exercise 1");


cout <<" Error" << endl;

03-25-2014, 06:36 AM
Reading the file is not working, thus it cannot draw.

Agent D
03-25-2014, 10:31 AM
First of all, I just compiled and ran your code and it drew the points from the file as expected.

Second, the file loading is unnecessarily complex and loading the file every frame is not a very good idea.

03-26-2014, 08:25 PM
So any advice where am getting wrong. Attached is the snapshot of the frame with no points.1249

Agent D
03-27-2014, 12:43 AM
I compiled your code and wrote the point coordinates from your first post into a propperly named text file.
I got a white window with red dots in the lower left corner. This is the output for "glxinfo | grep -i opengl" on my system:

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Sandybridge Mobile
OpenGL core profile version string: 3.1 (Core Profile) Mesa 10.1.0
OpenGL core profile shading language version string: 1.40
OpenGL core profile context flags: (none)
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 10.1.0
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:

Are you sure the file reading works? Is the file in the working directory?

03-27-2014, 05:58 AM
sure the file is in the working directory with project file, main, dependency file and the rest. I am using codeblock IDE in windows Xp. Now i want to try Dev C++ and see what will happen.