Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: I should see a Square, instead of a Rectangle...

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2016
    Posts
    11

    I should see a Square, instead of a Rectangle...

    Hello.

    I want to draw a Square on my OpenGL application.

    So, If I have the X,Y coordinates of the top-left vertex and the lenght of the sides (which are all the same for a square), with a very simple math I can find the other vertices in the 2D space.

    Code :
    // Vertex is a simple struct with a constructor taking 2 arguments: X and Y of the vertex
    // In this case I set the topLeft vertex to have [COLOR="#FF0000"]X = 0[/COLOR], [COLOR="#008000"]Y = 0.5f[/COLOR]
    Vertex topLeft{ 0.0f, 0.5f };
     
    //side is the lenght of the square's sides
    GLfloat side = 0.5f; 
     
    // DrawTriangle is a function that takes 3 vertices (each of those has a x,y coordinate) and draws them on the application
    // 1st argument: the topLeft vertex that we've already created
    // 2nd and 3rd agument: the other two vertices
    DrawTriangle(window, topLeft, Vertex{ topLeft.x + side, topLeft.y }, Vertex{ topLeft.x + side, topLeft.y - side });

    This code will draw half square. But not exactly as I expected.

    This is the result: (attachment)

    Click image for larger version. 

Name:	h3FtQrA.jpg 
Views:	98 
Size:	26.8 KB 
ID:	2243

    Now let's draw the other half square:

    Code :
    DrawTriangle(window, topLeft, Vertex{ topLeft.x, topLeft.y - side }, Vertex{ topLeft.x + side, topLeft.y - side });

    Aaaand, the result is rectangle, not a square

    Click image for larger version. 

Name:	iXyzxNI.jpg 
Views:	115 
Size:	20.3 KB 
ID:	2244

    Is my math wrong?
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	M6xee2B.jpg 
Views:	72 
Size:	8.6 KB 
ID:	2242  
    Last edited by gedamial; 03-21-2016 at 07:56 AM.

  2. #2
    Junior Member Regular Contributor
    Join Date
    Sep 2013
    Posts
    172
    The code you have shown us really isnt enough to give you any kind of feedback. Especially because you didnt show us any OpenGL code but code using some kind of framework.
    If I had to guess I would say you are using the old deprecated rendering pipeline and you have forgotten to set up your matrices.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2016
    Posts
    11
    Quote Originally Posted by Cornix View Post
    The code you have shown us really isnt enough to give you any kind of feedback. Especially because you didnt show us any OpenGL code but code using some kind of framework.
    If I had to guess I would say you are using the old deprecated rendering pipeline and you have forgotten to set up your matrices.
    Ok sorry.

    This is the DrawTriangle() function that I use above:

    Code :
    void DrawTriangle(SDL_Window* Win, Vertex v1, Vertex v2, Vertex v3)
    {
    	Vertex vertices[3] { v1, v2, v3 };
     
    	glGenBuffers(1, &vbo);
     
    	glBindBuffer(GL_ARRAY_BUFFER, vbo);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);	
     
    	glEnableVertexAttribArray(0);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
     
    	glDrawArrays(GL_TRIANGLES, 0, 3);
     
    	glDisableVertexAttribArray(0); 
    	glDisableVertexAttribArray(1);		
    	glBindBuffer(GL_ARRAY_BUFFER, 0);	
    }

  4. #4
    Intern Contributor
    Join Date
    Feb 2016
    Posts
    64
    You should take into account that clip space coordinates don't change ([-1;1] on all 3 axis) while the window's aspect ratio changes.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2016
    Posts
    11
    Quote Originally Posted by Spoops View Post
    You should take into account that clip space coordinates don't change ([-1;1] on all 3 axis) while the window's aspect ratio changes.
    Sorry, I don't get what you wanna say.

    How can I solve that problem?

  6. #6
    Intern Contributor
    Join Date
    Feb 2016
    Posts
    64
    I meant that OpenGL doesn't care what size your window is, the coordinates still range from -1 to 1 on the whole window.

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2016
    Posts
    11
    Quote Originally Posted by Spoops View Post
    I meant that OpenGL doesn't care what size your window is, the coordinates still range from -1 to 1 on the whole window.
    I know. But even though I just draw a triangle with these coordinates:

    First vertex:
    {0, 0.5f}

    Second vertex:
    { 0.5f, 0.5f }

    Third vertex:
    { 0.5f, 0 }

    OpenGL draws this:

    Click image for larger version. 

Name:	gr6b7t8.jpg 
Views:	84 
Size:	29.1 KB 
ID:	2245

    As you can see, the two catheti aren't equal but they should be

  8. #8
    Junior Member Regular Contributor
    Join Date
    Sep 2013
    Posts
    172
    They are equal. But the X-Axis has a different scaling factor.

    If your Window is 500 pixels in width and your rectangle is 0.5 length units in width with the bounds going from -1.0 to 1.0 then that is 125 pixels (=500/4).
    If your Window is 300 pixels in height and your rectangle is 0.5 length units in height with the bounds going from -1.0 to 1.0 then that is 75 pixels (=300/4).
    Its 0.5 length units on both axis, but the results in pixels are different.

    OpenGL doesnt care about pixels. You are supposed to set up your own coordinate system in the way that you want to use it.

  9. #9
    Junior Member Newbie
    Join Date
    Mar 2016
    Posts
    11
    Quote Originally Posted by Cornix View Post
    They are equal. But the X-Axis has a different scaling factor.

    If your Window is 500 pixels in width and your rectangle is 0.5 length units in width with the bounds going from -1.0 to 1.0 then that is 125 pixels (=500/4).
    If your Window is 300 pixels in height and your rectangle is 0.5 length units in height with the bounds going from -1.0 to 1.0 then that is 75 pixels (=300/4).
    Its 0.5 length units on both axis, but the results in pixels are different.

    OpenGL doesnt care about pixels. You are supposed to set up your own coordinate system in the way that you want to use it.
    How do I well render my geometry independently from the size of the Window?

  10. #10
    Intern Contributor
    Join Date
    Feb 2016
    Posts
    64
    Choose correct positions or send a matrix in your vertex shader to rescale/translate primitives.

Posting Permissions

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