#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <glut.h>
#include <stdio.h>

void drawCube();

float vertices [8][3];
int facets [6][4];



void read_file (char **argv);

void init(void)
{
    	glClearColor (0.0, 0.0, 0.0, 0.0);
    	glShadeModel (GL_FLAT);
}


void display(void)
{
        glClear(GL_COLOR_BUFFER_BIT);
	drawCube();
	glEnd();
	glFlush();
	glutSwapBuffers();    	
}
void reshape (int w, int h)
{
     	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
     	glMatrixMode (GL_PROJECTION);
     	glLoadIdentity ();
     	gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
     	glMatrixMode(GL_MODELVIEW);
     	glLoadIdentity();
	glTranslatef (0.0, 0.0, -5.0);
}
int main(int argc, char** argv)
{
	read_file(argv);
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize (500, 500);
	glutInitWindowPosition (100, 100);
	glutCreateWindow (argv[0]);
	init ();
	glutDisplayFunc(display);
     	glutReshapeFunc(reshape);
     	//glutKeyboardFunc(keyboard);
     	glutMainLoop();
     	return 0;
}

void drawCube()
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glTranslatef(225.0f, 225.0f, 0.0f);
	glBegin(GL_POLYGON);

	for (i = 0; i < 6; ++i)
	{
		glVertex3fv(vertices[facets[i][0]]);
		glVertex3fv(vertices[facets[i][1]]);
		glVertex3fv(vertices[facets[i][2]]);
		glVertex3fv(vertices[facets[i][3]]);

	}
	
	glEnd();
	glFlush();
	glutSwapBuffers();

}
void read_file (char **argv)
{
	FILE *Map;
	int loopcounter;
	Map = fopen("Map.txt", "r");
	
	if (Map == NULL)
	{
		printf("\ncan't open input file\n");
		exit (1);
	}
	
	for (loopcounter = 0 ; loopcounter < 8 ; loopcounter++ )
	{
		fscanf(Map,"%f", &vertices[loopcounter][0]);
        	printf("%f ",vertices[loopcounter][0]);
        	
		fscanf(Map,"%f", &vertices[loopcounter][1]);
		printf("%f ",vertices[loopcounter][1]);
		
		fscanf(Map,"%f", &vertices[loopcounter][2]);
		printf("%f\n",vertices[loopcounter][2]);
	}
	
	for (loopcounter = 0 ; loopcounter < 6 ; loopcounter++ )
	{
		fscanf(Map,"%d", &facets[loopcounter][0]);
		printf("%d ",facets[loopcounter][0]);
		
		fscanf(Map,"%d", &facets[loopcounter][1]);
		printf("%d ",facets[loopcounter][1]);
		
		fscanf(Map,"%d", &facets[loopcounter][2]);
		printf("%d ",facets[loopcounter][2]);
		
		fscanf(Map,"%d", &facets[loopcounter][3]);
		printf("%d\n",facets[loopcounter][3]);
	}
}

Dani AI

Generated

Quick, practical explanation for future readers and what fixed this thread: the visible GLUT window with no visible output usually comes from one or more rendering/buffering mistakes (geometry pushed outside the view frustum, mismatched begin/end, or swapping the wrong buffers) or from file/console issues (file not found, output buffered). was right to suggest commenting out glTranslatef(225.0f, 225.0f, 0.0f) — that large translation will move your model far outside the perspective frustum created with a near/far of 1.0/20.0 and the view translate back by about 5 units. Removing or reducing that translation (or moving the camera instead) typically makes the geometry visible again.

Checklist of concrete fixes and checks (apply in this order):

  • Only clear the buffer in the display callback and do all buffer swaps there. Don’t call glClear/glutSwapBuffers inside a helper like your draw function.
  • Match every glBegin with exactly one glEnd in the same logical scope; remove the stray glEnd in display. GL errors from mismatched begin/end will prevent drawing.
  • Use consistent buffering: either create a double-buffered window (GLUT_DOUBLE) and call glutSwapBuffers, or keep GLUT_SINGLE and use glFlush (do not call swap on a single buffer).
  • Keep transforms reasonable: use small model translations or scale the model, or change the camera (e.g., gluLookAt) rather than huge offsets. Check your near/far planes if you need large coordinates.

Debugging tips:

  • Confirm Map.txt is in the program’s working directory; test fopen/fscanf return values and print errors to stderr or call fflush(stdout) so debug text appears reliably.
  • After drawing calls, query glGetError() to catch OpenGL errors.
  • Prefer explicit primitives for quads/triangles (quads or two triangles per face) to avoid polygon ambiguity.

It looks like fixed the issue after trying the translation change; that is the most common cause in situations like this.

Recommended Answers

All 2 Replies

Inside drawCube , comment out this line and see what happens:

glTranslatef(225.0f, 225.0f, 0.0f);

Some other things you might consider:

  • GLUT already includes gl.h and glu.h , so you don't need to include them yourself.
  • In display , why are you calling glEnd ? There's no matching glBegin .
  • Why does read_file have a parameter? You're not using it.
  • You created your GLUT window with the GLUT_SINGLE option, which makes it single-buffered. There's never a need to call glutSwapBuffers .

hmm solved my problem.Thanks B happy.

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.