How to Make Draw an Equilateral Traingle Opengl
How to draw multiple triangle in OpenGL C++?
Lucas Huang
I was able to get to draw a single object with an array of vertices. However, it gave me a hard time when it comes to draw multiple objects.
I have an array of data like this and three vertices render one triangle:
-0.0570917f,0.490469f,-0.142433f -0.0581126f,0.5f,-0.160615f -0.0651903f,0.490827f,-0.147409f -0.0501295f,0.499886f,-0.158146f -0.0491703f,0.49039f,-0.140236f -0.0422027f,0.499864f,-0.157942f -0.041255f,0.49031f,-0.138206f -0.0333885f,0.490314f,-0.137995f -0.0967512f,0.48302f,-0.150394f ... And I am trying to render multiple triangles with the following code:
// this array contains arrays with one vertex (three elements) vector<vector<GLfloat>> pointVertex; // Render OpenGL here for (int i = 0; i < pointVertex.size(); i++) { vector<GLfloat> temp = pointVertex[i]; GLfloat *pointVertexArrayPoint = temp.data(); glEnable(GL_POINT_SMOOTH); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, pointVertexArrayPoint); glDrawArrays(GL_LINES, 0, temp.size()); glDisableClientState(GL_VERTEX_ARRAY); glDisable(GL_POINT_SMOOTH); } Now, I am getting a result but it all starts at the same point. I want it to render different multiple separate triangles. How can I change it?
Brandon
There's a few ways to do this without straying away from the technique you are using (Legacy pipeline)..
The below code will draw two triangles.. like so:
Each triangle is rendered with 1 draw call per triangle:
void onDisplayRefreshSO(void* ctx) { //Setup OpenGL once.. static std::once_flag flag; std::call_once(flag, []{ GLfloat view_port[4] = {0}; glGetFloatv(GL_VIEWPORT, &view_port[0]); GLfloat screen_width = view_port[2] - view_port[0]; GLfloat screen_height = view_port[3] - view_port[1]; glMatrixMode(GL_PROJECTION); gluPerspective(45.0, screen_width / screen_height, 0.1f, 1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations }); /** Setup Drawing Triangles **/ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); GLfloat normals[] = { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1 }; GLfloat colors[] = { 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1 }; GLfloat vertices[] = { 1, 1, 1, -1, 1, 1, -1,-1, 1, 1, 1, 1, 1, -1, 1, -1,-1, 1 }; //9 floats per triangle.. 3 * (X, Y, Z).. for (int i = 0; i < sizeof(vertices) / sizeof(vertices[0]) / 9; ++i) { glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glNormalPointer(GL_FLOAT, 0, normals + (i * 9)); glColorPointer(3, GL_FLOAT, 0, colors + (i * 9)); glVertexPointer(3, GL_FLOAT, 0, vertices + (i * 9)); glPushMatrix(); glTranslatef(-1.0f, 0.0f, -6.0f); glDrawArrays(GL_TRIANGLES, 0, 3); //3 vertices (X, Y, Z) per triangle.. glPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); } } All triangles rendered in a single draw call:
void onDisplayRefreshSO2(void* ctx) { //Setup OpenGL once.. static std::once_flag flag; std::call_once(flag, []{ GLfloat view_port[4] = {0}; glGetFloatv(GL_VIEWPORT, &view_port[0]); GLfloat screen_width = view_port[2] - view_port[0]; GLfloat screen_height = view_port[3] - view_port[1]; glMatrixMode(GL_PROJECTION); gluPerspective(45.0, screen_width / screen_height, 0.1f, 1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations }); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /** Setup Drawing Triangles **/ GLfloat normals[] = { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1 }; GLfloat colors[] = { 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1 }; GLfloat vertices[] = { 1, 1, 1, -1, 1, 1, -1,-1, 1, 1, 1, 1, 1, -1, 1, -1,-1, 1 }; //Draw both triangles in a single call.. glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glNormalPointer(GL_FLOAT, 0, normals); glColorPointer(3, GL_FLOAT, 0, colors); glVertexPointer(3, GL_FLOAT, 0, vertices); glPushMatrix(); glTranslatef(-1.0f, 0.0f, -6.0f); glDrawArrays(GL_TRIANGLES, 0, (sizeof(vertices) / sizeof(vertices[0])) / 3); //6 vertices (X, Y, Z) for 2 triangles.. glPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); } Collected from the Internet
Please contact [email protected] to delete if infringement.
edited at
Related
How to Make Draw an Equilateral Traingle Opengl
Source: https://www.javaer101.com/en/article/134055288.html
0 Response to "How to Make Draw an Equilateral Traingle Opengl"
Post a Comment