How do i draw a Cube in OpenGL/C++? -
i see lot of people using glbegin(gl_polygon) draw cubes whats about, learnt in class whats diffrence way teacher thought me worse.
glfloat verts[] = { -1.0f, +1.0f, +1.0f, // 0 +1.0f, +0.0f, +0.0f, // color +1.0f, +1.0f, +1.0f, // 1 +0.0f, +1.0f, +0.0f, // color +1.0f, +1.0f, -1.0f, // 2 +0.0f, +0.0f, +1.0f, // color -1.0f, +1.0f, -1.0f, // 3 +1.0f, +1.0f, +1.0f, // color -1.0f, +1.0f, -1.0f, // 4 +1.0f, +0.0f, +1.0f, // color +1.0f, +1.0f, -1.0f, // 5 +0.0f, +0.5f, +0.2f, // color +1.0f, -1.0f, -1.0f, // 6 +0.8f, +0.6f, +0.4f, // color -1.0f, -1.0f, -1.0f, // 7 +0.3f, +1.0f, +0.5f, // color +1.0f, +1.0f, -1.0f, // 8 +0.2f, +0.5f, +0.2f, // color +1.0f, +1.0f, +1.0f, // 9 +0.9f, +0.3f, +0.7f, // color +1.0f, -1.0f, +1.0f, // 10 +0.3f, +0.7f, +0.5f, // color +1.0f, -1.0f, -1.0f, // 11 +0.5f, +0.7f, +0.5f, // color -1.0f, +1.0f, +1.0f, // 12 +0.7f, +0.8f, +0.2f, // color -1.0f, +1.0f, -1.0f, // 13 +0.5f, +0.7f, +0.3f, // color -1.0f, -1.0f, -1.0f, // 14 +0.4f, +0.7f, +0.7f, // color -1.0f, -1.0f, +1.0f, // 15 +0.2f, +0.5f, +1.0f, // color +1.0f, +1.0f, +1.0f, // 16 +0.6f, +1.0f, +0.7f, // color -1.0f, +1.0f, +1.0f, // 17 +0.6f, +0.4f, +0.8f, // color -1.0f, -1.0f, +1.0f, // 18 +0.2f, +0.8f, +0.7f, // color +1.0f, -1.0f, +1.0f, // 19 +0.2f, +0.7f, +1.0f, // color +1.0f, -1.0f, -1.0f, // 20 +0.8f, +0.3f, +0.7f, // color -1.0f, -1.0f, -1.0f, // 21 +0.8f, +0.9f, +0.5f, // color -1.0f, -1.0f, +1.0f, // 22 +0.5f, +0.8f, +0.5f, // color +1.0f, -1.0f, +1.0f, // 23 +0.9f, +1.0f, +0.2f, // color }; glushort indices[] = { 0, 1, 2, 0, 2, 3, // top 4, 5, 6, 4, 6, 7, // front 8, 9, 10, 8, 10, 11, // right 12, 13, 14, 12, 14, 15, // left 16, 17, 18, 16, 18, 19, // 20, 22, 21, 20, 23, 22, // bottom }; gluint vertexbufferid; glgenbuffers(1, &vertexbufferid); glbindbuffer(gl_array_buffer, vertexbufferid); glbufferdata(gl_array_buffer, sizeof(verts), verts, gl_static_draw); glenablevertexattribarray(0); glvertexattribpointer(0, 2, gl_float, gl_false, sizeof(float) * 6, 0); glenablevertexattribarray(1); glvertexattribpointer(1, 3, gl_float, gl_false, sizeof(float) * 6, (char*)(sizeof(float) * 3)); //glushort indices[] = { 0, 1, 2, 3, 1, 2, 0, 1, 2, 3, 1, 2 }; gluint indexbufferid; glgenbuffers(1, &indexbufferid); glbindbuffer(gl_element_array_buffer, indexbufferid); glbufferdata(gl_element_array_buffer, sizeof(indices), indices, gl_static_draw);
so im trying use draw cube comes out looking ike square , when rotate not cube.
glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glviewport(0, 0, width(), height()); glmatrixmode(gl_projection); glloadidentity(); glint tripositionuniformlocation = glgetuniformlocation(programid, "triposition"); glint domcoloruniformlocation = glgetuniformlocation(programid, "domcolor"); vec3 domcolor(1.0f, 0.0f, 0.0f); gluniformmatrix4fv(tripositionuniformlocation, 1, gl_false, &triposition[0][0]); gluniform3fv(domcoloruniformlocation, 1, &domcolor[0]); gldrawelements(gl_triangles, 36, gl_unsigned_short, 0);
why no cube being drawn screen? methods missing or doing wrong?
mat4 moveobject(mat4 mat, int left, int up, int right, int down, float speed, float angle,float dt) { short left = getasynckeystate(left); short = getasynckeystate(up); short right = getasynckeystate(right); short down = getasynckeystate(down); mat4 mattemp = mat; if (left != 0) { mattemp = rotate(mattemp, angle, vec3(1.0f, 1.0f, 1.0f)); } if (right != 0) { mattemp = rotate(mattemp, -angle, vec3(0.0f, 0.0f, 1.0f)); } if (up != 0) { mattemp = translate(mattemp, vec3(0.0f, 0.0f, speed)* dt); } if (down != 0) { mattemp = translate(mattemp, vec3(0.0f, -speed, 0.0f)* dt); } return mattemp;
}
this how set triposition take value returned , set triposition equal that.
#version 430 in layout(location=0) vec3 position; in layout(location=1) vec3 vertexcolor; uniform mat4 triposition; uniform vec3 domcolor; out vec3 thecolor; void main() { vec4 v = vec4(position, 1.0); gl_position = triposition * v; thecolor = vertexcolor; };
my problem see cube cant move in 3d space might close camera cant move way when pressing up.
you've incorrectly specified vertex co-ordinates have 2 components in call glvertexattribpointer(i.e. 2 dimensional). square instead of cube.
second parameter should 3:
glvertexattribpointer(0, 3, gl_float, gl_false, sizeof(float) * 6, 0);
Comments
Post a Comment