OpenGL Problem

I do not see a glut display func :
http://www.opengl.org/resources/libraries/glut/spec3/node46.html

The 2 glutIdleFunc(initDraw); calls inside MouseFunc should be replaced by glutPostRedisplay();

You should separate the drawing part (in glut display func) from the updating part. Currently you mix both in initDraw…

Didn’t fix the problem…

You realize you are a very-hard-to-help person ?

Okay well a good idea came to me to record it and flish through each frame to see the text displayeed before the windo closes down (it’s only 1 frame REALLY FAST)

and it says
Fatal Error: redisplay needed for window 1, but no display callback

What’s that

I would like to think that’s going to make me a Very-easy-to-help-person.

Oops Zbuffer i didn’t see your Post. that should work

Okay well I’ve done some code changes to try optimize :smiley: and fixed my problem, BUT my squre doesn’t show when i click on the screen


#include <iostream>
#include <gl\glut.h>
using namespace std;
// Particle Spin
int Spin = 0;
// Box Position X, Y
float Position[2] = {0, 0};
// Operation Interval 1/60th of a second
float Interval = (1 / 60);

// Random number
int Rnd(int Min, int Max)
{
	int Random;
	int Range(Max - Min);
	Random = Min+int(Range*rand()/(RAND_MAX + 1));
	return Random;
}

void drawBox(void)
{
	Spin += Rnd(-2, 2);
	if(Spin >= 360)
		Spin = 0;
	if(Spin <= -360)
		Spin = 0;
	// Falling of the Box
	Position[0] -= (9.81 / Interval);
	// Drawing
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glOrtho(0, 100, 0, 200, 0, 1);
	glBegin(GL_POLYGON);
		glColor3f(0,0,0);
		glRectf(Position[0],Position[1],(Position[0] + 10),(Position[1] + 10 ));
		glRotatef(Spin, 0, 0, 0);
	glEnd();
	glFlush();
	glutSwapBuffers();
}

void bufferClearInit(void)
{
	// Sets Clear Buffer for Colour and Depth
	glClearColor(1, 1, 1, 1);
	glClearDepth(0);
}
void MouseFunc(int button, int state, int x, int y)
{
	if((button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN))
	{
		Position[0] = x;
		Position[1] = y;
		glutPostRedisplay();
	}
	if((button == GLUT_LEFT_BUTTON) & (state == GLUT_UP))
	{
		glutPostRedisplay();
	}
}
int main(int argc, char** argv)
{
	// Window Settings
	glutInit(&argc, argv);
	glutInitWindowPosition(300,100);
	glutInitWindowSize(400,800);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutCreateWindow("Boxes");
	bufferClearInit();
	glutDisplayFunc(drawBox);
	// Mouse Functions
	glutMouseFunc(MouseFunc);
	glutMainLoop();
}

Just a couple of things, I noticed:


#include <gl\glut.h>

using backslash “” as directory separator is a windows’ism, use forward slash “/”.


glBegin(GL_POLYGON);
glColor3f(0,0,0);
glRectf(Position[0],Position[1],(Position[0] + 2),(Position[1] + 2 ));
glRotatef(Spin, 0, 0, 0);
glEnd();

glRect and glRotate are not allowed between glBegin/glEnd. glRotate modifies the active transformation, which affects all vertices issued afterwards, so it would be clearer to do that first and then draw something otherwise the glRotate call will only have a visible effect in the next frame.
Finally, the axis around which you rotate is (0, 0, 0), hmm…

Why should it? What are the coordinate bounds of your screen? And what are the coordinates of your square? Are you sure your square is within the viewing volume? First suggestion, print out the coordinates of the square you are trying to generate. Make sure the numbers make sense. Next, read up on glOrtho. You have to use it to assign a coordinate system to the window you’ve opened. An example is below. This sets up a coordinate system from -20 to +20 in X and Y, and from +1 to -1 in Z. If your square lies outside this 3D viewing volume, nothing will be drawn on the screen.


    glMatrixMode   (GL_PROJECTION);
    glLoadIdentity ();
    glOrtho        (-20, 20, -20, 20, 1, -1);
    glMatrixMode   (GL_MODELVIEW);
    glLoadIdentity ();

MaXH What’s happening with the Matrixs What do they do?

AND applying what you write doesn’t help.