c++ - Drawing in different places of a program with OpenGL -
i'm trying run first opengl program. in main() function have infinity loop:
do { glclear(gl_color_buffer_bit); gluseprogram(programid); _collection[0].draw(); _collection[1].draw(); glfwswapbuffers(window); glfwpollevents(); } while(glfwgetkey(window, glfw_key_escape) != glfw_press && glfwwindowshouldclose(window) == 0) the function _collection[].draw() should draw rectangles:
static const glfloat g_vertex_buffer_data[] = { x, y, 0.0f, // lewy górny x, y - 0.4f, 0.0f, // lewy dolny x + 0.4f, y - 0.4f, 0.0f, // prawy dolny x + 0.4f, y, 0.0f, // lewy górny x + 0.02f, y - 0.02f, 0.0f, // lewy górny x + 0.02f, y - 0.4f + 0.02f, 0.0f, // lewy dolny x + 0.4f - 0.02f, y - 0.4f + 0.02f, 0.0f, // prawy dolny x + 0.4f - 0.02f, y - 0.02f, 0.0f, // lewy górny }; static const glfloat g_color_buffer_data[] = { 1.0f, 1.0f, 1.0f, // lewy górny 1.0f, 1.0f, 1.0f, // lewy dolny 1.0f, 1.0f, 1.0f, // prawy dolny 1.0f, 1.0f, 1.0f, // lewy górny 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, }; gluint vertexbuffer; glgenbuffers(1, &vertexbuffer); glbindbuffer(gl_array_buffer, vertexbuffer); glbufferdata(gl_array_buffer, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, gl_static_draw); gluint colorbuffer; glgenbuffers(1, &colorbuffer); glbindbuffer(gl_array_buffer, colorbuffer); glbufferdata(gl_array_buffer, sizeof(g_color_buffer_data), g_color_buffer_data, gl_static_draw); glenablevertexattribarray(vertexposition_modelspaceid); glbindbuffer(gl_array_buffer, vertexbuffer); glvertexattribpointer( vertexposition_modelspaceid, // attribute want configure 3, // size gl_float, // type gl_false, // normalized? 0, // stride (void*)0 // array buffer offset ); // przekazuję kolory wierzchołków glenablevertexattribarray(vertexcolorid); glbindbuffer(gl_array_buffer, colorbuffer); glvertexattribpointer( vertexcolorid, // attribute want configure 3, // size gl_float, // type gl_false, // normalized? 0, // stride (void*)0 // array buffer offset ); // rysuję wszystko gldrawarrays(gl_quads, 0, 8); gldisablevertexattribarray(vertexposition_modelspaceid); gldisablevertexattribarray(vertexcolorid); my problem that: when run programme see effect of run first function draw() - index 0. alter places these functions:
_collection[1].draw(); _collection[0].draw(); i still see effect of first function - in case index number 1.
it looks there blocking code sec draw() function run.
what problem? how can prepare it?
the sec draw function isn't beingness blocked executing. since vertice , color info defined static within body of draw() function, values won't alter regardless of element of _collection drawing. that's why drawing 2 collections yields same result -- drawing vertices in same location, , same colors.
to prepare problem, want store vertex , color info once. each of collections should contain x , y values, indicating position. don't want multiple collections of vertices , colors, want single collection of vertices , colors draw in several different locations.
you should create vertex , color arrays in main function before come in main loop. should utilize glgenbuffers , glbindbuffer followed glbufferdata tell opengl vertex , color arrays in main programme before main loop well. can take calls glgenbuffers , glbufferdata out of draw function. should phone call glvertexattribpointer both vertex , color arrays in main function , remove them draw() function.
// note vertex info isn't contingent on 'x' , 'y' positions. // utilize vertex shader move boxes around later. glfloat g_vertex_buffer_data[] = { 0.0f, 0, 0.0f, // lewy górny 0.0f, 0.4f, 0.0f, // lewy dolny 0.4f, 0.4f, 0.0f, // prawy dolny 0.4f, 0.0f, 0.0f, // lewy górny 0.02f, 0.02f, 0.0f, // lewy górny 0.02f, 0.4f + 0.02f, 0.0f, // lewy dolny 0.4f - 0.02f, 0.4f + 0.02f, 0.0f, // prawy dolny 0.4f - 0.02f, 0.02f, 0.0f, // lewy górny }; glfloat g_color_buffer_data[] = { 1.0f, 1.0f, 1.0f, // lewy górny 1.0f, 1.0f, 1.0f, // lewy dolny 1.0f, 1.0f, 1.0f, // prawy dolny 1.0f, 1.0f, 1.0f, // lewy górny 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, }; gluint vertexbuffer; glgenbuffers(1, &vertexbuffer); glbindbuffer(gl_array_buffer, vertexbuffer); glbufferdata(gl_array_buffer, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, gl_static_draw); glvertexattribpointer( vertexposition_modelspaceid, // attribute want configure 3, // size gl_float, // type gl_false, // normalized? 0, // stride (void*)0 // array buffer offset ); gluint colorbuffer; glgenbuffers(1, &colorbuffer); glbindbuffer(gl_array_buffer, colorbuffer); glbufferdata(gl_array_buffer, sizeof(g_color_buffer_data), g_color_buffer_data, gl_static_draw); glvertexattribpointer( vertexcolorid, // attribute want configure 3, // size gl_float, // type gl_false, // normalized? 0, // stride (void*)0 // array buffer offset ); // of above info need specify opengl once, not every time draw frame! you need alter shader accepts x , y offset each of collections:
#version 150 uniform float collectionx; uniform float collectiony; in vec3 vertexposition_modelspaceid; // vertex attribute name 'vertexposition_modelspaceid' corresponds to. // remember shader take color , give fragment shader, include code well. void main() { gl_position = vec4(vertexposition_modelspaceid.x + collectionx, vertexposition_modelspaceid.y + collectiony, vertexposition_modelspaceid.z, 1.0); } and need locations of uniform variables added shader in main programme before loop:
// phone call these functions after compile , link shaders. programid should compiled , linked shader program. gluint collectionxid = glgetuniformlocation(programid, "collectionx"); gluint collectionyid = glgetuniformlocation(programid, "collectiony"); your draw function simple now:
void draw() { gldrawarrays(gl_quads, 0, 8); } finally, main loop this:
do { glclear(gl_color_buffer_bit); gluseprogram(programid); glenablevertexattribarray(vertexposition_modelspaceid); glenablevertexattribarray(vertexcolorid); gluniform1f(collectionxid, _collection[0].x); gluniform1f(collectionyid, _collection[0].y); _collection[0].draw(); gluniform1f(collectionxid, _collection[1].x); gluniform1f(collectionyid, _collection[1].y); _collection[1].draw(); glfwswapbuffers(window); gldisablevertexattribarray(vertexposition_modelspaceid); gldisablevertexattribarray(vertexcolorid); glfwpollevents(); } while(glfwgetkey(window, glfw_key_escape) != glfw_press && glfwwindowshouldclose(window) == 0) note specifying location @ draw vertices shader programme passing individual collection's x , y position gluniform1f function. more mutual move vertices around transformation matrix, rather complicated topic itself.
assuming collections have different x , y positions, draw in different locations.
c++ opengl
No comments:
Post a Comment