Rendering a rectangle

Started by SpaceboyRoss, February 06, 2018, 01:38:51

Previous topic - Next topic

SpaceboyRoss

I am trying to draw a rectangle, and so far this is what I have. I need help getting the vectors correct.
/**
 * 
 */
package com.rosssoftware.space.graphics.shapes;

import com.rosssoftware.space.graphics.RenderableObject;
import com.rosssoftware.space.math.Vector3f;

/** Defines a rectangle
 * @author spaceboyross
 * @since 0.1.0-alpha
 */
public class RenderableRectangle {
	
	private RenderableObject obj;
	
	/** Creates a rectangle
	 * @param position The start position of the rectangle
	 * @param width The width of the rectangle
	 * @param height The height of the rectangle
	 * @author spaceboyross
	 * @since 0.1.0-alpha
	 */
	public RenderableRectangle(Vector3f position,float width,float height) {
		float[] vertices = {
			position.x,position.y,position.z,
			position.x,position.y+height,position.z,
			position.x,position.y-height,position.z,
			
			position.x+width,position.y,position.z,
			position.x+width,position.y-height,position.z,
			position.x+width,position.y+height,position.z
		};
		this.obj = new RenderableObject(vertices,6);
	}
	
	/** Draws this rectangle
	 * @author spaceboyross
	 * @since 0.1.0-alpha
	 */
	public void render() {
		this.obj.render();
	}
	
	/** Destroyes this rectangle
	 * @author spaceboyross
	 * @since 0.1.0-alpha
	 */
	public void destroy() {
		this.obj.destroy();
	}
}

KaiHH

Can you draw a rectangle on a piece of graph paper? If so, do it. Now, you are going to split that rectangle up into two triangles. There are exactly two possibilities to do so. Choose one arbitrarily.
Now, start from one arbitrary corner of either of the two triangles and trace that triangle along all its edges and corners (three of them) using one single stroke. Whenever you moved to the right or left in order to get from one corner to the next corner, your new coordinate will have a -width or +width added to the x coordinate. The y coordinate stays the same. Likewise, whenever you moved up/down your new position will have a +height or -height added to the y coordinate and the x coordinate stays the same. When you reached the corner of the triangle you started with, you have generated three (x, y, z) triples using the rules from above. One triple for each corner.
These are the vertex positions of one of the two triangles.
Now repeat the process for the other triangle. At the end you'll have 6 (x, y, z) triples.
It is also important that you keep the order of the generated triples and do not rearrange them, because in OpenGL three consecutive coordinate triples of (x, y, z) form a single triangle.

The way you currently declare your triangle vertices is: Start in the middle of the left side of the rectangle. Now, move up. And now move down. The problem is: This is not a triangle. It is what is called a degenerate triangle.

PCXDO

So you want quad vertex positions? Huh, this is tricky.

U know ur engine need to have 3 things in order to be rendered: Vertex Positions Array (VBO) (or equivalent translated from object loaders like Assimp), Vertex Index Array (VertexElementBuffer) and Texture Coordinates Array (usually 2D 'vec2').

I'll give one who works for me (It's a quad not a rect but a quad is a rect lul).

float[] vertexPositions = { -0.5f, 0.5f, 0, -0.5f,-0.5f, 0, 0.5f,-0.5f, 0, 0.5f, 0.5f, 0 };

int[] indicesArray = { 0, 1, 3, 3, 2, 1 };

float[] textureCoords = {0,0,0,1,1,1,1,0};

render this with glDrawElements and everything will be fine. Just don't forget to bind this in your object VAO! And remember all index you attrib to then. Example: Positions - index 0, Texture Coords - index 1. Don't worry about indices array, this is a Element Array Buffer and u have nothing to do with it.