How to light all faces of rotating cube using glium - rust

I made rotating cube using glium, but only one face is colored.
I am using Gouraud shading(from glium tutorial) for lighting.
The cube is made by defined 24 vertices and 6 normals.
Rotation is made by two matrices:
let m = [
[1.0, 0.0, 0.0, 0.0],
[0.0, t.cos(), -t.sin(), 0.0],
[0.0, t.sin(), t.cos(), 0.0],
[0.0, 0.0, 0.0, 1.0f32]
];
let n = [
[t.cos(), 0.0, t.sin(), 0.0],
[0.0, 1.0, 0.0, 0.0],
[-t.sin(), 0.0, t.cos(), 0.0],
[0.0, 0.0, 0.0, 1.0f32]
];
Fragment shader(glsl):
#version 150
in vec3 v_normal;
out vec4 color;
uniform vec3 u_light;
void main() {
float brightness = dot(normalize(v_normal), normalize(u_light));
vec3 dark_color = vec3(0.6, 0.0, 0.0);
vec3 regular_color = vec3(1.0, 0.0, 0.0);
color = vec4(mix(dark_color, regular_color, brightness), 1.0);
}
light:
let light = [-1.0, 0.4, 0.9f32];
The light should hit another face of cube in some point of rotation.
I try changing direction of light, that don't change anything.
Have anybody some idea?
for more info about the cube see this: my previous q. (some info like rotation is not actual here)
Images of the cube:

I think you have your normal direction in eye space and light direction in world space. So you have to convert light direction into camera (eye) space before passing into the shader, something like that (pseudo code):
normalize(VIEW_MATRIX * light)
Also, for the brightness I think you don't want to have negative value, so it have to be in a form
max(dot(N, L), 0.0)
I would recommend to read an introduction article about lighting, for example on learnOpenGL - https://learnopengl.com/Lighting/Basic-Lighting where you can see a description on what direction are for and how to prepare them.
or some GLSL tutorials about having light direction in camera space - https://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-lights/

Related

Change colors in colormap based on range of values

Is it possible to set the lower and/or upper parts of a colorbar based on ranges of values? For example, given the ROYGBIV colormap below and optionally an offset and a range value, I'd like to change the colors below offset and/or above range. In other words, suppose offset = 20 and range = 72, I'd like to color all the values less than or equal to 20 in black and all values greater than or equal to 72 in white. I'm aware of the methods set_under and set_over, but they require changing the parameters vmin and vmax (as far as I know), which is not what I want. I want to keep the original minimum and maximum values (e.g., vmin = 0 and vmax = 100), and only (optionally) change the colors of the extremities.
ROYGBIV = {
"blue": ((0.0, 1.0, 1.0),
(0.167, 1.0, 1.0),
(0.333, 1.0, 1.0),
(0.5, 0.0, 0.0),
(0.667, 0.0, 0.0),
(0.833, 0.0, 0.0),
(1.0, 0.0, 0.0)),
"green": ((0.0, 0.0, 0.0),
(0.167, 0.0, 0.0),
(0.333, 0.0, 0.0),
(0.5, 1.0, 1.0),
(0.667, 1.0, 1.0),
(0.833, 0.498, 0.498),
(1.0, 0.0, 0.0)),
"red": ((0.0, 0.5608, 0.5608),
(0.167, 0.4353, 0.4353),
(0.333, 0.0, 0.0),
(0.5, 0.0, 0.0),
(0.667, 1.0, 1.0),
(0.833, 1.0, 1.0),
(1.0, 1.0, 1.0))
}
rainbow_mod = matplotlib.colors.LinearSegmentedColormap("rainbow_mod", ROYGBIV, 256)
I found one way to do it using ListedColormap as explained here. The basic idea is to obtain the RGBA lists/tuples of the colors in the LinearSegmentedColormap object (numpy array) and replace the first or last few lists with replicates of the desired color.
It looks something like this:
under_color = [0.0, 0.0, 0.0, 1.0] # black (alpha = 1.0)
over_color = [1.0, 1.0, 1.0, 1.0] # white (alpha = 1.0)
all_colors = rainbow_mod(np.linspace(0, 1, 256))
vmin = 0.0
vmax = 100.0
all_colors[:int(np.round((20.0 - vmin) / (vmax - vmin) * 256)), :] = under_color
all_colors[int(np.round((72.0 - vmin) / (vmax - vmin) * 256)):, :] = over_color
rainbow_mod_list = matplotlib.colors.ListedColormap(all_colors.tolist())

Turtle graphics plotting

when I plot a list of data(freqList):
[0.09090909090909091, 0.0, 0.0, 0.09090909090909091, 0.18181818181818182, 0.0, 0.0, 0.0, 0.045454545454545456, 0.0, 0.0, 0.0, 0.0, 0.045454545454545456, 0.045454545454545456, 0.045454545454545456, 0.045454545454545456, 0.18181818181818182, 0.045454545454545456, 0.09090909090909091, 0.045454545454545456, 0.0, 0.045454545454545456, 0.0, 0.0, 0.0]
When I try to fill in the bar with color, only half of the bar gets filled, resembling a right triangle. Also I can't figure out how to label the x and y- axis to have the letters a-z on the x axis representing each decimal. And the frequency from 0.0 - max(freqList) on the y - axis that increments up based on each decimal number. I know I have to use t.write(), but I don't know where to place it
def letterFreqPlot(freqList):
border = 5
t = turtle.Turtle()
t.pensize(3)
screen = t.getscreen()
maxheight = max(freqList)
numbers = len(freqList)
screen.setworldcoordinates(0-border,-.05,numbers+1,maxheight)
t.goto(0,0)
t.speed(0)
t.lt(90)
t.fd(maxheight)
t.fd(-maxheight)
t.right(90)
for item in freqList:
t.fillcolor("blue")
for dist in [1, item, 1, item]:
t.begin_fill()
t.fd(dist)
t.lt(90)
t.fd(1)
t.end_fill()
Looks like you're making four begin_fill calls for every end_fill call. Try moving the begin call outside of the loop.
for item in freqList:
t.fillcolor("blue")
t.begin_fill()
for dist in [1, item, 1, item]:
t.fd(dist)
t.lt(90)
t.fd(1)
t.end_fill()

Java3D transform vectors odd behavior

This is part of code that's supposed to draw 2 cubes of side 0.3 next to each other
When I get instead is this: http://imageshack.us/photo/my-images/189/89254345.png/
(they are halfway into each other) I tried printing the transforms and they look alright:
1
1.0, 0.0, 0.0, 0.0
0.0, 1.0, 0.0, 0.0
0.0, 0.0, 1.0, 0.0
0.0, 0.0, 0.0, 1.0
2
1.0, 0.0, 0.0, 0.3
0.0, 1.0, 0.0, 0.0
0.0, 0.0, 1.0, 0.0
0.0, 0.0, 0.0, 1.0
It's as if the second box was only moved by 0.15 but if I multiply dx by 2 things break when more cuboids of different dimensions get added with transforms across the y or z axis
private BranchGroup rootGroup;
public void addBox(float dx){
Cuboid Cuboid1 = new Cuboid(0.3f ,0.3f, 0.3f, appearence);
TransformGroup tg = new TransformGroup();
Transform3D transform = new Transform3D();
Vector3f vector = new Vector3f(dx, 0f, 0f);
transform.setTranslation(vector);
tg.setTransform(transform);
tg.addChild(Cuboid1);
rootGroup.addChild(tg);
}
public void addBoxes(){
for(int i=0;i<2;i++){
addBox(i*0.3f);
}
}
The cuboid class is from here: http://www.java2s.com/Code/Java/3D/Java3DBoxandacustomCuboidimplementation.htm
It looks like you're creating a cuboid which is 0.6 in size so it will have +0.3 and -0.3 X,Y,Z values for the first box as the center point of the cuboid is 0,0,0.
The second box is the same but offset by 0.3 in one axis which gives 0.3-0.3=0.0 and +0.3+0.3 = 0.6 values in one of the axes.
This will give two boxes that overlap half way on one axis which seems to match your picture.
Perhaps you mean to create a box with width/height/depth of 0.3/2 and translate it by 0.3/2 so that the center of the box is at 0.15,0.15 and use a scale factor of 0.15 instead of 0.3?

vtkLookupTable specifing colors with opacities

I am trying to use a vtkLookupTable in order to set the colors for my mesh. I started with vtkColorTransferFunction which worked fined except that it does not enable me to set opacities for various colors.
vtkLookupTable offers this opportunity, but I am having problems to get it working.
My vtkPolyData have scalars value set that determine the color they shall get. Overall 7 values, set with the scalars 0 to 6.
My vtkLookupTable looks like this:
vtkLookupTable lut = new vtkLookupTable();
lut.SetNumberOfColors(7);
double opacity = 0.3;
lut.SetTableValue(0, 0, 0, 1, opacity);
lut.SetTableValue(1, 0, 1.0, 0, opacity);
lut.SetTableValue(2, 0.6, 1.0, 0.0, opacity);
lut.SetTableValue(3, 1.0, 1.0, 0.0, 0.7);
lut.SetTableValue(4, 1.0, 0.8, 0.0, opacity);
lut.SetTableValue(5, 1.0, 0.4, 0.0, opacity);
lut.SetTableValue(6, 1.0, 0.0, 0.0, 1);
If I use a vtkColorTransferFunction with the same values (just no opacity) it works. Anyone any idea why this is not working? I should be same?
Thanks for help.
From your pseudocode, two lines may be missing.
lut.SetTableRange(0.0,6.0)
lut.Build()
Do you call these functions?
If it is not this problem then it may be because of the polydatamapper you are using. Can you submit a whole pipeline for your problem?
As a guide the following code will need to be called to allow your scalar values to be
added to the mapper and visualised correctly.
mapper.SetLookupTable(lut)
mapper.SetScalarVisibility(1)
mapper.SetScalarRange(0.0,6.0)

CGContextSetRGBFillColor is not setting fill color

I have used
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
But it is not filled with any color.
Please help.

Resources