主题:  机器猫:我的openglXTRA试用手记贴上

酷狗

职务:版主
等级:2
金币:10.0
发贴:610
#12000/10/31 11:59:43
这阵子比较忙,加之人惰性十足所以没有认领什么XTRA的测试,但机器猫发了一份openglXTRA给我,没法如果不写太对不住他了。
对于opengl我不会,但还好会一点C(别向我扔烂苹果),所以带了一本书回家,边查边学边写,这也是我喜欢的。openlXTRA安装在D8的XTRAS下的RavOpenGL中,形成三个目录,其中Examples里带有三个例子。好了就拿每一个例子HARDWAREDEMO.DIR开刀吧!
HARDWAREDEMO.DIR显示了一个带纹理的旋转正方形。我想先分析一下代码,再加一点什么。
在on startMovie之前是一堆全局变量的设置与初始化。
on startMovie

-- Load the OpenGL Script Xtra
Set OpenGL = new (xtra "RAVOPENGL")
if (objectp(OpenGL) = false) then
put "Unable To Load OpenGL Xtra"
exit
end if
--调用了ravopengl,返回一个opengl对象,这个对象以后一直要用,RavWare OpenGL Xtra的函数与opengl差不多只是大多数函数的第一个参数要加上new返回的对象。如opengl:void glRectd(GLdouble x1,GLdouble y1,GLdouble x2,GLdouble y2) | RavWare OpenGL Xtra:glRectd(object XtraInstance ,float x1 ,float y1 ,float x2 ,float y2)。

