SignalWarrant
SignalWarrant

Reputation: 1

RGL: GL Library : Maximum texture size of 1024x1024 exceeded

I keep getting the following error. I'm using the template here https://github.com/Pecners/rayshader_portraits/blob/main/R/portraits/arizona/render_graphic.R

Warning messages:
1: In rgl.primitive0(type = "triangles", x = c(-2499, -2498, -2497,  :
  RGL: GL Library : Maximum texture size of 1024x1024 exceeded.
(Perhaps enabling mipmapping could help.)
2: In rgl.primitive0(type = "triangles", x = c(2982, 2982, -2981, -2981,  :
  RGL: GL Library : Maximum texture size of 1024x1024 exceeded.
(Perhaps enabling mipmapping could help.)


Version Info
platform       x86_64-w64-mingw32               
arch           x86_64                           
os             mingw32                          
crt            ucrt                             
system         x86_64, mingw32                  
status                                          
major          4                                
minor          2.3                              
year           2023                             
month          03                               
day            15                               
svn rev        83980                            
language       R                                
version. String R version 4.2.3 (2023-03-15 ucrt)
nickname       Shortstop Beagle 

Code I'm attempting to run:

library(sf)
library(tidyverse)
library(elevatr)
library(rayshader)
library(glue)
library(colorspace)
library(tigris)
library(stars)
library(MetBrewer)

# Set map name that will be used in file names, and 
# to get get boundaries from master NPS list

map <- "arizona"

# Kontur data source: https://data.humdata.org/dataset/kontur-population-united-states-of-america

data <- st_read("data/kontur/kontur_population_US_20220630.gpkg")

s <- states() |> 
  st_transform(crs = st_crs(data))

st <- s |> 
  filter(NAME == str_to_title("arizona"))

st |> 
  ggplot() +
  geom_sf()

int <- st_intersects(data, st)

st_dd <- map_dbl(int, function(i) {
  if (length(i) > 0) {
    return(i)
  } else {
    return(0)
  }
})

st_d <- data[which(st_dd == 1),]

#st_d <- st_intersection(data, st)

bb <- st_bbox(st_d)
yind <- st_distance(st_point(c(bb[["xmin"]], bb[["ymin"]])), 
                    st_point(c(bb[["xmin"]], bb[["ymax"]])))
xind <- st_distance(st_point(c(bb[["xmin"]], bb[["ymin"]])), 
                    st_point(c(bb[["xmax"]], bb[["ymin"]])))

if (yind > xind) {
  y_rat <- 1
  x_rat <- xind / yind
} else {
  x_rat <- 1
  y_rat <- yind / xind
}

size <- 8000
rast <- st_rasterize(st_d |> 
                       select(population, geom),
                     nx = floor(size * x_rat), ny = floor(size * y_rat))


mat <- matrix(rast$population, nrow = floor(size * x_rat), ncol = floor(size * y_rat))

# set up color palette

pal <- "demuth"

colors <- met.brewer("Tam", 10, direction = -1)

texture <- grDevices::colorRampPalette(colors, bias = 3)(256)

swatchplot(texture)



###################
# Build 3D Object #
###################

# Keep this line so as you're iterating you don't forget to close the
# previous window

try(rgl::rgl.close())

# Create the initial 3D object
mat |> 
  height_shade(texture = texture) |> 
  plot_3d(heightmap = mat, 
          # This is my preference, I don't love the `solid` in most cases
          solid = FALSE,
          soliddepth = 0,
          # You might need to hone this in depending on the data resolution;
          # lower values exaggerate the height
          z = 15,
          # Set the location of the shadow, i.e. where the floor is.
          # This is on the same scale as your data, so call `zelev` to see the
          # min/max, and set it however far below min as you like.
          shadowdepth = 0,
          # Set the window size relatively small with the dimensions of our data.
          # Don't make this too big because it will just take longer to build,
          # and we're going to resize with `render_highquality()` below.
          windowsize = c(800,800), 
          # This is the azimuth, like the angle of the sun.
          # 90 degrees is directly above, 0 degrees is a profile view.
          phi = 90, 
          zoom = 1, 
          # `theta` is the rotations of the map. Keeping it at 0 will preserve
          # the standard (i.e. north is up) orientation of a plot
          theta = 0, 
          background = "white") 

# Use this to adjust the view after building the window object
render_camera(phi = 60, zoom = .65, theta = 0)

###############################
# Create High Quality Graphic #
###############################

# You should only move on if you have the object set up
# as you want it, including colors, resolution, viewing position, etc.

# Ensure dir exists for these graphics
if (!dir.exists(glue("images/{map}"))) {
  dir.create(glue("images/{map}"))
}

# Set up outfile where graphic will be saved.
# Note that I am not tracking the `images` directory, and this
# is because these files are big enough to make tracking them on
# GitHub difficult. 
outfile <- str_to_lower(glue("images/{map}/{map}_{pal}.png"))

# Now that everything is assigned, save these objects so we
# can use then in our markup script
saveRDS(list(
  map = map,
  pal = pal,
  colors = colors,
  outfile = outfile,
  coords = coords
), glue("R/portraits/{map}/header.rds"))

{
  # Test write a PNG to ensure the file path is good.
  # You don't want `render_highquality()` to fail after it's 
  # taken hours to render.
  if (!file.exists(outfile)) {
    png::writePNG(matrix(1), outfile)
  }
  # I like to track when I start the render
  start_time <- Sys.time()
  cat(glue("Start Time: {start_time}"), "\n")
  render_highquality(
    # We test-wrote to this file above, so we know it's good
    outfile, 
    # See rayrender::render_scene for more info, but best
    # sample method ('sobol') works best with values over 256
    samples = 450, 
    preview = FALSE,
    light = TRUE,
    lightdirection = rev(c(200, 200, 210, 210)),
    lightcolor = c(colors[3], "white", colors[10], "white"),
    lightintensity = c(750, 125, 1000, 125),
    lightaltitude = c(10, 80, 10, 80),
    # All it takes is accidentally interacting with a render that takes
    # hours in total to decide you NEVER want it interactive
    interactive = FALSE,
    # HDR lighting used to light the scene
    # environment_light = "assets/env/phalzer_forest_01_4k.hdr",
    # # environment_light = "assets/env/small_rural_road_4k.hdr",
    # # Adjust this value to brighten or darken lighting
    # intensity_env = 1.5,
    # # Rotate the light -- positive values move it counter-clockwise
    # rotate_env = 130,
    # This effectively sets the resolution of the final graphic,
    # because you increase the number of pixels here.
    # width = round(6000 * wr), height = round(6000 * hr),
    width = 8000, height = 8000
  )
  end_time <- Sys.time()
  cat(glue("Total time: {end_time - start_time}"), "\n")
}

This is where I get the error

mat |> 
  height_shade(texture = texture) |> 
  plot_3d(heightmap = mat,
          zscale = 100 / 5,
          solid = FALSE,
          shadowdepth = 0,
          
          )

render_camera(theta = -20, phi = 45, zoom = .8)

I have went through the process of reinstalling R, Rstudio, rTools and all the packages required. I just had this code working yesterday, not sure what the problem is.

Upvotes: 0

Views: 169

Answers (1)

user2554330
user2554330

Reputation: 44957

Those messages

Warning messages:
1: In rgl.primitive0(type = "triangles", x = c(-2499, -2498, -2497,  :
  RGL: GL Library : Maximum texture size of 1024x1024 exceeded.
(Perhaps enabling mipmapping could help.)
2: In rgl.primitive0(type = "triangles", x = c(2982, 2982, -2981, -2981,  :
  RGL: GL Library : Maximum texture size of 1024x1024 exceeded.
(Perhaps enabling mipmapping could help.)

are warnings, not errors. They appear to be coming from rgl, which is reporting hardware limits from the underlying OpenGL library. But rgl doesn't choose the size, that's probably rayshader. I don't know how you can tell it not to exceed that limit.

Just looking at what the message says, it appears that your graphics driver can't support very large textures. (Textures are bitmapped images mapped onto the 3D surface.) Maybe the graphics system is also being used by some other window, and has run out of memory. Or if you've switched machines or video modes since the previous run, maybe you just don't have the resources in the new setup to run it.

I can't give more specific advice, because your code dies on my system with an out of memory error from R:

Error: vector memory exhausted (limit reached?)

This happens during the render_camera() call, or maybe the plot_3d() call before that: the same place where you had problems. Maybe reducing the window_size = c(800, 800) parameter would help.

Upvotes: 0

Related Questions