Added OpenGL2

This commit is contained in:
2016-04-25 11:03:39 +02:00
parent 3acb2b9370
commit fda7845f5f
12 changed files with 1443 additions and 0 deletions
+170
View File
@@ -0,0 +1,170 @@
#include <GL/freeglut.h>
#include <cstdio>
#define _USE_MATH_DEFINES
#include <cmath>
float lastFrameTime = 0;
int width, height;
struct Camera
{
float posX = 0;
float posY = -4;
float rotX = 0;
float rotY = 0;
} camera;
bool keys[255];
void drawCube()
{
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex3f(-1, -1, -1);
glVertex3f(1, -1, -1);
glVertex3f(1, 1, -1);
glVertex3f(-1, 1, -1);
glColor3f(1, 1, 0);
glVertex3f(-1, -1, 1);
glVertex3f(1, -1, 1);
glVertex3f(1, 1, 1);
glVertex3f(-1, 1, 1);
glColor3f(0, 0, 1);
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, -1);
glVertex3f(-1, 1, 1);
glVertex3f(-1, -1, 1);
glColor3f(1, -1, 1);
glVertex3f(1, -1, -1);
glVertex3f(1, 1, -1);
glVertex3f(1, 1, 1);
glVertex3f(1, -1, 1);
glColor3f(0, 1, 0);
glVertex3f(-1, -1, -1);
glVertex3f(1, -1, -1);
glVertex3f(1, -1, 1);
glVertex3f(-1, -1, 1);
glColor3f(1, 1, 0);
glVertex3f(-1, 1, -1);
glVertex3f(1, 1, -1);
glVertex3f(1, 1, 1);
glVertex3f(-1, 1, 1);
glEnd();
}
void display()
{
glClearColor(0.6f, 0.6f, 1, 1);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (float)width/height, 0.1, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(camera.rotX, 1, 0, 0);
glRotatef(camera.rotY, 0, 1, 0);
glTranslatef(camera.posX, 0, camera.posY);
glColor3f(0.1f, 1.0f, 0.2f);
glBegin(GL_QUADS);
glVertex3f(-15, -1, -15);
glVertex3f( 15, -1, -15);
glVertex3f( 15, -1, 15);
glVertex3f(-15, -1, 15);
glEnd();
for (int x = -10; x <= 10; x += 5)
{
for (int y = -10; y <= 10; y += 5)
{
glPushMatrix();
glTranslatef((float)x, 0.0f, (float)y);
drawCube();
glPopMatrix();
}
}
glutSwapBuffers();
}
void move(float angle, float fac)
{
camera.posX += (float)cos((camera.rotY + angle) / 180 * M_PI) * fac;
camera.posY += (float)sin((camera.rotY + angle) / 180 * M_PI) * fac;
}
void idle()
{
float frameTime = glutGet(GLUT_ELAPSED_TIME)/1000.0f;
float deltaTime = frameTime - lastFrameTime;
lastFrameTime = frameTime;
const float speed = 3;
if (keys['a']) move(0, deltaTime*speed);
if (keys['d']) move(180, deltaTime*speed);
if (keys['w']) move(90, deltaTime*speed);
if (keys['s']) move(270, deltaTime*speed);
glutPostRedisplay();
}
void mousePassiveMotion(int x, int y)
{
int dx = x - width / 2;
int dy = y - height / 2;
if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
{
camera.rotY += dx / 10.0f;
camera.rotX += dy / 10.0f;
glutWarpPointer(width / 2, height / 2);
}
}
void keyboard(unsigned char key, int, int)
{
if (key == 27)
exit(0);
keys[key] = true;
}
void keyboardUp(unsigned char key, int,int)
{
keys[key] = false;
}
int main(int argc, char* argv[])
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInit(&argc, argv);
glutCreateWindow("Hello World");
memset(keys, 0, sizeof(keys));
glEnable(GL_DEPTH_TEST);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc([](int w, int h) { width = w; height = h; glViewport(0, 0, w, h); });
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);
glutPassiveMotionFunc(mousePassiveMotion);
glutWarpPointer(width / 2, height / 2);
glutMainLoop();
return 0;
}