Sign in to follow this  

How to open a text file with openGL?

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 1
drawline 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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 -

Share this post


Link to post
Share on other sites

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this