VERY basic question: how to refresh drawing screen?

I’m the beginner of beginners… just started coding in OpenGL last night

OpenGL is very weird. I don’t understand how all those functions work. It seems like they don’t follow C++ syntax at all. How does it loop using glutMainLoop()… it looks like linear executions but it can’t be if it’s to perform the functions… how a function has another function as a parameter and the the parameter function has 4 parameters but when called, it’s called using only void as parameter… on and on… I just had to babble on my frustrations. Sorry.

I went around and looked at some sample codes and modified them. I’m using glut.

What my prog basically does is draw some voxels and move around if the mouse button is clicked.

I wanna refresh the screen everytime the mouse is clicked so I out the glutPostRedisplay() function inside the glutMouse() but it just draws over whatever was already there. Then I threw in glutPostRedisplay() everywhere but it still doesn’t refresh the screen.

My main() looks like this…
DrawPoints(view, vv);
glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (640,480);
glutCreateWindow (“View”);
glutDisplayFunc (glutDisplay);
glutReshapeFunc (glutResize);
glutMouseFunc(glutMouse);
glInit();
glutMainLoop();

Sorry for long post but can someone please help me?

Also if you know a good tutorial or reference, feel free to post the link.

[This message has been edited by b1st (edited 04-12-2000).]

[This message has been edited by b1st (edited 04-12-2000).]

here’s my source code:

#include <iostream.h>
#include <math.h>
#include <GL/glut.h>

class Point{
public:
Point(){
x=y=z=0.0;
}
Point(double X,double Y,double Z){
x=X;y=Y;z=Z;
}
double x,y,z;
bool exists;
};

class Voxel{
public:
Voxel(){}
Voxel(double x,double y, double z, int r){
location.x = x;
location.y = y;
location.z = z;
radius = r;
}
void draw(){}
void move(Point p){
location.x += p.x;
location.y += p.y;
location.z += p.z;
}
Point location;
int radius;
};

class Vector{
public:
Vector(){dx=dy=dz=0;}
Vector(double x, double y, double z){
dx = x; dy = y; dz = z;}
double dx;
double dy;
double dz;
};

ostream& operator<<(ostream &os, Point p){
os<<p.x<<" “<<p.y<<” "<<p.z;
return os;
}

ostream& operator<<(ostream &os, Voxel v){
os<<v.location;
return os;
}

ostream& operator<<(ostream &os, Vector v){
os<<v.dx<<" “<<v.dy<<” "<<v.dz;
return os;
}

bool operator==(Point a, Point b){
return (a.x==b.x && a.y==b.y && a.z==b.z);
}

Vector CrossProd(Vector v1, Vector v2){
Vector v(v1.dyv2.dz-v1.dzv2.dy, v1.dzv2.dx-v1.dxv2.dz, v1.dxv2.dy-v1.dyv2.dx);
return v;
}

void Fix(Vector &v){
if (v.dx == 0) v.dx = .0000001;
if (v.dy == 0) v.dy = .0000001;
if (v.dz == 0) v.dz = .0000001;
}

double Dx(Point view, Vector vv, Voxel voxel){
Fix(vv);

Vector vvv(vv.dx*-1,-1*(vv.dx*-1*vv.dx + vv.dz*-1*vv.dz)/vv.dy,vv.dz*-1);
vv = CrossProd(vvv,vv);

double d = -1*(vv.dx*(view.x+1) + vv.dy*(view.y+1) + vv.dz*(view.z-(vv.dx+vv.dy)/vv.dz));
return 
	(vv.dx*voxel.location.x + vv.dy*voxel.location.y + vv.dz*voxel.location.z + d)
	/ sqrt(vv.dx*vv.dx + vv.dy*vv.dy + vv.dz*vv.dz);

}

double Dy(Point view, Vector vv, Voxel voxel){
Fix(vv);

vv.dy = -1*(vv.dx*-1*vv.dx + vv.dz*-1*vv.dz)/vv.dy;
vv.dx = vv.dx*-1;
vv.dz = vv.dz*-1;

double d = -1*(vv.dx*(view.x+1) + vv.dy*(view.y+1) + vv.dz*(view.z-(vv.dx+vv.dy)/vv.dz));
return 
	(vv.dx*voxel.location.x + vv.dy*voxel.location.y + vv.dz*voxel.location.z + d)
	/ sqrt(vv.dx*vv.dx + vv.dy*vv.dy + vv.dz*vv.dz);

}

double Dz(Point view, Vector vv, Voxel voxel){
Fix(vv);

double d = -1*(vv.dx*(view.x+1) + vv.dy*(view.y+1) + vv.dz*(view.z-(vv.dx+vv.dy)/vv.dz));
return 
	(vv.dx*voxel.location.x + vv.dy*voxel.location.y + vv.dz*voxel.location.z + d)
	/ sqrt(vv.dx*vv.dx + vv.dy*vv.dy + vv.dz*vv.dz);

}

Point p[30000];
Voxel v[30000];
Point view(10,10,10);
Vector vv(4,4,4);
Vector vvv(1/4,1,-1/4);

int winH=640, winW=480;
//-------------------------Miller’s code, modified by me--------------------------------------
// Draw the axis’
void DrawAxis(void)
{
int step = 10;
int hashWidth = 5;

glColor3f(0.5, 0.5, 0.5);

// Hash marks for the y - axis
for (int y = step; y < winH / 2; y += step)
{
glBegin(GL_LINES);
glVertex2f(-hashWidth, y);
glVertex2f(hashWidth, y);
glVertex2f(-hashWidth, -y);
glVertex2f(hashWidth, -y);
glEnd();
}

// Hash marks for the x - axis
for (int x = step; x < winW / 2; x += step)
{
glBegin(GL_LINES);
glVertex2f(x, hashWidth);
glVertex2f(x, -hashWidth);
glVertex2f(-x, hashWidth);
glVertex2f(-x, -hashWidth);
glEnd();
}

glColor3f(1, 1, 1);
glBegin(GL_LINES);
// x - axis
glVertex2f(-winW / 2, 0);
glVertex2f(winW / 2, 0);
// y - axis
glVertex2f(0, winH / 2);
glVertex2f(0, -winH / 2);
glEnd();
}

void DrawPoints(Point view, Vector vv)
{
double dx, dy, dz;
int finalDx, finalDy;

for (int q=0; q&lt;30000; q++)
if (v[q].location.exists)
{

	dx = Dx(view, vv, v[q]);
	dy = Dy(view, vv, v[q]);
	dz = Dz(view, vv, v[q]);
	if (dz&gt;0){
		if (dx&gt;0) finalDx = dx - (dz/dx/10);
		if (dx&lt;0) finalDx = dx + (dz/dx/10);
		if (dx==0) finalDx = 0;
		if (dy&gt;0) finalDy = dy - (dz/dy/10);
		if (dy&lt;0) finalDy = dy + (dz/dy/10);
		if (dy==0) finalDy = 0;

		p[q].exists = true;
		p[q].x = finalDx;
		p[q].y = finalDy;
	}
}

}

void glutDisplay(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

for (int q=0; q<30000; q++)
if (p[q].exists)
{
glColor3f(1, 0, 0.5);
glBegin(GL_POINTS);
glVertex2f(p[q].x, p[q].y);
glEnd();
}

DrawAxis();
glutSwapBuffers();
}

void glutMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
view.x = view.x + vv.dx;
view.y = view.y + vv.dy;
view.z = view.z + vv.dz;
}
else if (button == GLUT_RIGHT_BUTTON){
view.x = view.x - vv.dx;
view.y = view.y - vv.dy;
view.z = view.z - vv.dz;
}
DrawPoints(view,vv);
glutPostRedisplay();
}

void glutResize(int w, int h)
{
if (!h)
return;

winW = w;
winH = h;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, winW, winH);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Creates a 2D projection
// winW / 2 is far right, - is far left
// winH / 2 is top, - is bottom

glOrtho(-winW / 2, winW / 2, -winH / 2, winH / 2, -1, 1);

glutPostRedisplay();
}

void glInit (void)
{
glClearColor(0, 0, 0, 0);
glPointSize(2);
// Makes our points round rather than boxes
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

//---------------------------------------Miller’s code-------------------------------

void main(){

for (int t=0; t&lt;30000; t++){
	v[t].location.exists = false;}

double a;
int yy;

int pos = 0;
for ( yy=0; yy&lt;40; yy++){
	for (a=0.0; a&lt;6.1; a = a + .3){
		v[pos].location.x = cos(a)*40.0;
		v[pos].location.y = sin(a)*40.0;
		v[pos].location.z = 20 + yy;
		v[pos].location.exists = true;
		pos++;
	}
}

for (t=0; t&lt;300; t+=3){
	for (yy=0; yy&lt;300; yy+=3){
		v[pos].location.x = t;
		v[pos].location.y = yy;
		v[pos].location.z = 50;
		v[pos].location.exists = true;
		pos++;
	}
}

for (a=0.0; a&lt;6.1; a = a + .0061){
	v[pos].location.x = 20;
	v[pos].location.y = sin(a)*200.0;
	v[pos].location.z = cos(a)*200.0;
	v[pos].location.exists = true;
	pos++;
}
cout&lt;&lt;"The number of Voxels displayed is: "&lt;&lt;pos&lt;&lt;endl;

	DrawPoints(view, vv); 
	glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize (640,480);
	glutCreateWindow ("View");
	glutDisplayFunc (glutDisplay);
	glutReshapeFunc (glutResize);
	glutMouseFunc(glutMouse);
	glInit();
	glutMainLoop();

}

Hello

OpenGL is not wierd, you have to learn it first

Well, I can understand that GLUT is looking abit confusing if you don’t know how it work. You call different glut functions in the beginning of the program to setup one or more redering winndow. Then you call a few glut functions to set functions to be called when something happens (move the mouse, press a key, redisplay/resize window). All those functions, like glutMouseFunc(), takes a functionpointer as argument. The argument should point to a function of a certain type (glutMouseFunc needs a pointer to a function with four integers as argument, button which is pressed, state of button and position on screen). When you are set, you call glutMainLoop. This fucntion is a never-ending function (you never exit from it), which call all functions you specified when something happens.

And yes, it does follow the C syntax (and then it should follow the C++ syntax too )

If you need a good tutorial you MUST (!!!) visit NeHe’s OpenGL tutorials @ http://nehe.gamedev.net/
NeHe is not using GLUT, and all examples are coded for Win32 exvironment using MSVC (ports to different platforms/compilers can be found too).

Bob