Re: How to draw a cube using indices ?
Florian NICOLAS <[email protected]> Sat, 28 Nov 2015 12:14:38 +0100
| Newsgroups | gmane.comp.python.opengl.user |
|---|---|
| Message-ID | <[email protected]> |
Yes it solves my problem. Thanks! > Le 27 nov. 2015 =E0 19:19, Nicolas P. Rougier <[email protected]> = a =E9crit : > = > Maybe you need to use np.uint32 for the dtype of the index array. > = > Nicolas > = > = >> On 27 Nov 2015, at 16:20, Florian NICOLAS <florian.nicolas@ensta-bretagn= e.org> wrote: >> = >> Yes it works (ctypes.c_void_p(0) also works). = >> However, it seems that only half the cube is rendered. It is a bit stran= ge as my vertex and associated indices come from a tutorial. >> = >> = >> = >> ------------------------------------------------------------------------= ------------------------------------------------ >> NICOLAS Florian >> Ing=E9nieur =E9tudes amont - Service Etudes G=E9n=E9rales Sonar >> THALES Underwater Systems >> ENSTA Bretagne (ex-ENSIETA) - Promotion 2014 - Sp=E9cialit=E9 SPID (Syst= =E8me Perception Information D=E9cision) >> Profil PSO (Perception et Syst=E8mes d'Observation) >> = >> ________________________________________ >> De : Nicolas P. Rougier <[email protected]> >> Envoy=E9 : vendredi 27 novembre 2015 16:04 >> =C0 : Florian NICOLAS >> Cc : [email protected] >> Objet : Re: [PyOpenGL-Users] How to draw a cube using indices ? >> = >> You've already bound your index buffer so you don't need to give it as a= rgument. >> In your display function: >> = >> gl.glDrawElements(gl.GL_TRIANGLES, len(index), gl.GL_UNSIGNED_INT, index) >> = >> becomes >> = >> gl.glDrawElements(gl.GL_TRIANGLES, len(index), gl.GL_UNSIGNED_INT, None) >> = >> = >> = >> Nicolas >> = >> = >>> On 27 Nov 2015, at 14:57, Florian NICOLAS <florian.nicolas@ensta-bretag= ne.org> wrote: >>> = >>> = >>> Hi everybody! >>> = >>> I recently started to learn OpenGL through Python thanks to several tut= orial (especially the Nicolas P. Rougier one: http://www.labri.fr/perso/nro= ugier/teaching/opengl/). >>> = >>> I am now switching to 3D and I am trying to draw a cube. >>> = >>> Thus, I manage to get some triangles which do not render a cube (this s= eems to be normal as I do not duplicate my vertices and I use the glDrawArr= ays function). >>> = >>> However, after, I build an index "vector" to further use the glDrawElem= ents function to render my cube. As a result, I do not get any error but no= thing appears on screen. >>> = >>> I hope you could be of some help! >>> = >>> Thanks. >>> = >>> Here is my code: >>> = >>> #! /usr/bin/env python >>> # -*- coding: utf-8 -*- >>> = >>> import sys >>> import ctypes >>> import numpy as np >>> import OpenGL.GL as gl >>> import OpenGL.GLUT as glut >>> = >>> vertex_code =3D """ >>> = >>> uniform float scale; >>> uniform mat4 matCam; >>> attribute vec4 color; >>> attribute vec3 position; >>> varying vec4 v_color; >>> void main() >>> { >>> gl_Position =3D matCam*vec4(scale*position, 1.0); >>> v_color =3D color; >>> } """ >>> = >>> fragment_code =3D """ >>> varying vec4 v_color; >>> void main() >>> { >>> gl_FragColor =3D v_color; >>> } """ >>> = >>> def display(): >>> gl.glClear(gl.GL_COLOR_BUFFER_BIT) >>> #gl.glDrawArrays(gl.GL_TRIANGLES, 0, 12) >>> gl.glDrawElements(gl.GL_TRIANGLES, len(index), gl.GL_UNSIGNED_INT, ind= ex) # render nothing (i.e. only the background color) >>> glut.glutSwapBuffers() >>> = >>> def reshape(width,height): >>> gl.glViewport(0, 0, width, height) >>> = >>> def keyboard( key, x, y ): >>> if key =3D=3D '\033': >>> sys.exit( ) >>> = >>> def timer(fps): >>> global clock >>> clock +=3D 0.0005*1000.0/fps >>> print(clock) >>> # eye =3D np.array([0,0,1]) >>> # center =3D np.array([0,clock,0]) >>> # up =3D np.array([0,1,0]) >>> # mat =3D computeLookAtMatrix(eye, center, up) >>> theta =3D clock; >>> mat =3D np.array([[np.cos(theta), 0, np.sin(theta), 0], >>> [0, 1, 0, 0], >>> [-np.sin(theta), 0, np.cos(theta), 0], >>> [0, 0, 0, 1]]) >>> loc =3D gl.glGetUniformLocation(program, "matCam") >>> gl.glUniformMatrix4fv(loc, 1, False, mat) >>> = >>> = >>> = >>> glut.glutTimerFunc(1000/fps, timer, fps) >>> glut.glutPostRedisplay() >>> = >>> = >>> # GLUT init >>> # -------------------------------------- >>> glut.glutInit() >>> glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA) >>> glut.glutCreateWindow('Hello world!') >>> glut.glutReshapeWindow(512,512) >>> glut.glutReshapeFunc(reshape) >>> glut.glutDisplayFunc(display) >>> glut.glutKeyboardFunc(keyboard) >>> glut.glutTimerFunc(1000/60, timer, 60) >>> = >>> # Build data >>> # -------------------------------------- >>> data =3D np.zeros(8, [("position", np.float32, 3), >>> ("color", np.float32, 4)]) >>> = >>> data['color'] =3D [ (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,1,0,1), >>> (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,1,0,1) ] >>> = >>> data['position'] =3D [ (-1,-1,1), >>> (1,-1,1), >>> (1,1,1), >>> (-1,1,1), >>> (-1,-1,-1), >>> (1,-1,-1), >>> (1,1,-1), >>> (-1,1,-1)] >>> = >>> index =3D np.array([0,1,2, >>> 2,3,0, >>> 1,5,6, >>> 6,2,1, >>> 7,6,5, >>> 5,4,7, >>> 4,0,3, >>> 3,7,4, >>> 4,5,1, >>> 1,0,4, >>> 3,2,6, >>> 6,7,3]) >>> = >>> # Build & activate program >>> # -------------------------------------- >>> = >>> # Request a program and shader slots from GPU >>> program =3D gl.glCreateProgram() >>> vertex =3D gl.glCreateShader(gl.GL_VERTEX_SHADER) >>> fragment =3D gl.glCreateShader(gl.GL_FRAGMENT_SHADER) >>> = >>> # Set shaders source >>> gl.glShaderSource(vertex, vertex_code) >>> gl.glShaderSource(fragment, fragment_code) >>> = >>> # Compile shaders >>> gl.glCompileShader(vertex) >>> gl.glCompileShader(fragment) >>> = >>> # Attach shader objects to the program >>> gl.glAttachShader(program, vertex) >>> gl.glAttachShader(program, fragment) >>> = >>> # Build program >>> gl.glLinkProgram(program) >>> = >>> # Get rid of shaders (no more needed) >>> gl.glDetachShader(program, vertex) >>> gl.glDetachShader(program, fragment) >>> = >>> # Make program the default program >>> gl.glUseProgram(program) >>> = >>> = >>> # Build buffer >>> # -------------------------------------- >>> = >>> # Request a buffer slot from GPU >>> buffer =3D gl.glGenBuffers(1) >>> = >>> # Make this buffer the default one >>> gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) >>> = >>> # Upload data >>> gl.glBufferData(gl.GL_ARRAY_BUFFER, data.nbytes, data, gl.GL_DYNAMIC_DR= AW) >>> = >>> # same for index buffer >>> buffer_index=3D gl.glGenBuffers(1) >>> gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, buffer_index) >>> gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, index.nbytes, index, gl.GL_= STATIC_DRAW) >>> = >>> = >>> # Bind attributes >>> # -------------------------------------- >>> stride =3D data.strides[0] >>> offset =3D ctypes.c_void_p(0) >>> loc =3D gl.glGetAttribLocation(program, "position") >>> gl.glEnableVertexAttribArray(loc) >>> gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) >>> gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, stride, offset) >>> = >>> offset =3D ctypes.c_void_p(data.dtype["position"].itemsize) >>> loc =3D gl.glGetAttribLocation(program, "color") >>> gl.glEnableVertexAttribArray(loc) >>> gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer) >>> gl.glVertexAttribPointer(loc, 4, gl.GL_FLOAT, False, stride, offset) >>> = >>> gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, buffer_index) >>> = >>> # Bind uniforms >>> # -------------------------------------- >>> loc =3D gl.glGetUniformLocation(program, "scale") >>> gl.glUniform1f(loc, 0.5) >>> clock =3D 0 >>> = >>> loc =3D gl.glGetUniformLocation(program, "matCam") >>> print(loc) >>> gl.glUniformMatrix4fv(loc, 1, False, np.eye(4)) >>> = >>> # Enter mainloop >>> # -------------------------------------- >>> glut.glutMainLoop() >>> -----------------------------------------------------------------------= ------- >>> _______________________________________________ >>> PyOpenGL Homepage >>> http://pyopengl.sourceforge.net >>> _______________________________________________ >>> PyOpenGL-Users mailing list >>> [email protected] >>> https://lists.sourceforge.net/lists/listinfo/pyopengl-users >> = >> = >> ------------------------------------------------------------------------= ------ >> _______________________________________________ >> PyOpenGL Homepage >> http://pyopengl.sourceforge.net >> _______________________________________________ >> PyOpenGL-Users mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/pyopengl-users > = ---------------------------------------------------------------------------= --- _______________________________________________ PyOpenGL Homepage http://pyopengl.sourceforge.net