GraphicsTest/funcs.cpp

70 lines
2.1 KiB
C++

#include "funcs.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
int windowX, windowY;
glViewport(0, 0, width, height);
glfwGetFramebufferSize(window, &windowX, &windowY);
float ratioX = (float)sizeX / (float)windowX;
float ratioY = (float)sizeY / (float)windowY;
float* screenCoords = scaleGridPoints(gridX, gridY, ratioX, ratioY, tileCoords);
glBufferData(GL_ARRAY_BUFFER, 5*gridX*gridY*sizeof(float), screenCoords, GL_STATIC_DRAW);
}
void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
float* makeGridPoints(unsigned int X, unsigned int Y) {
int numPoints = 5*X*Y;
float xStep = 1/ ((float)(X-1));
float yStep = 1/ ((float)(Y-1));
float* ret = new float[numPoints];
for (int i = 0; i < Y; i++) {
for (int j = 0; j < X; j++) {
ret[5*(i*X+j)] = 2*xStep*j-1.0f;
ret[5*(i*X+j)+1] = 2*yStep*i-1.0f;
ret[5*(i*X+j)+2] = 0.0;
ret[5*(i*X+j)+3] = yStep*i;
ret[5*(i*X+j)+4] = xStep*j;
}
}
return ret;
}
float* scaleGridPoints(unsigned int X, unsigned int Y, float ratioX, float ratioY, float* points) {
ratioX /= 2;
ratioY /= 2;
float* ret = new float[5*X*Y];
for (int i = 0; i < X*Y; i++) {
ret[5*i] = ratioX*points[5*i];
ret[5*i+1] = ratioY*points[5*i+1];
ret[5*i+2] = 0.0;
ret[5*i+3] = points[5*i+3];
ret[5*i+4] = points[5*i+4];
}
return ret;
}
unsigned int* makeGridIndex(unsigned int X, unsigned int Y) {
unsigned int tiles = (X-1)*(Y-1);
unsigned int* ret = new unsigned int[tiles*6];
unsigned int tileInd;
for (int i = 0; i < Y-1; i++) {
for (int j = 0; j < X-1; j++) {
tileInd = i*(X-1)+j;
ret[6*tileInd] = i*X+j;
ret[6*tileInd+1] = (i+1)*X+j;
ret[6*tileInd+2] = (i+1)*X+(j+1);
ret[6*tileInd+3] = i*X+j;
ret[6*tileInd+4] = i*X+j+1;
ret[6*tileInd+5] = (i+1)*X+(j+1);
}
}
return ret;
}