/*
* hello.c
* This is a simple, introductory OpenGL program.
*/
#include<stdlib.h>
#include <GL/glut.h>
#include<iostream>
#include<fstream>
using std::fstream;
using namespace std;
void draw(void)
{
/* clear all pixels */
char fileName[] = "house1.txt";
fstream instream;
instream.open("c:/house1.txt",ios::in);
if(instream.fail())
return;
glClear (GL_COLOR_BUFFER_BIT);
GLint numstrip,numlines,x, y;
instream>>numstrip;
for(int j=0;j<numstrip;j++)
{
instream>>numlines;
glBegin(GL_LINE_STRIP);
for(int i=0;i<numlines;i++)
{
instream>>x>>y;
glVertex2f(x,y);
}
}
/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
*/
glColor3f (1.0, 1.0, 1.0);
glEnd();
/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush ();
}
void init (void)
{
/* select clearing color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(draw);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
khalid_farooq 0 Newbie Poster
1
4
.25 .25
.75 .25
.75 .75
.25 .75
/*
* hello.c
* This is a simple, introductory OpenGL program.
*/
#include<stdlib.h>
#include <GL/glut.h>
#include<iostream>
#include<fstream>
using std::fstream;
using namespace std;
void draw(void)
{
/* clear all pixels */
char fileName[] = "house1.txt";
fstream instream;
instream.open("c:/house1.txt",ios::in);
if(instream.fail())
return;
glClear (GL_COLOR_BUFFER_BIT);
GLint numstrip,numlines,x, y;
instream>>numstrip;
for(int j=0;j<numstrip;j++)
{
instream>>numlines;
glBegin(GL_LINE_STRIP);
for(int i=0;i<numlines;i++)
{
instream>>x>>y;
glVertex2f(x,y);
}
}
/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
*/
glColor3f (1.0, 1.0, 1.0);
glEnd();
/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush ();
}
void init (void)
{
/* select clearing color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(draw);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Edited by Narue because: Added code tags
gusano79 247 Posting Shark
The problem is in this line:
GLint numstrip, numlines, x, y;
You're declaring x
and y
as integer variables, but the values in your text file are decimals. Try declaring your variables like this:
GLint numstrip, numlines;
GLfloat x, y;
That got it to draw something for me.
Also: You're loading the contents of house1.txt
inside your draw
function. I can see how this makes the program simpler to write, but realize that it will open and read the file every single time the GLUT window needs to redraw itself--put some output to cout
inside of draw
if you're not sure what I mean there. It is much better practice to attempt to read the file only once, before you even initialize GLUT, and store its contents into a variable somewhere that draw
can reference later.
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.