How to make basic line segments in LWJGL/OpenGL - graphics

I am in the process of learning LWJGL and also OpenGL. I have done the tutorials on quads, and also succesfully drawn polygons on a display. I am trying to draw lines using the same methods, but the lines are not created, or they are made invisible, possibly with a pixel width of 0? I have googled for an answer or a tutorial, but so far all of them seems to claim that I am doing the right thing. my method is as follows:
private void drawLine(Point point, Joint Point2) {
GL11.glColor3f(0.0f, 1.0f, 0.2f);
GL11.glBegin(GL11.GL_LINE);
GL11.glVertex2d(point.getX(), point.getY());
GL11.glVertex2d(point2.getX(), point2.getY());
GL11.glEnd();
}
I also tried to put this one in the middle, but no effect.
GL11.glLineWidth(3.8f);

As stated in the comments, The answer was that GL11.GL_LINE is not accepted as a constant in this case. GL11.LINE_STRIP however works like a charm.

Related

Calculate CV2 Homography by points and line

I have a list of points in the field (like upper_goal_point/ left_upper_outer_corner, etc.
I know their corresponding coordinates in destination image - so I can calculate the homography:
h, status = cv2.findHomography(pts_src, pts_dst)
I also have blue points in the upper corner line (look at image above), which I only know that their destination's y coordinates are 0 (because they are in the upper line), but I don't know where exactly they lay in that line.
Can I use those blue points in order to improve the homography?
P.S.
You can see that the upper corner line in the homography is not horizontal line, it's diagonal, which of course is not correct:
Actually it possible to use line correspondence in find homography.
https://www.researchgate.net/publication/220845575_Combining_Line_and_Point_Correspondences_for_Homography_Estimation
Several years ago we implement this approach in one project. During simplification all math we come up with simple trick. We transform every line a*x + b*y + c = 0 to point (a/c, b/c)
// *** Don't copy paste this code, read below! ***//
Point2f convertPointsToHomogeneousLine(Point2f p1, Point2f p2) {
Point3f p1h(p1.x, p1.y, 1);
Point3f p2h(p2.x, p2.y, 1);
Point3f lineHomo(p1h.y*p2h.z - p1h.z*p2h.y,
p1h.z*p2h.x - p1h.x*p2h.z,
p1h.x*p2h.y - p1h.y*p2h.x);
Point2f lineHomoNorm(lineHomo.x / lineHomo.z,
lineHomo.y / lineHomo.z);
return lineHomoNorm;
}
And pass this points inside. As I remember I also dig inside OpenCV implementation of findHomography and insert this lines somewhere inside to solve step. Inside OpenCV there some normalization applied to points before pass to solve step. So you need to skip this step for this kind of points.
We do not use it in production. User need to calibrate camera manually by providing lines and points on the image and in meter system. It has too complicated interface and bad stability. But in your case I think it can work better. If you will automatically find lines and correspondence.
P.S. Please note that in paper they use some normalization technique.
It will improve stability. We faced with stability problem, do not
solved it in our journey.

How to draw pixels for ray-tracing in C++?

I'm currently trying to learn ray-tracing in C++. I am getting help from two books: one is Ray Tracing from the Ground Up by Kevin Suffern, and the other one is Physically Based Rendering by Matt Pharr. These two books are great for learning basics and, later, advanced stuff.
I could create some basic shapes using user interface of Suffern's book. However when I tried to write all code on my own, things have gone wild. I realized that I don't even know how to open a window and fill pixels on that. Do you have any good resource to recommend that could teach me the basics of drawing in C++.
You could generate image files instead of drawing to windows. The PPM format is the simplest one to generate. Browsers usually can display PPM. Safari does.
If you want to generate PNG files use libpng.
SDL might work for you: http://www.libsdl.org/
You can allocate your own image buffer, write your pixels to it, and then save to file/draw to window as needed. I expect the Pharr book has its own version of this tucked away somewhere, courtesy of Literate Programming.
More concretely: GUI API's and image file format libraries will typically be able to read simple image buffer data, stored in row-major array order. I would recommend an RGBA pixel format, something like the following:
template<class T> class image_rgba {
unsigned m_rows, m_cols;
T *m_data;
public:
image_rgba(unsigned rows, unsigned columns)
: m_rows(rows)
, m_cols(columns)
, m_data(new T[rows*columns*4])
{}
~image_rgba() { delete[] m_data; }
typedef T pixel[4];
pixel index_pixel_ref(unsigned row, unsigned col) {
assert(row<m_rows && col<m_cols);
return m_data + (m_cols*row+col)*4;
}
}
(note that I have not tested the above -- best to treat it as pseudocode...)

Appropriate use of CCTextureCache

I'm currently creating a CCSprite like this:
CCSprite *s = [CCSprite spriteWithFile:#"image.png"];
This sprite is the background image of a CCLayer that's used relatively often. Is the following use of CCTextureCache more efficient?
CCTexture2D *t = [[CCTextureCache sharedTextureCache] addImage:#"image.png"];
CCSprite *s = [CCSprite spriteWithTexture:t];
No. Internally, all methods that use an image as a texture (not just CCSprite) will add the texture to the CCTextureCache.
The only reason why you would want to use addImage directly is when you want to pre-load certain textures so that the first appearance of a node using that texture won't cause a lag during gameplay.
First of all, if you look to the code of spriteWithFile: method, you will see that it adds image to the texture cache anyway if cannot find it there.
The second thing you must know, that if you store your art in atlases for reducing memory usage(for example, atlas 2048x2048 pixels with 20 different pictures), spriteWithTexture: will create sprite with whole huge atlas(2048x2048 pixels) texture.

How to light-up a sprite in cocos2d?

I've already know how to dark-down a CCSprite object by:
sprite.color = ccc3(x, x, x); // x is a value less then 255
(As far as i know, it should be a direct mapping of openGL functions, so its easy to achieve.)
But when it comes to light-up, my current solution is adding another mask sprite (same shape but all in white), changing its blendFunc to { GL_SRC_ALPHA, GL_ONE } and overlaying it onto the target. Besides all the codes added, there should be a mask image for each need-to-light-up one.
Is there a way to do light-up as easily as dark-down?
However, not as easy as setColor, in Cocos2d 2.x, with OpenGL ES 2.0 support, you can achieve this by using custom shaders. You can get started here:
http://www.raywenderlich.com/10862/how-to-create-cool-effects-with-custom-shaders-in-opengl-es-2-0-and-cocos2d-2-x
You may also try inverting the sprite's darker color to get a lighter one.

Disable surround sound with openAL

I'm french so sorry for my english.
I'm currently making a splitscreen 2D game with LWJGL.
I'm using the openAL API which is given with LWJGL. Everything seems to works perfectly. Well, too perfectly to be honest : because I'm making a splitscreen game and because I can't have 2 listener sharing the same context, I want to get rid of the left/right panning.
Sound attenuation work well. I change the position of sound depending on the closest player.
The listener doesn't change, always at (0,0,0). The sound position is (soundPosition - closestPlayerPosition).
So how do I get rid of the surround thing ? I want to keep sound attenuation over distance, of course.
I could simply put the sound on the Z-axis depending on the distance but this seem a bit silly (I have to compute the distance every time I have to update a sound position).
Thanks !
When you calculate the sound's position (soundPosition - closestPlayerPosition) take the length of the vector returned by that and then put that sound directly down the z axis that distance away from the player.
Example:
soundPosition = (1.4,0,1.4)
closestPlayerPosition = (0,0,0)
soundDirection = soundPosition - closestPlayerPosition = (1.4,0,1.4)
soundDistance = soundDirection.Length()
And finally, the final position of your sound:
finalSoundPosition = (0,0,soundDistance) = (0,0,2)
Edit: I didn't notice you already suggested this. To be honest I think this is fine, and its the only way to solve your problem beyond rewriting stuff internal to openAL

Resources