Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Window.h"
- #include <SDL/SDL_main.h>
- #include "Shader.h"
- #include <bitset>
- #include <vector>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #pragma comment(lib, "glu32.lib")
- GLuint LoadTexture(const char* name)
- {
- GLuint pData = 0;
- SDL_Surface* Image;
- Image = IMG_Load(name);
- if (Image)
- {
- glGenTextures(1, &pData);
- glBindTexture(GL_TEXTURE_2D, pData);
- int Mode = GL_RGB;
- if(Image->format->BytesPerPixel == 4) {
- Mode = GL_RGBA;
- }
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
- gluBuild2DMipmaps( GL_TEXTURE_2D, Image->format->BytesPerPixel, Image->w, Image->h, Mode, GL_UNSIGNED_BYTE, Image->pixels);
- }
- return pData;
- }
- float BilinearInterpolation(unsigned char* heights ,float x, float y, float scale,float texw)
- {
- float floorx = glm::floor(x);
- float floory = glm::floor(y);
- float ceilx = glm::ceil(x);
- float ceily = glm::ceil(y);
- if(floory == ceily)
- ceily += 1.0;
- if(floorx == ceilx)
- ceilx += 1.0;
- if(ceilx > texw-1)
- ceilx = texw-1;
- float h00 = (float)(heights[(int)((floorx * texw + floory))*4]) * scale;
- float h01 = (float)(heights[(int)((floorx * texw + ceily))*4]) * scale;
- float h10 = (float)(heights[(int)((ceilx * texw + floory))*4]) * scale;
- float h11 = (float)(heights[(int)((ceilx * texw + ceily))*4]) * scale;
- float fx = x - floorx;
- float fy = y - floory;
- float hor0 = h10 * fx + h00 * (1-fx);
- float hor1 = h11 * fx + h01 * (1-fx);
- return hor1 * fy + hor0 * (1-fy);
- }
- int main(int argc, char *argv[])
- {
- Window window; //create Window
- window.Initialize(); //init it
- GLuint shader = LoadShader("terrain.vert","terrain.frag");
- int nhexagonsX = 150;
- int nhexagonsY = 200;
- int nhexagons = (nhexagonsX / 2) * nhexagonsY + (nhexagonsY / 2) * (nhexagonsX % 2);
- float hexwidth = 0.2f;
- float xdim = (0.5f + nhexagonsX * 1.5f) * hexwidth;
- float ydim = (1.0f + nhexagonsY) * hexwidth;
- xdim = glm::ceil(xdim);
- ydim = glm::ceil(ydim);
- float mapres = 1;
- int texw = (int)(glm::pow(2.0f,glm::ceil(glm::log2(xdim*mapres))));
- int texh = (int)(glm::pow(2.0f,glm::ceil(glm::log2(ydim*mapres))));
- GLuint tilemap;
- glGenTextures(1,&tilemap);
- glBindTexture(GL_TEXTURE_2D,tilemap);
- unsigned char *tiles = new unsigned char[texw * texh];
- srand(SDL_GetTicks());
- for(int n = 0; n < texw * texh; n++)
- {
- tiles[n] = 0xFF * ((float)(rand()) / (float)(RAND_MAX));
- }
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D,0,1,texw,texh,0,GL_RED,GL_UNSIGNED_BYTE,tiles);
- delete[] tiles;
- GLuint hmap;
- glGenTextures(1,&hmap);
- glBindTexture(GL_TEXTURE_2D,hmap);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,texw,texh,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
- GLuint shader2 = LoadShader("perlin.vert","perlin.frag");
- GLuint FBO;
- glGenFramebuffers(1, &FBO);
- glBindFramebuffer(GL_FRAMEBUFFER,FBO);
- glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,hmap,0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(shader2);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D,tilemap);
- glUniform1i(glGetUniformLocation(shader2, "map"),0);
- glBegin(GL_QUADS);
- glVertex3f(-1.0f,-1.0f,0.0f);
- glVertex3f(1.0f,-1.0f,0.0f);
- glVertex3f(1.0f,1.0f,0.0f);
- glVertex3f(-1.0f,1.0f,0.0f);
- glEnd();
- glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,0,0);
- glBindFramebuffer(GL_FRAMEBUFFER,0);
- unsigned char* heights = new unsigned char[4 * texw * texh];
- glBindTexture(GL_TEXTURE_2D,hmap);
- glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_UNSIGNED_BYTE,heights);
- std::vector<glm::vec3> mesh;
- bool shift = true;
- int npoints = (nhexagonsX / 2) + ((nhexagonsX / 2) + (nhexagonsX % 2)) * 2 + 1;
- for(int y = 0; y < nhexagonsY + 2; y++)
- {
- for(int x = 0; x < npoints; x++)
- {
- if(x == npoints - 1)
- y = y;
- if(shift)
- {
- mesh.push_back(glm::vec3((x+0.5) * hexwidth,0.0f,y * hexwidth));
- }
- else
- {
- mesh.push_back(glm::vec3((x) * hexwidth,0.0f,y * hexwidth));
- }
- glm::vec3 last = mesh[mesh.size() -1];
- mesh[mesh.size() -1].y = BilinearInterpolation(heights,last.x * ((float)(texw) / xdim),last.z * ((float)(texh) / ydim),0.1f,texw);
- }
- shift = !shift;
- }
- GLuint IBO;
- glGenBuffers(1,&IBO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,IBO);
- unsigned int* indices = new unsigned int[nhexagons * 6 * 3];
- int i = 0;
- for(int yc = 0; yc < nhexagonsY; yc++)
- {
- int xlen = (nhexagonsX / 2) - (yc % 2) * (nhexagonsX % 2);
- for(int xc = 0; xc < xlen; xc++)
- {
- int y = yc;
- glm::ivec3 x = glm::ivec3(3 * xc,3*xc,3*xc);
- if(yc%2)
- {
- x+=glm::ivec3(2,1,2);
- }
- float aveheight = 0.0f;
- indices[i++] = (y+1) * npoints + (x[1]+1);
- indices[i++] = y * npoints + (x[0]+1);
- indices[i++] = y * npoints + x[0];
- aveheight += mesh[indices[i-2]].y;
- indices[i++] = (y+1) * npoints + (x[1]+1);
- indices[i++] = (y+1) * npoints + (x[1]+2);
- indices[i++] = y * npoints + (x[0]+1);
- aveheight += mesh[indices[i-2]].y;
- indices[i++] = (y+1) * npoints + (x[1]+1);
- indices[i++] = (y+2) * npoints + (x[2]+1);
- indices[i++] = (y+1) * npoints + (x[1]+2);
- aveheight += mesh[indices[i-2]].y;
- indices[i++] = (y+1) * npoints + (x[1]+1);
- indices[i++] = (y+2) * npoints + x[2];
- indices[i++] = (y+2) * npoints + (x[2]+1);
- aveheight += mesh[indices[i-2]].y;
- indices[i++] = (y+1) * npoints + (x[1]+1);
- indices[i++] = (y+1) * npoints + x[1];
- indices[i++] = (y+2) * npoints + x[2];
- aveheight += mesh[indices[i-2]].y;
- indices[i++] = (y+1) * npoints + (x[1]+1);
- indices[i++] = y * npoints + x[0];
- indices[i++] = (y+1) * npoints + x[1];
- aveheight += mesh[indices[i-2]].y;
- mesh[indices[i-3]].y = aveheight / 6.0f;
- }
- }
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,nhexagons * 6 * 3 * sizeof(unsigned int),indices,GL_STATIC_DRAW);
- delete[] indices;
- GLuint VBO;
- glGenBuffers(1,&VBO);
- float* data = new float[mesh.size() * 3];
- for(int n = 0; n < mesh.size(); n++)
- {
- memcpy(data + 3 * n, &(mesh[n].x),3*sizeof(float));
- }
- glBindBuffer(GL_ARRAY_BUFFER,VBO);
- glBufferData(GL_ARRAY_BUFFER,mesh.size() * 3 * sizeof(float),data,GL_STATIC_DRAW);
- delete[] data;
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, ((char*)NULL));
- glm::mat4 P = glm::perspective(60.0f,4.0f/3.0f,0.1f,500.0f);
- glm::mat4 V = glm::rotate(glm::mat4(1.0f),0.0f,glm::vec3(1.0f,0.0f,0.0f)) * glm::rotate(glm::mat4(1.0f),90.0f,glm::vec3(0.0f,1.0f,0.0f));
- V = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,-3.0f,0.0f)) * V;
- glm::mat4 VP = P * V;
- //glPolygonMode(GL_FRONT,GL_LINE);
- glBindTexture(GL_TEXTURE_2D,0);
- while(true)
- {
- glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
- glUseProgram(shader);
- glUniformMatrix4fv(glGetUniformLocation(shader,"WVP"),1,GL_FALSE,&(VP[0][0]));
- glUniform1i(glGetUniformLocation(shader,"npoints"),npoints);
- glDrawElements(GL_TRIANGLES, nhexagons * 6 * 3,GL_UNSIGNED_INT,NULL);
- SDL_GL_SwapBuffers();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment