Qt C++ Set Set Vtk Axis Scake - c++

I have the following code where i plot points in 3d space:
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
for(int in = 0; in<x.size(); in++)
{
points->InsertNextPoint (x[in], y[in], ry[in]);
}
vtkSmartPointer<vtkPolyData> pointsPolydata =
vtkSmartPointer<vtkPolyData>::New();
pointsPolydata->SetPoints(points);
vtkSmartPointer<vtkVertexGlyphFilter> vertexFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
vertexFilter->SetInputConnection(pointsPolydata->GetProducerPort());
#else
vertexFilter->SetInputData(pointsPolydata);
#endif
vertexFilter->Update();
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->ShallowCopy(vertexFilter->GetOutput());
// Setup colors
unsigned char red[3] = {255, 0, 0};
unsigned char green[3] = {0, 255, 0};
unsigned char blue[3] = {0, 0, 255};
vtkSmartPointer<vtkUnsignedCharArray> colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
colors->SetName ("Colors");
colors->InsertNextTupleValue(red);
colors->InsertNextTupleValue(green);
colors->InsertNextTupleValue(blue);
polydata->GetPointData()->SetScalars(colors);
// Visualization
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapper->SetInputConnection(polydata->GetProducerPort());
#else
mapper->SetInputData(polydata);
#endif
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(5);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
this->ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
renderWindowInteractor->Start();
When I get the resulting plot. All the points are plotted, but is hard to see anything due to the default scale. What I'm not sure is how to zoom in the view on the z axis. Any help would be appreciated.

Related

Textures created with SDL_CreateTexture don't appear to support transparency

I want to copy multiple surfaces (created with TTF_*) to a single texture, and I can't seem to get that resulting texture to render onto the window with transparency handled correctly.
static void example(void) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
TTF_Init();
SDL_Window* w = SDL_CreateWindow("", 0, 0, 200, 200, 0);
SDL_Renderer* r = SDL_CreateRenderer(w, -1, 0);
TTF_Font* f = TTF_OpenFont(MY_FONT, 100);
SDL_Color c = {.r = 0, .g = 255, .b = 0, .a = 255};
SDL_Surface* s = TTF_RenderGlyph32_Blended(f, 'A', c);
SDL_Texture* t = SDL_CreateTextureFromSurface(r, s);
#ifdef RENDER_COPY
SDL_Texture* t2 = SDL_CreateTexture(
r,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_TARGET,
s->w,
s->h);
SDL_SetRenderTarget(r, t2);
SDL_RenderCopy(r, t, NULL, NULL);
SDL_SetRenderTarget(r, NULL);
t = t2;
#endif
#ifdef RENDER_MEMCPY
SDL_Texture* t2 = SDL_CreateTexture(
r,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
s->w,
s->h);
SDL_Surface* s2;
sdl_try(SDL_LockTextureToSurface(t2, NULL, &s2));
memcpy(s2->pixels, s->pixels, s->w * s->h * sizeof(SDL_Color));
SDL_UnlockTexture(t2);
t = t2;
#endif
#ifdef RENDER_BLEND
SDL_SetTextureBlendMode(t, SDL_BLENDMODE_BLEND);
#endif
SDL_SetRenderDrawColor(r, 255, 255, 255, 255);
SDL_RenderClear(r);
SDL_Rect rect = {.x = 0, .y = 0};
SDL_QueryTexture(t, NULL, NULL, &rect.w, &rect.h);
SDL_RenderCopy(r, t, &rect, &rect);
SDL_RenderPresent(r);
SDL_Event event;
do { SDL_WaitEvent(&event); } while (event.type != SDL_KEYDOWN);
}
Without RENDER_COPY, I get a texture (created via SDL_CreateTextureFromSurface) that blends correctly onto a render target (this is what I want, but with multiple surfaces combined into one texture.)
With RENDER_COPY (i.e. a second texture is created and then copied onto) the background of the texture is black. This is a contrived example since there is only one surface being copied, but I want to copy multiple surfaces to t2.)
With RENDER_BLEND, the black is mostly gone but it's as if the texture was blended onto a black background.
Is there a way to create a texture that can be set completely transparent instead of a solid color? I've also tried to set the pixels directly (RENDER_MEMCPY) but that just ends up being a solid color as it appears the alpha in each pixel is ignored:
SDL version is 2.0.20.
Figured it out. When doing SDL_RenderCopy from the first texture to the second, the blend mode on the first texture should be set to none:
SDL_SetTextureBlendMode(t, SDL_BLENDMODE_NONE);
Now when the second texture is copied (with SDL_BLENDMODE_BLEND) the edges don't have the black artifacts.

Drawpixels by glfw only renderer quarter screen. buffer use screen width * height

I don't know how to resolve this problem, I want to use this function to make a soft-renderer engine to study 3D pipeline.
This is code:
#define GLFW_INCLUDE_GLU
#include <GLFW/glfw3.h>
int main(int argc, const char * argv[]) {
if(!glfwInit()){ //init error
return -1;
}
int width = 200; //screen width
int height = 200; //screen height
GLFWwindow* window = glfwCreateWindow(width, height, "Hello OpenGL", NULL, NULL); //create window
if(!window){ //create window fail
glfwTerminate();
return -1;
}
u_char* pixels = new unsigned char[width * height * 4]; //buffer
uint index; //temp
//set pixel
for (int count_a = 0; count_a < width; count_a++) {
for (int count_b = 0; count_b < height; count_b++) {
index = count_b * width + count_a;
pixels[index * 4 + 0] = 255; //red pixel
pixels[index * 4 + 1] = 0; //green
pixels[index * 4 + 2] = 0; //blue
pixels[index * 4 + 3] = 255; //alpha
}
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) { //close window
glClearColor(1.0, 1.0, 1.0, 1.0); //clear screen by white pixel
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2f(-1, -1); //set origin to screen bottom left
//glPixelZoom(2, 2); //if i use this function. renderer is right.
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); //draw pixel
glfwSwapBuffers(window); //swap buffer
}
glfwTerminate();
return 0;
}
I was having this issue when using GLUT, so I came to this before and while reimplementing my code using GLFW. This issue is caused by the Retina display (as a lot of other sources have figured out), and the following ended up fixing it for me:
int width = 1360*2; //screen width
int height = 768*2; //screen height
GLFWwindow* window = glfwCreateWindow(width/2, height/2, "Hello OpenGL", NULL, NULL); //create window
// ...

DirectX Toolkit C++ SpriteBatch Draw Rectangle

I am using DirectX 11, DirectX Toolkit and C++. How can I create a rectangle with a blue border for spritebatch ? I'm guessing I need to create a texture in memory perhaps with a 1 pixel blue border ?
ComPtr<ID3D11ShaderResourceView> spriteSheet_;
ComPtr<ID3D11Resource> resource;
CreateDDSTextureFromFile(d3dDevice_, L"mytex.dds", resource.GetAddressOf(),
spriteSheet_.ReleaseAndGetAddressOf());
batch->Draw(spriteSheet_.Get(), position, &sourceRect, DirectX::Colors::White,
rotation_, spriteOrigin_, scale_, DirectX::SpriteEffects_None, depth_);
You can use SpriteBatch to draw a single-pixel texture as you described--you could use a white texture and use the SpriteBatch tinting to make it Blue.
Here is some simple code for programmatically creating a 1x1 white texture in Direct3D 11:
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> pWhiteTexture;
static const uint32_t s_pixel = 0xffffffff;
D3D11_SUBRESOURCE_DATA initData = { &s_pixel, sizeof(uint32_t), 0 };
D3D11_TEXTURE2D_DESC desc;
memset( &desc, 0, sizeof(desc) );
desc.Width = desc.Height = desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
Microsoft::WRL::ComPtr<ID3D11Texture2D> tex;
HRESULT hr = mDevice->CreateTexture2D( &desc, &initData, &tex );
DX::ThrowIfFailed(hr);
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
hr = mDevice->CreateShaderResourceView( tex.Get(), &SRVDesc, &pWhiteTexture );
DX::ThrowIfFailed(hr);
A more efficient solution would be to use PrimitiveBatch to draw a blue quad where you want it located.
See the DirectX Tool Kit tutorial Simple rendering

Render a simple image using vtkDataImage

I want to render a simple image using vtkImageData in C++. I know that vtkImageDataGeometryFilter can do the job, but I'm asking if there is an other way to get the same result without using PolyData ? Here is my code, but I have an access violation reading while rendering, I don't know why...
vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetExtent(0, 5, 0, 5, 0, 1);
imageData->SetSpacing(1, 1, 1);
imageData->SetOrigin(0, 0, 0);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
imageData->SetScalarComponentFromFloat(i,j,0,0,0);
}
}
vtkSmartPointer<vtkImageActor> imageActor =
vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInputData(imageData);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(imageActor);
renderWindow->Render();
renderWindowInteractor->Start();
Thanks in advance for your help ^^
I found the bug finally. The line imageData->AllocateScalars(VTK_FLOAT, 3); was missing.
The following code displays a green square:
vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
imageData->SetExtent(0, 50, 0, 50, 0, 1);
imageData->SetSpacing(1, 1, 1);
imageData->SetOrigin(0, 0, 0);
imageData->AllocateScalars(VTK_FLOAT, 3);
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
imageData->SetScalarComponentFromFloat(i, j, 0, 1, 255);
}
}
vtkSmartPointer<vtkImageActor> imageActor =
vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInputData(imageData);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(imageActor);
renderer->SetBackground(1, 0, 0.5);
renderWindow->Render();
renderWindowInteractor->Start();

Create a 1x1 texture in SDL 2.0

In C# and XNA, you can create a 1x1 texture like this:
Texture2D white_pixel;
white_pixel = new Texture2D(GraphicsDevice, 1, 1);
white_pixel.SetData<Color[]>(new Color{ Color.White });
// Sorry if I got the syntax wrong, it's been a while
Then later on, you can arbitrarily draw the pixel to any size and color by doing this:
spriteBatch.Begin();
spriteBatch.Draw(white_pixel, new Rectangle(0, 0, width, height), Color.Whatever);
spriteBatch.End();
What is the equivalent in SDL?
SDL_Texture *tex = nullptr;
SDL_CreateTexture(renderer,
Uint32 format, // What do I put here
int access, // and here
1
1);
// Not sure if this is correct
SDL_SetTextureColorMod(tex,
255,
255,
255)
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 10;
rect.h = 10;
SDL_RenderCopy(renderer, tex, nullptr, &rect);
SDL_PIXELFORMAT_RGB24/SDL_PIXELFORMAT_BGR24 for format and SDL_TEXTUREACCESS_STATIC for access would be a good start.
Or you could just draw a colored rectangle directly via SDL_SetRenderDrawColor() and SDL_RenderFillRect().