Some Beginner questions

First of all: hi everyone :slight_smile:

I got some (maybe easy) questions for you and i hope someone will be so gentle and answer them. I started some days ago with opengl and i am using the ide Dev-C (i started c++ with it). Can you recommend me a “better” one (freeware and windows ?).

I started with a program, which moves balls and let them bounce in a square around. The first problem is, that the moving balls are giving me a graphic failure (see the pic below):

http://img703.imageshack.us/i/probv.jpg/

Code for the Ball-drawing:

void circle(float center_y, float center_x, float z, float radius) {
float x = 0.0f;
float y = 0.0f;
glBegin(GL_POLYGON);
for( double theta = 0.0; theta <= (2*M_PI); theta += ((M_PI) / 180.0) ) {
y = center_y + ( sin( theta ) * radius );
x = center_x + ( cos( theta ) * radius );
glVertex3f( x, y, z );
}
glEnd();
}

I tried to draw the ball in two halfsides, to vary the drawing angle (M_PI) - why do i get these drawing faults?

I added the whole code down. Red is the circle stuff and blue the moving-the-ball-part.

I also attached the files.

The other question is:
The program is not quite “fast”. I expected it to be faster such as adding a delay to slow it down. As i know the screen is redrawn with the command “SwapBuffers (hDC);”.
I am just concerned because if i want to create more complex stuff this will not work so. If i am doing a big mistake please tell me.

regards
claus

#include <windows.h>
#include <gl/gl.h>
#include <cmath>
#include <ctime>

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);

void circle(float center_y, float center_x, float z, float radius) {
float x = 0.0f;
float y = 0.0f;
glBegin(GL_POLYGON);
for( double theta = 0.0; theta <= (2*M_PI); theta += ((M_PI) / 180.0) ) {
y = center_y + ( sin( theta ) * radius );
x = center_x + ( cos( theta ) * radius );
glVertex3f( x, y, z );
}
glEnd();
}

double zufall(float r_wert, float o_grenze, float u_grenze)
{

int r1,r2,r3 = 0;
double r4,r5,r6;

time_t t;
time(&t);
// srand((unsigned int)t);

while (r6 < u_grenze or r6 > o_grenze or r3 == 0) {

r1 = rand() % 1000 + 1;
r2 = rand() % 1000 + 1;

r4 = (double) r1;
r5 = (double) r2;
r3 = (rand() % 3 - 1);

r6 = (r4/(r5*r_wert))*r3;

}

return(r6);
}

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
float theta = 0.0f;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;

wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);

hWnd = CreateWindow (
  "GLSample", "OpenGL Sample", 
  WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  0, 0, 500, 500,
  NULL, NULL, hInstance, NULL);

EnableOpenGL (hWnd, &hDC, &hRC);

float z1=1,z2=0.95,b=0.015; 
int anzahl = 2;

float rx[anzahl];
float ry[anzahl];
float x[anzahl];
float y[anzahl];

int i;

for (i;i&lt;anzahl;i++)
{
  x[i] = zufall(0.1, 1, -1);
  y[i] = zufall(0.1, 1, -1);
  
  rx[i] = zufall(100, 0.5, -0.5);
  ry[i] = zufall(100, 0.5, -0.5);
}

i = 1; 
float gr = 0.025;
float grenze = 0.86, lgrenze = 0.9;


glColor3f( 1.0f, 1.0f, 1.0f );
glBegin(GL_LINES);
glVertex3f( lgrenze, lgrenze, 0.0);
glVertex3f( -lgrenze,lgrenze, 0.0);

glVertex3f( -lgrenze, -lgrenze, 0.0);
glVertex3f( lgrenze, -lgrenze, 0.0);  

glVertex3f( -lgrenze, -lgrenze, 0.0);
glVertex3f( -lgrenze, lgrenze, 0.0);  

glVertex3f( lgrenze, lgrenze, 0.0);
glVertex3f( lgrenze, -lgrenze, 0.0);     
glEnd();
SwapBuffers (hDC);


while (!bQuit)
{
    if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
    {
        if (msg.message == WM_QUIT)
        {
            bQuit = TRUE;
        }
        else
        {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }
    else
    {

   /*     glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
        glClear (GL_COLOR_BUFFER_BIT);

        glPushMatrix ();
        glRotatef (theta, 0.0f, 0.0f, 1.0f);
        
   */

/*
glBegin(GL_TRIANGLES);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // Rot
glVertex2f(0.50f, 0.50f);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f); // GrĂźn
glVertex2f(0.0f, 0.50f);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // Blau
glVertex2f(0.50f, -0.50f);

glColor4f(1.0f, 0.0f, 0.0f, 1.0f);	// Rot
glVertex2f(-0.50f, -0.50f);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);	// GrĂźn
glVertex2f(-0.1f, -0.10f);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);	// Blau
glVertex2f(0.50f, -0.50f);

glEnd();
*/

// glColor3f (1, 1, 1);
// glPointSize (10);

  //  glBegin (GL_POINTS);      

[b] for (i=0;i<anzahl;i++)
{
if (x[i] >= grenze) rx[i] = -rx[i];
if (y[i] >= grenze) ry[i] = -ry[i];
if (x[i] <= -grenze && rx[i] < 0) rx[i] = -rx[i];
if (y[i] <= -grenze && ry[i] < 0) ry[i] = -ry[i];

         glColor3f (0.0, 0.0, 0.0);
         circle(y[i], x[i], 0.0, gr); 
 
             
         x[i] = x[i]+rx[i];
         y[i] = y[i]+ry[i];
                    
         glColor3f (1.0, 1.0, 1.0);           
         circle(y[i], x[i], 0.0, gr);
     }[/b]      
      //  glPopMatrix ();
        SwapBuffers (hDC);


      //  theta += 1.0f;
      //  Sleep (5);
    }
}

DisableOpenGL (hWnd, hDC, hRC);
DestroyWindow (hWnd);

return msg.wParam;

}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_CREATE:
    return 0;
case WM_CLOSE:
    PostQuitMessage (0);
    return 0;

case WM_DESTROY:
    return 0;

case WM_KEYDOWN:
    switch (wParam)
    {
    case VK_ESCAPE:
        PostQuitMessage(0);
        return 0;
    }
    return 0;

default:
    return DefWindowProc (hWnd, message, wParam, lParam);
}

}

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;

*hDC = GetDC (hWnd);

ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);

*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}

please use [ code][ /code] (no space after ‘[’) around code for better readability.

it’s hard to say with commented out code in your post (make it easy for people to help you by taking the extra step of stripping dead code before you post :wink: ), but i think you are not clearing the color buffer at the beginning of each frame.

A few suggestions:

  1. Make your code as readable as possible to people on the forum by doing what Carsten said.
    __ Use
 tags, delete anything not necessary, etc.

2) Go to NeHe tutorials, download, compile, link and run one of their basic lessions done in GLUT.
__ It's a lot cleaner to work with. Modify their code to your application.

3) For 2D circles, use large, anti-aliased, points. This would be faster than computing and drawing a circle.