3d - OpenGL: How to render 2 simple VAOs on Intel HD Graphics 4000 GPU? -
3d - OpenGL: How to render 2 simple VAOs on Intel HD Graphics 4000 GPU? -
summary: original question , observations followed updated working opengl code intel hd graphics 4000 gpu.
original question: 2 cubes shown on nvidia nvs 4200m gpu , 1 cube shown on intel hd graphics 4000 gpu.
using opengl 3.2 forwards profile , opentk render 2 simple cubes on screen it shows first cube centered @ (0,0,0) on intel hd graphics 4000 latest gpu driver 7/2/2014 ver 10.18.0010.3621. should show 2 cubes. we're using vertex array object (vao), vertex buffer, index buffer, attributes, normals , glsl shaders rebinding element array buffer after activating vao , before gldrawelements() still shows 1 cube on intel , 2 cubes on nvidia (vao , element array buffer state) shaders the vertex , fragment shaders compile , link ok the vertex , fragment shaders read input variables , write output variables. not read , write input/output/uniform. https://communities.intel.com/thread/36284 glgeterror() calls done after each opengl phone call , returned no errors. the first cube centered @ (0,0,0), sec 1 offset (4,0,0) , both cubes rotate (0,0,0) we build buffers first, attributes next, create vao , hook buffers/attributes vao. there special ordering needed intel work? see: opengl, vaos , multiple buffers deactivating vao after gldrawelements() not seem helphere source code based on opentk illustration programme "open gl 3.0". should show 2 cubes, rotating (0,0,0) directional lite source.
update: 11/17/2014: below code works on both nvidia , intel hd graphics 4000 gpu. changes:
create vao before creating buffers vertex, index, ... associated vao move gl.bindattriblocation() phone call before linking shader rebind element array buffer before using gl.drawelements() render 3d objectdeactivate buffer , vao after rendering
using system; using system.diagnostics; using system.io; using opentk; using opentk.graphics; using opentk.graphics.opengl; namespace examples.tutorial { [example("opengl 3.0 v2 2 vao works", examplecategory.opengl, "3.x", documentation="hellogl3 - v2 - create vao first")] public class hellogl3v3 : gamewindow { string vertexshadersource = @" #version 130 precision highp float; uniform mat4 projection_matrix; uniform mat4 modelview_matrix; in vec3 in_position; in vec3 in_normal; out vec3 normal; void main(void) { //works orthogonal modelview normal = (modelview_matrix * vec4(in_normal, 0)).xyz; gl_position = projection_matrix * modelview_matrix * vec4(in_position, 1); }"; string fragmentshadersource = @" #version 130 precision highp float; const vec3 ambient = vec3(0.1, 0.1, 0.1); const vec3 lightvecnormalized = normalize(vec3(0.5, 0.5, 20.0)); //const vec3 lightvecnormalized = normalize(vec3(0.5, 0.5, 2.0)); const vec3 lightcolor = vec3(0.9, 0.9, 0.7); in vec3 normal; out vec4 out_frag_color; void main(void) { float diffuse = clamp(dot(lightvecnormalized, normalize(normal)), 0.0, 1.0); out_frag_color = vec4(ambient + diffuse * lightcolor, 1.0); }"; int vertexshaderhandle, fragmentshaderhandle, shaderprogramhandle, modelviewmatrixlocation, projectionmatrixlocation; matrix4 projectionmatrix, modelviewmatrix; public hellogl3v3() : base(800, 600, new graphicsmode(), "opengl 3 example", 0, displaydevice.default, 3, 2, graphicscontextflags.forwardcompatible | graphicscontextflags.debug) { } protected override void onload (system.eventargs e) { vsync = vsyncmode.on; createshaders(); createvaoone(); createvaotwo(); // other state gl.enable(enablecap.depthtest); e(); gl.clearcolor(system.drawing.color.midnightblue); e(); } void createshaders() { vertexshaderhandle = gl.createshader(shadertype.vertexshader); e(); fragmentshaderhandle = gl.createshader(shadertype.fragmentshader); e(); gl.shadersource(vertexshaderhandle, vertexshadersource); e(); gl.shadersource(fragmentshaderhandle, fragmentshadersource); e(); gl.compileshader(vertexshaderhandle); e(); gl.compileshader(fragmentshaderhandle); e(); debug.writeline(gl.getshaderinfolog(vertexshaderhandle)); debug.writeline(gl.getshaderinfolog(fragmentshaderhandle)); // create programme shaderprogramhandle = gl.createprogram(); e(); gl.attachshader(shaderprogramhandle, vertexshaderhandle); e(); gl.attachshader(shaderprogramhandle, fragmentshaderhandle); e(); //bindattriblocation() shader needs done before linking shader programme gl.bindattriblocation(shaderprogramhandle, 0, "in_position"); e(); gl.bindattriblocation(shaderprogramhandle, 1, "in_normal"); e(); gl.linkprogram(shaderprogramhandle); e(); debug.writeline(gl.getprograminfolog(shaderprogramhandle)); gl.useprogram(shaderprogramhandle); e(); // uniform locations projectionmatrixlocation = gl.getuniformlocation(shaderprogramhandle, "projection_matrix"); e(); modelviewmatrixlocation = gl.getuniformlocation(shaderprogramhandle, "modelview_matrix"); e(); float aspectratio = clientsize.width / (float)(clientsize.height); matrix4.createperspectivefieldofview((float)math.pi / 4, aspectratio, 1, 100, out projectionmatrix); modelviewmatrix = matrix4.lookat(new vector3(0, 13, 15), new vector3(0, 0, 0), new vector3(0, 1, 0)); gl.uniformmatrix4(projectionmatrixlocation, false, ref projectionmatrix); e(); gl.uniformmatrix4(modelviewmatrixlocation, false, ref modelviewmatrix); e(); } int vaohandle, ebohandle; int[] indicesvbodata = new int[]{ // front end face 0, 1, 2, 2, 3, 0, // top face 3, 2, 6, 6, 7, 3, // face 7, 6, 5, 5, 4, 7, // left face 4, 0, 3, 3, 7, 4, // bottom face 0, 1, 5, 5, 4, 0, // right face 1, 5, 6, 6, 2, 1, }; void createvaoone() { int positionvbohandle, normalvbohandle; vector3[] positionvbodata = new vector3[]{ new vector3(-1.0f, -1.0f, 1.0f), new vector3( 1.0f, -1.0f, 1.0f), new vector3( 1.0f, 1.0f, 1.0f), new vector3(-1.0f, 2.0f, 1.0f), new vector3(-1.0f, -1.0f, -1.0f), new vector3( 1.0f, -1.0f, -1.0f), new vector3( 1.0f, 1.0f, -1.0f), new vector3(-1.0f, 1.0f, -1.0f) }; // gl3 allows store vertex layout in "vertex array object" (vao). // means not have re-issue vertexattribpointer calls // every time seek utilize different vertex layout - these calls // stored in vao need bind right vao. gl.genvertexarrays(1, out vaohandle); e(); gl.bindvertexarray(vaohandle); e(); //---------------------------------------------------------------------------- { gl.genbuffers(1, out positionvbohandle); e(); gl.bindbuffer(buffertarget.arraybuffer, positionvbohandle); e(); gl.bufferdata<vector3>(buffertarget.arraybuffer, new intptr(positionvbodata.length * vector3.sizeinbytes), positionvbodata, bufferusagehint.staticdraw); e(); gl.enablevertexattribarray(0); e(); gl.bindbuffer(buffertarget.arraybuffer, positionvbohandle); e(); gl.vertexattribpointer(0, 3, vertexattribpointertype.float, true, vector3.sizeinbytes, 0); e(); //gl.bindattriblocation(shaderprogramhandle, 0, "in_position"); e(); } //---------------------------------------------------------------------------- { gl.genbuffers(1, out normalvbohandle); e(); gl.bindbuffer(buffertarget.arraybuffer, normalvbohandle); e(); gl.bufferdata<vector3>(buffertarget.arraybuffer, new intptr(positionvbodata.length * vector3.sizeinbytes), positionvbodata, bufferusagehint.staticdraw); e(); gl.enablevertexattribarray(1); e(); gl.bindbuffer(buffertarget.arraybuffer, normalvbohandle); e(); gl.vertexattribpointer(1, 3, vertexattribpointertype.float, true, vector3.sizeinbytes, 0); e(); //gl.bindattriblocation(shaderprogramhandle, 1, "in_normal"); e(); } { gl.genbuffers(1, out ebohandle); e(); gl.bindbuffer(buffertarget.elementarraybuffer, ebohandle); e(); gl.bufferdata(buffertarget.elementarraybuffer, new intptr(sizeof(uint) * indicesvbodata.length), indicesvbodata, bufferusagehint.staticdraw); e(); gl.bindbuffer(buffertarget.elementarraybuffer, ebohandle); e(); } //---------------------------------------------------------------------------- gl.bindbuffer(buffertarget.arraybuffer, 0); e(); gl.bindbuffer(buffertarget.elementarraybuffer, 0); e(); gl.bindvertexarray(0); e(); } int vaohandle2, ebohandle2; int[] indicesvbodata2 = new int[]{ // front end face 0, 1, 2, 2, 3, 0, // top face 3, 2, 6, 6, 7, 3, // face 7, 6, 5, 5, 4, 7, // left face 4, 0, 3, 3, 7, 4, // bottom face 0, 1, 5, 5, 4, 0, // right face 1, 5, 6, 6, 2, 1, }; void createvaotwo() { int positionvbohandle2, normalvbohandle2; vector3[] positionvbodata2 = new vector3[]{ new vector3(-0.5f, -0.5f, 0.5f), new vector3( 0.5f, -0.5f, 0.5f), new vector3( 0.5f, 0.5f, 0.5f), new vector3(-0.5f, 0.5f, 5.5f), new vector3(-0.5f, -0.5f, -0.5f), new vector3( 0.5f, -0.5f, -0.5f), new vector3( 0.5f, 0.5f, -0.5f), new vector3(-0.5f, 0.5f, -0.5f) }; vector3[] normalsvbodata2 = new vector3[]{ new vector3(-1.0f, -1.0f, 1.0f), new vector3( 1.0f, -1.0f, 1.0f), new vector3( 1.0f, 1.0f, 1.0f), new vector3(-1.0f, 1.0f, 1.0f), new vector3(-1.0f, -1.0f, -1.0f), new vector3( 1.0f, -1.0f, -1.0f), new vector3( 1.0f, 1.0f, -1.0f), new vector3(-1.0f, 1.0f, -1.0f) }; //translate points in sec cube away origin (int = 0; < positionvbodata2.length; i++) positionvbodata2[i] = vector3.add(positionvbodata2[i], new vector3(2.0f, 0f, 0f)); gl.genvertexarrays(1, out vaohandle2); e(); gl.bindvertexarray(vaohandle2); e(); //---------------------------------------------------------------------------- //vertex array gl.genbuffers(1, out positionvbohandle2); e(); gl.bindbuffer(buffertarget.arraybuffer, positionvbohandle2); e(); gl.bufferdata<vector3>(buffertarget.arraybuffer, new intptr(positionvbodata2.length * vector3.sizeinbytes), positionvbodata2, bufferusagehint.staticdraw); e(); gl.enablevertexattribarray(0); e(); gl.bindbuffer(buffertarget.arraybuffer, positionvbohandle2); e(); gl.vertexattribpointer(0, 3, vertexattribpointertype.float, true, vector3.sizeinbytes, 0); e(); //gl.bindattriblocation(shaderprogramhandle, 0, "in_position"); e(); //------------------------------------------------------------ //normals array gl.genbuffers(1, out normalvbohandle2); e(); gl.bindbuffer(buffertarget.arraybuffer, normalvbohandle2); e(); gl.bufferdata<vector3>(buffertarget.arraybuffer, new intptr(normalsvbodata2.length * vector3.sizeinbytes), normalsvbodata2, bufferusagehint.staticdraw); e(); gl.enablevertexattribarray(1); e(); gl.bindbuffer(buffertarget.arraybuffer, normalvbohandle2); e(); gl.vertexattribpointer(1, 3, vertexattribpointertype.float, true, vector3.sizeinbytes, 0); e(); //gl.bindattriblocation(shaderprogramhandle, 1, "in_normal"); e(); //------------------------------------------------------------ //element (index) array gl.genbuffers(1, out ebohandle2); e(); gl.bindbuffer(buffertarget.elementarraybuffer, ebohandle2); e(); gl.bufferdata(buffertarget.elementarraybuffer, new intptr(sizeof(uint) * indicesvbodata2.length), indicesvbodata2, bufferusagehint.staticdraw); e(); gl.bindbuffer(buffertarget.elementarraybuffer, ebohandle2); e(); //------------------------------------------------------------ gl.bindbuffer(buffertarget.arraybuffer, 0); e(); gl.bindbuffer(buffertarget.elementarraybuffer, 0); e(); gl.bindvertexarray(0); e(); } protected override void onupdateframe(frameeventargs e) { if ((keyboard[opentk.input.key.controlleft]) || (keyboard[opentk.input.key.controlright])) return; matrix4 rotation = matrix4.createrotationy((float)e.time); matrix4.mult(ref rotation, ref modelviewmatrix, out modelviewmatrix); gl.uniformmatrix4(modelviewmatrixlocation, false, ref modelviewmatrix); if (keyboard[opentk.input.key.escape]) exit(); } protected override void onrenderframe(frameeventargs e) { gl.viewport(0, 0, width, height); e(); gl.clear(clearbuffermask.colorbufferbit | clearbuffermask.depthbufferbit); e(); //gl.polygonmode(materialface.frontandback, polygonmode.line); gl.bindvertexarray(vaohandle); e(); gl.bindbuffer(buffertarget.elementarraybuffer, ebohandle); e(); gl.drawelements(beginmode.triangles, indicesvbodata.length, drawelementstype.unsignedint, intptr.zero); e(); gl.bindbuffer(buffertarget.elementarraybuffer, 0); gl.bindvertexarray(0); //----------------------------------------------------------------------- gl.bindvertexarray(vaohandle2); e(); gl.bindbuffer(buffertarget.elementarraybuffer, ebohandle2); e(); gl.drawelements(beginmode.triangles, indicesvbodata2.length, drawelementstype.unsignedint, intptr.zero); e(); gl.bindbuffer(buffertarget.elementarraybuffer, 0); e(); gl.bindvertexarray(0); e(); swapbuffers(); e(); } [stathread] public static void main() { using (hellogl3v3 illustration = new hellogl3v3()) { utilities.setwindowtitle(example); example.run(30); } } private void e() { errorcode errorcode = gl.geterror(); if (errorcode == errorcode.noerror) return; throw new exception("error code - " + errorcode.tostring()); // open gl error } } }
opengl 3d gpu intel opentk
Comments
Post a Comment