OpenGL prevent tiles from rendering offscreen

Started by
2 comments, last by _WeirdCat_ 3 years, 12 months ago

So I have this java code,

public static void renderTileMap(int startX, int startY, int[][] map, int dwheelBuff) {
		int size = dwheelBuff;
					
		// Coordinate buffers, because we don't want for loop x, y to represent screen x, y
		int coor_x = startX; 
		int coor_y = startY; 
		
		for(int y = 0; y < map.length; y++) {
			coor_x = startX;
			if(y > 0)
				coor_y += size/2;
			
			for(int x = 0; x < map[y].length; x++) {
				if(x > 0)
					coor_x += size/2;
				
				renderTile(coor_x, coor_y, size);
				
			}
		}
	}
	
	private static void renderTile(int x1, int y1, int size) {
		glBegin(GL_TRIANGLES);

		double halfSize = size/2;
		
		glVertex2d(x1 + halfSize, y1 + halfSize);
		glVertex2d(x1 + halfSize, y1 - halfSize);
		glVertex2d(x1 - halfSize, y1 - halfSize);

		glVertex2d(x1 - halfSize, y1 - halfSize);
		glVertex2d(x1 - halfSize, y1 + halfSize);
		glVertex2d(x1 + halfSize, y1 + halfSize);
		glEnd();
	}

how can I set a view distance for this? Like, prevent the tiles from rendering offscreen

Advertisement

OpenGL will automatically cull triangles outside of the viewport for you - what problem are you actually trying to solve here?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Depending on implementation you need to find at which coord you end up at top left and bottom right then youll have your solution by not drawing tiles that are beyound that rectangle

This topic is closed to new replies.

Advertisement