How to open a text file with openGL?

Started by
4 comments, last by Acid-Chris 18 years, 2 months ago
Hi, i like to program an OpenGl program that opens a text file and take the first line of the text file and put it in the OpenGl Program. The text file contains numbers and commands. The Programm should take the numbers and commands and should print a picture out of it. For example the first line consist of 7/1 9/1 drawline, with this information the program should draw a line from the coordinate x=7 and y=1 to x=9 and y=1 on the screen. I hope you understand what i like to do. Thank you Frank
Advertisement
What programming language are you using?
You can use a Scripting language like angelscript or lua but you will have no control over the syntax.
The other way is just parsing the file... parser generators or even classes like regex could help you...

here a minimal example:
"c:\script.txt" :

drawline 7 1 9 1drawline 1 2 3 4


"main.cpp":
#include <fstream>#include <sstream>#include <iostream>using namespace std;void drawline(int x1,int y1,int x2, int y2){	//your ogl code	cout<<"DrawLine"<<"   x1="<<x1<<"  y1="<<y1<<"  x2="<<x2<<"  y2="<<y2<<endl;}void main(){	ifstream f("c:\\script.txt");	while(!f.eof())	{		string s;		int x1,y1,x2,y2;		f >> s;		if(0==s.compare("drawline"))		{			f>>x1>>y1>>x2>>y2;			drawline(x1,y1,x2,y2);		}//esle if(...)...else if()...else{ error }	}}


output:

DrawLine x1=7 y1=1 x2=9 y2=1
DrawLine x1=1 y1=2 x2=3 y2=4

[Edited by - Kambiz on January 26, 2006 12:59:19 PM]
You can no more open a text file in opengl than you can cook a fried egg in a toaster. In fact, somewhat less (it might be technically possible to cook a fried egg in a toaster, just very stupid and dangerous).

opengl is a graphics API and has no file IO routines, sound routines, input routines, or robot arm control routines.

Mark
I like to programm it with c++ and opengl to draw the scene.
Yes, it should be like a parser.

The program should be a Interpreter, reading the lines in the text file and drawing the picture out of it. I like to code it with c++ and opengl.
hi there,

You'll need following:
First of all, get some basic knowledge of OpenGL about rendering context initialization and windows/linux/whatever window creation. There are dozens of tutorials about this on the web. If you're using NeHe basecode, this is done for you automatically.

Next step would be to implement some kind of parser as stated in the previous posts. (again...lots of tutorials on the web and at the NeHe homepage)


Then you'll need some kind of Renderer class or some functions which handle primitive drawing.

for a very-basic-example: (don't know the whole code off-hand)
void DrawLinePrimitive(const Vector2D& start, const Vector2D& end){  //set up drawing parameters like line thickness,...  ...  //start drawing the line  glBegin(GL_LINES); //or glBegin(GL_POLYGON)    glVertex2i(start.x, start.y); glVertex2i(end.x, end.y);  glEnd();}


hope this helps!
good luck,
- christoph -

This topic is closed to new replies.

Advertisement