PDA

View Full Version : OpenGL Problem



Joshhua5
01-07-2011, 01:55 AM
I have a problem with some Code i was working on.

the Errors are


[CODE]1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\gl\glaux.h(17): warning C4068: unknown pragma
1>c:\program files (x86)\microsoft sdks\windows\v7.0a\include\gl\glaux.h(374): warning C4068: unknown pragma
1>c:\users\josh\documents\visual studio 2010\projects\glopen_game\glopen_game\main.cpp(50) : warning C4789: destination of memory copy is too small
1>c:\users\josh\documents\visual studio 2010\projects\glopen_game\glopen_game\main.cpp(51) : warning C4789: destination of memory copy is too small
1>c:\users\josh\documents\visual studio 2010\projects\glopen_game\glopen_game\main.cpp(69) : warning C4789: destination of memory copy is too small

and then it exits with 0x1
[\code]

the Code is


[CODE]
#include <cstdlib>
#include <gl\glut.h>
#include <gl\GLAUX.H>
#include <iostream>
#include <ctime>
using namespace std;
// Particle Shape
float partShape[4] = {-5, -5, 5, 5};
// Particle Position
float partPosition[4] = {0, 0, 0, 0};
// Particle Spin
float Spin = 0.0;
// Box Position (for Physics)
float Position[1] = {0};
// Operation Interval 1/60th of a second
float Interval = (1 / 60);
float mouseX = 0.0;
float mouseY = 0.0;

// 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)
{
// Drawing the Box
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(Spin, 0, 0, 0);
glRectf(partPosition[1],partPosition[2],partPosition[3],partPosition[4]);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void initDraw()
{
//Spinning of the Box
Spin += Rnd(-2, 2);
if(Spin >= 360)
Spin = 0;
if(Spin <= -360)
Spin = 0;
drawBox();
// Falling of the Box
Position[1] = mouseX;
Position[1] -= (9.81 / Interval);
}

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))
{
mouseX = x;
mouseY = y;
partPosition[1] = partShape[1] + x;
partPosition[2] = partShape[2] + y;
partPosition[3] = partShape[3] + x;
partPosition[4] = partShape[4] + y;
glutIdleFunc(initDraw);
}
if((button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN))
{
glutIdleFunc(initDraw);
}
}
int main(int argc, char** argv)
{
// Window Settings
glutInitWindowPosition(300,100);
glutInitWindowSize(400,800);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInit(&amp;argc, argv);
glutCreateWindow("Boxes");
bufferClearInit();
// Mouse Functions
glutMouseFunc(MouseFunc);
glutMainLoop();
}
[\code]

_arts_
01-07-2011, 02:01 AM
don't use glaux ! it's really old and not maintained since many years ! Plus, the code you did, doesn't need glaux at all.

Joshhua5
01-07-2011, 02:10 AM
i forgot to remove the header but that has been edited out. but the problem continues.

now it's only

1>c:\users\josh\documents\visual studio 2010\projects\glopen_game\glopen_game\main.cpp(49) : warning C4789: destination of memory copy is too small
1>c:\users\josh\documents\visual studio 2010\projects\glopen_game\glopen_game\main.cpp(50) : warning C4789: destination of memory copy is too small
1>c:\users\josh\documents\visual studio 2010\projects\glopen_game\glopen_game\main.cpp(68) : warning C4789: destination of memory copy is too small

_arts_
01-07-2011, 02:16 AM
main.cpp(49): warning C4789: destination of memory copy is too small

This is a C issue, not OpenGL. Have a deeper look at your code to find the error.

PS: between, these are not errors but warnings.

mobeen
01-07-2011, 02:17 AM
These are just warnings. Your code should run fine. What are u trying to do by the way? You have not setup ur modelview/projection matrices.

Joshhua5
01-07-2011, 02:27 AM
it compiles but then it jsut closes down randomly and it's a 2d box that spawns where you click and falls down off screen.

mobeen
01-07-2011, 03:48 AM
You should not hook the idle func each click. like the mouse func. hook the idle func. in main. If u want to call your drawing func, use glutPostRedisplay.

mhagain
01-07-2011, 03:54 AM
float Position[1] = {0};This array has only one element in it, but yet...

Position[1] = mouseX;
Position[1] -= (9.81 / Interval);You're setting the second element here. You're also using partPosition (float partPosition[4] = {0, 0, 0, 0};) wrongly:

glRectf(partPosition[1],partPosition[2],partPosition[3],partPosition[4]);And both partPosition and partShape (float partShape[4] = {-5, -5, 5, 5};) here:

partPosition[1] = partShape[1] + x;
partPosition[2] = partShape[2] + y;
partPosition[3] = partShape[3] + x;
partPosition[4] = partShape[4] + y;
I haven't looked elsewhere in your code for other similar mistakes.

Arrays in C/C++ are 0-based, not 1-based. An array with 4 elements (float partPosition[4]) should index them as 0 to 3 (partPosition[0], partPosition[1], partPosition[2], partPosition[3]), not 1 to 4 (partPosition[1], partPosition[2], partPosition[3], partPosition[4]). You've basically got buffer overflows all over your code.

Joshhua5
01-07-2011, 06:16 AM
Okay well thanks on the Array, It's clear in my head now but the worst part is...

Now I'm getting no errors and the same thing is happening... it runs openGL then text appears in the C++ window and closes down.. all i can see id the word Debug before it closes.

mobeen
01-07-2011, 06:22 AM
Could u try and make glutInit(&amp;argc, argv); the first line of main.

Joshhua5
01-07-2011, 06:28 AM
Nope

mobeen
01-07-2011, 06:32 AM
Could u post the whole code now after the changes?
One more thing, could u tell me
the compiler u r using,
the glut version u r using,
anything else that may be useful

Joshhua5
01-07-2011, 07:02 AM
GLUT 3.7
Microsoft Visual C++ Express 2010


#include <cstdlib>
#include <gl\glut.h>
#include <iostream>
#include <ctime>
using namespace std;
// Particle Shape
int partShape[4] = {-5, -5, 5, 5};
// Particle Position
int partPosition[4] = {0, 0, 0, 0};
// Particle Spin
int Spin = 0;
// Box Position (for Physics)
float Position[2] = {0, 0};
// Operation Interval 1/60th of a second
float Interval = (1 / 60);
int mouseX = 0;
int mouseY = 0;

// 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)
{
// Drawing the Box
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glRotatef(Spin, 0, 0, 0);
glRectf(partPosition[0],partPosition[1],partPosition[2],partPosition[3]);
glEnd();
glFlush();
glutSwapBuffers();
}
void initDraw()
{
//Spinning of the Box
Spin += Rnd(-2, 2);
if(Spin >= 360)
Spin = 0;
if(Spin <= -360)
Spin = 0;
drawBox();
// Falling of the Box
Position[0] = mouseX;
Position[0] -= (9.81 / Interval);
partPosition[0] -= Position[0]; //X1
partPosition[2] -= Position[0]; //X2
partPosition[1] -= Position[1]; //Y1
partPosition[3] -= Position[1]; //Y2
}

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))
{
mouseX = x;
mouseY = y;
partPosition[0] = partShape[0] + x;
partPosition[1] = partShape[1] + y;
partPosition[2] = partShape[2] + x;
partPosition[3] = partShape[3] + y;
glutIdleFunc(initDraw);
}
if((button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN))
{
glutIdleFunc(initDraw);
}
}
int main(int argc, char** argv)
{
// Window Settings
glutInit(&amp;argc, argv);
bufferClearInit();
glutInitWindowPosition(300,100);
glutInitWindowSize(400,800);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Boxes");
glutIdleFunc(initDraw);
// Mouse Functions
glutMouseFunc(MouseFunc);
glutMainLoop();
}

mhagain
01-07-2011, 07:07 AM
Have you run this in the debugger?

Joshhua5
01-07-2011, 08:34 AM
?

Joshhua5
01-07-2011, 08:36 AM
'glOpen_Game.exe': Loaded 'C:\Users\Josh\Documents\Visual Studio 2010\Projects\glOpen_Game\Release\glOpen_Game.exe' , Symbols loaded.
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\opengl32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\glu32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\ddraw.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\dciman32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\devobj.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\glut32.dll', Binary was not built with debug information.
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\winmm.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\msvcr100.dll', Symbols loaded.
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\atiglpxx.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\atioglxx.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\atigktxx.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\aticfx32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\atiadlxy.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\wintrust.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\crypt32.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\msasn1.dll', Cannot find or open the PDB file
'glOpen_Game.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Cannot find or open the PDB file
The program '[2176] glOpen_Game.exe: Native' has exited with code 1 (0x1).

trinitrotoluene
01-07-2011, 09:07 AM
'glOpen_Game.exe': Loaded 'C:\Users\Josh\Documents\Visual Studio 2010\Projects\glOpen_Game\Release\glOpen_Game.exe' , Symbols loaded.

You are compiling your program in release mode, so no debugging information is available with the compiled code. Compile your code in debug mode, then run the debugger on your program. But do not try to step inside a function that is included in the foreign dll's because those will not have debugging symbol even in debugging mode.

mhagain
01-07-2011, 09:19 AM
'glOpen_Game.exe': Loaded 'C:\Users\Josh\Documents\Visual Studio 2010\Projects\glOpen_Game\Release\glOpen_Game.exe' , Symbols loaded.

You are compiling your program in release mode, so no debugging information is available with the compiled code. Compile your code in debug mode, then run the debugger on your program. But do not try to step inside a function that is included in the foreign dll's because those will not have debugging symbol even in debugging mode.
This, precisely.

Switch to the Debug build in Configuration Manager, Ctrl-Shift-B to compile, then F5 to run it in the debugger. It should break at the point at which it's crashing, which will tell you which line of code is causing your trouble.

It's probably also worth while switching the Character Set from Unicode (the default in MSVC 2010) to Multi-Byte; GLUT is a really old library that predates more widespread use of Unicode so there may be interoperability problems.

Joshhua5
01-07-2011, 09:51 AM
it doesn't break it just closes after the window had been initialized.

mobeen
01-07-2011, 10:06 AM
Ah i see. Call bufferClearInit() after your window is created. You are calling a gl function even before the opengl context is created.


// Window Settings
glutInit(&amp;argc, argv);
glutInitWindowPosition(300,100);
glutInitWindowSize(400,800);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Boxes");
bufferClearInit();
//rest of the functions

ZbuffeR
01-07-2011, 10:10 AM
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...

Joshhua5
01-07-2011, 11:58 AM
Didn't fix the problem...

ZbuffeR
01-07-2011, 12:00 PM
You realize you are a very-hard-to-help person ?

Joshhua5
01-07-2011, 12:11 PM
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

Joshhua5
01-07-2011, 12:17 PM
I would like to think that's going to make me a Very-easy-to-help-person.

Joshhua5
01-07-2011, 12:53 PM
Oops Zbuffer i didn't see your Post. that should work

Joshhua5
01-07-2011, 01:24 PM
Okay well I've done some code changes to try optimize :D 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(&amp;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();
}

carsten neumann
01-07-2011, 01:53 PM
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...

MaxH
01-07-2011, 02:00 PM
... my squre doesn't show when i click on the screen ... 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 ();

Joshhua5
01-07-2011, 02:24 PM
MaXH What's happening with the Matrixs What do they do?

Joshhua5
01-08-2011, 06:17 AM
AND applying what you write doesn't help.