I am trying to render a pyramid in openGL as basic practice, but the position of the vertices is not rendering as expected. I want the pyramid centered on the world coordinate system and the points I am using are as such.
glm::vec3 pos0(-width/2, -height/2, width/2); //front left vertex
glm::vec3 pos1(width/2, -height/2, width/2); //front right vertex
glm::vec3 pos2(-width/2, -height/2, -width/2);//back left vertex
glm::vec3 pos3(width/2, -height/2, -width/2); //back right vertex
glm::vec3 pos4(0.0f, height/2, 0.0f); //top vertex
I am drawing the pyramid with GL_DRAW_TRIANGLES in this order and showing CCW as the front face.
indices.push_back(5); /////////// front face
indices.push_back(2);
indices.push_back(1);
indices.push_back(5); /////////// left face
indices.push_back(3);
indices.push_back(1);
indices.push_back(5); /////////// back face
indices.push_back(3);
indices.push_back(4);
indices.push_back(5); /////////// right face
indices.push_back(4);
indices.push_back(2);
indices.push_back(1); /////////// left bottom
indices.push_back(3);
indices.push_back(2);
indices.push_back(2); /////////// right bottom
indices.push_back(3);
indices.push_back(4);
I expect the first three points to be the front face of the triangle, but for some reason that doesn't seem to be the case. The colors are not what would be expected (each vertex has its own color) and the height doesn't even seem tor each the top of the pyramid. Does anyone see a problem with my code?
OpenGL indices are 0-based. With 5 vertices, the range of indices must be from 0 to 4.
Related
I'm trying to draw a simple square with dx11, but the order of the indices of each triangle determines whether or not it shows up. I set the cull mode to none in the rasterizer state, but it doesn't seem to change anything.
If I specify the vertices of the first triangle to be 0, 1, 2 instead of 2, 1, 0 then the triangle doesn't show up. So my question is, do I need to order the vertices of a triangle in a specific way regardless of the cull mode?
P.S. I'm drawing a triangle list and not a strip.
UINT indices[] = {2, 1, 0,
1, 3, 0};
MeshVertex vertices[] =
{
{ Vector3(1.0f, 1.0f, 0.0f)}, //Top Right
{ Vector3(-1.0f, -1.0f, 0.0f)}, //Bottom Left
{ Vector3(1.0f, -1.0f, 0.0f)}, //Bottom Right
{ Vector3(-1.0f, 1.0f, 0.0f)} //Top Left
};
mesh = new Mesh(vertices, indices, 4, 6, shader);
So my question is, do I need to order the vertices of a triangle in a
specific way regardless of the cull mode?
** Define/Draw your vertices in clockwise order, and don't change the default cull mode.**
To determine whether a triangle can be displayed, you have to considering 2 factors
The front face, or you call it winding order, by default Direct3D treat a vertices defined in clockwise order as a front face, and all faces other than front faces are back faces.
The culling mode, by default, Direct3D will cull the back faces(the faces which vertices defined in counter-clockwise order).
You can set the front face and culling mode in D3D11_RASTERIZER_DESC structure
The reason why you didn't see difference when you change the culling mode to NONE is because both triangle were in the same order and D3D11_CULL_NONE means didn't cull any faces.
If triangle 1 is in clockwise order and triangle 2 is in counter clockwise order, you will only see one triangle when you set the cull mode to D3D11_CULL_FRONT or D3D11_CULL_BACK, and see both when you set it to D3D11_CULL_NONE.
I am trying to understand how to specify texture coordinates for a GL_QUAD_STRIP.
I have managed to get things working for one quad:
float vertices[] = { 0.0f, 0.0f, 1.0f, +1.0f, 0.0f, 0.0f, // bottom line
0.0f, 1.0f, 1.0f, +1.0f, 1.0f, 0.0f}; // top line
unsigned int indices[] = {2, 0, // x = 0
3, 1}; // x = +1
float textureCoordinates[] = { 1.0f, 0.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f};
...
glBindBuffer(GL_ARRAY_BUFFER, 0); // unbinds any buffer object previously bound
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibufferid);
glDrawElements(GL_QUAD_STRIP, 4, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
And here is how the result looks (white rectangle with image, rest is drawn on to help explain):
However I do not understand the logic behind the choice of textureCoordinates[] :-(.
The first texture coordinate is (1,0); I would assume that this corresponds to lower right corner?
Also I would assume that when OpenGL reads the first index: 2, it uses this to look up the vertex: (0,1,1): upper left corner. Next it reads the first texture coordinate: (1,0).
But as mentioned above I would assume this to be the lower right corner of the texture !?
However the texture is shown unrotated so this can not be the case!?
Just like the vertices, the texture coordinates are also selected based on the indices used by glDrawElements(). So the first texture coordinate is not (1,0), but (1,1) because the first index is 2. Vertices and coordinates would be according to the following table, where i = index, v = vertex and t = texture coordinate. (I'll only take the x and y coordinates into consideration for the vertices, as the z coordinate doesn't really matter in this case.)
i v t
2 (0,1) (1,1)
0 (0,0) (1,0)
3 (1,1) (0,1)
1 (1,0) (0,0)
If we draw this on a piece of paper, we can see that this means the coordinates make more sense, since the indices matter. (I recommend that you do this! I had to do that to understand what was going on.) Notice in the table how the y coordinates match perfectly between the vertex and texture coordinate for a given index. But the x coordinates don't match: when the vertex has x = 0, the texture coordinate has x = 1 and vice versa. I assume this would make the image appear mirrored around the y axis instead of rotated in any way. What does the original image look like? Is it mirrored compared to what we see in the image you posted so that the building is on the left? If so, the texture coordinates would be the explanation. In that case, texture coordinate 2 and 3 should switch places.
In case you are curious, you could take a look at the OpenGL 2.1 specification on page 18, Figure 2.5(a), to see why the vertex indices were selected as they were. It would create a quad with vertices specified in a counterclockwise direction when projected on the screen. This is good because the initial value for glFrontFace() is GL_CCW, which means we see the front face of the polygons in the rendered image and the polygons would not have been culled if culling was enabled (see glCullFace()). (Culling is not enabled by default though, so it may or may not have mattered in your case.)
I hope this helped. Do comment if something is unclear!
I am looking for a technique in OpenGL that I can use in order to map color points on a surface.
Each point is defining a display color and three coordinates (X, Y, Z).
The surface on which to map those data is built from all the points' coordinates in the main usage (complex shape) but can be built normally from standard shape such as a cone or a sphere.
Since there are voids between the points (for example one millimeter step between two points along the X axis), it would be also needed to interpolate the points data on the surface.
I am thinking about building bitmaps from the points and then applying those bitmaps on my surfaces but I am wondering if OpenGL does have a feature that allow to do that in a "smart way".
It sounds to me like what you are asking for is basic OpenGl behaviour.
If you draw a triangle:
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top vertex
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom left vertex
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom right vertex
glEnd();
The result is a smoothly ( if garishly) coloured solid triangle.
So your problem is to construct a series of polygons (possibly just triangles) which cover your surface and have the point set as vertices.
For a great intro to OpenGl, see NeHe's tutorials, including the above example.
In OpenGL, for the following situation in 2D, how can I rotate this one quad - and only this one quad (everything else in the scene should stay where it is)?
// Draw in immediate mode
glBegin(GL_QUADS); // begin drawing quads
glVertex2f(box.x,box.y); // top-left corner
glVertex2f(box.x+box.w,box.y); // top-right corner
glVertex2f(box.x+box.w,box.y+box.h); // bottom-right corner
glVertex2f(box.x,box.y+box.h); // bottom-left corner
glEnd(); // end drawing quads
glRotatef(angle, x,y,z) seems to rotate my whole scene.
Enclose it in glPushMatrix and popMatrix
glPushMatrix(GL_MODELVIEW);
glRotatef(angle, x,y,z);
// Draw in immediate mode
glBegin(GL_QUADS); // begin drawing quads
glVertex2f(box.x,box.y); // top-left corner
glVertex2f(box.x+box.w,box.y); // top-right corner
glVertex2f(box.x+box.w,box.y+box.h); // bottom-right corner
glVertex2f(box.x,box.y+box.h); // bottom-left corner
glEnd(); // end drawing quads
glPopMatrix(GL_MODELVIEW);
Basically in the above example you are pushing the modevliew matrix one position up into the stack, saving it in an essence. Then you rotate the modelview and draw your quad.
Afterwards you pop back one position in the modelview taking back to how it was before the rotation and the drawing.
I'm trying to put a texture on one surface of a cube (if facing the XY plane the texture would be facing you).
No texture is getting drawn, only the wireframe and I'm wondering what I'm doing wrong. I think it's the vertex coordinates?
Here's some code:
struct paperVertex {
D3DXVECTOR3 pos;
DWORD color; // The vertex color
D3DXVECTOR2 texCoor;
paperVertex(D3DXVECTOR3 p, DWORD c, D3DXVECTOR2 t) {pos = p; color = c; texCoor = t;}
paperVertex() {pos = D3DXVECTOR3(0,0,0); color = 0; texCoor = D3DXVECTOR2(0,0);}
};
D3DCOLOR color1 = D3DCOLOR_XRGB(255, 255, 255);
D3DCOLOR color2 = D3DCOLOR_XRGB(200, 200, 200);
vertices[0] = paperVertex(D3DXVECTOR3(-1.0f, -1.0f, -1.0f), color1, D3DXVECTOR2(1,0)); // bottom left corner of tex
vertices[1] = paperVertex(D3DXVECTOR3(-1.0f, 1.0f, -1.0f), color1, D3DXVECTOR2(0,0)); // top left corner of tex
vertices[2] = paperVertex(D3DXVECTOR3( 1.0f, 1.0f, -1.0f), color1, D3DXVECTOR2(0,1)); // top right corner of tex
vertices[3] = paperVertex(D3DXVECTOR3(1.0f, -1.0f, -1.0f), color1, D3DXVECTOR2(1,1)); // bottom right corner of tex
vertices[4] = paperVertex(D3DXVECTOR3(-1.0f, -1.0f, 1.0f), color1, D3DXVECTOR2(0,0));
vertices[5] = paperVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f), color2, D3DXVECTOR2(0,0));
vertices[6] = paperVertex(D3DXVECTOR3(1.0f, 1.0f, 1.0f), color2, D3DXVECTOR2(0,0));
vertices[7] = paperVertex(D3DXVECTOR3(1.0f, -1.0f, 1.0f), color1, D3DXVECTOR2(0,0));
D3DXCreateTextureFromFile( md3dDev, "texture.bmp", &gTexture);
md3dDev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
md3dDev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
md3dDev->SetTexture(0, gTexture);
md3dDev->SetStreamSource(0, mVtxBuf, 0, sizeof(paperVertex));
md3dDev->SetVertexDeclaration(paperDecl);
md3dDev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
md3dDev->SetIndices(mIndBuf);
md3dDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, VTX_NUM, 0, NUM_TRIANGLES);
disclaimer: I have no Direct3D experience, but solid OpenGL and general computer graphics experience. And since the underlying concepts don't really differ, I attempt an answer, of whose correctness I'm 99% sure.
You call md3dDev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME) immediately before rendering and wonder why only the wireframe is drawn?
Keep in mind that using a texture doesn't magically turn a wireframe model into a solid model. It is still a wireframe model with the texture only applied to the wireframe. You can only draw the whole primitve as wireframe or not.
Likewise does using texture coordinates of (0,0) not magically disable texturing for individual faces. You can only draw the whole primitive textured or not, though you might play with the texture coordinates and the texture's wrapping mode (and maybe the texture border) to make the "non-textured" faces use a uniform color from the texture and thus look like not textured.
But in general to achieve such deviating render styles (like textured/non-textured, but especially wireframe/solid) in a single primitive, you won't get around splitting the primitive into multiple ones and drawing each one with its dedicated render style.
EDIT: According to your comment: If you don't need wireframe, why enable it then? Besides disabling wireframe, with your current texture coordinates the other faces won't just have a single color from the texture but some strange distorted version of the texture. This is because your vertices (and their texture coordinates) are shared between different faces, but the texture coordinates at the moment are created only for the front face to look reasonable.
In such a situation, you won't get around duplicating vertices, so that each face uses a set of 4 unique vertices. In the case of a cube you won't actually need an index array anymore, because each face needs its own vertices. This is due to the fact, that a vertex conceptually represents all of the vertex' attributes (position, color, texCoord, ...) and you cannot have a two vertices sharing a position but having different texture coordinates (you can but you need two distinct vertices). Once you've duplicated the vertices accordingly, you can give each of the corner vertices their respective texture coordinates (which would be the usual [0,1]-quad if you want them textured normally, or all 0s if you want them to have a single color, in this case the color of the bottom left (or top left in D3D?) corner of the texture).
The same problem arises if you want to light the cube and need normals per-face, istead of interpolated per-vertex normals. In this case you also have to introduce duplicate vertices only deviating in their normal attribute. Always keep in mind that a vertex conceptually consists of all the vertex attributes and if two vertices have the same position but a different color/normal/texCoord/... they are conceptually (and practically) different vertices.