1

I have an OpenGL Graphics assignment and well for some reason I'm trying to create a flat square on the X,Z plane (Y is just 1.0 for every vertices). I've tried making the problem smaller by just trying to create 2 flat vertices instead of four, and for some reason 2 of the vertices always seem to go wayyy into the negative y direction.

glMaterialfv(GL_FRONT, GL_DIFFUSE, drone_mat_diffuseBUILDING);
glEnableClientState(GL_VERTEX_ARRAY);
float buildingsize = 8 * floors + 4;
GLfloat quadV[300];

quadV[0] = -6.5;
printf("%.5f", quadV[0]);
quadV[1] = 1.0;
printf("%.5f", quadV[1]);
quadV[2] = 8.5;
printf("%.5f", quadV[2]);

quadV[3] = -6.5;
printf("%.5f", quadV[3]);
quadV[4] = 1.0;
printf("%.5f", quadV[4]);
quadV[5] = -7.5;
printf("%.5f", quadV[5]);


glVertexPointer(3, GL_FLOAT, 0, quadV);
glDrawArrays(GL_POLYGON, 0, buildingsize);
glDisableClientState(GL_VERTEX_ARRAY);here

Here's what it looks like when ran:

Snapshot

The red line should be a flat line on the X Z plane.

For further simplicty, I'm trying to convert this code:

    glBegin(GL_POLYGON);
glVertex3f(firstCubeXPOS - firstCubeXSCALE/2.0, 1.0, firstCubeZPOS + firstCubeZSCALE/2.0);
glVertex3f(firstCubeXPOS - firstCubeXSCALE / 2.0, 1.0, firstCubeZPOS - firstCubeZSCALE / 2.0);
glVertex3f(firstCubeXPOS + firstCubeXSCALE / 2.0, 1.0, firstCubeZPOS - firstCubeZSCALE / 2.0);
glVertex3f(firstCubeXPOS + firstCubeXSCALE / 2.0, 1.0, firstCubeZPOS + firstCubeZSCALE / 2.0);
glEnd();
glFlush();

Into a vertex array so I can add new elements / modify elements by each vertex. Because as of right now the code right above me shows what I need to see (2d square laying on the XZ plane).

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 3
    To make that a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) you have to tell at least what the model view matrix and the projection matrix is. – Rabbid76 Nov 04 '18 at 16:04
  • Here's what it looks like when ran: https://imgur.com/a/SPfT1oz , the red line should be a flat line on the X Z plane – noahlattari Nov 04 '18 at 16:11
  • @Rabbid76 I'm not using a projection matrix as of right now, or model view. – noahlattari Nov 04 '18 at 17:05
  • 3
    You store 6 values to `quadV`. In `glDrawArrays(GL_POLYGON, 0, buildingsize);`, the third parameter is `count` (see here: [`glDrawArrays()`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawArrays.xhtml)). Why `buildingsize` is `float`? Assuming that `floors` is an `int` and >= 0, the `count` is either 0 or >= 12. I suspect, it draws some additional vertices you didn't intend. – Scheff's Cat Nov 04 '18 at 17:10
  • @Scheff Floors is also a float, it's for a future part of this project where I need to loop the array and add new vertices into it floors amount of times. If I have only 9 indexes (so 3 vertices) would that be a count of 9? – noahlattari Nov 04 '18 at 17:14
  • `float` for the `count` of `glDrawArrays()` appears to me like a bad choice. `float`s are always prone to rounding errors which may unexpectedly hit you when it's converted to `GLsizei`. – Scheff's Cat Nov 04 '18 at 17:16
  • If you pass two vertices, and OpenGL renders a triangle from it then I would bed that `buildingsize` results in 3 (or more). – Scheff's Cat Nov 04 '18 at 17:19
  • @Scheff I changed count to 12 instead of a float variable, still getting the same thing in the image : ( – noahlattari Nov 04 '18 at 17:21
  • If you want to render two vertices, I would set count to 2. – Scheff's Cat Nov 04 '18 at 17:22
  • You cant draw the primitve type `GL_POLYGON` like this! Use `GL_QUAD` instead! - `glDrawArrays(GL_QUAD, 0, buildingsize);`. `GL_POLYGON` would draw a singe polygon with 12 vertices. – Rabbid76 Nov 04 '18 at 17:22
  • @Scheff Here's my current updated code: https://pastebin.com/thV177GU – noahlattari Nov 04 '18 at 17:23
  • 1
    @Scheff OHHH COUNT IS THE AMOUNT OF VERTICES!!! I thought it was the amount of indexes in the array!!!!! It worked!! Bless your soul! – noahlattari Nov 04 '18 at 17:24
  • The `indices` in the doc. confused me a bit as well. I assume this addresses "index buffers". In your case, there is no index buffer - you just provide vertices (which are indexed 0, 1, 2, 3, 4, ...). (I hope I'm not wrong. I just looked into my code but it uses `glDrawElements()` which for sure need index buffers.) – Scheff's Cat Nov 04 '18 at 17:26
  • @Scheff Are you saying it needs indices? I have to do that after, but right now after this I need to loop through this array and creates layers of vertices for however many "floors" I have, the indices would connect everything together right? – noahlattari Nov 04 '18 at 17:31
  • I believe, not necessarily. Before I tell you something wrong: a tutorial: [Tutorial 2 : The first triangle](http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/). It used `glDrawArrays()` without index buffer as well. – Scheff's Cat Nov 04 '18 at 17:37
  • I googled a bit more and came to the conclusion that `glDrawArrays()` - no index buffer. To draw with index buffer `glDrawElements()` is intended for. Another link hint: [OGLdev](http://ogldev.atspace.co.uk/), [Tutorial 3: First triangle](http://ogldev.atspace.co.uk/www/tutorial03/tutorial03.html), [Tutorial 10: Indexed draws](http://ogldev.atspace.co.uk/www/tutorial10/tutorial10.html). Please, don't ask me why khronos' "official" doc. says "indices" concerning the count in `glDrawArrays()` - no idea. – Scheff's Cat Nov 04 '18 at 17:46

0 Answers0