Skip to content

Instantly share code, notes, and snippets.

@vumbumy
Created February 2, 2017 04:56
Show Gist options
  • Select an option

  • Save vumbumy/bc73d1cda587400fcd7fbf74fd26b020 to your computer and use it in GitHub Desktop.

Select an option

Save vumbumy/bc73d1cda587400fcd7fbf74fd26b020 to your computer and use it in GitHub Desktop.
#include "main.h"
void DoDisplay();
void DoKeyboard(unsigned char key, int x, int y);
GLfloat xAngle, yAngle, zAngle;
tex_object_2D decal;
int main()
{
glutCreateWindow("OpenGL");
glutDisplayFunc(DoDisplay);
glutKeyboardFunc(DoKeyboard);
glutMainLoop();
return 0;
}
void DoKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'a':yAngle += 2; break;
case 'd':yAngle -= 2; break;
case 'w':xAngle += 2; break;
case 's':xAngle -= 2; break;
case 'q':zAngle += 2; break;
case 'e':zAngle -= 2; break;
case 'z':xAngle = yAngle = zAngle = 0.0; break;
}
char info[128];
sprintf(info, "x=%.1f, y=%.1f, z=%.1f", xAngle, yAngle, zAngle);
glutSetWindowTitle(info);
glutPostRedisplay();
}
const GLfloat size = 0.8f;
#define VERTEX_POINT 3
void DoDisplay()
{
static GLfloat vert[] = {
-size, -size, 0,
size, -size, 0,
-size, size, 0,
size, size, 0,
};
static GLubyte index[] = {
0, 2, 3,
1, 0, 3,
};
float jump = 0.2f;
vector<GLfloat> vertex;
vector<GLubyte> indices;
for (GLfloat y = -size; y <= size; y += jump){
for (GLfloat x = -size; x <= size; x += jump){
vertex.push_back(x);
vertex.push_back(y);
vertex.push_back(0);
}
}
int w = 2.0 * size / jump + 1;
int n = w - 1;
int h = w;
int m = h - 1;
for (int j = 0; j < m; j++){
for (int i = 0; i < n; i++){
int index = w * j + i;
indices.push_back(index);
indices.push_back(index + w);
indices.push_back(index + w + 1);
indices.push_back(index + 1);
indices.push_back(index );
indices.push_back(index + w + 1);
}
}
//return;
/*
for (auto v : vert)
vertex.push_back(v);
for (auto i : index)
indices.push_back(i);
*/
glClear(GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef(xAngle, 1.0f, 0.0f, 0.0f);
glRotatef(yAngle, 0.0f, 1.0f, 0.0f);
glRotatef(zAngle, 0.0f, 0.0f, 1.0f);
//glRectf(-0.5, 0.5, 0.5, -0.5);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(VERTEX_POINT, GL_FLOAT, 0, &vertex[0]);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_BYTE, &indices[0]);
glPopMatrix();
glFlush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment