Files
CGR3D-KM/OpenGL2/Main.cpp
T
2016-04-25 13:35:04 +02:00

353 lines
8.7 KiB
C++

#include <GL/freeglut.h>
#include <cstdio>
#define _USE_MATH_DEFINES
#include <cmath>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
float lastFrameTime = 0;
int width, height;
GLuint grassTexture, blocksTexture;
struct Camera
{
float posX = 0;
float posY = -4;
float posZ = 0;
float rotX = 0;
float rotY = 0;
} camera;
bool keys[255];
void drawCubeSolid(int index)
{
int rowNum = index / 16;
int columnNum = index % 16;
float part = (float)1 / 16;
float row = rowNum * part;
float column = columnNum * part;
float rowEnd = row + part;
float columnEnd = column + part;
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 1.0, 1.0);
//Side 1
glTexCoord2f(column, rowEnd); glVertex3f(-1, -1, -1); //Linksonder
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, -1); //Rechtsonder
glTexCoord2f(columnEnd, row); glVertex3f(1, 1, -1); //Rechtsboven
glTexCoord2f(column, row); glVertex3f(-1, 1, -1); //Linksboven
//Side 2
glTexCoord2f(column, rowEnd); glVertex3f(-1, -1, 1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, 1);
glTexCoord2f(columnEnd, row); glVertex3f(1, 1, 1);
glTexCoord2f(column, row); glVertex3f(-1, 1, 1);
//Side 3
glTexCoord2f(column, rowEnd); glVertex3f(-1, -1, -1);
glTexCoord2f(column, row); glVertex3f(-1, 1, -1);
glTexCoord2f(columnEnd, row); glVertex3f(-1, 1, 1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(-1, -1, 1);
//Side 4
glTexCoord2f(column, rowEnd); glVertex3f(1, -1, -1);
glTexCoord2f(column, row); glVertex3f(1, 1, -1);
glTexCoord2f(columnEnd, row); glVertex3f(1, 1, 1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, 1);
//Bottom
glTexCoord2f(column, row); glVertex3f(-1, -1, -1);
glTexCoord2f(column, rowEnd); glVertex3f(1, -1, -1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, 1);
glTexCoord2f(columnEnd, row); glVertex3f(-1, -1, 1);
//Top
glTexCoord2f(column, row); glVertex3f(-1, 1, -1);
glTexCoord2f(column, rowEnd); glVertex3f(1, 1, -1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, 1, 1);
glTexCoord2f(columnEnd, row); glVertex3f(-1, 1, 1);
glEnd();
}
void drawCubeSideTop(int sides, int top)
{
int rowNum = sides / 16;
int columnNum = sides % 16;
float part = (float)1 / 16;
float row = rowNum * part;
float column = columnNum * part;
float rowEnd = row + part;
float columnEnd = column + part;
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 1.0, 1.0);
//Side 1
glTexCoord2f(column, rowEnd); glVertex3f(-1, -1, -1); //Linksonder
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, -1); //Rechtsonder
glTexCoord2f(columnEnd, row); glVertex3f(1, 1, -1); //Rechtsboven
glTexCoord2f(column, row); glVertex3f(-1, 1, -1); //Linksboven
//Side 2
glTexCoord2f(column, rowEnd); glVertex3f(-1, -1, 1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, 1);
glTexCoord2f(columnEnd, row); glVertex3f(1, 1, 1);
glTexCoord2f(column, row); glVertex3f(-1, 1, 1);
//Side 3
glTexCoord2f(column, rowEnd); glVertex3f(-1, -1, -1);
glTexCoord2f(column, row); glVertex3f(-1, 1, -1);
glTexCoord2f(columnEnd, row); glVertex3f(-1, 1, 1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(-1, -1, 1);
//Side 4
glTexCoord2f(column, rowEnd); glVertex3f(1, -1, -1);
glTexCoord2f(column, row); glVertex3f(1, 1, -1);
glTexCoord2f(columnEnd, row); glVertex3f(1, 1, 1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, 1);
if (top == -1)
{
glEnd();
return;
}
//Different textures
rowNum = top / 16;
columnNum = top % 16;
row = rowNum * part;
column = columnNum * part;
rowEnd = row + part;
columnEnd = column + part;
//Bottom
glTexCoord2f(column, row); glVertex3f(-1, -1, -1);
glTexCoord2f(column, rowEnd); glVertex3f(1, -1, -1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, -1, 1);
glTexCoord2f(columnEnd, row); glVertex3f(-1, -1, 1);
//Top
glTexCoord2f(column, row); glVertex3f(-1, 1, -1);
glTexCoord2f(column, rowEnd); glVertex3f(1, 1, -1);
glTexCoord2f(columnEnd, rowEnd); glVertex3f(1, 1, 1);
glTexCoord2f(columnEnd, row); glVertex3f(-1, 1, 1);
glEnd();
}
void drawTree(int xlocation, int ylocation, int zlocation, int height)
{
for (int z = zlocation; z <= zlocation+height; z += 2)
{
glPushMatrix();
glTranslatef((float)xlocation, (float)z, (float)ylocation);
drawCubeSolid(20);
glPopMatrix();
}
for (int x = xlocation-(height/2); x <= xlocation+ (height / 2); x += 2)
{
for (int y = ylocation- (height / 2); y <= ylocation+ (height / 2); y += 2)
{
for (int z = zlocation + height -(height/3); z <= zlocation + height+(height / 3); z += 2)
{
glPushMatrix();
glTranslatef((float)x, (float)z, (float)y);
drawCubeSolid(53);
glPopMatrix();
}
}
}
}
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.5, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(camera.rotX, 1, 0, 0);
glRotatef(camera.rotY, 0, 1, 0);
glTranslatef(camera.posX, camera.posZ, camera.posY);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, grassTexture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-30, -1, -30);
glTexCoord2f(0, 8); glVertex3f( 30, -1, -30);
glTexCoord2f(8, 8); glVertex3f( 30, -1, 30);
glTexCoord2f(8, 0); glVertex3f(-30, -1, 30);
glEnd();
glBindTexture(GL_TEXTURE_2D, blocksTexture);
int count = 0;
for (int x = -20; x <= 20; x += 5)
{
for (int y = -20; y <= 20; y += 5)
{
glPushMatrix();
glTranslatef((float)x, 0.0f, (float)y);
drawCubeSolid(count);
glPopMatrix();
count++;
count %= 255;
}
}
glPushMatrix();
glTranslatef(0.0f, 5.0f, 0.0f);
drawCubeSideTop(20, 21);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
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 moveZ(float direction, float fac)
{
camera.posZ += (fac * direction);
}
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);
if (keys['q']) moveZ(-1, deltaTime*speed);
if (keys['e']) moveZ(1, 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);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.01f);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc([](int w, int h) { width = w; height = h; glViewport(0, 0, w, h); });
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);
glutPassiveMotionFunc(mousePassiveMotion);
//Cursor
glutWarpPointer(width / 2, height / 2);
//glutSetCursor(GLUT_CURSOR_NONE);
//Load Textures :: grass
int width1, height1, bpp1;
unsigned char* imgData1 = stbi_load("grass.jpg", &width1, &height1, &bpp1, 4);
glGenTextures(1, &grassTexture);
glBindTexture(GL_TEXTURE_2D, grassTexture);
glTexImage2D(GL_TEXTURE_2D,
0, //level
GL_RGBA, //internal format
width1, //width
height1, //height
0, //border
GL_RGBA, //data format
GL_UNSIGNED_BYTE, //data type
imgData1); //data
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(imgData1);
//Load Textures :: blocks
int width2, height2, bpp2;
unsigned char* imgData2 = stbi_load("terrain.png", &width2, &height2, &bpp2, 4);
glGenTextures(1, &blocksTexture);
glBindTexture(GL_TEXTURE_2D, blocksTexture);
glTexImage2D(GL_TEXTURE_2D,
0, //level
GL_RGBA, //internal format
width2, //width
height2, //height
0, //border
GL_RGBA, //data format
GL_UNSIGNED_BYTE, //data type
imgData2); //data
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
stbi_image_free(imgData2);
glutMainLoop();
return 0;
}