How to draw a cube using indices ?
Florian NICOLAS <[email protected]> Fri, 27 Nov 2015 13:57:00 +0000
| Newsgroups | gmane.comp.python.opengl.user |
|---|---|
| Message-ID | <VI1PR05MB1168E8CE8FBBA4C04C5B166EC8030@VI1PR05MB1168.eurprd05.prod.outlook.com> |
--===============4959901050919684517==
Content-Language: fr-FR
Content-Type: multipart/alternative;
boundary="_000_VI1PR05MB1168E8CE8FBBA4C04C5B166EC8030VI1PR05MB1168eurp_"
--_000_VI1PR05MB1168E8CE8FBBA4C04C5B166EC8030VI1PR05MB1168eurp_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi everybody!
I recently started to learn OpenGL through Python thanks to several tutoria=
l (especially the Nicolas P. Rougier one: http://www.labri.fr/perso/nrougie=
r/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 seems=
to be normal as I do not duplicate my vertices and I use the glDrawArrays =
function).
However, after, I build an index "vector" to further use the glDrawElements=
function to render my cube. As a result, I do not get any error but nothin=
g 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, inde=
x) # 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_DRAW)
# 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_STAT=
IC_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()
--_000_VI1PR05MB1168E8CE8FBBA4C04C5B166EC8030VI1PR05MB1168eurp_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;back=
ground-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p><br>
</p>
<p>Hi everybody!</p>
<p><br>
</p>
<p>I recently started to learn OpenGL through Python thanks to several tuto=
rial (especially the Nicolas P. Rougier one: http://www.labri.fr/perso/nrou=
gier/teaching/opengl/).
<br>
</p>
<p><br>
</p>
<p>I am now switching to 3D and I am trying to draw a cube.</p>
<p><br>
</p>
<p>Thus, I manage to get some triangles which do not render a cube (this se=
ems to be normal as I do not duplicate my vertices and I use the glDrawArra=
ys function). </p>
<p><br>
</p>
<p>However, after, I build an index "vector" to further use the g=
lDrawElements function to render my cube. As a result, I do not get any err=
or but nothing appears on screen.</p>
<p><br>
</p>
<p>I hope you could be of some help!</p>
<p><br>
</p>
<p>Thanks.<br>
</p>
<p><br>
</p>
<p>Here is my code:</p>
<p><br>
</p>
<p>#! /usr/bin/env python<br>
# -*- coding: utf-8 -*-<br>
<br>
import sys<br>
import ctypes<br>
import numpy as np<br>
import OpenGL.GL as gl<br>
import OpenGL.GLUT as glut<br>
<br>
vertex_code =3D """<br>
<br>
uniform float scale;<br>
uniform mat4 matCam;<br>
attribute vec4 color;<br>
attribute vec3 position;<br>
varying vec4 v_color;<br>
void main()<br>
{<br>
gl_Position =3D matCam*vec4(scal=
e*position, 1.0);<br>
v_color =3D color;<br>
} """<br>
<br>
fragment_code =3D """<br>
varying vec4 v_color;<br>
void main()<br>
{<br>
gl_FragColor =3D v_color;<br>
} """<br>
<br>
def display():<br>
gl.glClear(gl.GL_COLOR_BUFFER_BIT)<br>
#gl.glDrawArrays(gl.GL_TRIANGLES, 0, 12)<br>
</p>
<p> gl.glDrawElements(gl.GL_TRIANGLES, len(index), gl.GL_=
UNSIGNED_INT, index) # render nothing (i.e. only the background color)<br>
</p>
glut.glutSwapBuffers()<br>
<br>
def reshape(width,height):<br>
gl.glViewport(0, 0, width, height)<br>
<br>
def keyboard( key, x, y ):<br>
if key =3D=3D '\033':<br>
sys.exit( )<br>
<br>
def timer(fps):<br>
global clock<br>
clock +=3D 0.0005*1000.0/fps<br>
print(clock)<br>
# eye =3D np.array([0,0,1])<br>
# center =3D np.array([0,clock,0])<br>
# up =3D np.array([0,1,0])<br>
# mat =3D computeLookAtMatrix(eye, center, up)<br>
theta =3D clock;<br>
mat =3D np.array([[np.cos(theta), 0, np.sin(theta), 0],<=
br>
&nb=
sp; [0, 1, 0, 0],<br>
&nb=
sp; [-np.sin(theta), 0, np.cos(theta), 0],<br>
&nb=
sp; [0, 0, 0, 1]])<br>
loc =3D gl.glGetUniformLocation(program, "matCam&qu=
ot;)<br>
gl.glUniformMatrix4fv(loc, 1, False, mat)<br>
&nb=
sp; <br>
<br>
<br>
glut.glutTimerFunc(1000/fps, timer, fps)<br>
glut.glutPostRedisplay()<br>
<br>
<br>
# GLUT init<br>
# --------------------------------------<br>
glut.glutInit()<br>
glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA)<br>
glut.glutCreateWindow('Hello world!')<br>
glut.glutReshapeWindow(512,512)<br>
glut.glutReshapeFunc(reshape)<br>
glut.glutDisplayFunc(display)<br>
glut.glutKeyboardFunc(keyboard)<br>
glut.glutTimerFunc(1000/60, timer, 60)<br>
<br>
# Build data<br>
# --------------------------------------<br>
data =3D np.zeros(8, [("position", np.float32, 3),<br>
&nb=
sp; ("color", &nbs=
p; np.float32, 4)])<br>
&nb=
sp; <br>
data['color'] =3D [ (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,1=
,0,1),<br>
&nb=
sp; (1,0,0,1), (0,1,0,1), (0,0,1,1), (1=
,1,0,1) ]<br>
&nb=
sp; <br>
data['position'] =3D [ (-1,-1,1),<br>
&nb=
sp; (1,-1,1),<br>
&nb=
sp; (1,1,1),&nb=
sp; <br>
&nb=
sp; (-1,1,1),<b=
r>
&nb=
sp; (-1,-1,-1),=
<br>
&nb=
sp; (1,-1,-1),<=
br>
&nb=
sp; (1,1,-1),<b=
r>
&nb=
sp; (-1,1,-1)]<=
br>
&nb=
sp; <br>
index =3D np.array([0,1,2,<br>
&nb=
sp; 2,3,0,<br>
&nb=
sp; 1,5,6,<br>
&nb=
sp; 6,2,1,<br>
&nb=
sp; 7,6,5,<br>
&nb=
sp; 5,4,7,<br>
&nb=
sp; 4,0,3,<br>
&nb=
sp; 3,7,4,<br>
&nb=
sp; 4,5,1,<br>
&nb=
sp; 1,0,4,<br>
&nb=
sp; 3,2,6,<br>
&nb=
sp; 6,7,3])<br>
<br>
# Build & activate program<br>
# --------------------------------------<br>
<br>
# Request a program and shader slots from GPU<br>
program =3D gl.glCreateProgram()<br>
vertex =3D gl.glCreateShader(gl.GL_VERTEX_SHADER)<br>
fragment =3D gl.glCreateShader(gl.GL_FRAGMENT_SHADER)<br>
<br>
# Set shaders source<br>
gl.glShaderSource(vertex, vertex_code)<br>
gl.glShaderSource(fragment, fragment_code)<br>
<br>
# Compile shaders<br>
gl.glCompileShader(vertex)<br>
gl.glCompileShader(fragment)<br>
<br>
# Attach shader objects to the program<br>
gl.glAttachShader(program, vertex)<br>
gl.glAttachShader(program, fragment)<br>
<br>
# Build program<br>
gl.glLinkProgram(program)<br>
<br>
# Get rid of shaders (no more needed)<br>
gl.glDetachShader(program, vertex)<br>
gl.glDetachShader(program, fragment)<br>
<br>
# Make program the default program<br>
gl.glUseProgram(program)<br>
<br>
<br>
# Build buffer<br>
# --------------------------------------<br>
<br>
# Request a buffer slot from GPU<br>
buffer =3D gl.glGenBuffers(1)<br>
<br>
# Make this buffer the default one<br>
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer)<br>
<br>
# Upload data<br>
gl.glBufferData(gl.GL_ARRAY_BUFFER, data.nbytes, data, gl.GL_DYNAMIC_DRAW)<=
br>
<br>
# same for index buffer<br>
buffer_index=3D gl.glGenBuffers(1)<br>
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, buffer_index)<br>
gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, index.nbytes, index, gl.GL_STAT=
IC_DRAW)<br>
<br>
<br>
# Bind attributes<br>
# --------------------------------------<br>
stride =3D data.strides[0]<br>
offset =3D ctypes.c_void_p(0)<br>
loc =3D gl.glGetAttribLocation(program, "position")<br>
gl.glEnableVertexAttribArray(loc)<br>
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer)<br>
gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, False, stride, offset)<br>
<br>
offset =3D ctypes.c_void_p(data.dtype["position"].itemsize)<br>
loc =3D gl.glGetAttribLocation(program, "color")<br>
gl.glEnableVertexAttribArray(loc)<br>
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffer)<br>
gl.glVertexAttribPointer(loc, 4, gl.GL_FLOAT, False, stride, offset)<br>
<br>
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, buffer_index)<br>
<br>
# Bind uniforms<br>
# --------------------------------------<br>
loc =3D gl.glGetUniformLocation(program, "scale")<br>
gl.glUniform1f(loc, 0.5)<br>
clock =3D 0<br>
<br>
loc =3D gl.glGetUniformLocation(program, "matCam")<br>
print(loc)<br>
gl.glUniformMatrix4fv(loc, 1, False, np.eye(4))<br>
<br>
# Enter mainloop<br>
# --------------------------------------<br>
glut.glutMainLoop()<br>
<p></p>
</div>
</body>
</html>
--_000_VI1PR05MB1168E8CE8FBBA4C04C5B166EC8030VI1PR05MB1168eurp_--
--===============4959901050919684517==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
--===============4959901050919684517==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
PyOpenGL Homepage
http://pyopengl.sourceforge.net
_______________________________________________
PyOpenGL-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyopengl-users
--===============4959901050919684517==--