Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Rectangle is being drawn insted of a square, Using Quads

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    5

    Rectangle is being drawn insted of a square, Using Quads

    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
    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
    Code :
    #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.

  2. #2
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,160
    The projection is wrong. glFrustum() has wrong parameters. Both bottom and top values are 1. It is strange that you have any output.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    5
    Ok I fixed that. Nothing has changed.

    This is my new code
    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.

  4. #4
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,160
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    5
    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.
    Code :
    #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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •