|
Página 1 de 6
|
Autor |
Mensaje |
Shell
Analista Programador
Registrado: Marzo 2010
Mensajes: 5278
Edad: 53 Ubicación: Al otro lado de la pantalla
|
OpenGL. Tutoriales
Hola!.
Para no mezclar el tema de OpenGL con el de colisiones. He creado este post.
Para comenzar. Aquí tenéis un tutorial sobre OpenGL. Y ahora...habrá que aprender como se puede implementar eso a gambas.
Tutorial sobre OpenGL
Sus autores son:
Alberto Jaspe Villanueva y Julián Dorado de la Calle
Saludos
=================== Gambas Básico
"No es un bug, es una característica no documentada"
|
#1 Martes, 15 Diciembre 2015, 13:42 |
|
|
tercoIDE
Analista Programador
Registrado: Noviembre 2013
Mensajes: 713
Edad: 54
|
Re: OpenGL. Tutoriales
Bueno, ya que me has puesto presión, y visto y considerando que el tutorial del link no me gusta y esta en C
Tutorial 1: Un dibujo en 2D (y para que queremos OpenGL para 2D!!!!, bueno, por algo se empieza)
-----------
1-creamos proyecto grafico
2-agregamos el componente gb.qt4.openGL ( gambas se va a quejar que hay que remover algunos componentes incompatiibles, asi que a removerlos!)
3-metemos una GLArea en el formulario, desde la pestaña Special de controles
4-Codigo
' gambas class file
Public Sub Form_Resize()
gl.Viewport(0, 0, GLArea1.w, GLArea1.h) 'esto le dice a GL adonde debe dibujar
End
Public Sub GLArea1_Draw()
'establecemos el color hasta nuevo aviso
gl.Color3f(255, 0, 0)
gl.Begin(gl.TRIANGLES) 'dibujamos triangulos hasta nuevo aviso
gl.Vertex2i(0, 0) 'el punto 0,0 no esta en arriba a la izquierda, para openGL esta en el medio!!!
gl.Vertex2i(1, 0) ' 1 no es pixels, las coordenadas van de -1 a 1 , o sea, para eje X, 1 de openGL es GLArea1.width/2
gl.Vertex2i(0, 1) ' idem para la coordenada Y
gl.Color3f(0, 255, 0) 'verde hasta nuevo aviso, como veran puede estar dentro o fuera de los gl.Begin() - gl.End
gl.Vertex2i(0, 0)
gl.Vertex2i(-1, 0)
gl.Vertex2i(0, -1)
gl.End 'esto es similar al Paint, todo se encierra entre Begin-End
End
última edición por tercoIDE el Martes, 15 Diciembre 2015, 15:49; editado 1 vez
|
#2 Martes, 15 Diciembre 2015, 15:49 |
|
|
jguardon
Administrador
Registrado: Septiembre 2009
Mensajes: 2708
Edad: 57 Ubicación: Granada
|
Re: OpenGL. Tutoriales
Con la clase Paint no es necesario usar .Begin() ni .End() cuando dibujamos dentro del evento Draw con la propiedad Cached a False.
¿Es necesario hacerlo en OpenGL? Porque también estás dibujando dentro del evento Draw del glArea.
¿Se podría usar el mismo mecanismo que describo en el artículo del blog mediante un timer y el método Refresh?
Saludos
=================== Jesús Guardón
Por favor, usemos el corrector ortográfico antes de pulsar el botón "Enviar".
"uo ǝs ʇɐu pıɟıɔıן ɐdɹǝupǝɹ ɐ dɹoƃɹɐɯɐɹ, soןo ɥɐʎ bnǝ dɹodouǝɹsǝןo"
|
#3 Martes, 15 Diciembre 2015, 17:06 |
|
|
tercoIDE
Analista Programador
Registrado: Noviembre 2013
Mensajes: 713
Edad: 54
|
Re: OpenGL. Tutoriales
Con la clase Paint no es necesario usar .Begin() ni .End() cuando dibujamos dentro del evento Draw con la propiedad Cached a False.
¿Es necesario hacerlo en OpenGL? Porque también estás dibujando dentro del evento Draw del glArea.
¿Se podría usar el mismo mecanismo que describo en el artículo del blog mediante un timer y el método Refresh?
Saludos
no, se usa Begin-End para indicar que estamos dibujando: triangulos, romboides, poligonos, etc
Public Sub GLArea1_Draw()
gl.ClearColor(0, 0.5, 0, 1) 'elijo el color con el que voy a borrar la pantalla, en vez de 0-255, aqui los colores van de 0.0-1.0
' el formato es R-G-B-Alpha
gl.Clear(gl.COLOR_BUFFER_BIT) 'ahora borro la pantalla
'establecemos el color hasta nuevo aviso
gl.Color3f(255, 0, 0)
gl.Begin(gl.TRIANGLES) 'dibujamos triangulos hasta nuevo aviso
gl.Vertex2i(0, 0) 'el punto 0,0 no esta en arriba a la izquierda, para openGL esta en el medio!!!
gl.Vertex2i(1, 0) ' 1 no es pixels, las coordenadas van de -1 a 1 , o sea, para eje X, 1 de openGL es GLArea1.width/2
gl.Vertex2i(0, 1) ' idem para la coordenada Y
gl.Color3f(0, 255, 0) 'verde hasta nuevo aviso, como veran puede estar dentro o fuera de los gl.Begin() - gl.End
gl.Vertex2i(0, 0)
gl.Vertex2i(-1, 0)
gl.Vertex2i(0, -1)
gl.End 'esto es similar al Paint, todo se encierra entre Begin-End
gl.Begin(gl.QUADS)
gl.Color3f(0, 0, 255)
gl.Vertex2f(-0.5, -0.5)
gl.Vertex2f(-0.5, 0.5)
gl.Vertex2f(0.5, 0.5)
gl.Vertex2f(0.5, -0.5)
gl.End
End
Prueben con esto.
esto es en el modo directo, tambien hay otras maneras de enviarle los puntos a OpenGL
última edición por tercoIDE el Martes, 15 Diciembre 2015, 18:05; editado 1 vez
|
#4 Martes, 15 Diciembre 2015, 17:53 |
|
|
vuott
Analista Programador
Registrado: Agosto 2013
Mensajes: 2086
Edad: 60 Ubicación:
|
Re: OpenGL. Tutoriales
.... el tutorial del link no me gusta y esta en C
Porque tener miedo ? El C es amigo de gambas, no es como el malvado... Python !
Vamos a ver el secundo ejemplo de aquel enlace:
(se necesita tener instaladas las librerias: libglut.so.3.9.0 - libGL.so - libGLU.so.1.3.1)
Private anguloCuboX As Single ' angulo del cubo
Private anguloCuboY As Single ' "
Private anguloEsfera As Single ' "
Private hazPerspectiva As Integer
Library "libglut:3.9.0"
Private Const GLUT_RGB As Integer = 0
Private Const GLUT_DOUBLE As Integer = 2
' void glutInit(int *argcp, char **argv)
' Initialize the GLUT library.
Private Extern glutInit(pargc As Pointer, argv As Pointer)
' void glutInitDisplayMode(unsigned int mode)
' Sets the initial display mode.
Private Extern glutInitDisplayMode(mode As Integer)
' void glutInitWindowPosition(int x, int y)
' Sets the initial window position.
Private Extern glutInitWindowPosition(x As Integer, y As Integer)
' void glutInitWindowSize(int width, int height)
' Sets the initial window size.
Private Extern glutInitWindowSize(width As Integer, height As Integer)
' int glutCreateWindow(char *name)
' Creates a top-level window.
Private Extern glutCreateWindow(name As String) As Integer
' void glutWireSphere( GLdouble radius, GLint slices, GLint stacks )
' Renders a solid or wireframe sphere respectively.
Private Extern glutWireSphere(radius As Float, slices As Integer, stacks As Integer)
' void glutSwapBuffers(void)
' Swaps the buffers of the current window if double buffered.
Private Extern glutSwapBuffers()
Library "libGL"
Private Const GL_DEPTH_TEST As Integer = &0B71
Private Const GL_COLOR_BUFFER_BIT As Integer = &4000
Private Const GL_DEPTH_BUFFER_BIT As Integer = &100
Private Const GL_QUADS As Integer = 7
Private Const GL_PROJECTION As Integer = &1701
Private Const GL_MODELVIEW As Integer = &1700
' void glClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
' Specify clear values for the color buffers.
Private Extern glClearColor(red As Single, green As Single, blue As Single, alpha As Single)
' void glEnable( GLenum cap )
' Enable or disable server-side GL capabilities.
Private Extern glEnable(cap As Integer)
' void glClear( GLbitfield mask)
' Clear buffers to preset values.
Private Extern glClear(mask As Integer)
' void glLoadIdentity( void )
' Replace the current matrix with the identity matrix.
Private Extern glLoadIdentity()
' void glTranslatef( GLfloat x, GLfloat y, GLfloat z )
' Multiply the current matrix by a translation matrix.
Private Extern glTranslatef(x As Single, y As Single, z As Single)
' void glRotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
' Multiply the current matrix by a rotation matrix.
Private Extern glRotatef(GLangle As Single, x As Single, y As Single, z As Single)
' void glColor3f( GLfloat red, GLfloat green, GLfloat blue )
' Sets the current color.
Private Extern glColor3f(red As Single, green As Single, blue As Single)
' void glBegin( GLenum mode)
' Delimits the vertices of a primitive or a group of like primitives.
Private Extern glBegin(mode As Integer)
' void glVertex3f( GLfloat x, GLfloat y, GLfloat z )
' Specify a vertex.
Private Extern glVertex3f(x As Single, y As Single, z As Single)
' void glEnd( void )
' Delimit the vertices of a primitive or a group of like primitives.
Private Extern glEnd()
' void glFlush( void )
' Force execution of GL commands in finite time.
Private Extern glFlush()
' void glViewport( GLint x, GLint y, GLsizei width, GLsizei height )
' Set the viewport.
Private Extern glViewport(x As Integer, y As Integer, width As Integer, height As Integer)
' void glMatrixMode( GLenum mode )
' Specify which matrix is the current matrix.
Private Extern glMatrixMode(mode As Integer)
' void glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val )
' Multiply the current matrix with an orthographic matrix.
Private Extern glOrtho(GLleft As Float, GLright As Float, bottom As Float, top As Float, near_val As Float, far_val As Float)
Library "libGLU:1.3.1"
' void gluPerspective (GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
' Set up a perspective projection matrix.
Private Extern gluPerspective(fovy As Float, aspect As Float, zNear As Float, zFar As Float)
Public Sub Main()
Dim argcp As Pointer
glutInit(VarPtr(argcp), 0)
glutInitDisplayMode(GLUT_DOUBLE Or GLUT_RGB)
glutInitWindowPosition(100, 100)
glutInitWindowSize(400, 400)
glutCreateWindow("Cubo")
glClearColor(0, 0, 0, 0)
glEnable(GL_DEPTH_TEST)
While True
Display()
Reshape(400, 400)
Wend
End
Private Procedure Display()
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0.0, 0.0, -5.0)
glRotatef(anguloCuboX, 1.0, 0.0, 0.0)
glRotatef(anguloCuboY, 0.0, 1.0, 0.0)
drawCube()
glLoadIdentity()
glTranslatef(0.0, 0.0, -5.0)
glRotatef(anguloEsfera, 0.0, 1.0, 0.0)
glTranslatef(3.0, 0.0, 0.0)
glColor3f(1.0, 1.0, 1.0)
glutWireSphere(0.5, 8, 8)
glFlush()
glutSwapBuffers()
anguloCuboX += 0.1
anguloCuboY += 0.1
anguloEsfera += 0.2
End
Private Procedure drawCube()
glColor3f(1.0, 0.0, 0.0)
glBegin(GL_QUADS) ' cara frontal
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glEnd()
glColor3f(0.0, 1.0, 0.0)
glBegin(GL_QUADS) ' cara trasera
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(-1.0, 1.0, -1.0)
glVertex3f(1.0, 1.0, -1.0)
glEnd()
glColor3f(0.0, 0.0, 1.0)
glBegin(GL_QUADS) ' cara lateral izq
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, -1.0)
glEnd()
glColor3f(1.0, 1.0, 0.0)
glBegin(GL_QUADS) ' cara lateral dcha
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(1.0, 1.0, -1.0)
glVertex3f(1.0, 1.0, 1.0)
glEnd()
glColor3f(0.0, 1.0, 1.0)
glBegin(GL_QUADS) ' cara arriba
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(1.0, 1.0, 1.0)
glVertex3f(1.0, 1.0, -1.0)
glVertex3f(-1.0, 1.0, -1.0)
glEnd()
glColor3f(1.0, 0.0, 1.0)
glBegin(GL_QUADS) ' cara abajo
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
glEnd()
End
Private Procedure Reshape(width As Integer, height As Integer)
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
If hazPerspectiva Then
gluPerspective(60.0, CSingle(width) / CSingle(height), 1.0, 20.0)
Else
glOrtho(-4, 4, -4, 4, 1, 10)
Endif
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
End
'' Referencias:
' https://www.opengl.org/documentation/specs/glut/spec3/spec3.html
' https://www.opengl.org/sdk/docs/man/html/indexflat.php
' http://sabia.tic.udc.es/gc/Tutorial%20OpenGL/index.htm
última edición por vuott el Martes, 15 Diciembre 2015, 18:48; editado 1 vez
|
#5 Martes, 15 Diciembre 2015, 18:44 |
|
|
tercoIDE
Analista Programador
Registrado: Noviembre 2013
Mensajes: 713
Edad: 54
|
Re: OpenGL. Tutoriales
sin animos de ofender...tu codigo ni funciona, ni esta explicado
como pueden aprender de esa manera?
|
#6 Martes, 15 Diciembre 2015, 19:47 |
|
|
Shell
Analista Programador
Registrado: Marzo 2010
Mensajes: 5278
Edad: 53 Ubicación: Al otro lado de la pantalla
|
Re: OpenGL. Tutoriales
tercoIDE:
Citar:
Bueno, ya que me has puesto presión, y visto y considerando que el tutorial del link no me gusta y esta en C
No era esa mi intención. Pero tu ejemplo ha estado bien. No te gusta C?!?....blasfemia!, herejía!.
Acabas de alegrarle el día a Vuott!.
Entonces OpenGL respeta las coordenadas cartesianas!!!.
Ok, prepararé cuestiones. Fáciles, ya que no tengo mucha idea.
Vuott:
Citar:
Porque tener miedo ? El C es amigo de gambas, no es como el malvado... Python !
Ejem, ejem..cof, cof.
Cuando comencé a crear el proyecto pensaba que iba a ser como en el de tercoIDE.
Y es un proyecto de consola.
Vaya cantidad de funciones externas que tienes que declarar!..
¿ Cómo haces para recordar todo esos nombres ?. Creo que en parte es lo más complejo que puede tener C.
Acordarte de todo eso, buscar los nombres de esas funciones.
Ah, el ejemplo también muy bien.
Gracias a los dos.
Ejem, ejem...
Citar:
sin animos de ofender...tu código ni funciona, ni esta explicado
Si funciona....cachis!. jajaja. No olvides instalar las librerías.
Lo que si veo es que para salir del ejemplo hay que pararlo desde el menú de proyecto!.
Por mucho que le doy a cerrar en la ventana no lo hace. Eso si es raro.
Vuott, ¿ Sabes por que ocurre eso ?.
Citar:
como pueden aprender de esa manera?
Eso dependerá de cada programador. El ha hecho que funcione el código y lo ha traducido muy rápidamente.
No sabemos si Vuott conoce el tema. Pero lo que si ha demostrado es que sabe hacerlo funcionar...y darle
importancia a C.
Saludos
=================== Gambas Básico
"No es un bug, es una característica no documentada"
última edición por Shell el Martes, 15 Diciembre 2015, 20:06; editado 2 veces
|
#7 Martes, 15 Diciembre 2015, 19:50 |
|
|
jsbsan
Analista Programador
Registrado: Septiembre 2009
Mensajes: 4175
Edad: 51 Ubicación: dos hermanas, sevilla
|
Re: OpenGL. Tutoriales
Yo prefiero verlo explilcado en gambas....
Veo (para empezar) que con C es mucho más largo y menos intuitivo el código que hay que escribir
|
#8 Martes, 15 Diciembre 2015, 20:52 |
|
|
tercoIDE
Analista Programador
Registrado: Noviembre 2013
Mensajes: 713
Edad: 54
|
Re: OpenGL. Tutoriales
por eso, para voy a
si puedo
gl.ClearColor( red, green, blue, alpha)
|
#9 Martes, 15 Diciembre 2015, 22:25 |
|
|
vuott
Analista Programador
Registrado: Agosto 2013
Mensajes: 2086
Edad: 60 Ubicación:
|
Re: OpenGL. Tutoriales
última edición por vuott el Jueves, 17 Diciembre 2015, 02:49; editado 2 veces
|
#10 Martes, 15 Diciembre 2015, 23:39 |
|
|
|
Temas parecidos
Temas parecidos
|
Página 1 de 6
|
Usuarios navegando en este tema: 0 registrados, 0 ocultos y 1 invitado Usuarios registrados conectados: Ninguno
|
No puede crear mensajes No puede responder temas No puede editar sus mensajes No puede borrar sus mensajes No puede votar en encuestas No puede adjuntar archivos Puede descargar archivos No puede publicar eventos en el calendario
|
|
|
|
|