Cylinder drawn with triangles in OpenGL - opengl

It's drawing a cylinder with stacks and edges but the problem is that stacks are connected to one point instead of a new one.
Maybe a picture will show it better:
And here's how I'm rendering the side because disks are rendered separately:
for (int i = 1; i <= height; ++i) {
for (int j = 0; j < edges; ++j) {
glBegin(GL_TRIANGLES); {
// 0 bottom
glVertex3f(x + radius * cos(theta + interval), y , z + radius * sin(theta + interval));
// 1 bottom
glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
// 3 top
glVertex3f(x + radius * cos(theta + interval), y + y_value * i, z + radius * sin(theta + interval));
// 0 bottom
glVertex3f(x + radius * cos(theta + interval), y , z + radius * sin(theta + interval));
} glEnd();
theta += interval;
}
theta = 0.0;
}
I've been trying to solve it for days and I run out of ideas. Do you know what I am doing wrong?
UPDATE:
I've changed it to be render with quads using ybungalobill advice. Now I'm struggling with UV mapping. And hopefully once this part is solved it'll be easy enough to convert into triangles.
That's what I have now:
And that's the code I'm using for UV mapping:
u = 0.0,
v = 0.0,
u_inter = 1.0 / edges,
v_inter = 1.0 / y_value; // (y_value = height / edges)
for (int i = 1; i <= height; ++i) {
for (int j = 0; j < edges; ++j) {
glBegin(GL_QUAD_STRIP); {
// 0 bottom
glTexCoord2f(u, v);
// 1 bottom
glTexCoord2f(u + u_inter, v);
// 2 top
glTexCoord2f(u + u_inter, v + v_inter);
// 3 top
glTexCoord2f(u, v + v_inter);
} glEnd();
theta += interval;
u += u_inter;
}
v += v_inter;
theta = 0.0;
}

float y0 = y + y_value * (i-1);
float y1 = y + y_value * i;
// 0 bottom
glVertex3f(x + radius * cos(theta + interval), y0, z + radius * sin(theta + interval));
// 1 bottom
glVertex3f(x + radius * cos(theta), y0, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y1, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y1, z + radius * sin(theta));
// 3 top
glVertex3f(x + radius * cos(theta + interval), y1, z + radius * sin(theta + interval));
// 0 bottom
glVertex3f(x + radius * cos(theta + interval), y0, z + radius * sin(theta + interval));

Related

How can I adjust my mouse coord to isometric tile coordinate to compensate for diamond shaped tiles?

My drawing function looks like this:
for(std::size_t y = 0; y < mapSizeY; ++y)
{
for(std::size_t x = mapSizeX-1; x != static_cast<std::size_t>(-1); --x)
{
auto tile = GetTile(x,y);
int64_t xPos = (x * TILE_WIDTH / 2) + (y * TILE_WIDTH / 2);
int64_t yPos = (y * TILE_HEIGHT / 2) - (x * TILE_HEIGHT / 2);
xPos += camera.GetXOffset();
yPos += camera.GetYOffset();
yPos -= tile->z * TILE_HEIGHT/2; // everything is at height 1 for now
auto zoom = camera.GetZoomFactor();
xPos *= zoom;
yPos *= zoom;
if(xPos < 0-TILE_WIDTH*zoom || yPos < 0-TILE_HEIGHT*zoom)
continue;
if(xPos > GetScreenDimensions().x || yPos > GetScreenDimensions().y)
continue;
// x is up right
// y is down right
Blit(m_grass,{xPos,yPos,TILE_WIDTH*zoom,TILE_HEIGHT*zoom+TILE_HEIGHT/2*zoom});
}
}
When a user clicks the mouse, I'm trying to get the x,y coord of the tile clicked on. To translate the mouse coords to the map coords I'm doing the following:
// x and y are the mouse coords
auto zoom = camera.GetZoomFactor();
auto xOffset = camera.GetXOffset();
auto yOffset = camera.GetYOffset();
int z = 1; // every tile is at height 1 for now
int32_t xTile = -(TILE_HEIGHT * (2 * xOffset * zoom + TILE_WIDTH * z * zoom - 2 * x) + 2 * TILE_WIDTH * (y - yOffset * zoom)) / (2 * TILE_HEIGHT * TILE_WIDTH * zoom);
int32_t yTile = (-2 * TILE_HEIGHT * xOffset * zoom + TILE_HEIGHT * TILE_WIDTH * z * zoom + 2 * TILE_HEIGHT * x - 2 * yOffset * TILE_WIDTH * zoom + 2 * TILE_WIDTH * y) / (2 * TILE_HEIGHT * TILE_WIDTH * zoom);
I came up with the above by solving my draw positions for x,y
The results I get appear to be on the correct tile. But the issue is that if I click past the "halfway" point of a tile, in either x or y coordinate, it attributes it to the next tile (because they're squares with transparent corners to make the "diamond" shape).
How can I adjust the above to compensate?

Algorithm to draw a sphere using quadrilaterals

I am attempting to draw a sphere from scratch using OpenGL. The function must be defined as void drawSphere(float radius, int nSegments, int nSlices), must be centred at the (0, 0, 0) origin and must be created using GL_QUADS.
Firstly, are the "slices" the sort of tapered cylinder shapes that are stacked on top of each other to create the sphere, and the "segments" are the quads that are generated in a circle to generate the wall/side of each of these tapered cylinder slices?
Secondly, I cannot seem to find any algorithms or examples of how to make the calculations to generate this sphere using quadrilaterals - most example seem to be generated from triangles instead.
EDIT
Here is what I have just tried, which is definitely in the right direction, but my coordinate calculations are off somewhere:
void drawSphere(float radius, int nSegments, int nSlices) {
/*
* TODO
* Draw sphere centered at the origin using GL_QUADS
* Compute and set normal vectors for each vertex to ensure proper shading
* Set texture coordinates
*/
for (float slice = 0.0; slice < nSlices; slice += 1.0) {
float lat0 = M_PI * (((slice - 1) / nSlices) - 0.5);
float z0 = sin(lat0);
float zr0 = cos(lat0);
float lat1 = M_PI * ((slice / nSlices) - 0.5);
float z1 = sin(lat1);
float zr1 = cos(lat1);
glBegin(GL_QUADS);
for (float segment = 0.0; segment < nSegments; segment += 1.0) {
float long0 = 2 * M_PI * ((segment -1 ) / nSegments);
float x0 = cos(long0);
float y0 = sin(long0);
float long1 = 2 * M_PI * (segment / nSegments);
float x1 = cos(long1);
float y1 = sin(long1);
glVertex3f(x0 * zr0, y0 * zr0, z0);
glVertex3f(x1 * zr1, y1 * zr1, z0);
glVertex3f(x0 * zr0, y0 * zr0, z1);
glVertex3f(x1 * zr1, y1 * zr1, z1);
}
glEnd();
}
}
I'm not seeing radius being used. Probably a simple omission. Let's assume that for the rest of your computation the radius is 1. You should generate your 4 values by using 0-1 on both (x, y), and (z, zr), but not mix within those tuples.
So x1 * zr1, y1 * zr1, z0 is not right because you're mixing zr1 and z0. You can see that the norm of this vector is not 1 anymore. Your 4 values should be
x0 * zr0, y0 * zr0, z0
x1 * zr0, y1 * zr0, z0
x0 * zr1, y0 * zr1, z1
x1 * zr1, y1 * zr1, z1
I'm not too sure about the order since I don't use Quads but triangles.

Incomplete sphere OpenGL

I want to draw a sphere using VBO for vertex, color and UV coordinates for texture. My problem is that the sphere is not 'closed', there is a hole in the origin. I know that this is because my code depends on (1/segments) distance between each vertex; I am working with segments = 40.
I know that, if I rise that value, the hole will be lower, but program is slower. I don't know if there's a way to eliminate the hole without rise the variable.
Here's the code:
for(int i = 0; i <= segments; i++){
double lat0 = pi * (-0.5 + (double)(i - 1) / segments);
double z0 = sin(lat0);
double zr0 = cos(lat0);
// lat1 = [-pi/2..pi/2]
double lat1 = pi * (-0.5 + (double)i / segments);
double z1 = sin(lat1);
double zr1 = cos(lat1);
for (int j = 0; j <= segments; j++){ // Longitud
// lng = [0..2*pi]
double lng = 2 * pi * (double)(j - 1) / segments;
double x = cos(lng);
double y = sin(lng);
//glNormal3f(x * zr0, y * zr0, z0); // Normals
ballVerts.push_back(x * zr0); //X
ballVerts.push_back(y * zr0); //Y
ballVerts.push_back(z0); //Z
ballVerts.push_back(0.0f);
ballVerts.push_back(0.0f);
ballVerts.push_back(0.0f);
ballVerts.push_back(1.0f); //R,G,B,A
texX = abs(1 - (0.5f + atan2(z0, x * zr0) / (2.0 * pi)));
texY = 0.5f - asin(y * zr0) / pi;
ballVerts.push_back(texX); // Texture coords
ballVerts.push_back(texY); // U, V
//glNormal3f(x * zr1, y * zr1, z1); //Normals
ballVerts.push_back(x * zr1); //X
ballVerts.push_back(y * zr1); //Y
ballVerts.push_back(z1); //Z
ballVerts.push_back(0.0f);
ballVerts.push_back(0.0f);
ballVerts.push_back(1.0f);
ballVerts.push_back(1.0f); //R,G,B,A
texX = abs(1 - (0.5f + atan2(z1, x * zr1) / (2.0 * pi)));
texY = 0.5f - asin(y * zr1) / pi;
ballVerts.push_back(texX); // Texture coords
ballVerts.push_back(texY);
}
}
// Create VBO....
And this is the output I have:
I don't think that's a hole. You're drawing one segment too many, and causing it to draw additional triangles at the south pole, with the texture wrapped around:
for(int i = 0; i <= segments; i++){
double lat0 = pi * (-0.5 + (double)(i - 1) / segments);
In the first loop iteration, with i = 0, the angle will be less than -0.5 * pi, resulting in the extra triangles shown in your picture.
If you want to split the latitude range into segments pieces, you only need to run through the outer loop segments times. With the code above, with the loop from 0 up to and including segments, you're iterating segments + 1 times.
The easiest way to fix this is to start the loop at 1:
for(int i = 1; i <= segments; i++){
double lat0 = pi * (-0.5 + (double)(i - 1) / segments);
I would probably loop from 0 and make the end exclusive, and change the angle calculations. But that's really equivalent:
for(int i = 0; i < segments; i++){
double lat0 = pi * (-0.5 + (double) / segments);
...
double lat1 = pi * (-0.5 + (double)(i + 1) / segments);

Texture mapping a circle made using GL_POLYGON

I am trying to map a texture to a circle using GL_POLYGON using this code:
void drawCircleOutline(Circle c, int textureindex)
{
float angle, radian, x, y; // values needed by drawCircleOutline
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureLib[textureindex]);
glBegin(GL_POLYGON);
for (angle=0.0; angle<360.0; angle+=2.0)
{
radian = angle * (pi/180.0f);
x = (float)cos(radian) * c.r + c.pos.x;
y = (float)sin(radian) * c.r + c.pos.y;
glTexCoord2f(x, y);
glVertex2f(x, y);
}
glEnd();
glDisable(GL_TEXTURE_2D);
}
it looks like this when running.
And should look like this:
Try:
radian = angle * (pi/180.0f);
xcos = (float)cos(radian);
ysin = (float)sin(radian);
x = xcos * c.r + c.pos.x;
y = ysin * c.r + c.pos.y;
tx = xcos * 0.5 + 0.5;
ty = ysin * 0.5 + 0.5;
glTexCoord2f(tx, ty);
glVertex2f(x, y);

OpenGL Hemisphere Texture Mapping

I need to have a hemisphere in opengl. I found a drawSphere function which I modified to draw half the lats (which ends up drawing half of the sphere) which is what I wanted. It does this correctly.
However, I don't know what i should do with glTexCoordf to get the textures to map properly onto this half sphere. I'm really not great with opengl, and I've tried countless variations but I just can't get the textures to appear properly on it.
void drawHemisphere(double r, int lats, int longs)
{
int i, j;
int halfLats = lats / 2;
for(i = 0; i <= halfLats; i++)
{
double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats);
double z0 = sin(lat0);
double zr0 = cos(lat0);
double lat1 = M_PI * (-0.5 + (double) i / lats);
double z1 = sin(lat1);
double zr1 = cos(lat1);
glBegin(GL_QUAD_STRIP);
for(j = 0; j <= longs; j++)
{
double lng = 2 * M_PI * (double) (j - 1) / longs;
double x = cos(lng);
double y = sin(lng);
// glTexCoordf()
glNormal3f(x * zr0, y * zr0, z0);
glVertex3f(x * zr0, y * zr0, z0);
// glTexCoordf()
glNormal3f(x * zr1, y * zr1, z1);
glVertex3f(x * zr1, y * zr1, z1);
}
glEnd();
}
}
Does anyone have any idea of what values I should be putting in? Or what I need to calculate for it?
Thanks!
Basically, you shouldn't need anything fancy there. The texture coordinate space ranges from zero to one. So pick some intermediate values for the vertices in between. I can't explain it more thoroughly without image, so the best I can do is to point You to this article: UV mapping, it's a good starting point. Hope this helps as a starter.
Here's my guess:
{
double lng = 2 * M_PI * (double) (j - 1) / longs;
double x = cos(lng);
double y = sin(lng);
double s1, s2, t;
s1 = ((double) i) / halfLats;
s2 = ((double) i + 1) / halfLats;
t = ((double) j) / longs;
glTexCoord2d(s1, t);
glNormal3d(x * zr0, y * zr0, z0);
glVertex3d(x * zr0, y * zr0, z0);
glTexCoord2d(s2, t);
glNormal3d(x * zr1, y * zr1, z1);
glVertex3d(x * zr1, y * zr1, z1);
}
Remember to properly set texture in OpenGL. An example call with texture:
glActiveTexture(GL_TEXTURE0);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
// texture must be bound & enabled
texture.bind();
texture.enable();
drawHemisphere(1, 40, 40);
texture.disable();
I used Java + JOGL to test it, so it's not one-to-one C++ solution, but conceptually it should be the same. At least You have proper glTexCoord2d() calls.