Functions within loops C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm using OpenGL to load a lot of different textures. As I'm loading a lot of textures though I have around 50 lines that are all calls of the same function, I was just wondering if there is a way to condense this or make it more efficient.
For reference here is what the function call and a section of the code looks like:
//Load the barrier texture image
LoadTexture("barrier/ConcreteBarrier_Texture_stripes.jpg", textureId3, true);
//Load the shed texture image
LoadTexture("shed/shed1.jpg", textureId4, true);
//Load the banana texture image
LoadTexture("banana/Banana_D01.png", textureId5, true);
//Load the UI button 1 image
LoadTexture("ui/button1.png", textureIdB1, false);
//Load the UI button 2 image
LoadTexture("ui/button2.png", textureIdB2, false);

You can put the values into an array, and then loop through the array calling the function, eg:
struct paramInfo {
string file;
GLuint id;
bool b;
};
paramInfo params[] = {
{"barrier/ConcreteBarrier_Texture_stripes.jpg", textureId3, true},
{"shed/shed1.jpg", textureId4, true},
{"banana/Banana_D01.png", textureId5, true},
{"ui/button1.png", textureIdB1, false},
{"ui/button2.png", textureIdB2, false},
...
};
for (auto &p : params) {
LoadTexture(p.file.c_str(), p.id, p.b);
}

Related

Vulkan flickering for small meshes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am rendering different models. Big models (more than a thousand triangles) are rendered without issue, however small models(a single triangle, or 12 triangles) flicker.
The way I am currently rendering involves 2 passes, so I assume the flickering comes from an absence of proper barriers.
The execution goes:
start to render pass,
render to texture (image attachment of FB),
stop pass.
start pass,
render to main display
end pass.
There is only a single global graphics queue. Following the vulkan tutorial, I have a command buffer for every image in my swapchain, and I record commands to each buffer in that list, like so:
for(auto &cmd : cmd_buffers)
{
std::array<vk::ClearValue, 2> clears = {};
clears[0].color = vk::ClearColorValue(array<float, 4>({0,0,0,0}));
clears[1].depthStencil = vk::ClearDepthStencilValue(1.0f, 0.0f);
vk::RenderPassBeginInfo render_pass_info(
*render_pass, *(framebuffers[index]), {{0,0}, extent},
clears.size(), clears.data()
);
cmd.beginRenderPass(&render_pass_info, vk::SubpassContents::eInline);
index++;
}
/* submit more instructions to the command buffers later */
For the off screen target (FB) I only record a single command buffer.
I call my first render pass as described above, and then, immediately before calling the second render pass, I try waiting on an image barrier:
void RenderTarget::WaitForImage(VulkanImage* image)
{
vk::ImageMemoryBarrier barrier = {};
// TODO(undecided): maybe this isn't always true
barrier.oldLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
barrier.srcAccessMask = vk::AccessFlagBits::eShaderWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image->GetImage();
barrier.subresourceRange =
vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1);
vk::CommandBuffer command_buffer = h_interface->BeginSingleTimeCommands();
command_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eAllGraphics,
vk::PipelineStageFlagBits::eFragmentShader, {}, 0, nullptr, 0,
nullptr, 1, &barrier);
h_interface->EndSingleTimeCommands(command_buffer);
}
And then somewhere else in the code I call it:
display_target.WaitForImage(&image);
active_target_id = 0;
display_target.StartFrame();
render_presentation.AddDrawData(quad_buffer, {uniform_data},
{{&image, 1}});
display_target.EndFrame();
Based on the behaviour I have seen, I assume my issue is a synchronization problem, however, the image barrier code doesn't seem to solve the flickering.
Am I using the barrier wrong? Should I look into other possible sources of error (and if so, which)?

precise texture mapping for polygon voxelization [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am recently implement a voxel cone tracing algorithm, the first step is to voxelize polygons...
I am using sparse octree to convert the polygon, everything was fine except for texture mapping
mipmap=6 (64x64x64)
// We find its smallest AABB
AABB aabb = FindAABB(in);
// better aabb with same width, height and depth
AABB betterAABB = FixAABB(aabb);
// Create Octree and marked the grids that are touched by triangles in the mesh
// dfunc is a callable object which return true when triangle and grid are overlapped
// cb is a callable object, it is called when octree reaches its leaf..
Octree<uint8> tree(betterAABB, mipmap);
for(auto &f: in.faces)
tree.findTouchedLeaves(f, dfunc, cb);
Callback function...
struct Callback
{
void operator()(OctreeNode<uint8> *node, const Face &obj)
{
// Check if the node is already marked.
if(!node->data.value)
{
const glm::vec3 &center=node->aabb.getCenter();
out.push_back(center);
// Problem here!! how to calculate precise uv for texture mapping
color.push_back(text->getPixel((obj.o.texCoord+obj.a.texCoord+obj.b.texCoord)/3.0f));
}
node->data.value=1;
}
std::vector<glm::vec3> out;
std::vector<glm::vec4> color;
const ImageTexture *text;
} cb;
as you can see... I directly average three UVs of o,a,b.(terrible right?)
It is more imprecise when a triangle is larger than a grid.
Should I need to use rasterize-based algorithm?
My question is: How to precisely coloring a grid?

OPENGL Terrain from text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create terrain which takes height values from txt file .
During my search I realized that generally the terrains are created from bmp files or another kind of picture taking values from pixels. I read height values from file into a 2d array.
void File(){
FILE* file = fopen("data.txt", "r"); // data.txt opened with read only mode to get heights
int i,j;
for (i = 0; i < 17; i++){
for (j = 0; j < 21; j++){
fscanf(file, "%d", &data[i][j]);
}
}
fclose(file);
}
and then load these values to vertex to create triangle.
But there are triangles everywhere when I change the x y z values .
The intended project is to create a terrain.
Is there a special way or code to create terrain by using just height values from a txt file?
OpenGL renders primitives like triangles and triangle strips. You'll need to convert your terrain heightmap into primitives that OpenGL understands.
Perhaps this tutorial found by a quick google search can help you.
http://www.codeproject.com/Articles/14154/OpenGL-Terrain-Generation-An-Introduction
Depending on how the data looks in the heightmap, say its in the format of vertex/height.
You could read in a line, say its
v 1.7/2.1/3.7 h 10
store off the vertex and translate that vertex up 10 (height).
Repeat for each vertex and connect them with faces.
If it's just height values then you could substitue in arbitrary verticies for each height value.
I.E.
heightmap:
h 10
h 20
h 30
Then as a function:
void generateTerrain(int length, int width)
{
for(;length>= 0; --length)
{
for(; width >= 0; --width)
{
//read line from file
//store the height in a temp hieght variable
//createVertex(length, width, height);
//store this vertex somewhere for later
}
}
}
There are probably way more efficient ways of doing that, but for simple terrain generation that just popped in my head :)

PaintEvent in Qt painting all widgets the same for each instance of the class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a class called MotionVectorDisplay which inherits from QWidget and I override the paintevent, what I do with this class is draw motion vectors for a pariticular macroblock which is 16x16 in size and there are multiple of these macroblocks in a frame so I create a new instance of this class for each macroblock, pass in multiple parameters for building the motion vectors and parent that widget to another widget for displaying. This all works as expected but I get output like this
It seems to me that when the paintevent is called it remembers the last time the paint event is called and keeps the drawn lines and creates the ugly mess that is there in the picture, that should show a few lines for a few macroblocks not all of them. This is the code
mv = new MotionVectorDisplay(pics[frameCounter].motionVect,
pics[frameCounter].subMotionVector,
macBlockParent);
mv->stackUnder(cooefsLink);
QGraphicsOpacityEffect* effect =
new QGraphicsOpacityEffect(mv);
effect->setOpacity(0.9);
mv->setGraphicsEffect(effect);
if(mvToggle->checkState() == Qt::Checked)
{
mv->show();
}
else
{
mv->hide();
}
motionVectorsContain.push_back(mv);
That constructs the macroblock in the MainWindow class, this is the constructor and PaintEvent from the MotionVectorDisplay class
MotionVectorDisplay::MotionVectorDisplay(const pair<string, string>& motionVect,
const vector<pair<string, string> >& subMotionVect,
QWidget* parent)
: QWidget(parent)
{
this->setFixedSize(16, 16);
this->setStyleSheet("background-color: transparent;");
motionVectors = &motionVect;
subMotionVectors = &subMotionVect;
}
void MotionVectorDisplay::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(QPen(Qt::black, 1.5));
for(int subMotVects = 0; subMotVects < subMotionVectors->size(); subMotVects++)
{
string x = subMotionVectors->at(subMotVects).first;
string y = subMotionVectors->at(subMotVects).second;
if(subMotVects == 0)
{
painter.drawLine(0, 0, atoi(x.c_str()), atoi(y.c_str()));
}
else if(subMotVects == 1)
{
painter.drawLine(4, 4, atoi(x.c_str()), atoi(y.c_str()));
}
else if(subMotVects == 2)
{
painter.drawLine(8, 8, atoi(x.c_str()), atoi(y.c_str()));
}
else
{
painter.drawLine(12, 12, atoi(x.c_str()), atoi(y.c_str()));
}
}
}
I suspect that you need to set the OpaquePaintEvent flag on your widget to false:
this->setAttribute(Qt::WA_OpaquePaintEvent, false);
Alternatively (and more commonly), you'd repaint every pixel of your widget in every paint event so that it essentially writes over anything that was painted there before, something like this at the beginning of your paint event:
painter.setBrush( Qt::black ); // Or whatever your background color is
painter.drawRect( rect() );

Converting a Matrix Class into IplImage* [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do have a class of Matrix and I access to it with two loops and have stored in it all the values I wanted to have in it.
Matrix MatriceJ(width, height);
for (int i=0;i<width;i++)
{
for (int j=0;j<height;j++)
{
MatriceJ.at(i,j)=....
}
}
But now, I would like to store the MatriceJ in an IplImage* for that I can multiply its different elements, one by one, with the other IplImages.
Can you help me with that?
This should get you started. I assume the data to be unsigned char and one channel, please adjust accordingly.
// Create the image
int depth = IPL_DEPTH_8U; // please adjust
int channels = 1; // please adjust
IplImage* img = cvCreateImage(cvSize(width,height), depth, channels);
// Now assume there is a matrix MatriceJ
// Copy the data to our newly created IplImage*
for (int i=0;i<height;i++)
{
uchar* ptr = (uchar*)(img->imageData + i*img->widthStep);
for (int j=0;j<width;j++)
{
ptr[j] = MatriceJ(i,j);
}
}