PDA

View Full Version : Rectangle is being drawn insted of a square, Using Quads



nickkorta
03-25-2014, 08:35 AM
Hi and thanks for the help in advance. I'm using SDL2.0 in Visual Studio on Windows 8. If you look at my Code to draw a quad it should draw a square but it draws a rectangle. The only thing I can think of is that I somehow must of messed up setting up my projection. 1280 by 720 is my window size. I also tried glOrtho and got the same results. I looked and I am calling all my functions.

Quad Draw Code

glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3f(0, 0, 0);
glTexCoord2i(0, 0); glVertex3f(0, 0.25f, 0);
glTexCoord2i(1, 0); glVertex3f(0.25f, 0.25f, 0);
glTexCoord2i(1, 1); glVertex3f(0.25f, 0, 0);
glEnd();


OVER ALL CODE FOR RENDERERING

#include "GraphicsDevice.h"

GraphicsDevice::GraphicsDevice()
{
}

GraphicsDevice::~GraphicsDevice()
{
}

void GraphicsDevice::Initalize(SDL_Window* win)
{
//Use OpenGL 2.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

//Create OpenGL Context
glContext = SDL_GL_CreateContext(win);

//Initalize OpenGl Clear Color
glClearColor(0,0,0,1);
}

void GraphicsDevice::UpdateProjection()
{
glViewport(0, 0, 1280, 720);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glFrustum(0, 1, 1, 1, 0, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glFlush();
}

void GraphicsDevice::Clear(SDL_Window* win, Color* color)
{
SDL_GL_SwapWindow(win);
glClearColor(color->r, color->g, color->b, color->a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();
}

void GraphicsDevice::BeginDraw()
{
glEnable(GL_TEXTURE_2D);
}

void GraphicsDevice::EndDraw()
{
glDisable(GL_TEXTURE_2D);
}

void GraphicsDevice::DrawQuad(Texture2D* texture, Vector3* position, Vector3* origin, Vector3* rotation, Vector3* scale)
{


glPushMatrix();

//Translate
//Sclae
//Rotate

glBindTexture(GL_TEXTURE_2D, texture->id);


glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3f(0, 0, 0);
glTexCoord2i(0, 0); glVertex3f(0, 0.25f, 0);
glTexCoord2i(1, 0); glVertex3f(0.25f, 0.25f, 0);
glTexCoord2i(1, 1); glVertex3f(0.25f, 0, 0);
glEnd();

glPopMatrix();
glFlush();
}


Thank you for your time and help.

Aleksandar
03-25-2014, 10:06 AM
The projection is wrong. glFrustum() has wrong parameters. Both bottom and top values are 1. It is strange that you have any output.

nickkorta
03-25-2014, 12:55 PM
Ok I fixed that. Nothing has changed.

This is my new code

glViewport(0, 0, 1280, 720);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(0, 1, 1, 0, 0, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

If I delete all that code it still draws. I am very confused. This is the only class where I am using OpenGL related stuff. I looked around the internet for about 2 hours but found nothing that helped me.

Thanks for the help so far.

Aleksandar
03-26-2014, 07:05 AM
Did you take a look at the specification of the function glFrustum?

With glVieport(0,0,1280,720) you have set viewport to a whole screen or window (probably), that is not a square, but a rectangle.
On the other hand, you said left side of that viewport is at 0 (logical coordinate), right side is at 1, bottom is 1 (which is wrong), and top is 0 (which is also wrong).
On that way you are stretching a quad (0,1,1,0) to a rectangle (0,0,1280,720), and it is vertically flipped (since bottom is greater than top). That's the problem.

Another (hidden) problem is 0 for the near clipping plane. That parameter SHOULD NEVER BE 0. Both near and far distance should be positive in glFrustum() call.
And further more, far/near ratio should not be greater than 10e5 with 24-bit depth buffer.

It is interesting that you didn't use gluPerspective() instead of glFrustum(), since it is more intuitive function to use.

nickkorta
03-26-2014, 09:57 AM
Everything works now. Thank you very much for your help Aleksandar. I was not using gluPerspective because I did not have it. I downloaded the libraries for that and now i'm using it.
I am new to OpenGL and this was very informative and I feel like I understand everything that happened here.

Here is the final code so others can see my mistakes and hopefully learn from them.

#include "graphicsdevice.h"

GraphicsDevice::GraphicsDevice()
{
}

GraphicsDevice::~GraphicsDevice()
{
}

void GraphicsDevice::Intalize(SDL_Window *window)
{
//Set Graphics Devices Window
this->window = window;

//Use OpenGL 2.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

//Create OpenGL Context
glContext = SDL_GL_CreateContext(window);

//Initalize OpenGl Clear Color
glClearColor(0,0,0,1);


//Enable Alpha
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void GraphicsDevice::Update(SDL_Window* window)
{
//Set Viewport and Get Aspect Ratio
glViewport(0, 0, 1280, 720);
float aspectRatio = 1280.0f / 720.0f;

//Load In Our Projection Matrix and create Projection
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,aspectRatio,1.0f,500.0f);
glPopMatrix();


//Load In Our ModelView Matrix So We Can Draw
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPopMatrix();

}

void GraphicsDevice::Clear(Color *color)
{
SDL_GL_SwapWindow(window);
glClearColor(color->r, color->g, color->b, color->a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void GraphicsDevice::BeginDraw()
{
glEnable(GL_TEXTURE_2D);
}

void GraphicsDevice::EndDraw()
{
glDisable(GL_TEXTURE_2D);
}

void GraphicsDevice::DrawQuad(Texture2D* texture)
{

glPushMatrix();
//Translate
//Sclae
//Rotate

glTranslated(0.0f,0.0f,-5.0f);

glBindTexture(GL_TEXTURE_2D, texture->id);


glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3f(0, 0, 0);
glTexCoord2i(0, 0); glVertex3f(0, 0.25f,0);
glTexCoord2i(1, 0); glVertex3f(0.25f, 0.25f,0);
glTexCoord2i(1, 1); glVertex3f(0.25f, 0, 0);
glEnd();

glPopMatrix();
glFlush();
}



Again thanks for your help Aleksandar.