//Draws all the sprites in the supplied slice func (sheet SpriteSheet) Draw(sprites []*Sprite) { gl.Enable(gl.TEXTURE_2D) sheet.texture.Bind(gl.TEXTURE_2D) for _, sprite := range sprites { gl.Begin(gl.TRIANGLE_STRIP) { gl.TexCoord2f(sprite.left, sprite.bottom) gl.Vertex2f(sprite.X, sprite.Y) gl.TexCoord2f(sprite.left, sprite.top) gl.Vertex2f(sprite.X, sprite.Y+sprite.H) gl.TexCoord2f(sprite.right, sprite.bottom) gl.Vertex2f(sprite.X+sprite.W, sprite.Y) gl.TexCoord2f(sprite.right, sprite.top) gl.Vertex2f(sprite.X+sprite.W, sprite.Y+sprite.H) } gl.End() } sheet.texture.Unbind(gl.TEXTURE_2D) gl.Disable(gl.TEXTURE_2D) }
func (t *Texture) PreloadRender() { t.Bind() gl.Begin(gl.QUADS) gl.TexCoord2f(0, 1) gl.Vertex3f(0, 0, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(0, 0, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(0, 0, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(0, 0, 1) gl.End() }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.LoadIdentity() // 重置当前矩阵 texture.Bind(gl.TEXTURE_2D) var x_m, y_m, z_m, u_m, v_m float32 xtrans := -xpos ztrans := -zpos ytrans := -walkbias - 0.25 sceneroty := 360.0 - yrot var numtriangles int gl.Rotatef(lookupdown, 1.0, 0, 0) gl.Rotatef(sceneroty, 0, 1.0, 0) gl.Translatef(xtrans, ytrans, ztrans) numtriangles = sector1.numtriangles // Process Each Triangle for loop_m := 0; loop_m < numtriangles; loop_m++ { gl.Begin(gl.TRIANGLES) gl.Normal3f(0.0, 0.0, 1.0) x_m = sector1.triangles[loop_m].vertex[0].x y_m = sector1.triangles[loop_m].vertex[0].y z_m = sector1.triangles[loop_m].vertex[0].z u_m = sector1.triangles[loop_m].vertex[0].u v_m = sector1.triangles[loop_m].vertex[0].v gl.TexCoord2f(u_m, v_m) gl.Vertex3f(x_m, y_m, z_m) x_m = sector1.triangles[loop_m].vertex[1].x y_m = sector1.triangles[loop_m].vertex[1].y z_m = sector1.triangles[loop_m].vertex[1].z u_m = sector1.triangles[loop_m].vertex[1].u v_m = sector1.triangles[loop_m].vertex[1].v gl.TexCoord2f(u_m, v_m) gl.Vertex3f(x_m, y_m, z_m) x_m = sector1.triangles[loop_m].vertex[2].x y_m = sector1.triangles[loop_m].vertex[2].y z_m = sector1.triangles[loop_m].vertex[2].z u_m = sector1.triangles[loop_m].vertex[2].u v_m = sector1.triangles[loop_m].vertex[2].v gl.TexCoord2f(u_m, v_m) gl.Vertex3f(x_m, y_m, z_m) gl.End() } }
func (cube *Cube) Render() { x, y, z := cube.Position.X, cube.Position.Y, cube.Position.Z gl.Begin(gl.QUADS) gl.Color3d(0.5-(x/50), 0.5-(y/50), 0.5-(z/50)) // Front Side gl.TexCoord2f(0, 0) gl.Vertex3d(x-0.5, y-0.5, z+0.5) gl.TexCoord2f(1, 0) gl.Vertex3d(x+0.5, y-0.5, z+0.5) gl.TexCoord2f(1, 1) gl.Vertex3d(x+0.5, y+0.5, z+0.5) gl.TexCoord2f(0, 1) gl.Vertex3d(x-0.5, y+0.5, z+0.5) // Left Side gl.Color3d(0.5-(x/20), 0.5-(y/20), 0.5-(z/20)) gl.TexCoord2f(0, 0) gl.Vertex3d(x-0.5, y-0.5, z-0.5) gl.TexCoord2f(1, 0) gl.Vertex3d(x-0.5, y-0.5, z+0.5) gl.TexCoord2f(1, 1) gl.Vertex3d(x-0.5, y+0.5, z+0.5) gl.TexCoord2f(0, 1) gl.Vertex3d(x-0.5, y+0.5, z-0.5) gl.End() }
func RenderAtlas(a Atlas) { a.Bind() xratio := float32(a.Width()) / float32(a.Height()) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 1) gl.Vertex3f(-0.5, -0.5, 1) gl.TexCoord2f(1, 1) gl.Vertex3f((xratio)-0.5, -0.5, 1) gl.TexCoord2f(1, 0) gl.Vertex3f((xratio)-0.5, 0.5, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(-0.5, 0.5, 1) gl.End() }
func (t *Texture) Render() { t.Bind() xratio := float32(t.width) / float32(t.height) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 1) gl.Vertex3f(-0.5, -0.5, 1) gl.TexCoord2f(1, 1) gl.Vertex3f((xratio)-0.5, -0.5, 1) gl.TexCoord2f(1, 0) gl.Vertex3f((xratio)-0.5, 0.5, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(-0.5, 0.5, 1) gl.End() }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) texture.Bind(gl.TEXTURE_2D) for loop = 0; loop < num; loop++ { gl.LoadIdentity() // 绘制每颗星星之前,重置模型观察矩阵 gl.Translatef(0.0, 0.0, zoom) // 深入屏幕里面 gl.Rotatef(tilt, 1.0, 0.0, 0.0) // 倾斜视角 gl.Rotatef(ztilt, 0.0, 0.0, 1.0) // 倾斜视角 gl.Rotatef(star[loop].angle, 0.0, 1.0, 0.0) // 旋转至当前所画星星的角度 gl.Translatef(star[loop].dist, 0.0, 0.0) // 沿X轴正向移动 gl.Rotatef(-star[loop].angle, 0.0, 1.0, 0.0) // 取消当前星星的角度 gl.Rotatef(-ztilt, 0.0, 0.0, 1.0) // 取消屏幕倾斜 gl.Rotatef(-tilt, 1.0, 0.0, 0.0) // 取消屏幕倾斜 if twinkle { // 启用闪烁效果 // 使用byte型数值指定一个颜色 gl.Color4ub(star[(num-loop)-1].r, star[(num-loop)-1].g, star[(num-loop)-1].b, 255) gl.Begin(gl.QUADS) // 开始绘制纹理映射过的四边形 gl.TexCoord2f(0.0, 0.0) gl.Vertex3f(-1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 0.0) gl.Vertex3f(1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 1.0) gl.Vertex3f(1.0, 1.0, 0.0) gl.TexCoord2f(0.0, 1.0) gl.Vertex3f(-1.0, 1.0, 0.0) gl.End() // 四边形绘制结束 } gl.Rotatef(spin, 0.0, 0.0, 1.0) // 绕z轴旋转星星 // 使用byte型数值指定一个颜色 gl.Color4ub(star[loop].r, star[loop].g, star[loop].b, 255) gl.Begin(gl.QUADS) // 开始绘制纹理映射过的四边形 gl.TexCoord2f(0.0, 0.0) gl.Vertex3f(-1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 0.0) gl.Vertex3f(1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 1.0) gl.Vertex3f(1.0, 1.0, 0.0) gl.TexCoord2f(0.0, 1.0) gl.Vertex3f(-1.0, 1.0, 0.0) gl.End() // 四边形绘制结束 spin += 0.01 // 星星的公转 star[loop].angle += float32(loop) / num // 改变星星的自转角度 star[loop].dist -= 0.01 // 改变星星离中心的距离 if star[loop].dist < 0.0 { // 星星到达中心了么 star[loop].dist += 5 // 往外移5个单位 //fmt.Println(loop, star[loop].dist) star[loop].r = uint8(rand.Int() % 256) // 赋一个新红色分量 star[loop].g = uint8(rand.Int() % 256) // 赋一个新绿色分量 star[loop].b = uint8(rand.Int() % 256) // 赋一个新蓝色分量 } } }
func drawQuad(x, y, w, h int, u, v, u2, v2 float32) { gl.Begin(gl.QUADS) gl.TexCoord2f(float32(u), float32(v)) gl.Vertex2i(int(x), int(y)) gl.TexCoord2f(float32(u2), float32(v)) gl.Vertex2i(int(x+w), int(y)) gl.TexCoord2f(float32(u2), float32(v2)) gl.Vertex2i(int(x+w), int(y+h)) gl.TexCoord2f(float32(u), float32(v2)) gl.Vertex2i(int(x), int(y+h)) gl.End() }
func (h *Hex) Render(alpha float32, drawFromCenter bool) { gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) if h.Kind == HexFlower { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) starTex.Bind(gl.TEXTURE_2D) } else { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) hexTex.Bind(gl.TEXTURE_2D) gl.GetError() var r, g, b uint8 r = uint8(colors.Colors[h.Kind-1][0]) g = uint8(colors.Colors[h.Kind-1][1]) b = uint8(colors.Colors[h.Kind-1][2]) if alpha < 1 { gl.Color4ub(r, g, b, uint8(alpha*255)) } else { gl.Color3ub(r, g, b) } } gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) if drawFromCenter { gl.Vertex2i(HEX_WIDTH/2, HEX_HEIGHT/2) } else { gl.Vertex2i(HEX_WIDTH, HEX_HEIGHT) } gl.TexCoord2f(0, 1) if drawFromCenter { gl.Vertex2i(HEX_WIDTH/2, -HEX_HEIGHT/2) } else { gl.Vertex2i(HEX_WIDTH, 0) } gl.TexCoord2f(1, 1) if drawFromCenter { gl.Vertex2i(-HEX_WIDTH/2, -HEX_HEIGHT/2) } else { gl.Vertex2i(0, 0) } gl.TexCoord2f(1, 0) if drawFromCenter { gl.Vertex2i(-HEX_WIDTH/2, HEX_HEIGHT/2) } else { gl.Vertex2i(0, HEX_HEIGHT) } gl.End() }
func (v *Video) Render() { for running { select { case dimensions := <-v.resize: v.ResizeEvent(dimensions[0], dimensions[1]) case val := <-v.tick: slice := make([]uint8, len(val)*3) for i := 0; i < len(val); i = i + 1 { slice[i*3+0] = (uint8)((val[i] >> 16) & 0xff) slice[i*3+1] = (uint8)((val[i] >> 8) & 0xff) slice[i*3+2] = (uint8)((val[i]) & 0xff) } gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) v.tex.Bind(gl.TEXTURE_2D) if ppu.OverscanEnabled { gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 240, 224, 0, gl.RGB, gl.UNSIGNED_BYTE, slice) } else { gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 256, 240, 0, gl.RGB, gl.UNSIGNED_BYTE, slice) } gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.Begin(gl.QUADS) gl.TexCoord2f(0.0, 1.0) gl.Vertex3f(-1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 1.0) gl.Vertex3f(1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 0.0) gl.Vertex3f(1.0, 1.0, 0.0) gl.TexCoord2f(0.0, 0.0) gl.Vertex3f(-1.0, 1.0, 0.0) gl.End() if v.screen != nil { sdl.GL_SwapBuffers() } v.frametick <- true } } }
func drawHex(x, y, kind int, alpha float32) { if kind == 6 { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) starTex.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(x, y) gl.TexCoord2f(0, 1) gl.Vertex2i(x, y+HEX_HEIGHT) gl.TexCoord2f(1, 1) gl.Vertex2i(x+HEX_WIDTH, y+HEX_HEIGHT) gl.TexCoord2f(1, 0) gl.Vertex2i(x+HEX_WIDTH, y) gl.End() } else { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) var r, g, b float32 switch kind { case 0: r = 1 case 1: g = 1 case 2: b = 1 case 3: r = 1 g = 1 case 4: r = 1 b = 1 case 5: g = 1 - 222/255 b = 1 } hexTex.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) if alpha < 1 { gl.Color4f(r, g, b, alpha) } else { gl.Color3f(r, g, b) } gl.TexCoord2f(0, 0) gl.Vertex2i(x, y) gl.TexCoord2f(0, 1) gl.Vertex2i(x, y+HEX_HEIGHT) gl.TexCoord2f(1, 1) gl.Vertex2i(x+HEX_WIDTH, y+HEX_HEIGHT) gl.TexCoord2f(1, 0) gl.Vertex2i(x+HEX_WIDTH, y) gl.End() } }
func TexturedQuad(t *glh.Texture, x, y, w, h int) { glh.With(t, func() { gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Color4f(255.0, 255.0, 255.0, 1.0) gl.Begin(gl.TRIANGLE_FAN) gl.TexCoord2f(0, 0) gl.Vertex2i(x, y) gl.TexCoord2f(1, 0) gl.Vertex2i(x+w, y) gl.TexCoord2f(1, 1) gl.Vertex2i(x+w, y+h) gl.TexCoord2f(0, 1) gl.Vertex2i(x, y+h) gl.End() }) }
func (v *Video) Render(frame []byte, frame_w int, frame_h int) { gl.Clear(gl.COLOR_BUFFER_BIT) v.Texture.Bind(gl.TEXTURE_2D) gl.TexImage2D(gl.TEXTURE_2D, 0, 3, frame_w, frame_h, 0, gl.RGB, gl.UNSIGNED_BYTE, frame) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.Begin(gl.QUADS) gl.TexCoord2f(0.0, 1.0) gl.Vertex3f(-1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 1.0) gl.Vertex3f(1.0, -1.0, 0.0) gl.TexCoord2f(1.0, 0.0) gl.Vertex3f(1.0, 1.0, 0.0) gl.TexCoord2f(0.0, 0.0) gl.Vertex3f(-1.0, 1.0, 0.0) gl.End() glfw.SwapBuffers() }
// texcoord defines vertex texture coordinates. // Used in classic render mode. func (a *Attr) texcoord(i int) { i *= a.size switch a.size { case 1: switch v := a.data.(type) { case []int16: gl.TexCoord1s(v[i]) case []int32: gl.TexCoord1i(int(v[i])) case []float32: gl.TexCoord1f(v[i]) case []float64: gl.TexCoord1d(v[i]) } case 2: switch v := a.data.(type) { case []int16: gl.TexCoord2s(v[i], v[i+1]) case []int32: gl.TexCoord2i(int(v[i]), int(v[i+1])) case []float32: gl.TexCoord2f(v[i], v[i+1]) case []float64: gl.TexCoord2d(v[i], v[i+1]) } case 3: switch v := a.data.(type) { case []int16: gl.TexCoord3s(v[i], v[i+1], v[i+2]) case []int32: gl.TexCoord3i(int(v[i]), int(v[i+1]), int(v[i+2])) case []float32: gl.TexCoord3f(v[i], v[i+1], v[i+2]) case []float64: gl.TexCoord3d(v[i], v[i+1], v[i+2]) } case 4: switch v := a.data.(type) { case []int16: gl.TexCoord4s(v[i], v[i+1], v[i+2], v[i+3]) case []int32: gl.TexCoord4i(int(v[i]), int(v[i+1]), int(v[i+2]), int(v[i+3])) case []float32: gl.TexCoord4f(v[i], v[i+1], v[i+2], v[i+3]) case []float64: gl.TexCoord4d(v[i], v[i+1], v[i+2], v[i+3]) } } }
func main() { err := initGL() if err != nil { log.Printf("InitGL: %v", err) return } defer glfw.Terminate() // Create our texture atlas. atlas := glh.NewTextureAtlas(AtlasSize, AtlasSize, 4) defer atlas.Release() // Fill the altas with image data. fillAtlas(atlas) // Display the atlas texture on a quad, so we can see // what it looks like. for glfw.WindowParam(glfw.Opened) > 0 { gl.Clear(gl.COLOR_BUFFER_BIT) // Bind the atlas texture and render a quad with it. atlas.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2f(0, 0) gl.TexCoord2f(1, 0) gl.Vertex2f(AtlasSize, 0) gl.TexCoord2f(1, 1) gl.Vertex2f(AtlasSize, AtlasSize) gl.TexCoord2f(0, 1) gl.Vertex2f(0, AtlasSize) gl.End() atlas.Unbind(gl.TEXTURE_2D) glfw.SwapBuffers() } }
func drawBorderAtXY(x, y float32, reverse int) { if x <= 80 || y <= 80 { return } gl.PushMatrix() gl.Translatef(x, y, 0) if reverse == 1 { gl.Rotatef(60, 0, 0, 1) } gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) borderTex.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(-38, -38) gl.TexCoord2f(0, 1) gl.Vertex2i(-38, 38) gl.TexCoord2f(1, 1) gl.Vertex2i(38, 38) gl.TexCoord2f(1, 0) gl.Vertex2i(38, -38) gl.End() gl.PopMatrix() }
func (r *RenderTarget) Render(verts []Vertex, primType PrimitiveType, states RenderStates) { // Nothing to draw? if len(verts) == 0 { return } // First set the persistent OpenGL states if it's the very first call if !r.glStatesSet { r.resetGlStates() } // Check if the vertex count is low enough so that we can pre-transform them useVertexCache := len(verts) <= vertexCacheSize if useVertexCache { // Pre-transform the vertices and store them into the vertex cache for i := 0; i < len(verts); i++ { r.vpCache[i] = states.Transform.TransformPoint(verts[i].Pos) r.vcCache[i] = verts[i].Color r.vtCache[i] = verts[i].TexCoords } // Since vertices are transformed, we must use an identity transform to render them if !r.useVertexCache { r.applyTransform(IdentityTransform()) } } else { r.applyTransform(states.Transform) } // Apply the view if r.viewChanged { r.applyCurrentView() } // Apply the blend mode if states.BlendMode != r.lastBlendMode { r.applyBlendMode(states.BlendMode) } // Apply the texture var textureId uint64 if states.Texture != nil { textureId = states.Texture.cacheId } if textureId != r.lastTextureId { r.applyTexture(states.Texture) } // Apply the shader // TODO /*if states.shader { applyShader(states.shader); }*/ // ######################################### if !useVertexCache { // Find the OpenGL primitive type modes := [...]gl.GLenum{gl.POINTS, gl.LINES, gl.LINE_STRIP, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.QUADS} mode := modes[primType] gl.Begin(mode) for i, _ := range verts { gl.TexCoord2f(verts[i].TexCoords.X, verts[i].TexCoords.Y) gl.Color4f(float32(verts[i].Color.R)/255, float32(verts[i].Color.G)/255, float32(verts[i].Color.B)/255, float32(verts[i].Color.A)/255) gl.Vertex2f(verts[i].Pos.X, verts[i].Pos.Y) } gl.End() } // ######################################### // Setup the pointers to the vertices' components // ... and if we already used it previously, we don't need to set the pointers again if useVertexCache { if !r.useVertexCache { gl.VertexPointer(2, gl.FLOAT, 0, r.vpCache[:]) gl.ColorPointer(4, gl.UNSIGNED_BYTE, 0, r.vcCache[:]) gl.TexCoordPointer(2, gl.FLOAT, 0, r.vtCache[:]) } // Find the OpenGL primitive type modes := [...]gl.GLenum{gl.POINTS, gl.LINES, gl.LINE_STRIP, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.QUADS} mode := modes[primType] // Draw the primitives gl.DrawArrays(mode, 0, len(verts)) } // Unbind the shader, if any // TODO /*if (states.shader) { r.applyShader(nil) }*/ // Update the cache r.useVertexCache = useVertexCache }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) /// 清除屏幕及深度缓存 gl.LoadIdentity() /// 重置模型观察矩阵 gl.Translatef(0, 0, -5) /// 并移入屏幕 5.0 gl.Rotatef(rotation[0], 1, 0, 0) ///以x为轴旋转 参数:角度,X,Y,Z gl.Rotatef(rotation[1], 0, 1, 0) gl.Rotatef(rotation[2], 0, 0, 1) rotation[0] += 0.3 rotation[1] += 0.2 rotation[2] += 0.4 /* 选择我们使用的纹理。如果您在您的场景中使用多个纹理, 应该使用来 glBindTexture(GL_TEXTURE_2D, texture[ 所使用纹理对应的数字 ]) 选择要绑定的纹理。 当想改变纹理时,应该绑定新的纹理。有一点值得指出的是,您不能在 glBegin() 和 glEnd() 之间绑定纹理, 必须在 glBegin() 之前或 glEnd() 之后绑定。注意在后面是如何使用 glBindTexture 来指定和绑定纹理的。 */ textures[0].Bind(gl.TEXTURE_2D) ///选择纹理 /* 为了将纹理正确的映射到四边形上,必须将纹理的右上角映射到四边形的右上角,纹理的左上角映射到四边形的左上角, 纹理的右下角映射到四边形的右下角,纹理的左下角映射到四边形的左下角。 如果映射错误的话,图像显示时可能上下颠倒,侧向一边或者什么都不是。 glTexCoord2f 的第一个参数是X坐标。 0.0f 是纹理的左侧。 0.5f 是纹理的中点, 1.0f 是纹理的右侧。 glTexCoord2f 的第二个参数是Y坐标。 0.0f 是纹理的底部。 0.5f 是纹理的中点, 1.0f 是纹理的顶部。 所以纹理的左上坐标是 X:0.0f,Y:1.0f ,四边形的左上顶点是 X: -1.0f,Y:1.0f 。其余三点依此类推。 */ gl.Begin(gl.QUADS) ///绘制正方形 gl.TexCoord2f(0, 0) ///前面 gl.Vertex3f(-1, -1, 1) ///左下 gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, 1) ///右下 gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, 1) ///右上 gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, 1) ///左上 gl.TexCoord2d(1, 0) ///后面 gl.Vertex3f(-1, -1, -1) ///右下 gl.TexCoord2d(1, 1) gl.Vertex3f(-1, 1, -1) ///右上 gl.TexCoord2d(0, 1) gl.Vertex3f(1, 1, -1) ///左上 gl.TexCoord2d(0, 0) gl.Vertex3f(1, -1, -1) //左下 gl.TexCoord2d(0, 1) ///上面 gl.Vertex3f(-1, 1, -1) gl.TexCoord2d(0, 0) gl.Vertex3f(-1, 1, 1) gl.TexCoord2d(1, 0) gl.Vertex3f(1, 1, 1) gl.TexCoord2d(1, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2d(1, 1) ///下面 gl.Vertex3f(-1, -1, -1) gl.TexCoord2d(0, 1) gl.Vertex3f(1, -1, -1) gl.TexCoord2d(0, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2d(1, 0) gl.Vertex3f(-1, -1, -1) gl.TexCoord2d(1, 0) ///右面 gl.Vertex3f(1, -1, -1) gl.TexCoord2d(1, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2d(0, 1) gl.Vertex3f(1, 1, 1) gl.TexCoord2d(0, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2d(0, 0) ///左面 gl.Vertex3f(-1, -1, -1) gl.TexCoord2d(1, 0) gl.Vertex3f(-1, -1, 1) gl.TexCoord2d(1, 1) gl.Vertex3f(-1, 1, 1) gl.TexCoord2d(0, 1) gl.Vertex3f(-1, 1, -1) gl.End() ///正方形绘制结束 glfw.SwapBuffers() ///必须交换显示区才能展现 }
func main() { sys := Make() sys.Startup() defer sys.Shutdown() // InitQueue() sys.CreateWindow(1024, 768, "Gexic") gl.ClearColor(1, 1, 1, 0.) initGL() prevSelectPos = []int{0, 0, 0} // PurgeQueue() // genHexMap() hexMap2 = GenHexMap() // for matches := checkHexMap(); len(matches) > 0; matches = checkHexMap() { // removeHexAndGenNew(matches) // } glfw.SetMouseButtonCallback(MouseButtonCallback) glfw.SetCharCallback(charCallback) glfw.SetMousePosCallback(MousePosCallback) currExX = -1 currExY = -1 for sys.CheckExitMainLoop() { start := glfw.Time() wait := float64(1) / float64(30) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.Disable(gl.DEPTH_TEST) // renderHexMap() wallpaper.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(0, 0) gl.TexCoord2f(0, 1) gl.Vertex2i(0, 768) gl.TexCoord2f(1, 1) gl.Vertex2i(1024, 768) gl.TexCoord2f(1, 0) gl.Vertex2i(1024, 0) gl.End() hexMap2.Render() hexRotate.AnimateAndExecute() hexShrink.AnimateAndExecute() hexFall.AnimateAndExecute() if !mouse.locked { x, y := mouse.GetXY() hexMap2.CalcClosestCenter(x, y) } gl.Flush() gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.BLEND) sys.Refresh() diff := glfw.Time() - start if diff < wait { glfw.Sleep(wait - diff) } } }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.LoadIdentity() gl.Translatef(0, 0, z) gl.Rotatef(rotation[0], 1, 0, 0) gl.Rotatef(rotation[1], 0, 1, 0) rotation[0] += speed[0] rotation[1] += speed[1] textures[filter].Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) // Front Face gl.Normal3f(0, 0, 1) // Normal Pointing Towards Viewer gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, 1) // Point 1 (Front) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, 1) // Point 2 (Front) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, 1) // Point 3 (Front) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, 1) // Point 4 (Front) // Back Face gl.Normal3f(0, 0, -1) // Normal Pointing Away From Viewer gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, -1) // Point 1 (Back) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, -1) // Point 2 (Back) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, -1) // Point 3 (Back) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, -1) // Point 4 (Back) // Top Face gl.Normal3f(0, 1, 0) // Normal Pointing Up gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) // Point 1 (Top) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, 1, 1) // Point 2 (Top) gl.TexCoord2f(1, 0) gl.Vertex3f(1, 1, 1) // Point 3 (Top) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) // Point 4 (Top) // Bottom Face gl.Normal3f(0, -1, 0) // Normal Pointing Down gl.TexCoord2f(1, 1) gl.Vertex3f(-1, -1, -1) // Point 1 (Bottom) gl.TexCoord2f(0, 1) gl.Vertex3f(1, -1, -1) // Point 2 (Bottom) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) // Point 3 (Bottom) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) // Point 4 (Bottom) // Right face gl.Normal3f(1, 0, 0) // Normal Pointing Right gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, -1) // Point 1 (Right) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) // Point 2 (Right) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, 1) // Point 3 (Right) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) // Point 4 (Right) // Left Face gl.Normal3f(-1, 0, 0) // Normal Pointing Left gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, -1) // Point 1 (Left) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) // Point 2 (Left) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, 1) // Point 3 (Left) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.End() glfw.SwapBuffers() }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translatef(0, 0, -3.0) gl.Rotatef(rotx, 1, 0, 0) gl.Rotatef(roty, 0, 1, 0) rotx += 0.5 roty += 0.5 texture.Bind(gl.TEXTURE_2D) gl.Color4f(1, 1, 1, 1) gl.Begin(gl.QUADS) gl.Normal3f(0, 0, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, 1) gl.Normal3f(0, 0, -1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, -1) gl.Normal3f(0, 1, 0) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, 1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) gl.Normal3f(0, -1, 0) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, -1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) gl.Normal3f(1, 0, 0) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, -1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) gl.Normal3f(-1, 0, 0) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, 1) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.End() }
// loadFont loads the given font data. This does not deal with font scaling. // Scaling should be handled by the independent Bitmap/Truetype loaders. // We therefore expect the supplied image and charset to already be adjusted // to the correct font scale. // // The image should hold a sprite sheet, defining the graphical layout for // every glyph. The config describes font metadata. func loadFont(img *image.RGBA, config *FontConfig) (f *Font, err error) { f = new(Font) f.config = config // Resize image to next power-of-two. img = glh.Pow2Image(img).(*image.RGBA) ib := img.Bounds() // Create the texture itself. It will contain all glyphs. // Individual glyph-quads display a subset of this texture. f.texture = gl.GenTexture() f.texture.Bind(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, ib.Dx(), ib.Dy(), 0, gl.RGBA, gl.UNSIGNED_BYTE, img.Pix) // Create display lists for each glyph. f.listbase = gl.GenLists(len(config.Glyphs)) texWidth := float32(ib.Dx()) texHeight := float32(ib.Dy()) for index, glyph := range config.Glyphs { // Update max glyph bounds. if glyph.Width > f.maxGlyphWidth { f.maxGlyphWidth = glyph.Width } if glyph.Height > f.maxGlyphHeight { f.maxGlyphHeight = glyph.Height } // Quad width/height vw := float32(glyph.Width) vh := float32(glyph.Height) // Texture coordinate offsets. tx1 := float32(glyph.X) / texWidth ty1 := float32(glyph.Y) / texHeight tx2 := (float32(glyph.X) + vw) / texWidth ty2 := (float32(glyph.Y) + vh) / texHeight // Advance width (or height if we render top-to-bottom) adv := float32(glyph.Advance) gl.NewList(f.listbase+uint(index), gl.COMPILE) { gl.Begin(gl.QUADS) { gl.TexCoord2f(tx1, ty2) gl.Vertex2f(0, 0) gl.TexCoord2f(tx2, ty2) gl.Vertex2f(vw, 0) gl.TexCoord2f(tx2, ty1) gl.Vertex2f(vw, vh) gl.TexCoord2f(tx1, ty1) gl.Vertex2f(0, vh) } gl.End() switch config.Dir { case LeftToRight: gl.Translatef(adv, 0, 0) case RightToLeft: gl.Translatef(-adv, 0, 0) case TopToBottom: gl.Translatef(0, -adv, 0) } } gl.EndList() } err = glh.CheckGLError() return }
func (r *RenderTarget) Render(verts []Vertex, primType PrimitiveType, states RenderStates) { // Nothing to draw? if len(verts) == 0 { return } // First set the persistent OpenGL states if it's the very first call if !r.glStatesSet { r.resetGlStates() } // Check if the vertex count is low enough so that we can pre-transform them // TODO: Fix vertex cache useVertexCache := /*len(verts) <= vertexCacheSize*/ false if useVertexCache { // Pre-transform the vertices and store them into the vertex cache for i := 0; i < len(verts); i++ { r.vertexCache[i].Pos = states.Transform.TransformPoint(verts[i].Pos) r.vertexCache[i].Color = verts[i].Color r.vertexCache[i].TexCoords = verts[i].TexCoords } // Since vertices are transformed, we must use an identity transform to render them if !r.useVertexCache { r.applyTransform(IdentityTransform()) } } else { r.applyTransform(states.Transform) } // Apply the view if r.viewChanged { r.applyCurrentView() } // Apply the blend mode if states.BlendMode != r.lastBlendMode { //r.applyBlendMode(states.BlendMode) } // Apply the texture var textureId uint64 if states.Texture != nil { textureId = states.Texture.cacheId } if textureId != r.lastTextureId { r.applyTexture(states.Texture) } // Apply the shader // TODO /*if states.shader { applyShader(states.shader); }*/ // If we pre-transform the vertices, we must use our internal vertex cache if useVertexCache { // ... and if we already used it previously, we don't need to set the pointers again if !r.useVertexCache { verts = r.vertexCache[:] } else { verts = nil } } // ######################################### if len(verts) > 0 { // Find the OpenGL primitive type modes := [...]gl.GLenum{gl.POINTS, gl.LINES, gl.LINE_STRIP, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.QUADS} mode := modes[primType] gl.Begin(mode) for i, _ := range verts { gl.TexCoord2f(verts[i].TexCoords.X, verts[i].TexCoords.Y) gl.Color4f(verts[i].Color.R/255, verts[i].Color.G/255, verts[i].Color.B/255, verts[i].Color.A/255) gl.Vertex2f(verts[i].Pos.X, verts[i].Pos.Y) } gl.End() } // ######################################### // Setup the pointers to the vertices' components /*if len(verts) > 0 { vData := make([]Vector2, len(verts)) //cData := make([]byte, len(verts)) tData := make([]Vector2, len(verts)) for i, _ := range verts { vData[i] = verts[i].Pos //cData[i] = verts[i].Color tData[i] = verts[i].TexCoords } //const char* data = reinterpret_cast<const char*>(vertices); gl.VertexPointer(2, gl.FLOAT, 0, vData) //gl.ColorPointer(4, gl.UNSIGNED_BYTE, unsafe.Sizeof(Vertex), cData)) gl.TexCoordPointer(2, gl.FLOAT, 0, tData) } // Find the OpenGL primitive type modes := [...]gl.GLenum{gl.POINTS, gl.LINES, gl.LINE_STRIP, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN, gl.QUADS} mode := modes[primType] // Draw the primitives gl.DrawArrays(mode, 0, len(verts))*/ // Unbind the shader, if any // TODO /*if (states.shader) { r.applyShader(nil) }*/ // Update the cache r.useVertexCache = useVertexCache }
func (d *DisplayEngine) drawFrame() { toDraw := make([][]*resources.Bitmap, d.config.MapW*d.config.MapH) drawPasses := 0 for x := 0; x < d.config.MapW; x++ { for y := 0; y < d.config.MapH; y++ { toDraw[x*d.config.MapW+y] = (*d.gameEngine).GetTile(x, y) length := len(toDraw[x*d.config.MapW+y]) if length > drawPasses { drawPasses = length } } } viewport := d.viewport font := allegro.CreateBuiltinFont() // Don't want anyone changing the viewport mid frame or any such highjinks d.Display.SetTargetBackbuffer() allegro.RunInThread(func() { r, g, b, a := d.config.BGColor.GetRGBA() gl.ClearColor( gl.GLclampf(r)/255.0, gl.GLclampf(g)/255.0, gl.GLclampf(b)/255.0, gl.GLclampf(a)/255.0) gl.Clear(gl.COLOR_BUFFER_BIT) viewport.SetupTransform() for p := 0; p < drawPasses; p++ { m := d.config.MapW n := d.config.MapH for s := 0; s < m+n; s++ { for x := 0; x < s; x++ { y := s - x - 1 if x >= m || y < 0 || y >= n { continue } if len(toDraw[x*d.config.MapW+y]) < p { continue } // Coordinates in terms of pixels px := (y - x) * d.config.TileW / 2 py := (x + y) * d.config.TileH / 2 bmp := toDraw[x*d.config.MapW+y][p] /* ox := bmp.OffX oy := bmp.OffY*/ bw, bh := bmp.W, bmp.H if viewport.OnScreen(px, py, bw, bh) { gl.Begin(gl.QUADS) bmp.Tex.Bind(gl.TEXTURE_2D) gl.TexCoord2f(0, 0) gl.Vertex3i(px, py, 0) gl.TexCoord2f(0, 1) gl.Vertex3i(px, py+bw, 0) gl.TexCoord2f(1, 1) gl.Vertex3i(px+bh, py+bw, 0) gl.TexCoord2f(1, 0) gl.Vertex3i(px+bh, py, 0) gl.End() } } } } gl.Flush() }) var trans allegro.Transform trans.Identity() trans.Use() font.Draw(allegro.CreateColor(0, 255, 0, 255), 0, 0, 0, fmt.Sprint(int(d.fps))) allegro.Flip() d.frameDrawing.Unlock() }
func renderHexMap() { gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.Disable(gl.DEPTH_TEST) wallpaper.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(0, 0) gl.TexCoord2f(0, 1) gl.Vertex2i(0, 768) gl.TexCoord2f(1, 1) gl.Vertex2i(1024, 768) gl.TexCoord2f(1, 0) gl.Vertex2i(1024, 0) gl.End() gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.DECAL) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.PushMatrix() gl.Translatef(80, 80, 0) for x := 0; x < 10; x++ { maxy := 8 topy := 19 if x%2 == 1 { maxy = 9 topy = 0 } starty := 0 for y := starty; y < maxy; y++ { if currExX > -1 && currExY > -1 && starRotate { if y == currExY && x == currExX || y == currExY+1 && x == currExX || y == currExY-1 && x == currExX || currExX%2 == 0 && ((currExX == x-1 || currExX == x+1) && currExY == y-1 || (currExX == x-1 || currExX == x+1) && currExY == y) || currExX%2 == 1 && ((currExX == x-1 || currExX == x+1) && currExY == y || (currExX == x-1 || currExX == x+1) && currExY == y+1) { continue } } else if timesToRotate > 0 { // if y == currExY && x == currExX || currExX%2 == 0 && (x == currExX+1 && y == currExY || x == currExX+1 && y == currExY+1) || currExX%2 == 1 && (x == currExX+1 && y == currExY || x == currExX+1 && y == currExY-1) { // continue // } found := false for _, v := range selectedHex { if y == v[1] && x == v[0] { found = true break } } if found { continue } } found := false for _, v := range currentMatches { if scale > 0 && v[0] == x && v[1] == y || scale <= 0 && v[0] == x && v[1] >= y { found = true break } } for _, v := range starMatches { if starAlpha > 0 && v[0] == x && v[1] == y || starAlpha <= 0 && v[0] == x && v[1] >= y { found = true break } } if found || len(currStarCenter) > 0 && currStarCenter[0] == x && currStarCenter[1] == y { continue } gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(x*33, topy+y*38, hexMap[x][y], 1) } } gl.PopMatrix() if len(currentMatches) > 0 || len(starMatches) > 0 { mouseLock = true if len(currentMatches) > 0 && scale > 0 { scale -= 0.1 for _, v := range currentMatches { gl.PushMatrix() topy := 19 if v[0]%2 == 1 { topy = 0 } gl.Translatef(float32(v[0]*33+102), float32(v[1]*38+topy+94), 0) gl.Scalef(scale, scale, 1) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[v[0]][v[1]], 1) gl.PopMatrix() } } else if len(starMatches) > 0 && starAlpha > 0 { starAlpha -= 0.05 // starAlpha = 0.7 starScale += 0.05 // starScale = 1.4 gl.PushMatrix() topy := 19 pm := 0 if currStarCenter[0]%2 == 1 { topy = 0 pm = -1 } gl.Translatef(float32(currStarCenter[0]*33+102), float32(currStarCenter[1]*38+topy+94), 0) drawHex(-22, -14, 6, 1) gl.PopMatrix() gl.PushMatrix() gl.Translatef(float32(currStarCenter[0]*33+102), float32(currStarCenter[1]*38+topy+94), 0) gl.Scalef(starScale, starScale, 1) drawHex(-22, -14-HEX_HEIGHT, hexMap[currStarCenter[0]][currStarCenter[1]-1], starAlpha) drawHex(-22, -20+HEX_HEIGHT, hexMap[currStarCenter[0]][currStarCenter[1]+1], starAlpha) drawHex(-52, -36, hexMap[currStarCenter[0]-1][currStarCenter[1]+pm], starAlpha) drawHex(-52, -40+HEX_HEIGHT, hexMap[currStarCenter[0]-1][currStarCenter[1]+pm+1], starAlpha) drawHex(8, -36, hexMap[currStarCenter[0]+1][currStarCenter[1]+pm], starAlpha) drawHex(8, -40+HEX_HEIGHT, hexMap[currStarCenter[0]+1][currStarCenter[1]+pm+1], starAlpha) gl.PopMatrix() } else { if fallticks == 0 { animateFall = make([]*freefall, 0) for x := 0; x < 10; x++ { topy := 19 if x%2 == 1 { topy = 0 } fromy := -1 for _, v := range currentMatches { if v[0] != x { continue } if v[1] > fromy { fromy = v[1] } } for _, v := range starMatches { if v[0] != x { continue } if v[1] > fromy { fromy = v[1] } } if fromy == -1 { continue } for y := fromy; y >= 0; y-- { found := false for _, v := range currentMatches { if v[0] == x && v[1] == y { found = true break } } for _, v := range starMatches { if v[0] == x && v[1] == y { found = true break } } if found { continue } animateFall = append(animateFall, &freefall{x, y, getFallTargetY(x, y), math.Pow(float64(y), 2)/16 + 0.5}) gl.PushMatrix() gl.Translatef(float32(x*33+102), float32(y*38+topy+94), 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[x][y], 1) gl.PopMatrix() } } fallticks++ } else { stillFalling := 0 for _, v := range animateFall { topy := 19 if v.x%2 == 1 { topy = 0 } newy := v.accel * math.Pow(float64(fallticks), 2) / 2 gl.PushMatrix() gl.Translatef(float32(v.x*33+102), float32(math.Min(float64(v.y*38+topy+94)+newy, float64(v.targetY*38+topy+94))), 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[v.x][v.y], 1) gl.PopMatrix() if float64(v.y*38+topy+94)+newy < float64(v.targetY*38+topy+94) { stillFalling++ } } fallticks++ if stillFalling == 0 { starScale = 1 starAlpha = 0 removeHexAndGenNew(currentMatches) makeStarAndGenNew(starMatches) currentMatches = checkHexMap() starMatches = checkHexStar() currStarCenter = []int{} scale = 1 fallticks = 0 mouseLock = false fmt.Println("Mouse unlocked 1") animateFall = nil } } } } if currExX > -1 && currExY > -1 { gl.PushMatrix() topy := 19 if currExX%2 == 1 { topy = 0 } if starRotate { timesToRotate = 0 gl.Translatef(float32(currExX*33+102), float32(currExY*38+topy+94), 0) gl.Scalef(1.3, 1.3, 1) gl.Rotatef(rotate, 0, 0, 1) drawHex(-22, -14, 6, 1) drawHex(-22, -14-HEX_HEIGHT, hexMap[currExX][currExY-1], 1) drawHex(-22, -20+HEX_HEIGHT, hexMap[currExX][currExY+1], 1) pm := 0 if currExX%2 == 1 { pm = -1 } drawHex(-52, -36, hexMap[currExX-1][currExY+pm], 1) drawHex(-52, -40+HEX_HEIGHT, hexMap[currExX-1][currExY+pm+1], 1) drawHex(8, -36, hexMap[currExX+1][currExY+pm], 1) drawHex(8, -40+HEX_HEIGHT, hexMap[currExX+1][currExY+pm+1], 1) } else { // gl.Translatef(float32(currExX*33+HEX_WIDTH+80), float32(currExxY*38+topy+20+80), 0) gl.Translatef(float32(prevSelectPos[0]), float32(prevSelectPos[1]), 0) gl.Scalef(1.3, 1.3, 1) gl.Rotatef(rotate, 0, 0, 1) for _, v := range selectedHex { switch v[2] { case 1: drawHex(-32, -34, hexMap[v[0]][v[1]], 1) case 2: drawHex(0, -17, hexMap[v[0]][v[1]], 1) case 3: drawHex(-32, 0, hexMap[v[0]][v[1]], 1) case 4: drawHex(-44, -19, hexMap[v[0]][v[1]], 1) case 5: drawHex(-12, -36, hexMap[v[0]][v[1]], 1) case 6: drawHex(-12, -2, hexMap[v[0]][v[1]], 1) } } // if currExX%2 == 0 { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -36, hexMap[currExX+1][currExY], 1) // } else { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -36, hexMap[currExX+1][currExY-1], 1) // } // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-44, -19, hexMap[currExX][currExY], 1) // if currExX%2 == 0 { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -2, hexMap[currExX+1][currExY+1], 1) // } else { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -2, hexMap[currExX+1][currExY], 1) // } } gl.PopMatrix() if !starRotate && rotate < 120 { rotate += 15 // rotate = 15 } else if starRotate && rotate < 60 { rotate += 6 } else { if starRotate { if currExX%2 == 0 { hexMap[currExX][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX+1][currExY+1], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY+1], hexMap[currExX-1][currExY] = hexMap[currExX-1][currExY], hexMap[currExX][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX+1][currExY+1], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY+1] } else { hexMap[currExX][currExY-1], hexMap[currExX+1][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY], hexMap[currExX-1][currExY-1] = hexMap[currExX-1][currExY-1], hexMap[currExX][currExY-1], hexMap[currExX+1][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY] } } else { v2 := make([][]int, 3) for _, v := range selectedHex { idx := 0 switch v[2] { case 1, 4: idx = 0 case 2, 5: idx = 1 case 3, 6: idx = 2 } v2[idx] = []int{v[0], v[1]} fmt.Println(idx, v[0], v[1]) } hexMap[v2[0][0]][v2[0][1]], hexMap[v2[1][0]][v2[1][1]], hexMap[v2[2][0]][v2[2][1]] = hexMap[v2[2][0]][v2[2][1]], hexMap[v2[0][0]][v2[0][1]], hexMap[v2[1][0]][v2[1][1]] } starMatches = checkHexStar() if len(starMatches) > 6 { fmt.Println(starMatches) } if len(starMatches) >= 6 { timesToRotate = 0 rotate = 0 currExX = -1 currExY = -1 starScale = 1 starAlpha = 1 starRotate = false currStarCenter = getStarCenter(starMatches) hexMap[currStarCenter[0]][currStarCenter[1]] = 6 // makeStarAndGenNew(starMatches) } else { matches := checkHexMap() if len(matches) > 0 { currentMatches = matches scale = 1 currExX = -1 currExY = -1 rotate = 0 timesToRotate = 0 starRotate = false } else { if timesToRotate == 0 { currExX = -1 currExY = -1 rotate = 0 timesToRotate = 0 starRotate = false mouseLock = false fmt.Println("Mouse unlocked 3") } rotate = 0 timesToRotate-- } } } } if !mouseLock { prevSelectPos = calcClosestCenter(posX, posY) drawBorderAtXY(float32(prevSelectPos[0]), float32(prevSelectPos[1]), prevSelectPos[2]) } gl.Flush() gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.BLEND) }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) //清除屏幕和深度缓存 gl.LoadIdentity() //重置当前的模型观察矩阵 gl.Translatef(0, 0, z) //移入/移出屏幕 z 个单位 gl.Rotatef(rotation[0], 1, 0, 0) //绕X轴旋转 gl.Rotatef(rotation[1], 0, 1, 0) //绕Y轴旋转 rotation[0] += speed[0] rotation[1] += speed[1] textures[filter].Bind(gl.TEXTURE_2D) //绑定的纹理 /* Normal就是法线的意思,所谓法线是指经过面(多边形)上的一点且垂直于这个面(多边形)的直线。 使用光源的时候必须指定一条法线。法线告诉OpenGL这个多边形的朝向,并指明多边形的正面和背面。 如果没有指定法线,什么怪事情都可能发生:不该照亮的面被照亮了,多边形的背面也被照亮....。对了,法线应该指向多边形的外侧。 看着木箱的前面您会注意到法线与Z轴正向同向。这意味着法线正指向观察者-您自己。这正是我们所希望的。 对于木箱的背面,也正如我们所要的,法线背对着观察者。 如果立方体沿着X或Y轴转个180度的话,前侧面的法线仍然朝着观察者,背面的法线也还是背对着观察者。 换句话说,不管是哪个面,只要它朝着观察者这个面的法线就指向观察者。由于光源紧邻观察者,任何时候法线对着观察者时,这个面就会被照亮。 并且法线越朝着光源,就显得越亮一些。如果您把观察点放到立方体内部,你就会法线里面一片漆黑。因为法线是向外指的。 如果立方体内部没有光源的话,当然是一片漆黑。 */ gl.Begin(gl.QUADS) // Front Face 前面 gl.Normal3f(0, 0, 1) // Normal Pointing Towards Viewer 法线指向观察者 gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, 1) // Point 1 (Front) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, 1) // Point 2 (Front) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, 1) // Point 3 (Front) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, 1) // Point 4 (Front) // Back Face 后面 gl.Normal3f(0, 0, -1) // Normal Pointing Away From Viewer 法线背向观察者 gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, -1) // Point 1 (Back) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, -1) // Point 2 (Back) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, -1) // Point 3 (Back) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, -1) // Point 4 (Back) // Top Face 顶面 gl.Normal3f(0, 1, 0) // Normal Pointing Up 法线向上 gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) // Point 1 (Top) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, 1, 1) // Point 2 (Top) gl.TexCoord2f(1, 0) gl.Vertex3f(1, 1, 1) // Point 3 (Top) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) // Point 4 (Top) // Bottom Face 底面 gl.Normal3f(0, -1, 0) // Normal Pointing Down 法线朝下 gl.TexCoord2f(1, 1) gl.Vertex3f(-1, -1, -1) // Point 1 (Bottom) gl.TexCoord2f(0, 1) gl.Vertex3f(1, -1, -1) // Point 2 (Bottom) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) // Point 3 (Bottom) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) // Point 4 (Bottom) // Right face 右面 gl.Normal3f(1, 0, 0) // Normal Pointing Right 法线朝右 gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, -1) // Point 1 (Right) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) // Point 2 (Right) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, 1) // Point 3 (Right) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) // Point 4 (Right) // Left Face 左面 gl.Normal3f(-1, 0, 0) // Normal Pointing Left 法线朝左 gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, -1) // Point 1 (Left) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) // Point 2 (Left) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, 1) // Point 3 (Left) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.End() glfw.SwapBuffers() }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.LoadIdentity() gl.Translatef(0, 0, -5) gl.Rotatef(rotation[0], 1, 0, 0) gl.Rotatef(rotation[1], 0, 1, 0) gl.Rotatef(rotation[2], 0, 0, 1) rotation[0] += 0.3 rotation[1] += 0.2 rotation[2] += 0.4 textures[0].Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) // Front Face gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, 1) // Bottom Left Of The Texture and Quad gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, 1) // Bottom Right Of The Texture and Quad gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, 1) // Top Right Of The Texture and Quad gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, 1) // Top Left Of The Texture and Quad // Back Face gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, -1) // Bottom Right Of The Texture and Quad gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, -1) // Top Right Of The Texture and Quad gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, -1) // Top Left Of The Texture and Quad gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, -1) // Bottom Left Of The Texture and Quad // Top Face gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) // Top Left Of The Texture and Quad gl.TexCoord2f(0, 0) gl.Vertex3f(-1, 1, 1) // Bottom Left Of The Texture and Quad gl.TexCoord2f(1, 0) gl.Vertex3f(1, 1, 1) // Bottom Right Of The Texture and Quad gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) // Top Right Of The Texture and Quad // Bottom Face gl.TexCoord2f(1, 1) gl.Vertex3f(-1, -1, -1) // Top Right Of The Texture and Quad gl.TexCoord2f(0, 1) gl.Vertex3f(1, -1, -1) // Top Left Of The Texture and Quad gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) // Bottom Left Of The Texture and Quad gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) // Bottom Right Of The Texture and Quad // Right face gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, -1) // Bottom Right Of The Texture and Quad gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) // Top Right Of The Texture and Quad gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, 1) // Top Left Of The Texture and Quad gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) // Bottom Left Of The Texture and Quad // Left Face gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, -1) // Bottom Left Of The Texture and Quad gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) // Bottom Right Of The Texture and Quad gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, 1) // Top Right Of The Texture and Quad gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) // Top Left Of The Texture and Quad gl.End() glfw.SwapBuffers() }