-- Do Registration
if (RavRegisterOpenGL(OpenGL,"BAD-SERIALNUMBER") = #false) then
put "Failed to Register Xtra"
exit
end if

-- Set the OpenGL Constants and Texturing List
InitVariables

-- Load GL DLL
if (RavLoadGL(OpenGL,"","") = #false) then
put "Failed to Load OpenGL DLL's"
exit
end if

-- Create the OpenGL Buffer
--MainBufferID = RavCreateBuffer(OpenGL,MainBufferWidth,MainBufferHeight,32,32)
建立opengl缓冲,用来做动画。
MainBufferID = RavCreateHardWareBuffer(OpenGL,0,0,400,400,32,32)

-- Set the Main Drawing Buffer
RavSetProp(OpenGL,#ravCurrentGLBuffer,MainBufferID)

-- Set the Sharing Mode Between Script and Sprite Xtras
Set DC_ID = RavGetBufferProp(OpenGL,MainBufferID,#ravGC)
--RavShareBuffer(sprite 1,DC_ID,#true)

-- Do the initial Drawing * Time Consuming *
SetupRendering OpenGL
ChangeSize OpenGL,MainBufferWidth,MainBufferHeight


RenderScene OpenGL
这个函数以后要定以的,用来绘制画面。
put glGetString(openGL, GL_RENDERER)


--RavCreateOpenGLWindow(openGL)
end
------------------------------------------------
on stopMovie
.....
end
释放opengl。on startmovie与on stopmovie中的内容可以作用公式套用
-------------------------------------------------
on exitFrame
go to the frame
end

on enterFrame
fRot = fRot + 3.0
if(fRot > 360.0) then
fRot = -.0

xRot = xRot + 3.0
if(xRot > 360.0) then
xRot = 0.0
end if
yRot = yRot + 3.0
if(yRot > 360.0) then
yRot = 0.0
end if
end if
spin=spin+2.0
if(spin > 360.0) then
spin = -.0
end if
RenderScene(OpenGL)
end
在enterframe中不断改变frot,xrot,yrot的值,在Renderscene函数中调用DrawCube(OpenGL)来绘制正方形。DrawCube(OpenGL)是跟据frot,xrot,yrot来绘制六个面的,所以形成了动画。
-----------------------------------------------
on LoadTexture --用来设置纹理
on ChangeSize --设定取景,模型变换,投景变换,视见区变换
on SetupRendering
on DrawCube --绘制六面体
有一些函数我也不懂。不敢乱分析。
我们现在来加一点东西。最简单的加一块旋转的方块。
为了让方块转起来所以要设一个变量spin在程式的顶部写上

global spin

在函数 InitVariables 中初始化

set spin=0.0

在on enterFrame 中让spin每过一frame转动2

spin=spin+2.0
if(spin > 360.0) then
spin = -.0
end if

前面已经讲过了所有的绘图过程是调用RenderScene函数来完成的,所以我们的绘制方块也要放在RenderScene中。请在
glFlush(OpenGL)
RavSwapBuffers(OpenGL) --形成动画的关键所在,双缓冲存储机制。
前插入
glColor3f(OpenGL,1.0,0.0,1.0) --设置Color为品红
glRotatef(OpenGL,spin,0.0,0.0,1.0)对当前矩阵变换
glRectf(OpenGL,-25.0,-25.0,25.0,25.0)绘制矩形
好了全动起来了,动得乱七八糟,没有办法。我说过了我不会opengl:)。
对了在RenderScene函数glClear(OpenGL,GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT)后加上一行
glColor3f(OpenGL,1.0,1.0,1.0)
这样六面体用白色绘制。

大家不要骂我。现在是31日的0:58,我要睡了,明天上班。
附上源码:二块旋转的box。可以套用,只要修改一下RenderScene函数。
以后我会把我的学习过程写出来,与大家一起用opengl。

openglXTRa在inster下。单击后在cast中生成opengl演员拖入score.

----------------------------------------------------------------------------
-- Texture Binding Example For RavWare OpenGL Xtra
-- Also Demonstrates how to share a Display Buffer
-- Based on Articles From the Gamasutra WebSite
--
--
----------------------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------------------
global OpenGL
global MainBufferID
global tList
global tListName
global MainBufferWidth
global MainBufferHeight
global r
global g
global b
global spin
----------------------------------------------------------------------------
-- OpenGL Constants
----------------------------------------------------------------------------
global GL_MODELVIEW
global GL_PROJECTION
global GL_TEXTURE_2D
global GL_TEXTURE_WRAP_S
global GL_TEXTURE_WRAP_T
global GL_REPEAT
global GL_NEAREST
global GL_LINEAR
global GL_TEXTURE_MAG_FILTER
global GL_TEXTURE_MIN_FILTER
global GL_DEPTH_TEST
global GL_CULL_FACE
global GL_CCW
global GL_QUADS
global GL_COLOR_BUFFER_BIT
global GL_DEPTH_BUFFER_BIT
global GL_SRC_ALPHA
global GL_ONE_MINUS_SRC_ALPHA
global GL_BLEND
global GL_RGB
global GL_UNSIGNED_BYTE
global GL_BGR_EXT
global GL_UNPACK_ALIGNMENT
global GL_UNPACK_ROW_LENGTH
global GL_UNPACK_SKIP_ROWS
global GL_UNPACK_SKIP_PIXELS
----------------------------------------------------------------------------
-- Initialize OpenGL Constants and Internal Variables
----------------------------------------------------------------------------
on InitVariables
-- Set all the Constants
Set GL_MODELVIEW = 5888
Set GL_PROJECTION = 5889
Set GL_TEXTURE_2D = 3553
Set GL_TEXTURE_WRAP_S = 10242
Set GL_TEXTURE_WRAP_T = 10243
Set GL_REPEAT = 10497
Set GL_NEAREST = 9728
Set GL_LINEAR = 9729
Set GL_TEXTURE_MAG_FILTER = 10240
Set GL_TEXTURE_MIN_FILTER = 10241
Set GL_DEPTH_TEST = 2929
Set GL_CULL_FACE = 2884
Set GL_CCW = 2305
Set GL_QUADS = 7
Set GL_COLOR_BUFFER_BIT = 16384
Set GL_DEPTH_BUFFER_BIT = 256
Set GL_SRC_ALPHA = 770
Set GL_ONE_MINUS_SRC_ALPHA = 771
Set GL_BLEND = 3042
Set GL_RGB = 6407
Set GL_BGR_EXT = 32992
Set GL_UNSIGNED_BYTE = 5121
Set GL_UNPACK_ALIGNMENT = 3317
Set GL_UNPACK_ROW_LENGTH = 3314
Set GL_UNPACK_SKIP_ROWS = 3315
Set GL_UNPACK_SKIP_PIXELS = 3316
set r=0.0
set g=0.0
set b=0.0
set spin=0.0
Set MainBufferWidth = 400
Set MainBufferHeight = 400

-- Setup the Internal Variables
if (voidp(tList) = true) then
Set tList = [0,0,0,0,0,0,0]
end if

Set tListName = ["Cup","Couple","Wolverine","RavWare","OpenGLLogo","D7Logo","Floor"]

Set xRot = float(0.0)
Set yRot = float(0.0)
Set fRot = float(0.0)
end

----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
on startMovie

-- Load the OpenGL Script Xtra
Set OpenGL = new (xtra "RAVOPENGL")
if (objectp(OpenGL) = false) then
put "Unable To Load OpenGL Xtra"
exit
end if

-- Do Registration
if (RavRegisterOpenGL(OpenGL,"BAD-SERIALNUMBER") = #false) then
put "Failed to Register Xtra"
exit
end if

-- Set the OpenGL Constants and Texturing List
InitVariables

-- Load GL DLL
if (RavLoadGL(OpenGL,"","") = #false) then
put "Failed to Load OpenGL DLL's"
exit
end if

-- Create the OpenGL Buffer
MainBufferID = RavCreateBuffer(OpenGL,MainBufferWidth,MainBufferHeight,32,32)

-- Set the Main Drawing Buffer
RavSetProp(OpenGL,#ravCurrentGLBuffer,MainBufferID)

-- Set the Sharing Mode Between Script and Sprite Xtras
Set DC_ID = RavGetBufferProp(OpenGL,MainBufferID,#ravGC)
RavShareBuffer(sprite 1,DC_ID,#true)

-- Do the initial Drawing * Time Consuming *
SetupRendering OpenGL
ChangeSize OpenGL,MainBufferWidth,MainBufferHeight
RenderScene OpenGL

end

----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
on stopMovie

if (objectp(OpenGL) = false) then
exit
end if

RavShareBuffer(sprite 1,0,#false)

if (voidp(tList) = false) then
glDeleteTextures(OpenGL,7, tList)
end if

-- Unload the OpenGL Script Xtra
Set OpenGL = 0

end

----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
on exitFrame
-- Increment the rotation angle in 5 Degree Increments

-- Call OpenGL drawing code and swap to foreground
r = r +0.1
if(r > 1.0) then
r = 0.0
end if
g = g +0.1
if(r > 1.0) then
g = 0.0
end if
b = b +0.1
if(b > 1.0) then
b = 0.0
end if
spin=spin+2.0
if(spin > 360.0) then
spin = -.0
end if
RenderScene(OpenGL)

-- Copy Data to Display Buffer , if Not in Share Buffer Mode
-- Set DC_ID = RavGetBufferProp(OpenGL,MainBufferID,#ravGC)
-- RavCopyImageBuffer(sprite 1,DC_ID)

-- go to the frame
end




----------------------------------------------------------------------------
-- Convert the Bitmap Cast Members to OpenGL Textures
----------------------------------------------------------------------------
on LoadTexture OpenGL,TextureName

Set TextureID = RavCreateTextureFromName(OpenGL,TextureName,GL_BGR_EXT,1,0)

-- This is specific to the binary format of the data read in.
glPixelStorei(OpenGL,GL_UNPACK_ALIGNMENT, 1)
glPixelStorei(OpenGL,GL_UNPACK_ROW_LENGTH, 0)
glPixelStorei(OpenGL,GL_UNPACK_SKIP_ROWS, 0)
glPixelStorei(OpenGL,GL_UNPACK_SKIP_PIXELS, 0)

Set TextureWidth = RavGetTextureProp(OpenGL,TextureID,#width)
Set TextureHeight = RavGetTextureProp(OpenGL,TextureID,#height)

glTexImage2D(OpenGL,GL_TEXTURE_2D, 0, 3, TextureWidth, TextureHeight,0,GL_BGR_EXT,GL_UNSIGNED_BYTE,TextureID)

RavDestroyTexture OpenGL,TextureID

end

----------------------------------------------------------------------------
-- Change viewing volume and viewport. Called when window is resized
----------------------------------------------------------------------------
on ChangeSize OpenGL,w,h

Set fAspect = float(0.0)
Set nRange = float(100.00)

-- Prevent a divide by zero
if (h = 0) then
h = 1
end if

fAspect = float(w) / float(h)

-- Set Viewport to window dimensions
glViewPort(OpenGL,0,0,w,h)
glMatrixMode(OpenGL,GL_PROJECTION)

-- Reset coordinate system
glLoadIdentity(OpenGL)

-- Setup perspective for viewing
gluPerspective(OpenGL,45.0,fAspect,60.0,300.0)

-- Viewing transformation
glMatrixMode(OpenGL,GL_MODELVIEW)
glLoadIdentity(OpenGL)
glTranslatef(OpenGL,0.0, 40.0, -200.0)
end

----------------------------------------------------------------------------
-- Initialize the Rendering Context - Slow Function
----------------------------------------------------------------------------
on SetupRendering OpenGL

-- Generate 7 texture object ID's
tList = glGenTextures(OpenGL,7)

-- Do Each Side of the Cube
repeat with i = 1 to 7

glBindTexture(OpenGL,GL_TEXTURE_2D, tList[i])

-- Set Texture mapping parameters
glTexParameteri(OpenGL,GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(OpenGL,GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(OpenGL,GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(OpenGL,GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR)
LoadTexture OpenGL,tListName[i]

end repeat

-- Enable texturing
glEnable(OpenGL,GL_TEXTURE_2D)

glEnable(OpenGL,GL_DEPTH_TEST) -- Hidden surface removal
glEnable(OpenGL,GL_CULL_FACE) -- Do not draw inside of cube
glFrontFace(OpenGL,GL_CCW) -- Counter clock-wise polygons face out

-- BackGround Color
glClearColor(OpenGL,1.0, 0.5, 0.5, 0.0)
end

----------------------------------------------------------------------------
-- Draw the cube by loading each texture individually, and drawing the
-- corresponding side.
----------------------------------------------------------------------------

on RenderScene OpenGL

-- Clear the window with current clearing color
-- glClear(OpenGL,GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glClear(OpenGL,GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT)

glColor3f(Opengl,r,g,b)
glRecti(Opengl,-25,-25,25,25)
-- Save matrix state and do the rotation, then draw the cube
glColor3f(OpenGL,1.0,0.0,1.0)
glRotatef(OpenGL,spin,0.0,0.0,1.0)
glRectf(OpenGL,-25.0,-25.0,25.0,25.0)
glFlush(OpenGL)
end

----------------------------------------------------------------------------