Re: Help with Pointers

new topic     » goto parent     » topic index » view thread      » older message » newer message
Icy_Viking said...

C example being ported to Eu [https://github.com/ColleagueRiley/RGFW/blob/main/examples/gears/gears.c]

//From gl.h 
WINGDIAPI void APIENTRY glLightiv(GLenum light,GLenum pname,const GLint *params); 

OpenGL functions follow a predictable nomenclature:

OpenGL said...

Function type suffixes are typically defined in the following pattern:

FunctionName{1234}{b s i i64 f d ub us ui ui64}{v} 

The first set of braces is an optional number from 1 to 4. This defines the number of parameters of those types that the function takes.

The second set of braces defines the types themselves, as defined by the table to the left.

If the optional v​ suffix is provided, this means the function takes a "vector": a pointer to an array of some number of values.

The function glLightiv takes a vector (a pointer to an array) of GLint values. Sometimes, however, the number of values in the array varies based on some parameter.

In the case of glLight, you have to consult the options for the pname parameter to determine how many values you actually need to provide in the params array.

OpenGL said...

The ten light parameters are as follows:

  • GL_AMBIENT - four integer or floating-point values
  • GL_DIFFUSE - four integer or floating-point values
  • GL_SPECULAR - four integer or floating-point values
  • GL_POSITION - four integer or floating-point values
  • GL_SPOT_DIRECTION - three integer or floating-point values
  • GL_SPOT_EXPONENT - a single integer or floating-point value
  • GL_SPOT_CUTOFF - a single integer or floating-point value
  • GL_CONSTANT_ATTENUATION - a single integer or floating-point value
  • GL_LINEAR_ATTENUATION - a single integer or floating-point value
  • GL_QUADRATIC_ATTENUATION - a single integer or floating-point value

I would say that technically all you have to do is feed it any number of integers and assume they line up.

public procedure glLightiv(integer light, integer pname, sequence params) 
 
    atom pparams = allocate_data(sizeof(C_INT) * length(params)) 
    poke4(pparams, params) 
 
    c_proc(_glLightiv, {light,pname,pparams}) 
 
    free(pparams) 
 
end procedure 

Given the max seems to be four values, you could just "round up" by padding the remaining values first.

public procedure glLightiv(integer light, integer pname, sequence params) 
 
    if length(params) < 4 then 
        params &= repeat(0, 4-length(params)) 
    end if 
 
    atom pparams = allocate_data(sizeof(C_INT) * length(params)) 
    poke4(pparams, params) 
 
    c_proc(_glLightiv, {light,pname,pparams}) 
 
    free(pparams) 
 
end procedure 

But to be safe, you should check the length of params corresponds to the provided pname value.

public procedure glLightiv(integer light, integer pname, sequence params) 
 
    integer required = 0 
 
    switch pname do 
        case GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_POSITION then 
            required = 4 
        case GL_SPOT_DIRECTION then 
            required = 3 
        case GL_SPOT_EXPONENT, GL_SPOT_CUTOFF, GL_CONSTANT_ATTENUATION, 
            GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION then 
            required = 1 
    end switch 
 
    if required != length(params) then 
        error:crash("expected %d params but received %d", {required,length(params)}) 
    end if 
 
    atom pparams = allocate_data(sizeof(C_INT) * length(params)) 
    poke4(pparams, params) 
 
    c_proc(_glLightiv, {light,pname,pparams}) 
 
    free(pparams) 
 
end procedure 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu