c++ - OpenGL drawing triangles -
i pretty new opengl, have followed tutorials , trying play on own. i've got structure:
struct vertex { vec3 position; vec3 color; };
and if have done of this:
glgenvertexarrays(1, &vaoid); glgenbuffers(1, &vboverticesid); glbindvertexarray(vaoid); glbindbuffer(gl_array_buffer, vboverticesid); glbufferdata(gl_array_buffer, sizeof(vertex) * mesh.vertcount, mesh.verts, gl_static_draw); glenablevertexattribarray(shader["vvertex"]); glvertexattribpointer(shader["vvertex"], 3, gl_float, gl_false, sizeof(vertex), (const glvoid*)offsetof(vertex, position)); glenablevertexattribarray(shader["vcolor"]); glvertexattribpointer(shader["vcolor"], 3, gl_float, gl_false, sizeof(vertex), (const glvoid*)offsetof(vertex, color));
mesh.vertices
vertex*
type. put in array every following 3 forms triangle.
the question how call render triangles? have tried gldrawarrays(gl_triangles, 0, mesh.vertcount);
nothing happened, no errors thrown , no triangles rendered. before had separate arrays vertices position, color , indices , gldrawelements
working fine.
mesh constructor:
mesh::mesh(const string sourcepath) { ifstream modelfile; modelfile.open(sourcepath.c_str(), ios_base::in); if (!modelfile.good()) { cout << "cannot load model file (" << sourcepath << ")!\n"; } string line; vector<vec3> tempvertices; vector<vec3> tempnormals; vector<vertex> verticesvector; while (getline(modelfile, line)) { if (line[0] == 'v') { if (line[1] == ' ') { vec3 v; sscanf_s(line.c_str(), "%*s %f %f %f", &v.x, &v.y, &v.z); tempvertices.push_back(v); } else if (line[1] == 'n') { vec3 n; sscanf_s(line.c_str(), "%*s %f %f %f", &n.x, &n.y, &n.z); tempnormals.push_back(n); } } else if (line[0] == 'f') { vector<string> elems; stringstream s(line); string item; while (getline(s, item, ' ')) { elems.push_back(item); } if (elems.size() != 4) { cout << sourcepath << " parsing error!\n"; } (int = 1; < 4; ++i) { stringstream s(elems[i]); string item; vector<unsigned int> vs; while (getline(s, item, '/')) { vs.push_back((unsigned int)atoi(item.c_str())-1); } if (vs.size() != 3) cout << sourcepath << " parsing error!\n"; vertex vert; vert.position = tempvertices[vs[0]]; vert.color = vec3(.5f); // vert.normal = vs[2] != -1 ? tempnormals[vs[2]] : vec3(0.f, 1.f, 0.f); verticesvector.push_back(vert); } } } vertcount = verticesvector.size(); verts = (vertex*)malloc(vertcount * sizeof(vertex)); (int = 0; < vertcount; ++i) { verts[i] = verticesvector[i]; cout << verticesvector[i].position.x << " " << verticesvector[i].position.y << " " << verticesvector[i].position.z << "\n"; } cout << "model loaded(" << sourcepath << ")!\n"; }
init function:
void oninit() { gl_check_errors; shader.loadfromfile(gl_vertex_shader, "shaders/shader.vert"); shader.loadfromfile(gl_fragment_shader, "shaders/shader.frag"); shader.createandlingprogram(); shader.use(); shader.addattribute("vvertex"); shader.addattribute("vcolor"); // shader.addattribute("vnormal"); shader.adduniform("mvp"); // shader.adduniform("lightdir"); shader.unuse(); gl_check_errors; glgenvertexarrays(1, &vaoid); glgenbuffers(1, &vboverticesid); glbindvertexarray(vaoid); glbindbuffer(gl_array_buffer, vboverticesid); glbufferdata(gl_array_buffer, sizeof(vertex) * mesh.vertcount, mesh.verts, gl_static_draw); gl_check_errors; glenablevertexattribarray(shader["vvertex"]); glvertexattribpointer(shader["vvertex"], 3, gl_float, gl_false, sizeof(vertex), (const glubyte*)null + offsetof(vertex, position)); gl_check_errors; glenablevertexattribarray(shader["vcolor"]); glvertexattribpointer(shader["vcolor"], 3, gl_float, gl_false, sizeof(vertex), (const glubyte*)null + offsetof(vertex, color)); gl_check_errors; // glenablevertexattribarray(shader["vnormal"]); // glvertexattribpointer(shader["vnormal"], 3, gl_float, gl_false, sizeof(vertex), (const glvoid*)offsetof(vertex, normal)); // gl_check_errors; glclearcolor(.3, .3, .3, 1); glutpassivemotionfunc(mousefunc); glutwarppointer(width / 2, height / 2); cout << "initialization completed.\n" << endl; }
and render function:
void onrender() { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); shader.use(); gluniformmatrix4fv(shader("mvp"), 1, gl_false, glm::value_ptr(p * v * m)); gldrawarrays(gl_points, 0, mesh.vertcount); gl_check_errors; shader.unuse(); glutswapbuffers(); glutpostredisplay(); }
i have resoved finally, code here ok, wrong use of mesh constructor (mesh mesh = mesh(filepath)
instead of mesh mesh(filepath)
) help.
Comments
Post a Comment