# Problem with creating a class for drawing....

## Recommended Posts

MylesEvans    151
class QuadDrawing {
private:

public:
void Draw();
};

{
}

{
glPushMatrix();
//Set color
glColor4f(1.0,0.0,0.0,0.0);

glVertex2f(90.0,50);
glVertex2f(90.0,-80);
glVertex2f(0.0,-80);
glVertex2f(0.0,50);

glEnd();

glPopMatrix();

}

int main( int argc, char *argv[] )
{
//Quit flag
bool quit = false;

//Initialize
if( init() == false )
{
return 1;
}

//The frame rate regulator
Timer fps;
Drawing Square1(300.0, 300.0);
int x = 0, y = 0;
//Wait for user exit
while( quit == false )
{
//Start the frame timer
fps.start();

//While there are events to handle
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}

else if( event.type == SDL_KEYDOWN )
{
//Handle keypress with current mouse position

SDL_GetMouseState( &x, &y );

handleKeys (event.key.keysym.unicode. x. y);

}
}

Square1.Draw();

update();

//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}

//Clean up
clean_up();

return 0;
}



The problem is I want to draw a square to test the class I made which should just take two floating point numbers and draw a square at those points, but the square is never drawn...instead the window comes up but stays completely blank. The window was even clear (no clear color) so I thought that was the problem and added glClear( GL_COLOR_BUFFER_BIT ); to the Draw function in the class making it...

void QuadDrawing::Draw()
{

glClear( GL_COLOR_BUFFER_BIT);

glPushMatrix();
//Set color
glColor4f(1.0,0.0,0.0,0.0);

glVertex2f(90.0,50);
glVertex2f(90.0,-80);
glVertex2f(0.0,-80);
glVertex2f(0.0,50);

glEnd();

glPopMatrix();

}



Still didn't work and the window come up clear/blank (not even the clear color).

But the thing is if I just make it a function and not a class and pass the floating X/Y to draw the square there it works fine. So its a problem with the class.

Edited by GDsnakes

##### Share on other sites
BaneTrapper    1531

Put your code inside of "(code) my code goes here (/code)" swap () with [], its a hell to read it.

Good luck, cant help.

##### Share on other sites
kd7tck    735

While reading this keep in mind I have little experience using openGL.

The code bellow was taken from http://stackoverflow.com/questions/5877728/want-an-opengl-2d-example-vc-draw-a-rectangle. However yours is very similar, so it should work. Try making the draw function static. If it works then that means you need to use callbacks for drawing from within classes, or have drawing done statically, by the way static is never recommended in C++. If it doesn't work then I'm stumped, it might just be some little bit that is out of order, I wish I knew openGL better.

glPushMatrix();  //Make sure our transformations don't affect any other transformations in other code
glTranslateF(pRect->x, pRect->y);  //Translate rectangle to its assigned x and y position
//Put other transformations here
glBegin(GL_QUADS);   //We want to draw a quad, i.e. shape with four sides
glColor3F(1, 0, 0); //Set the colour to red
glVertex2F(0, 0);            //Draw the four corners of the rectangle
glVertex2F(0, pRect->h);
glVertex2F(pRect->w, pRect->h);
glVertex2F(pRect->w, 0);
glEnd();
glPopMatrix();

P.S. The functions gluOrtho2D and glFlush might need to be used. Lastly glRectf can draw rectangles too.

##### Share on other sites
Trienco    2555

The code for all the setup isn't shown. What does your viewport, view and projection look like?

Since you don't clear the depth buffer, did you make sure that depth testing is disabled?

Are you sure that based on your view/projection "front" is where you think it is?

Why are you setting alpha to 0 (100% transparent, depending on blend mode)?

##### Share on other sites
MylesEvans    151

The set up:

bool init() {
//Initialize SDL if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
return false;
}
//Create Window if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL ) {
return false;
}
//Initialize OpenGL if( initGL() == false ) {
return false;
}
//Set caption SDL_WM_SetCaption( "OpenGL Test", NULL );
return true;
}

bool initGL() {
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
//Initialize clear color glClearColor( 0.f, 0.f, 0.f, 1.f );
//Check for error GLenum error = glGetError();
if( error != GL_NO_ERROR ) {
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) ); return false;
}
return true;
}



##### Share on other sites
JTippetts    12950
So your projection matrix is identity, but you are trying to draw a quad that is 90x130 units in size? When projection is identity, the screen will be mapped to the ranges (-1,1) on X and Y, so trying to draw a quad from (0, -80) to (90,50) will draw a quad that is vastly larger than the screen. You really should set your projection matrix to something other than identity; ie, a projection or orthographic matrix.

Also, as Trienco indicated, since you added the call to glClear(GL_COLOR_BUFFER_BIT) your depth buffer is no longer being cleared. Yet you never disable depth writing or depth testing, so even if you did get something to draw on the first frame, the depth buffer would never be cleared so if the camera or any objects moved on subsequent frames, the old depth buffer would still be around clipping fragments you probably don't want clipped. If you are going to disable clearing depth buffer in glClear() then you should also disable depth testing and depth writing.

##### Share on other sites
MylesEvans    151

Ugh, sorry guys. I copied that from the wrong file I have. That's not the setup (after reading your suggestions I realized my mistake. Sorry for wasting time)....

#include "SDL.h"
#include <SDL_opengl.h>

//Screen attributes
const int SCREEN_WIDTH = 1500;
const int SCREEN_HEIGHT = 1500;
const int SCREEN_BPP = 32;

bool initGL()
{
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );

//Initialize Modelview Matrix
glOrtho(0, 1500, 1500, 0, -1, 1);
glMatrixMode( GL_MODELVIEW );

//Initialize clear color
glClearColor(1.0,.4,0.0,0.0);

//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false;
}

return true;
}

bool init()
{
//Initialize SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}

//Create Window
if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
{
return false;
}

//Enable unicode
SDL_EnableUNICODE( SDL_TRUE );

//Initialize OpenGL
if( initGL() == false )
{
return false;
}

//Set caption
SDL_WM_SetCaption( "Project Birth Game Window. Guns&Player.", NULL );

return true;
}



So this is what my set-up actually looks like.
I just want to clarify that I was drawing Quads and whatnot without a problem when I was just calling a "draw" function, but as soon as I made it into a class I've been getting the problems described above in the OP. 

##### Share on other sites
MylesEvans    151

What could possibly be the problem? I call a function to draw a square and it works fine....but if that EXACT same function is part of a class it doesn't work....Window is up, but screen clear.

##### Share on other sites
Trienco    2555

One kind of obvious thing that jumps at me is your constructor. Take a good look at it and if you don't notice it, describe to me in detail what it does ,-)

##### Share on other sites
kd7tck    735
One kind of obvious thing that jumps at me is your constructor. Take a good look at it and if you don't notice it, describe to me in detail what it does ,-)

Trienco that looks like it might be why it doesn't work.

GDsnakes you should make all function inputs const unless you have to modify it.

##### Share on other sites
MylesEvans    151
One kind of obvious thing that jumps at me is your constructor. Take a good look at it and if you don't notice it, describe to me in detail what it does ,-)

Trienco that looks like it might be why it doesn't work.

GDsnakes you should make all function inputs const unless you have to modify it.

I changed it to this. Still the exact same same issues.

QuadDrawing::QuadDrawing( float Xoff, float Yoff)
{
}

##### Share on other sites
MylesEvans    151

Fixed it. Needed to Swap Buffers.