c++ - OpenGL Height Map from text segmentation fault -
i trying create heightmap 25 float values so:
#define height_verts 5 #define vals_per_vert_height 5 float heightmapverts[ height_verts*vals_per_vert_height ] = { //5 -0.9, -0.6, -0.4, -0.6, -0.9, -0.2, 0.1, 0.3, 0.1, -0.3, 0, 0.4, 0.8, 0.4, 0, -0.2, 0.1, 0.3, 0.1, -0.3, 0.5, -0.6, -0.4, -0.6, -0.9, };
i getting segmentation fault when calling:
gldrawarrays(gl_triangles, 0, height_verts);
i have been suggested it's because size argument of glvertexattribpointer() must 1, 2, 3, or 4. pass 5 with:
glvertexattribpointer(vertlocheight, vals_per_vert_height, gl_float, gl_false, 0, 0);
but error saying have many vertices if these values smaller (eg: #define vals_per_vert_height 3)
error: many initializers ‘float [15]’
i have attached rest of code context, very new opengl apologize if code messy.
#include <stdio.h> // glew loads opengl extensions. required opengl programs. #include <gl/glew.h> #ifdef __apple__ #include <glut/glut.h> #else #include <gl/glut.h> #endif // utility code load , compile glsl shader programs #include "shader.hpp" #include <iostream> #include <fstream> #include <vector> #include "glm/glm.hpp" #define window_width 400 #define window_height 400 #define vals_per_vert_height 5//5 #define vals_per_colour_height 4 #define height_verts 5 //height map vertices per line using namespace std; // handle our vao generated in setshaderdata method //heightmap unsigned int vertexvaohandleheight; // handle our shader program unsigned int programid; /** * sets shader uniforms , vertex data * happens once only, before frames rendered * @param id, shader program object use * @returns 0 success, error otherwise */ int setshaderdata(const unsigned int &id) { float heightmapverts[ height_verts*vals_per_vert_height ] = { //5 -0.9, -0.6, -0.4, -0.6, -0.9, -0.2, 0.1, 0.3, 0.1, -0.3, 0, 0.4, 0.8, 0.4, 0, -0.2, 0.1, 0.3, 0.1, -0.3, 0.5, -0.6, -0.4, -0.6, -0.9, }; // colours each vertex; red, green, blue , alpha // data indexed same order vertex data, reads 4 values // alpha not used directly in example program float heightcolours[ height_verts*vals_per_colour_height ] = { 0.8f, 0.7f, 0.5f, 1.0f, 0.3f, 0.7f, 0.1f, 1.0f, 0.8f, 0.2f, 0.5f, 1.0f, }; // heightmap stuff ################################################## // generate storage on gpu our triangle , make current. // vao set of data buffers on gpu glgenvertexarrays(1, &vertexvaohandleheight); glbindvertexarray(vertexvaohandleheight); // generate new buffers in our vao // single data buffer store generic, per-vertex attributes unsigned int bufferheight[2]; glgenbuffers(2, bufferheight); // allocate gpu memory our vertices , copy them on glbindbuffer(gl_array_buffer, bufferheight[0]); glbufferdata(gl_array_buffer, sizeof(float)*height_verts*vals_per_vert_height, heightmapverts, gl_static_draw); // same our vertex colours glbindbuffer(gl_array_buffer, bufferheight[1]); glbufferdata(gl_array_buffer, sizeof(float)*height_verts*vals_per_colour_height, heightcolours, gl_static_draw); // tell opengl how interpret data gave // tell opengl shader variable corresponds // tell opengl how it's formatted (floating point, 3 values per vertex) int vertlocheight = glgetattriblocation(id, "a_vertex"); glbindbuffer(gl_array_buffer, bufferheight[0]); glenablevertexattribarray(vertlocheight); glvertexattribpointer(vertlocheight, vals_per_vert_height, gl_float, gl_false, 0, 0); // same vertex colours int colourlocheight = glgetattriblocation(id, "a_colour"); glbindbuffer(gl_array_buffer, bufferheight[1]); glenablevertexattribarray(colourlocheight); glvertexattribpointer(colourlocheight, vals_per_colour_height, gl_float, gl_false, 0, 0); // heightmap stuff ################################################## // argument of 0 un-binds vao's , stops // accidentally changing vao state glbindvertexarray(0); // same true buffers, un-bind glbindbuffer(gl_array_buffer, 0); return 0; // return success } /** * renders frame of state , shaders have set window * executed each time frame drawn. */ void render() { // clear previous pixels have drawn colour buffer (display buffer) // called each frame don't draw on top of previous glclear(gl_color_buffer_bit | gl_depth_buffer_bit); gluseprogram(programid); // height map stuff ################################### // make vao our vertex data buffer current glbindvertexarray(vertexvaohandleheight); // send command gpu draw data in current vao triangles //crashes here gldrawarrays(gl_triangles, 0, height_verts); glbindvertexarray(0); // un-bind vao // height map stuff ################################### glutswapbuffers(); // swap buffer front buffer, showing has been rendered glflush(); // guarantees previous commands have been completed before continuing }
you messing things up.
1) glvertexattribpointer()
, setup vertex attributes - vectors of kind. vertex position, if need draw scene in 2d, pass size = 2 (because each vertex has x , y coordinates), 3d - pass 3 (x, y, z).
2) think interpretation of heightmap quite incorrect. filled array height values (in 3d space, y coordinates). x , z? need render vertices, need pass x, y , z coords, opengl can know each point should rendered.
your program crashes, because send not enough data , opengl tries read memory, doesn't belong you.
i assume, want heightmap, 5x5 grid? init data way:
float heightmapverts[25] = { //leave right }; vec3 vertices[5][5]; for(int z_num = 0; z_num < 5; ++z_num) { for(int x_num = 0; x_num < 5; ++x_num) { vertices[z_num][x_num].x = x_num * 0.5f; vertices[z_num][x_num].z = z_num * 0.5f; vertices[z_num][x_num].y = heightmapverts[z_num * 5 + x_num]; } }
then can call:
glvertexattribpointer(vertlocheight, 3, gl_float, gl_false, 0, 0);
update:
vec3 stands 3-dimensional vector. wrote pseudocode illustrate conception, sake of simplicity, may want use great library: opengl mathematics.
update 2:
one more thing: color data set improperly. want color rgb value, every vertex needs additional 3 floats represent color. also, position , color should place in single vbo, there no need separating them.
i not sure, if got basics required simple drawings. may want read these articles:
Comments
Post a Comment