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 5 years ago.
Improve this question
I want to create a lens flare in OpenGL C++. I referred this video and star rendering code.
double calculateGlowSize(double diameter, double temperature, double distance) {
static const double DSUN = 1392684.0;
static const double TSUN = 5778.0;
// Georg's magic formula
double d = distance; // Distance
double D = diameter * DSUN;
double L = (D * D) * pow(temperature / TSUN, 4.0); // Luminosity
return 0.016 * pow(L, 0.25) / pow(d, 0.5); // Size
}
but i want the output to be more like the image shown below.
How do i create a lens flare effect?
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 10 months ago.
Improve this question
I have a simple 2D game of pool. You can give a ball some speed, and it'll move around and hit other balls. But I want a ball to stop eventually, so I added some acceleration, by running this code every frame:
balls[i].ax = -balls[i].vx * 0.1;
balls[i].ay = -balls[i].vy * 0.1;
...
if(hypot(balls[i].vx, balls[i].vy) < 0.2){
balls[i].vx = 0;
balls[i].vy = 0;
}
And it works... But I find it weird, not realistic. I have no physics knowledge, but I'm pretty sure friction should not depend on speed.
How can I improve physics of slowing down without too much complexity?
The rolling friction formula is this: F_k,r=μ_k,r_Fn. It only factors in the properties of the surface (μ_k) and the force on the ball (r_Fn). This should decelerate with a constant value, just adjust it until it looks roughly correct.
Example code:
x = 1 // mess around with this until it looks right
if (ball.xVelocity > x) { ball.xVelocity -= x } else { ball.xVelocity = 0 }
if (ball.yVelocity > x) { ball.yVelocity -= x } else { ball.yVelocity = 0 }
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 3 years ago.
Improve this question
I am building a physics engine, and therefore, I am also learning opengl to be able to visualize what the physics engine is doing. I am wondering how I convert natural units (e.g. 1 meter, 1 inch, etc.) to opengl units.
I have done some research, and it seems that the opengl unit is not really defined. Does this mean that I could map the number 0.01f to be equivalent to 1 cm. Therefore, if I had a circle centered at cx and cy, and I wanted it to drop by 1 cm, then I could do the following?:
float cx = 0.05f;
float cy = 0.05f;
cy -= .01;
Here is some snippets of the toy code for the graphics so far:
void drawCircle() {
float r = 0.05;
int num_segments = 1000;
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_LOOP);
float thetaStep = 2.0f*M_PI/num_segments;
for(float theta = 0; theta < 2.0f*M_PI; theta += thetaStep) {
float x = r * cosf(theta);
float y = r * sinf(theta);
glVertex2f(x+cx, y+cy);
}
glEnd();
glFlush();
}
GLvoid Timer(int value) {
cy -= 0.01;
if(value) {
glutPostRedisplay();
}
glutTimerFunc(30, Timer, value);
}
The above code snippets work. It will move the ball down at a constant rate of 0.01 opengl units.
Since the opengl coordinate system is between the values -1 and 1, it seems the correct way is to scale. If I want the world to be on a scale of 300 meters, then it becomes between -300 and 300. To convert to opengl coordinate you simply divide the natural coordinate by the scaler. For example, if I had a circle at position <0,100m> in the world coordinate system, you would convert to opengl coordinate system by dividing each component by 300; that is assuming that the opengl coordinate system is on a scale of 300 meters. This would result to opengl coordinate <0, .3333>.
The units you use are entirely up to you. If you want to treat 1 unit as a meter, or an inch, or a mm, that's entirely up to you. If everything is scaled accordingly, it won't make a difference to OpenGL, so choose the unit that makes sense wrt your physics engine and use those (so that you avoid the need to constantly keep converting units).
The only consideration is really whether your units can accommodate floats, or if you need to fall back to double for position values. For example, if using floats for position, and assuming 1 GL unit = 1 meter, then you'll run out of millimetre precision at about 25km from the origin.
As for saying OpenGL has a range between -1 and 1, that's entirely incorrect. That's the range of values once they have been transformed into screen space. It doesn't make any difference whether you scale those between -FLT_MAX and +FLT_MAX, or within a range of -10 to +10. You'll have the exact same result.
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 5 years ago.
Improve this question
I am trying to implement balls that change their direction when they hit an obstacle(in this case a fixed ball). I can detect when a collision occurs but I don't know how to modify the direction of a ball once it hits an obstacle. Here's some code:
struct Vec2
{
float x, y;
};
struct Ball
{
void onCollision(const Ball& fixedBall)
{
// This function will be called when a collision occurs
// Speed will be constant, only direction needs to change
}
void update()
{
position += direction * speed;
}
Vec2 position, direction; // direction is a normalized vector
float speed, radius;
};
You will need to invert the speed by negating it.
if (collision)
speed = speed * -1
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 ¢er=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?
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);
}
}