VertexBuffer::VertexBuffer(GLRenderingContext *rc, GLenum target) : rc(rc), target(target) { if (GLEW_ARB_vertex_buffer_object) glGenBuffers(1, &ptr->id); }
void CoordinateSystemsComponent::Initialize() { std::vector<GLfloat> color{ 0.2f, 0.3f, 0.3f, 1.0f }; //Background color mVertices = { //FRONT FACE //Positions //Texture Coords. 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // Top Right = 0 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // Bottom Right = 1 -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // Bottom Left = 2 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // Top Right = 0 -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // Bottom Left = 2 -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // Top Left = 3 //BACK FACE 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // Top Right = 0 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // Bottom Right = 1 -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom Left = 2 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // Top Right = 0 -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom Left = 2 -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // Top Left = 3 //RIGHT FACE 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, //LEFT FACE -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, //TOP FACE 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, //BOTTOM FACE 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f }; mCubePositions = { glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.0f), glm::vec3(-1.5f, -2.2f, -2.5f), glm::vec3(-3.8f, -2.0f, -12.3f), glm::vec3(2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f), glm::vec3(1.3f, -2.0f, -2.5f), glm::vec3(1.5f, 2.0f, -2.5f), glm::vec3(1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.5f) }; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, mVertices.size() * sizeof(GLfloat), &mVertices[0], GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(2); glBindVertexArray(0); mShader = Shader("shaders/vertexshader.vert", "shaders/fragmentshader.frag"); //---------- TEXTURE ----------// mTextureContainer.SetProgram(mShader.Program()); mTextureContainer.BindTexture(GL_TEXTURE0); mTextureContainer.SetTextureParameter(GL_TEXTURE_WRAP_S, GL_REPEAT); mTextureContainer.SetTextureParameter(GL_TEXTURE_WRAP_T, GL_REPEAT); mTextureContainer.SetTextureParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); mTextureContainer.SetTextureParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); mTextureContainer.LoadTexture("res\\container.jpg"); mTextureContainer.GenerateTexture(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE); mTextureContainer.GenerateMipmaps(); mTextureContainer.FreeImageData(); mTextureContainer.UnbindTexture(); mTextureAwesomeFace.SetProgram(mShader.Program()); mTextureAwesomeFace.BindTexture(GL_TEXTURE1); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_WRAP_S, GL_REPEAT); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_WRAP_T, GL_REPEAT); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); mTextureAwesomeFace.SetTextureParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); mTextureAwesomeFace.LoadTexture("res\\awesomeface.png"); mTextureAwesomeFace.GenerateTexture(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE); mTextureAwesomeFace.GenerateMipmaps(); mTextureAwesomeFace.FreeImageData(); mTextureAwesomeFace.UnbindTexture(); }
void R_DrawParticles (void) { particle_t *p, *kill; float grav; int i; float time2, time3; float time1; float dvel; float frametime; #ifdef GLQUAKE vec3_t up, right; float scale; GL_Use (gl_coloredpolygon1textureprogram); glUniformMatrix4fv(gl_coloredpolygon1textureprogram_transform, 1, 0, gl_polygon_matrix); GL_Bind(particletexture); glEnable (GL_BLEND); VectorScale (vup, 1.5, up); VectorScale (vright, 1.5, right); #else D_StartParticles (); VectorScale (vright, xscaleshrink, r_pright); VectorScale (vup, yscaleshrink, r_pup); VectorCopy (vpn, r_ppn); #endif frametime = cl.time - cl.oldtime; time3 = frametime * 15; time2 = frametime * 10; // 15; time1 = frametime * 5; grav = frametime * sv_gravity.value * 0.05; dvel = 4*frametime; for ( ;; ) { kill = active_particles; if (kill && kill->die < cl.time) { active_particles = kill->next; kill->next = free_particles; free_particles = kill; continue; } break; } #ifdef GLQUAKE int mark = Hunk_LowMark(); int vertexcount = 0; for (p=active_particles ; p ; p=p->next) { vertexcount += 3; } if (vertexcount == 0) return; GLfloat* vertices = Hunk_AllocName (vertexcount * 9 * sizeof(GLfloat), "vertex_buffer"); int vertexpos = 0; #endif for (p=active_particles ; p ; p=p->next) { for ( ;; ) { kill = p->next; if (kill && kill->die < cl.time) { p->next = kill->next; kill->next = free_particles; free_particles = kill; continue; } break; } #ifdef GLQUAKE // hack a scale up to keep particles from disapearing scale = (p->org[0] - r_origin[0])*vpn[0] + (p->org[1] - r_origin[1])*vpn[1] + (p->org[2] - r_origin[2])*vpn[2]; if (scale < 20) scale = 1; else scale = 1 + scale * 0.004; unsigned c = d_8to24table[(int)p->color]; GLfloat r = (GLfloat)(c & 0xFF) / 255.0; GLfloat g = (GLfloat)((c >> 8) & 0xFF) / 255.0; GLfloat b = (GLfloat)((c >> 16) & 0xFF) / 255.0; GLfloat a = (GLfloat)((c >> 24) & 0xFF) / 255.0; vertices[vertexpos++] = p->org[0]; vertices[vertexpos++] = p->org[1]; vertices[vertexpos++] = p->org[2]; vertices[vertexpos++] = r; vertices[vertexpos++] = g; vertices[vertexpos++] = b; vertices[vertexpos++] = a; vertices[vertexpos++] = 0.0; vertices[vertexpos++] = 0.0; vertices[vertexpos++] = p->org[0] + up[0] * scale; vertices[vertexpos++] = p->org[1] + up[1] * scale; vertices[vertexpos++] = p->org[2] + up[2] * scale; vertices[vertexpos++] = r; vertices[vertexpos++] = g; vertices[vertexpos++] = b; vertices[vertexpos++] = a; vertices[vertexpos++] = 1.0; vertices[vertexpos++] = 0.0; vertices[vertexpos++] = p->org[0] + right[0] * scale; vertices[vertexpos++] = p->org[1] + right[1] * scale; vertices[vertexpos++] = p->org[2] + right[2] * scale; vertices[vertexpos++] = r; vertices[vertexpos++] = g; vertices[vertexpos++] = b; vertices[vertexpos++] = a; vertices[vertexpos++] = 0.0; vertices[vertexpos++] = 1.0; #else D_DrawParticle (p); #endif p->org[0] += p->vel[0]*frametime; p->org[1] += p->vel[1]*frametime; p->org[2] += p->vel[2]*frametime; switch (p->type) { case pt_static: break; case pt_fire: p->ramp += time1; if (p->ramp >= 6) p->die = -1; else p->color = ramp3[(int)p->ramp]; p->vel[2] += grav; break; case pt_explode: p->ramp += time2; if (p->ramp >=8) p->die = -1; else p->color = ramp1[(int)p->ramp]; for (i=0 ; i<3 ; i++) p->vel[i] += p->vel[i]*dvel; p->vel[2] -= grav; break; case pt_explode2: p->ramp += time3; if (p->ramp >=8) p->die = -1; else p->color = ramp2[(int)p->ramp]; for (i=0 ; i<3 ; i++) p->vel[i] -= p->vel[i]*frametime; p->vel[2] -= grav; break; case pt_blob: for (i=0 ; i<3 ; i++) p->vel[i] += p->vel[i]*dvel; p->vel[2] -= grav; break; case pt_blob2: for (i=0 ; i<2 ; i++) p->vel[i] -= p->vel[i]*dvel; p->vel[2] -= grav; break; case pt_grav: #ifdef QUAKE2 p->vel[2] -= grav * 20; break; #endif case pt_slowgrav: p->vel[2] -= grav; break; } } #ifdef GLQUAKE GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, vertexcount * 9 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(gl_coloredpolygon1textureprogram_position); glVertexAttribPointer(gl_coloredpolygon1textureprogram_position, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (const GLvoid *)0); glEnableVertexAttribArray(gl_coloredpolygon1textureprogram_color); glVertexAttribPointer(gl_coloredpolygon1textureprogram_color, 4, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (const GLvoid *)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(gl_coloredpolygon1textureprogram_texcoords); glVertexAttribPointer(gl_coloredpolygon1textureprogram_texcoords, 2, GL_FLOAT, GL_FALSE, 9 * sizeof(GLfloat), (const GLvoid *)(7 * sizeof(GLfloat))); glDrawArrays(GL_TRIANGLES, 0, vertexcount); glDisableVertexAttribArray(gl_coloredpolygon1textureprogram_texcoords); glDisableVertexAttribArray(gl_coloredpolygon1textureprogram_color); glDisableVertexAttribArray(gl_coloredpolygon1textureprogram_position); glBindBuffer(GL_ARRAY_BUFFER, 0); glDeleteBuffers(1, &vertexbuffer); Hunk_FreeToLowMark (mark); glDisable (GL_BLEND); #else D_EndParticles (); #endif }
int main() { int width = 640; int height = 480; sf::Window window(sf::VideoMode(width, height), "sfml", sf::Style::Default, sf::ContextSettings(0,0,0,3,3)); // using glew since sfml doesn create a core context if(glewInit()) { std::cerr << "failed to init GLEW" << std::endl; return 1; } // shader source code std::string vertex_source = "#version 330\n" "layout(location = 0) in vec4 vposition;\n" "layout(location = 1) in vec4 vcolor;\n" "out vec4 fcolor;\n" "void main() {\n" " fcolor = vcolor;\n" " gl_Position = vposition;\n" "}\n"; std::string fragment_source = "#version 330\n" "in vec4 fcolor;\n" "layout(location = 0) out vec4 FragColor;\n" "void main() {\n" " FragColor = fcolor;\n" "}\n"; // program and shader handles GLuint shader_program, vertex_shader, fragment_shader; // we need these to properly pass the strings const char *source; int length; // create and compiler vertex shader vertex_shader = glCreateShader(GL_VERTEX_SHADER); source = vertex_source.c_str(); length = vertex_source.size(); glShaderSource(vertex_shader, 1, &source, &length); glCompileShader(vertex_shader); if(!check_shader_compile_status(vertex_shader)) { return 1; } // create and compiler fragment shader fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); source = fragment_source.c_str(); length = fragment_source.size(); glShaderSource(fragment_shader, 1, &source, &length); glCompileShader(fragment_shader); if(!check_shader_compile_status(fragment_shader)) { return 1; } // create program shader_program = glCreateProgram(); // attach shaders glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); // link the program and check for errors glLinkProgram(shader_program); if(!check_program_link_status(shader_program)) { return 1; } // vao and vbo handle GLuint vao, vbo; // generate and bind the vao glGenVertexArrays(1, &vao); glBindVertexArray(vao); // generate and bind the buffer object glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); // data for a fullscreen quad GLfloat vertexData[] = { // X Y Z R G B 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // vertex 0 -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // vertex 1 1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // vertex 2 1.0f,-1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // vertex 3 -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // vertex 4 -1.0f,-1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // vertex 5 }; // 6 vertices with 6 components (floats) each // fill with data glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*6*6, vertexData, GL_STATIC_DRAW); // set up generic attrib pointers glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (char*)0 + 0*sizeof(GLfloat)); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (char*)0 + 3*sizeof(GLfloat)); bool closed = false; while(!closed) { sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed) { closed = true; } } // clear first glClear(GL_COLOR_BUFFER_BIT); // use the shader program glUseProgram(shader_program); // bind the vao glBindVertexArray(vao); // draw glDrawArrays(GL_TRIANGLES, 0, 6); // check for errors GLenum error = glGetError(); if(error != GL_NO_ERROR) { std::cerr << error << std::endl; break; } // finally swap buffers window.display(); } // delete the created objects glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); glDetachShader(shader_program, vertex_shader); glDetachShader(shader_program, fragment_shader); glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); glDeleteProgram(shader_program); return 0; }
OceanSurface::OceanSurface(int N, int M, float L_x, float L_z, float A, glm::vec2 _wind, float g, bool _lines, bool _gpu) : N(N), M(M), L_x(L_x), L_z(L_z), A(A), wind(_wind), g(g), lines(_lines), gpu(_gpu) { grid = new surface_vertex[N * M]; // construct wave height field grid init_positions = new vertex_2d[N * M]; grid_tex_cord = new tex_img_coord[N * N]; h_t0 = new complex_number[N * M]; h_t0_cc = new complex_number[N * M]; h_fft = new complex_number[N * M]; dx_fft = new complex_number[N * M]; dz_fft = new complex_number[N * M]; gradient_x = new complex_number[N * M]; gradient_z = new complex_number[N * M]; lambda = -1.0f; // numeric constant for calculating displacement num_indices = 0; num_triangle_indices = 0; indices = new GLuint[(N - 1) * (M - 1) * 6 + 2 * (N + M - 2)]; triangle_indices = new GLuint[(N - 1) * (M - 1) * 6 * 10]; if (gpu) { wind = glm::vec2(wind.y, wind.x); } //fft myFFT = new MyFFT(N, M); // compute shader FFT stuff char *s_name = "compute_shaders/fft_compute.glsl"; if (N == 32) { s_name = "compute_shaders/fft_compute_32.glsl"; } if (N == 64) { s_name = "compute_shaders/fft_compute_64.glsl"; } if (N == 128) { s_name = "compute_shaders/fft_compute_128.glsl"; } if (N == 256) { s_name = "compute_shaders/fft_compute_256.glsl"; } if (N == 512) { s_name = "compute_shaders/fft_compute_512.glsl"; } fft_comp_program = create_comp_program_from_file(s_name); int bits = log2(N * 1.0f); calc_binary_reverse(bits); glGenBuffers(1, &rev_ind_buffer); glBindBuffer(GL_SHADER_STORAGE_BUFFER, rev_ind_buffer); glBufferData(GL_SHADER_STORAGE_BUFFER, N * sizeof(int), binary_reverse, GL_STATIC_DRAW); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); GLint steps_location = glGetUniformLocation(fft_comp_program, "steps"); fft_column_location = glGetUniformLocation(fft_comp_program, "fft_column"); glUseProgram(fft_comp_program); glUniform1i(steps_location, bits); // texture for inpute height map glGenTextures(1, &texture_H_t); glBindTexture(GL_TEXTURE_2D, texture_H_t); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, N, N, 0, GL_RG, GL_FLOAT, 0); /*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/ // texture for output height map per rows glGenTextures(1, &texture_H_fft_t_row); glBindTexture(GL_TEXTURE_2D, texture_H_fft_t_row); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); /*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/ // texture for output height map per columns glGenTextures(1, &texture_H_fft_t_col); glBindTexture(GL_TEXTURE_2D, texture_H_fft_t_col); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); /*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/ // texture for fft dx glGenTextures(1, &tex_dx_fft_row); glBindTexture(GL_TEXTURE_2D, tex_dx_fft_row); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glGenTextures(1, &tex_dx_fft); glBindTexture(GL_TEXTURE_2D, tex_dx_fft); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); // texture for fft dz glGenTextures(1, &tex_dz_fft_row); glBindTexture(GL_TEXTURE_2D, tex_dz_fft_row); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glGenTextures(1, &tex_dz_fft); glBindTexture(GL_TEXTURE_2D, tex_dz_fft); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); // texture for fft gradx glGenTextures(1, &tex_gradx_fft_row); glBindTexture(GL_TEXTURE_2D, tex_gradx_fft_row); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glGenTextures(1, &tex_gradx_fft); glBindTexture(GL_TEXTURE_2D, tex_gradx_fft); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); // texture for fft gradz glGenTextures(1, &tex_gradz_fft_row); glBindTexture(GL_TEXTURE_2D, tex_gradz_fft_row); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glGenTextures(1, &tex_gradz_fft); glBindTexture(GL_TEXTURE_2D, tex_gradz_fft); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); // unbind all textures glBindTexture(GL_TEXTURE_2D, 0); // assign each point its original 3d coordinates for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { int pos = i * M + j; // one-dimensional pos from linear two-dimensional grid grid_tex_cord[pos] = tex_img_coord(i, j); // precomputer amplitudes at each point h_t0[pos] = h_t_0(i, j); h_t0_cc[pos] = h_t_0(-i, -j).cc(); // we project our N*M grid coordinates to real world L_x*L_z coordinates grid[pos].x = (i - (N >> 1)) * L_x / N; grid[pos].y = 0.0f; // height is 0 grid[pos].z = (j - (M >> 1)) * L_z / M; // save initial positions init_positions[pos].x = grid[pos].x; init_positions[pos].z = grid[pos].z; // construct indices for index drawing if (i != N - 1 && j != M - 1) { indices[num_indices++] = pos; indices[num_indices++] = pos + 1; indices[num_indices++] = pos; indices[num_indices++] = pos + M; indices[num_indices++] = pos; indices[num_indices++] = pos + M + 1; } if (i != N - 1 && j == M - 1) { indices[num_indices++] = pos; indices[num_indices++] = pos + M; } if (i == N - 1 && j != M - 1) { indices[num_indices++] = pos; indices[num_indices++] = pos + 1; } // construct indices for triangle index drawing if (i != N - 1 && j != M - 1) { triangle_indices[num_triangle_indices++] = pos; triangle_indices[num_triangle_indices++] = pos + N + 1; triangle_indices[num_triangle_indices++] = pos + 1; triangle_indices[num_triangle_indices++] = pos; triangle_indices[num_triangle_indices++] = pos + N; triangle_indices[num_triangle_indices++] = pos + N + 1; } } // check that its okay assert(num_indices == (N - 1) * (M - 1) * 6 + 2 * (N + M - 2)); //assert(num_triangle_indices, (N - 1) * (M - 1) * 6); prepare_for_pipeline(); // update height map compute shader program upd_height_program = create_comp_program_from_file("compute_shaders/update_height_map.glsl"); GLint len_location = glGetUniformLocation(upd_height_program, "L");; GLint n_location = glGetUniformLocation(upd_height_program, "N"); total_time_location = glGetUniformLocation(upd_height_program, "totalTime"); glUseProgram(upd_height_program); glUniform1f(len_location, L_x); glUniform1i(n_location, N); glUseProgram(0); // height map textures glGenTextures(1, &texture_H_t0); glGenTextures(1, &texture_H_t0_cc); glBindTexture(GL_TEXTURE_2D, texture_H_t0); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, N, N, GL_RG, GL_FLOAT, h_t0); glBindTexture(GL_TEXTURE_2D, texture_H_t0_cc); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, N, N, GL_RG, GL_FLOAT, h_t0_cc); glBindTexture(GL_TEXTURE_2D, 0); // displacement map textures glGenTextures(1, &texture_Dx); glGenTextures(1, &texture_Dz); glBindTexture(GL_TEXTURE_2D, texture_Dx); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glBindTexture(GL_TEXTURE_2D, texture_Dz); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glBindTexture(GL_TEXTURE_2D, 0); // gradient map textures glGenTextures(1, &texture_Gradx); glGenTextures(1, &texture_Gradz); glBindTexture(GL_TEXTURE_2D, texture_Gradx); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glBindTexture(GL_TEXTURE_2D, texture_Gradz); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG32F, N, N); glBindTexture(GL_TEXTURE_2D, 0); // Fresnel textures int fres_size = 128; float g_SkyBlending = 16.0f; unsigned int *buffer = new unsigned int[fres_size]; for (int i = 0; i < fres_size; i++) { float cos_a = i / (float)fres_size; // Using water's refraction index 1.33 unsigned int fresnel = (unsigned int)(D3DXFresnelTerm(cos_a, 1.33f) * 255); unsigned int sky_blend = (unsigned int)(powf(1 / (1 + cos_a), g_SkyBlending) * 255); buffer[i] = (sky_blend << 8) | fresnel; } glGenTextures(1, &tex_Fresnel); glBindTexture(GL_TEXTURE_1D, tex_Fresnel); glTexStorage1D(GL_TEXTURE_1D, 1, GL_RGBA8, fres_size); glTexSubImage1D(GL_TEXTURE_1D, 0, 0, fres_size, GL_RGBA, GL_UNSIGNED_INT, buffer); const GLfloat border[] = { 1.0f, 1.0f, 1.0f, 1.0f }; glTexParameterfv(GL_TEXTURE_1D, GL_TEXTURE_BORDER_COLOR, border); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_COMPARE_FUNC, GL_NEVER); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); glBindTexture(GL_TEXTURE_1D, 0); delete[] buffer; // Reflect cube textures tex_ReflectCube = SOIL_load_OGL_single_cubemap ( "textures/sky_cube.dds", SOIL_DDS_CUBEMAP_FACE_ORDER, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT ); }
void* AEVAMap(AEVA* va, unsigned int elementCount, int writereadmode){ unsigned int writereadmodeOGL = 0; switch (writereadmode) { case AEVAMapFlagWritable: #ifdef AEiOS writereadmodeOGL=GL_WRITE_ONLY_OES; #else writereadmodeOGL=GL_WRITE_ONLY; #endif break; case AEVAMapFlagReadable: #ifdef AEiOS return NULL; #else writereadmodeOGL=GL_READ_ONLY; #endif break; default: #ifdef AEiOS return NULL; #else writereadmodeOGL=GL_READ_WRITE; #endif break; } #ifdef AEiOS if(va->format.indexType==AEVAFormatIndexType32Bit) va->format.indexType=AEVAFormatIndexType16Bit; #endif unsigned int arrayType=va->format.indexType?GL_ELEMENT_ARRAY_BUFFER:GL_ARRAY_BUFFER; //if(sizeof(GLfloat) not_eq sizeof(char[4]) and sizeof(GLuint)!=sizeof(char[4])) AEError("A GLfloat or GLuint is not equal to 4 bytes on your system! This is very, very, bad."); if(va->elementCount and va->elementCount not_eq elementCount) return NULL; //if(sizeof(AEVec3) not_eq 3*sizeof(float)) AEError("AEVec3 is padded, this is bad!"); if(va->elementCount==0){ va->elementCount=elementCount; #ifdef AEiOS if(va->format.storageType==AEVAFormatStorageTypeStream) va->format.storageType=0; #endif if(va->format.storageType){ glGenBuffers(1, &va->data.vbo); //glBindBuffer(GL_ARRAY_BUFFER, va->data.vbo); unsigned int vbotype=0; switch(va->format.storageType){ #ifndef AEiOS case AEVAFormatStorageTypeStream: vbotype=GL_STREAM_DRAW; break; #endif case AEVAFormatStorageTypeDynamic: vbotype=GL_DYNAMIC_DRAW; break; case AEVAFormatStorageTypeStatic: vbotype=GL_STATIC_DRAW; break; } glBindBuffer(arrayType, va->data.vbo); glBufferData(arrayType, elementCount * AEVAFormatByteSize(& va->format), NULL,vbotype); #ifdef AEiOS void* data = glMapBufferOES(arrayType, writereadmodeOGL); #else void* data = glMapBuffer(arrayType, writereadmodeOGL); #endif return data; } else va->data.pointer=malloc(elementCount * AEVAFormatByteSize(& va->format)); } if(va->format.storageType){ glBindBuffer(arrayType, va->data.vbo); #ifdef AEiOS return glMapBufferOES(arrayType, writereadmodeOGL); #else return glMapBuffer(arrayType, writereadmodeOGL); #endif } return va->data.pointer; }
COpenGLVertexBuffer::COpenGLVertexBuffer() { glGenBuffers(1, &m_Handle); }
int main(int argc, char** argv) { if (!glfwInit()) // 初始化glfw库 { std::cout << "Error::GLFW could not initialize GLFW!" << std::endl; return -1; } // 开启OpenGL 3.3 core profile std::cout << "Start OpenGL core profile version 3.3" << std::endl; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_SAMPLES, 4); // 设置采样点个数 注意这里设置GLFW选项 不要写成了GL_SAMPLES // 创建窗口 GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo of anti-aliasing(press O on, F off)", NULL, NULL); if (!window) { std::cout << "Error::GLFW could not create winddow!" << std::endl; glfwTerminate(); std::system("pause"); return -1; } // 创建的窗口的context指定为当前context glfwMakeContextCurrent(window); // 注册窗口键盘事件回调函数 glfwSetKeyCallback(window, key_callback); // 注册鼠标事件回调函数 glfwSetCursorPosCallback(window, mouse_move_callback); // 注册鼠标滚轮事件回调函数 glfwSetScrollCallback(window, mouse_scroll_callback); // 鼠标捕获 停留在程序内 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // 初始化GLEW 获取OpenGL函数 glewExperimental = GL_TRUE; // 让glew获取所有拓展函数 GLenum status = glewInit(); if (status != GLEW_OK) { std::cout << "Error::GLEW glew version:" << glewGetString(GLEW_VERSION) << " error string:" << glewGetErrorString(status) << std::endl; glfwTerminate(); std::system("pause"); return -1; } // 设置视口参数 glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); //Section1 顶点属性数据 // 指定立方体顶点属性数据 顶点位置 纹理 GLfloat cubeVertices[] = { -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // A 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // B 0.5f, 0.5f, 0.5f,1.0f, 1.0f, // C 0.5f, 0.5f, 0.5f,1.0f, 1.0f, // C -0.5f, 0.5f, 0.5f,0.0f, 1.0f, // D -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A -0.5f, -0.5f, -0.5f,0.0f, 0.0f, // E -0.5f, 0.5f, -0.5f,0.0, 1.0f, // H 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G 0.5f, -0.5f, -0.5f,1.0f, 0.0f, // F -0.5f, -0.5f, -0.5f,0.0f, 0.0f, // E -0.5f, 0.5f, 0.5f,0.0f, 1.0f, // D -0.5f, 0.5f, -0.5f,1.0, 1.0f, // H -0.5f, -0.5f, -0.5f,1.0f, 0.0f, // E -0.5f, -0.5f, -0.5f,1.0f, 0.0f, // E -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A -0.5f, 0.5f, 0.5f,0.0f, 1.0f, // D 0.5f, -0.5f, -0.5f,1.0f, 0.0f, // F 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G 0.5f, 0.5f, 0.5f,0.0f, 1.0f, // C 0.5f, 0.5f, 0.5f,0.0f, 1.0f, // C 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // B 0.5f, -0.5f, -0.5f,1.0f, 0.0f, // F 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G -0.5f, 0.5f, -0.5f,0.0, 1.0f, // H -0.5f, 0.5f, 0.5f,0.0f, 0.0f, // D -0.5f, 0.5f, 0.5f,0.0f, 0.0f, // D 0.5f, 0.5f, 0.5f,1.0f, 0.0f, // C 0.5f, 0.5f, -0.5f,1.0f, 1.0f, // G -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,// E 0.5f, -0.5f, -0.5f,1.0f, 1.0f, // F 0.5f, -0.5f, -0.5f,1.0f, 1.0f, // F 0.5f, -0.5f, 0.5f,1.0f, 0.0f, // B -0.5f, -0.5f, 0.5f,0.0f, 0.0f, // A }; // Section2 准备缓存对象 GLuint cubeVAOId, cubeVBOId; glGenVertexArrays(1, &cubeVAOId); glGenBuffers(1, &cubeVBOId); glBindVertexArray(cubeVAOId); glBindBuffer(GL_ARRAY_BUFFER, cubeVBOId); glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW); // 顶点位置数据 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GL_FLOAT), (GLvoid*)0); glEnableVertexAttribArray(0); // 顶点纹理数据 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GL_FLOAT), (GLvoid*)(3 * sizeof(GL_FLOAT))); glEnableVertexAttribArray(1); glBindVertexArray(0); // Section3 准备着色器程序 Shader shader("scene.vertex", "scene.frag"); glEnable(GL_MULTISAMPLE); // 开启multisample glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); // 开始游戏主循环 while (!glfwWindowShouldClose(window)) { GLfloat currentFrame = (GLfloat)glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; glfwPollEvents(); // 处理例如鼠标 键盘等事件 do_movement(); // 根据用户操作情况 更新相机属性 glClearColor(0.18f, 0.04f, 0.14f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader.use(); glm::mat4 projection = glm::perspective(camera.mouse_zoom, (GLfloat)(WINDOW_WIDTH) / WINDOW_HEIGHT, 1.0f, 100.0f); // 投影矩阵 glm::mat4 view = camera.getViewMatrix(); // 视变换矩阵 glUniformMatrix4fv(glGetUniformLocation(shader.programId, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(shader.programId, "view"), 1, GL_FALSE, glm::value_ptr(view)); glm::mat4 model; model = glm::mat4(); model = glm::rotate(model, glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glUniformMatrix4fv(glGetUniformLocation(shader.programId, "model"), 1, GL_FALSE, glm::value_ptr(model)); // 这里填写场景绘制代码 glBindVertexArray(cubeVAOId); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); glBindVertexArray(0); glUseProgram(0); glfwSwapBuffers(window); // 交换缓存 } // 释放资源 glDeleteVertexArrays(1, &cubeVAOId); glDeleteBuffers(1, &cubeVBOId); glfwTerminate(); return 0; }
level* level_load_file(char* filename) { for(int i = 0; i < num_tile_types; i++) { tile_counts[i] = 0; } level* l = malloc(sizeof(level)); l->num_tile_sets = num_tile_types; l->tile_sets = malloc(sizeof(tile_set) * num_tile_types); l->tile_map = calloc(sizeof(int), MAX_WIDTH * MAX_HEIGHT); SDL_RWops* file = SDL_RWFromFile(filename, "r"); char line[MAX_WIDTH]; int y = 0; int x = 0; while(SDL_RWreadline(file, line, 1024)) { for(x = 0; x < strlen(line); x++) { char c = line[x]; int type = tile_for_char(c); l->tile_map[x + y * MAX_WIDTH] = type; tile_counts[type]++; } y++; } SDL_RWclose(file); /* Start from 1, type 0 is none! */ for(int i = 1; i < num_tile_types; i++) { int num_tiles = tile_counts[i]; float* position_data = malloc(sizeof(float) * 3 * 4 * num_tiles); float* uv_data = malloc(sizeof(float) * 2 * 4 * num_tiles); int pos_i = 0; int uv_i = 0; for(x = 0; x < MAX_WIDTH; x++) for(y = 0; y < MAX_HEIGHT; y++) { int type = l->tile_map[x + y * MAX_WIDTH]; if( type == i ) { position_data[pos_i] = x * TILE_SIZE; pos_i++; position_data[pos_i] = y * TILE_SIZE; pos_i++; position_data[pos_i] = 0; pos_i++; position_data[pos_i] = (x+1) * TILE_SIZE; pos_i++; position_data[pos_i] = y * TILE_SIZE; pos_i++; position_data[pos_i] = 0; pos_i++; position_data[pos_i] = (x+1) * TILE_SIZE; pos_i++; position_data[pos_i] = (y+1) * TILE_SIZE; pos_i++; position_data[pos_i] = 0; pos_i++; position_data[pos_i] = x * TILE_SIZE; pos_i++; position_data[pos_i] = (y+1) * TILE_SIZE; pos_i++; position_data[pos_i] = 0; pos_i++; uv_data[uv_i] = 0; uv_i++; uv_data[uv_i] = 0; uv_i++; uv_data[uv_i] = 1; uv_i++; uv_data[uv_i] = 0; uv_i++; uv_data[uv_i] = 1; uv_i++; uv_data[uv_i] = 1; uv_i++; uv_data[uv_i] = 0; uv_i++; uv_data[uv_i] = 1; uv_i++; } } l->tile_sets[i].num_tiles = num_tiles; glGenBuffers(1, &l->tile_sets[i].positions_buffer); glGenBuffers(1, &l->tile_sets[i].texcoords_buffer); glBindBuffer(GL_ARRAY_BUFFER, l->tile_sets[i].positions_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 4 * num_tiles, position_data, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, l->tile_sets[i].texcoords_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 4 * num_tiles, uv_data, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); free(position_data); free(uv_data); } return l; }