Compiler problem - Please Help

I have just started the last few weeks learning OpenGL, although I’m familiar with C/C++. My problem is that the code that I write on a UNIX terminal, my compiler at home will not compile it.

The problem occurs when I try to use the keyboard and mouse functions on open a data (map) file. In both cases the compilation takes place and looks fine, but when I run the program, it either does nothing or crashes.

At home I have tried a Watcom 11.0a and MS Visual C++ 6.0 compiler, while on the Unix compiler everything runs fine. I have included the windows.h.

Feel free to mail me if you think you can help me.

Thanks for your time and help.

Vasilis

If you are using glut on both systems, it should compile. But if you are using motif
for making your windows, it will not compile or run for you on a windows machine.

I’m using Glut, but I am wondering if I the keyboard and mouse functions, have different parameters, or used differently under the different terminals.

Should I post up my code for somebody to have a look at? It’s only about 50-70 lines…

Also would it be better to get input from the keyboard using ascii numbers or the actual letters? What should I use for the special keys, i.e. ESC, Ctrl etc ??

Thanks again for your help

Vasilis

/* Vas Paint Program */

#include <windows.h> /* Windows Library /
#include <GL/gl.h> /
OpenGL Library /
#include <GL/glut.h> /
Glut Library /
#include <math.h> /
Math Library */

int down = 0;
int true = 1;
int false = 0;

void circle(int xcentre,int ycentre, float radius){
float angle, angle_increment;
float next_x,next_y;
int i;

angle_increment = (3.14159*2/100);

glBegin(GL_LINE_LOOP);
for (i=0; i<=100; i++){
angle = angle + angle_increment;
next_x = radius * cos(angle);
next_y = radius * sin(angle);
next_x = next_x + xcentre;
next_y = next_y + ycentre;
glVertex2f(next_x,next_y);
}
glEnd();
}

void display(void){
glClear(GL_COLOR_BUFFER_BIT);
}

void mouse(int button, int state, int x, int y){
if (state == GLUT_UP)
down = false;
if (state == GLUT_DOWN)
down = true;
}

void motion(int x, int y){
if (down == true)
circle(x,399-y,10);
}

void keyboard(unsigned char key, int x,int y){
if (key==‘r’)
glColor3f(1.0,0.0,0.0);
if (key==‘b’)
glColor3f(1.0,1.0,1.0);
}

/----------------------------------------------------------------------------------------------------------------------/

void init(void){

glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,399.0,0.0,399.0,-100.0,100.0);
}

int main(int argc, char** argv){

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow(“Vas Simple Paint Program”);
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Sorry, gonna post the code with some modifcations:

Basically, you forgot to go back to modelview after you had done the orthographic thing in init(). Also I made it double buffered.

Dont know about your crashes, works fine for me. (using ms vc++6). Maybe its a calling convention thing, atleast watcom uses register calling but glut wants stack based calling. That have caused troubles for me.

#include <windows.h> /* Windows Library /
#include <GL/gl.h> /
OpenGL Library /
#include <GL/glut.h> /
Glut Library /
#include <math.h> /
Math Library */

int down = 0;
int true = 1;
int false = 0;
int mx,my;

void circle(int xcentre,int ycentre, float radius){
float angle, angle_increment;
float next_x,next_y;
int i;

angle_increment = (3.14159*2/100);
angle=0;
glBegin(GL_LINE_LOOP);
for (i=0; i&lt;=100; i++){
	angle = angle + angle_increment;
	next_x = radius * cos(angle);
	next_y = radius * sin(angle);
	next_x = next_x + xcentre;
	next_y = next_y + ycentre;
	glVertex2f(next_x,next_y);
}
glEnd();

}

void mouse(int button, int state, int x, int y){
if (state == GLUT_UP)
down = false;
if (state == GLUT_DOWN)
down = true;
}

void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
circle(mx,400-my,10);
glPopMatrix();
glutSwapBuffers();
}

void motion(int x, int y){
if (down == true){
mx=x;
my=y;
}
display();
}

void keyboard(unsigned char key, int x,int y){
if (key==‘r’)
glColor3f(1.0,0.0,0.0);
if (key==‘b’)
glColor3f(1.0,1.0,1.0);
}

/----------------------------------------------------------------------------------------------------------------------/

void init(void){

glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 399.0, 0.0, 399.0, -100.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char** argv){

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow(“Vas Simple Paint Program”);
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutIdleFunc(display);
glutMainLoop();
return 0;
}

The main problem is that in the function circle, angle is used before initialized. That can cause problems if it starts out being too large to be affected by the addition of the increment.

I was able to get your original code to work simply by adding in circle, angle=0;

I would also however load the modelview matrix with the identity matrix in init() after setting the projection matrix, just in case the OpenGL implementation does not initialize it as such by default when the RC is obtained by GLUT.

Note: I compiled it using MSVC++ 6.0

[This message has been edited by DFrey (edited 10-18-2000).]

I modified my code according to what AndersO suggested, but I still have no luck. I do get the circle(paintbrush) at the top left corner, but I cannot seem to be able to move it, so I’m not sure if the mouse function is working properly…

Did you guys manage it to make it work properly?

Thanks
Vasilis

I had no problem making it work with just the one addition I made. I’ve emailed you the code I used that worked fine with MSVC++ 6.0 and my TNT.

I got your email thanks…

I can compile the program, but I do not see anything at all, I cannot change the colour of the circle neither actually draw anything.

Does the .exe from the MSVC++ 6.0 work fine?
Maybe I need to try a different compiler, or I need to change some settings on my Watcom??

Vasilis

Yes, the exe worked fine (as long as window was not resized) on my computer using my TNT. I could draw trails of circles and change the color with no problem. I’ll email the exe if you want it.

[This message has been edited by DFrey (edited 10-19-2000).]

Thanks for getting back…
It seems as if it is Watcom then…
I’ll see what settings I can play with…I hope I can fithis problem, cause I would rather use Watcom than VC++ really…

No need to main me the exe…but I wish I knew what I need to change in Watcom…

Vasilis

It sounds like you are using registerbased calling. Change to stack based calling since glut uses that.

Thanks everybody for your help )

I did get it finally to work…
It was Watcom…I did find how to enable the stack based calling, instead of the register, so it’s all up and working now…

Thanks again…
Vasilis