func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) /// 清除屏幕及深度缓存 gl.LoadIdentity() /// 重置模型观察矩阵 gl.Translatef(-1.5, 0, -6) /// 左移 1.5 单位,并移入屏幕 6.0 gl.Begin(gl.TRIANGLES) /// 绘制三角形 gl.Color3f(1, 0, 0) /// 设置当前色为红色 gl.Vertex3f(0, 1, 0) ///上顶点 gl.Color3f(0, 1, 0) /// 设置当前色为绿色 gl.Vertex3f(-1, -1, 0) /// 左下 gl.Color3f(0, 0, 1) ///设置当前色为蓝色 gl.Vertex3f(1, -1, 0) ///右下 gl.End() ///三角形绘制结束,三角形将被填充。 //但是因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。这就是平滑着色。 gl.Translatef(3, 0, 0) ///右移3单位 gl.Color3f(0.5, 0.5, 1.0) ///一次性将当前色设置为蓝色 /// @note 顺时针绘制的正方形意味着我们所看见的是四边形的背面 gl.Begin(gl.QUADS) ///绘制正方形 gl.Vertex3f(-1, 1, 0) /// 左上 gl.Vertex3f(1, 1, 0) /// 右上 gl.Vertex3f(1, -1, 0) /// 右下 gl.Vertex3f(-1, -1, 0) /// 左下 gl.End() ///正方形绘制结束 glfw.SwapBuffers() ///必须交换显示区才能展现 }
func main() { if !glfw.Init() { log.Fatal("glfw failed to initialize") } defer glfw.Terminate() window, err := glfw.CreateWindow(640, 480, "Deformable", nil, nil) if err != nil { log.Fatal(err.Error()) } window.MakeContextCurrent() glfw.SwapInterval(1) window.SetMouseButtonCallback(handleMouseButton) window.SetKeyCallback(handleKeyDown) window.SetInputMode(glfw.Cursor, glfw.CursorHidden) gl.Init() initGL() i := 16 m = GenerateMap(1600/i, 1200/i, i) for running && !window.ShouldClose() { x, y := window.GetCursorPosition() if drawing != 0 { m.Add(int(x)+int(camera[0]), int(y)+int(camera[1]), drawing, brushSizes[currentBrushSize]) } gl.Clear(gl.COLOR_BUFFER_BIT) gl.LoadIdentity() gl.PushMatrix() gl.PushAttrib(gl.CURRENT_BIT | gl.ENABLE_BIT | gl.LIGHTING_BIT | gl.POLYGON_BIT | gl.LINE_BIT) gl.Translatef(-camera[0], -camera[1], 0) m.Draw() gl.PopAttrib() gl.PopMatrix() gl.PushAttrib(gl.COLOR_BUFFER_BIT) gl.LineWidth(2) gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE_MINUS_DST_COLOR, gl.ZERO) // gl.Enable(gl.LINE_SMOOTH) // gl.Hint(gl.LINE_SMOOTH_HINT, gl.NICEST) gl.Translatef(float32(x), float32(y), 0) gl.EnableClientState(gl.VERTEX_ARRAY) gl.VertexPointer(2, gl.DOUBLE, 0, cursorVerts) gl.DrawArrays(gl.LINE_LOOP, 0, 24) gl.PopAttrib() window.SwapBuffers() glfw.PollEvents() } }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.LoadIdentity() gl.Translatef(-1.5, 0, -6) gl.Rotatef(trisAngle, 0, 1, 0) gl.Begin(gl.TRIANGLES) gl.Color3f(1, 0, 0) gl.Vertex3f(0, 1, 0) gl.Color3f(0, 1, 0) gl.Vertex3f(-1, -1, 0) gl.Color3f(0, 0, 1) gl.Vertex3f(1, -1, 0) gl.End() gl.LoadIdentity() gl.Translatef(1.5, 0, -6) gl.Rotatef(quadAngle, 1, 0, 0) gl.Color3f(0.5, 0.5, 1.0) gl.Begin(gl.QUADS) gl.Vertex3f(-1, 1, 0) gl.Vertex3f(1, 1, 0) gl.Vertex3f(1, -1, 0) gl.Vertex3f(-1, -1, 0) gl.End() trisAngle += 0.2 quadAngle -= 0.15 glfw.SwapBuffers() }
func draw() { // 从这里开始进行所有的绘制 gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // 清除屏幕和深度缓存 gl.LoadIdentity() // 重置当前的模型观察矩阵 gl.Translatef(-1.5, 0.0, -6.0) // 左移 1.5 单位,并移入屏幕 6.0 gl.Rotatef(rtri, 0.0, 1.0, 0.0) // 绕Y轴旋转三角形 gl.Begin(gl.TRIANGLES) // 绘制三角形 gl.Color3f(1.0, 0.0, 0.0) // 设置当前色为红色 gl.Vertex3f(0.0, 1.0, 0.0) // 上顶点 gl.Color3f(0.0, 1.0, 0.0) // 设置当前色为绿色 gl.Vertex3f(-1.0, -1.0, 0.0) // 左下 gl.Color3f(0.0, 0.0, 1.0) // 设置当前色为蓝色 gl.Vertex3f(1.0, -1.0, 0.0) // 右下 gl.End() // 三角形绘制结束 gl.LoadIdentity() // 重置当前的模型观察矩阵 gl.Translatef(1.5, 0.0, -6.0) // 右移1.5单位,并移入屏幕 6.0 gl.Rotatef(rquad, 1.0, 0.0, 0.0) // 绕X轴旋转四边形 gl.Color3f(0.5, 0.5, 1.0) // 一次性将当前色设置为蓝色 gl.Begin(gl.QUADS) // 绘制正方形 gl.Vertex3f(-1.0, 1.0, 0.0) // 左上 gl.Vertex3f(1.0, 1.0, 0.0) // 右上 gl.Vertex3f(1.0, -1.0, 0.0) // 左下 gl.Vertex3f(-1.0, -1.0, 0.0) // 右下 gl.End() rtri += 0.2 // 增加三角形的旋转变量 rquad -= 0.15 // 减少四边形的旋转变量 }
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 main() { var err error if err = glfw.Init(); err != nil { log.Fatalf("%v\n", err) return } defer glfw.Terminate() // Open window with FSAA samples (if possible). glfw.OpenWindowHint(glfw.FsaaSamples, 4) if err = glfw.OpenWindow(400, 400, 0, 0, 0, 0, 0, 0, glfw.Windowed); err != nil { log.Fatalf("%v\n", err) return } defer glfw.CloseWindow() glfw.SetWindowTitle("Aliasing Detector") glfw.SetSwapInterval(1) if samples := glfw.WindowParam(glfw.FsaaSamples); samples != 0 { fmt.Printf("Context reports FSAA is supported with %d samples\n", samples) } else { fmt.Printf("Context reports FSAA is unsupported\n") } gl.MatrixMode(gl.PROJECTION) glu.Perspective(0, 1, 0, 1) for glfw.WindowParam(glfw.Opened) == 1 { time := float32(glfw.Time()) gl.Clear(gl.COLOR_BUFFER_BIT) gl.LoadIdentity() gl.Translatef(0.5, 0, 0) gl.Rotatef(time, 0, 0, 1) gl.Enable(GL_MULTISAMPLE_ARB) gl.Color3f(1, 1, 1) gl.Rectf(-0.25, -0.25, 0.25, 0.25) gl.LoadIdentity() gl.Translatef(-0.5, 0, 0) gl.Rotatef(time, 0, 0, 1) gl.Disable(GL_MULTISAMPLE_ARB) gl.Color3f(1, 1, 1) gl.Rectf(-0.25, -0.25, 0.25, 0.25) glfw.SwapBuffers() } }
func renderSwitchBlocks(s *Switch) { // TODO constant v := SwitchSize / 2 x, y := float32(s.X+v), float32(s.Y+v) gl.LoadIdentity() gl.Translatef(x, y, 0) if s.rotate != 0 { gl.Rotatef(float32(s.rotate), 0, 0, 1) } bsf := float32(BlockSize - s.Z) padding := float32(BlockPadding) var b *Block // Render block top left b = g.level.blocks[s.line][s.col] if !b.Rendered { gl.PushMatrix() gl.Translatef(-bsf-padding, -bsf-padding, 0) renderBlock(b, bsf) gl.PopMatrix() b.Rendered = true } // Render block top right b = g.level.blocks[s.line][s.col+1] if !b.Rendered { gl.PushMatrix() gl.Translatef(padding, -bsf-padding, 0) renderBlock(b, bsf) gl.PopMatrix() b.Rendered = true } // Render block bottom right b = g.level.blocks[s.line+1][s.col+1] if !b.Rendered { gl.PushMatrix() gl.Translatef(padding, padding, 0) renderBlock(b, bsf) gl.PopMatrix() b.Rendered = true } // render block bottom left b = g.level.blocks[s.line+1][s.col] if !b.Rendered { gl.PushMatrix() gl.Translatef(-bsf-padding, padding, 0) renderBlock(b, bsf) gl.PopMatrix() b.Rendered = true } }
func drawPool(cards [5]DeckCard, x float32) { gl.PushMatrix() gl.Translatef(x, 590-CardHeight, 0) for i, card := range cards { if i > 0 { gl.Translatef(0, -(CardHeight + 5), 0) } card.Draw() } gl.PopMatrix() }
func (m *Map) Draw() { // gl.Enable(gl.PRIMITIVE_RESTART) // gl.PrimitiveRestartIndex(PRIMITIVE_RESTART) gl.EnableClientState(gl.VERTEX_ARRAY) gl.Translatef(float32(m.gridSize/2), float32(m.gridSize/2), 0) if m.renderSmooth { gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.POLYGON_SMOOTH) gl.Hint(gl.POLYGON_SMOOTH_HINT, gl.NICEST) } if m.renderMode == 1 { gl.LineWidth(1) gl.VertexPointer(2, gl.FLOAT, 0, m.gridLines) gl.Color3f(0.2, 0.2, 0.2) gl.DrawArrays(gl.LINES, 0, len(m.gridLines)/2) gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) } for _, vl := range m.vl { if len(vl.vertices) > 0 { gl.VertexPointer(2, gl.FLOAT, 0, vl.vertices) gl.Color3f(vl.colors[0], vl.colors[1], vl.colors[2]) gl.DrawElements(gl.TRIANGLES, len(vl.indices), gl.UNSIGNED_INT, vl.indices) } } }
func renderSwitch(s *Switch) { gl.LoadIdentity() // TODO constant v := SwitchSize / 2 x, y := float32(s.X+v), float32(s.Y+v) gl.Translatef(x, y, 0) // Render the switch gl.Color3f(1, 1, 1) gl.Begin(gl.TRIANGLE_FAN) gl.Vertex2d(0, 0) vv := float64(v) for i := float64(0); i <= SwitchSegments; i++ { a := 2 * math.Pi * i / SwitchSegments gl.Vertex2d(math.Sin(a)*vv, math.Cos(a)*vv) } gl.End() if LineWidth != 0 { // Render the shape gl.Color3i(0, 0, 0) gl.LineWidth(LineWidth) gl.Begin(gl.LINE_LOOP) for i := float64(0); i <= SwitchSegments; i++ { a := 2 * math.Pi * i / SwitchSegments gl.Vertex2d(math.Sin(a)*vv, math.Cos(a)*vv) } gl.End() } // Write the switch name gl.LoadIdentity() w, h := fonts[6].Metrics(s.name) gl.Color3i(0, 0, 0) fonts[6].Printf(x-float32(w)/2, y-float32(h)/2+2, s.name) }
func display() { gl.Clear(gl.COLOR_BUFFER_BIT) bitmap_output(40, 35, "This is written in a GLUT bitmap font.", glut.BITMAP_TIMES_ROMAN_24) bitmap_output(30, 210, "More bitmap text is a fixed 9 by 15 font.", glut.BITMAP_9_BY_15) bitmap_output(70, 240, " Helvetica is yet another bitmap font.", glut.BITMAP_HELVETICA_18) gl.MatrixMode(gl.PROJECTION) gl.PushMatrix() gl.LoadIdentity() glu.Perspective(40.0, 1.0, 0.1, 20.0) gl.MatrixMode(gl.MODELVIEW) gl.PushMatrix() glu.LookAt(0.0, 0.0, 4.0, /* eye is at (0,0,30) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.0) /* up is in postivie Y direction */ gl.PushMatrix() gl.Translatef(0, 0, -4) gl.Rotatef(50, 0, 1, 0) stroke_output(-2.5, 1.1, " This is written in a", glut.STROKE_ROMAN) stroke_output(-2.5, 0, " GLUT stroke font.", glut.STROKE_ROMAN) stroke_output(-2.5, -1.1, "using 3D perspective.", glut.STROKE_ROMAN) gl.PopMatrix() gl.MatrixMode(gl.MODELVIEW) gl.PopMatrix() gl.MatrixMode(gl.PROJECTION) gl.PopMatrix() gl.MatrixMode(gl.MODELVIEW) gl.Flush() }
// OpenGL draw function func draw() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.BLEND) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.LoadIdentity() gl.Begin(gl.LINES) gl.Color3f(.2, .2, .2) for i := range staticLines { x := staticLines[i].GetAsSegment().A.X y := staticLines[i].GetAsSegment().A.Y gl.Vertex3f(float32(x), float32(y), 0) x = staticLines[i].GetAsSegment().B.X y = staticLines[i].GetAsSegment().B.Y gl.Vertex3f(float32(x), float32(y), 0) } gl.End() gl.Color4f(.3, .3, 1, .8) // draw balls for _, ball := range balls { gl.PushMatrix() pos := ball.Body.Position() rot := ball.Body.Angle() * chipmunk.DegreeConst gl.Translatef(float32(pos.X), float32(pos.Y), 0.0) gl.Rotatef(float32(rot), 0, 0, 1) drawCircle(float64(ballRadius), 60) gl.PopMatrix() } }
func stroke_output(x, y float32, str string, font glut.StrokeFont) { gl.PushMatrix() gl.Translatef(x, y, 0) gl.Scalef(0.005, 0.005, 0.005) for _, ch := range str { font.Character(ch) } gl.PopMatrix() }
func reshape(w, h int) { gl.Viewport(0, 0, w, h) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, float64(w), 0, float64(h), -1, 1) gl.Scalef(1, -1, 1) gl.Translatef(0, float32(-h), 0) gl.MatrixMode(gl.MODELVIEW) }
func (p *Playfield) Draw() { drawPool(p.playerCards, 10) drawPool(p.opponentCards, 790-CardWidth) // draw the slots gl.PushMatrix() gl.Translatef(CardWidth+60, 580-CardHeight, 0) for i, slot := range p.slots { if i > 0 { gl.Translatef(CardWidth+2, 0, 0) if i%4 == 0 { gl.Translatef(-((CardWidth + 2) * 4), -(CardHeight + 2), 0) } } slot.card.Draw() } gl.PopMatrix() }
func drawPaddle(x float32, y float32) { gl.PushMatrix() gl.Translatef(float32(x), float32(y), 0) gl.Color3f(1.0, 1.0, 1.0) gl.Begin(gl.LINE_STRIP) gl.Vertex2f(0, -5) gl.Vertex2f(0, 5) gl.End() gl.PopMatrix() }
func renderDashboard() { gl.LoadIdentity() setColor(Blue) fonts[32].Printf(float32(XMin), float32(WindowHeight-DashboardHeight), "Level %d", g.currentLevel) gl.Translatef(float32(XMin)+300, float32(WindowHeight-DashboardHeight), 0) line := 0 for _, c := range g.level.winSignature { if c == '\n' { line++ gl.LoadIdentity() gl.Translatef(float32(XMin)+300, float32(WindowHeight-DashboardHeight+SignatureBlockSize*line+BlockPadding), 0) continue } if c != '-' { renderBlockSignature(atoc(string(c))) } gl.Translated(SignatureBlockSize+BlockPadding, 0, 0) } }
func draw() { // 从这里开始进行所有的绘制 gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // 清除屏幕和深度缓存 gl.LoadIdentity() // 重置当前的模型观察矩阵 gl.Translatef(-1.5, 0.0, -6.0) // 左移 1.5 单位,并移入屏幕 6.0 gl.Begin(gl.TRIANGLES) // 绘制三角形 gl.Vertex3f(0.0, 1.0, 0.0) // 上顶点 gl.Vertex3f(-1.0, -1.0, 0.0) // 左下 gl.Vertex3f(1.0, -1.0, 0.0) // 右下 gl.End() // 三角形绘制结束 gl.Translatef(3.0, 0.0, 0.0) // 右移3单位 gl.Begin(gl.QUADS) // 绘制正方形 gl.Vertex3f(-1.0, 1.0, 0.0) // 左上 gl.Vertex3f(1.0, 1.0, 0.0) // 右上 gl.Vertex3f(1.0, -1.0, 0.0) // 左下 gl.Vertex3f(-1.0, -1.0, 0.0) // 右下 gl.End() }
/* new window size or exposure */ func reshape(width int, height int) { h := float64(height) / float64(width) gl.Viewport(0, 0, width, height) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Frustum(-1.0, 1.0, -h, h, 5.0, 60.0) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translatef(0.0, 0.0, -40.0) }
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 drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) /// 清除屏幕及深度缓存 gl.LoadIdentity() /// 重置模型观察矩阵 gl.Translatef(-1.5, 0, -6) /// 左移 1.5 单位,并移入屏幕 6.0 gl.Rotatef(trisAngle, 0, 1, 0) ///以y为轴 参数:角度,X,Y,Z gl.Begin(gl.TRIANGLES) /// 绘制三角形 gl.Color3f(1, 0, 0) /// 设置当前色为红色 gl.Vertex3f(0, 1, 0) ///上顶点 gl.Color3f(0, 1, 0) /// 设置当前色为绿色 gl.Vertex3f(-1, -1, 0) /// 左下 gl.Color3f(0, 0, 1) ///设置当前色为蓝色 gl.Vertex3f(1, -1, 0) ///右下 gl.End() ///三角形绘制结束,三角形将被填充。 //但是因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。这就是平滑着色。 /// 要让对象绕自身的轴旋转,必须让对象的中心坐标总是(0.0f,0,0f,0,0f),因此这里的四边形是满屏跑的 gl.LoadIdentity() ///将当前点移到了屏幕中心,X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。 ///OpenGL屏幕中心的坐标值是X和Y轴上的0.0f点。中心左面的坐标值是负值,右面是正值。移向屏幕顶端是正值,移向屏幕底端是负值。移入屏幕深处是负值,移出屏幕则是正值。 gl.Rotatef(quadAngle, 1, 0, 0) /// 以x为轴 gl.Translatef(1.5, 0, -6) ///以当前点为起始点移动?(是的)-6为距离 //gl.Translatef(3, 0, -6); ///右移3单位,看不见?(因为loadIdentity()置中了) gl.Color3f(0.5, 0.5, 1.0) ///一次性将当前色设置为蓝色 /// @note 顺时针绘制的正方形意味着我们所看见的是四边形的背面 gl.Begin(gl.QUADS) ///绘制正方形 gl.Vertex3f(-1, 1, 0) /// 左上 gl.Vertex3f(1, 1, 0) /// 右上 gl.Vertex3f(1, -1, 0) /// 右下 gl.Vertex3f(-1, -1, 0) /// 左下 gl.End() ///正方形绘制结束 trisAngle += 0.2 quadAngle -= 0.15 glfw.SwapBuffers() ///必须交换显示区才能展现 }
func reshape(w, h int) { /* Because Gil specified "screen coordinates" (presumably with an upper-left origin), this short bit of code sets up the coordinate system to correspond to actual window coodrinates. This code wouldn't be required if you chose a (more typical in 3D) abstract coordinate system. */ gl.Viewport(0, 0, w, h) /* Establish viewing area to cover entire window. */ gl.MatrixMode(gl.PROJECTION) /* Start modifying the projection matrix. */ gl.LoadIdentity() /* Reset project matrix. */ gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */ gl.Scalef(1, -1, 1) /* Invert Y axis so increasing Y goes down. */ gl.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */ }
func (m HexMap) Render() { for x := 0; x < 11; x++ { maxy := 8 if x%2 == 1 { maxy = 9 } for y := 0; y < maxy; y++ { if m[x][y].State != StateNormal { continue } gl.PushMatrix() posX, posY := m.GetTopLeft(x, y).WithOffset() gl.Translatef(float32(posX), float32(posY), 0) m[x][y].Render(1, false) gl.PopMatrix() } } }
func main() { err := initGL() if err != nil { log.Printf("InitGL: %v", err) return } defer glfw.Terminate() mb := createBuffer() defer mb.Release() // Perform the rendering. var angle float32 for glfw.WindowParam(glfw.Opened) > 0 { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.LoadIdentity() gl.Translatef(0, 0, -6) gl.Rotatef(angle, 1, 1, 1) // Render a solid cube at half the scale. gl.Scalef(0.2, 0.2, 0.2) gl.Enable(gl.COLOR_MATERIAL) gl.Enable(gl.POLYGON_OFFSET_FILL) gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) mb.Render(gl.QUADS) // Render wireframe cubes, with incremental size. gl.Disable(gl.COLOR_MATERIAL) gl.Disable(gl.POLYGON_OFFSET_FILL) gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) for i := 0; i < 50; i++ { scale := 0.004*float32(i) + 1.0 gl.Scalef(scale, scale, scale) mb.Render(gl.QUADS) } angle += 0.5 glfw.SwapBuffers() } }
func drawBall(angle float32, ball_x float32, ball_y float32) { gl.PushMatrix() gl.Color3f(1, 1, 1) gl.Translatef(ball_x, ball_y, 0.0) gl.Begin(gl.LINE_LOOP) for rad := 0.0; rad < 1.0; rad += 0.01 { gl.Vertex2f( float32(2.0*math.Cos(2.0*math.Pi*rad)), float32(2.0*math.Sin(2.0*math.Pi*rad)+0.2)) } gl.End() gl.Rotatef(angle, 0.0, 0.0, 1.0) gl.Begin(gl.LINE_STRIP) gl.Vertex2f(0, 0) gl.Vertex2f(2, 0) gl.End() gl.PopMatrix() }
func drawBlock(loc Loc, b *Block) { // Save current modelview matrix on to the stack gl.PushMatrix() // Move block gl.Translatef(loc.X, loc.Y, loc.Z) // Rotate block gl.Rotatef(b.Pitch, 1, 0, 0) gl.Rotatef(b.Yaw, 0, 1, 0) gl.Rotatef(b.Roll, 0, 0, 1) // Start specifying the vertices of quads. gl.Begin(gl.QUADS) for _, quad := range b.Quads { gl.Color3fv(quad.Color) for _, vertex := range quad.Vertices { gl.Vertex3fv(b.Vertices[vertex]) } } gl.End() // Restore old modelview matrix gl.PopMatrix() }
func redraw() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() var viewport [4]int32 gl.GetIntegerv(gl.VIEWPORT, viewport[:4]) aspect := float64(viewport[2]) / float64(viewport[3]) glu.Perspective(60, aspect, 1, 100) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translatef(POV.X, POV.Y, POV.Z) gl.Rotatef(POV.Pitch, 1, 0, 0) gl.Rotatef(POV.Yaw, 0, 1, 0) gl.Rotatef(POV.Roll, 0, 0, 1) for x, row := range World { for z, block := range row { drawBlock(translateCoordinates(x, 0, z), block) if x == CursorX && z == CursorZ { drawCursor() } } } }
func (f *AnimateFall) AnimateAndExecute() { if len(f.FallHex) == 0 { return } stillFalling := 0 for _, hex := range f.FallHex { gl.PushMatrix() x, y := hexMap2.GetTopLeft(hex.Pos.X, hex.Pos.Y).WithOffset() displaceY := hex.Accel * math.Pow(f.FallTicks, 2) / 2 _, tY := hexMap2.GetTopLeft(hex.Pos.X, hex.Target.Y).WithOffset() newY := math.Min(y+displaceY, tY) gl.Translatef(float32(x), float32(newY), 0) hex.Hex.Render(1, false) gl.PopMatrix() if newY < tY { stillFalling++ } } f.FallTicks++ if stillFalling == 0 { for _, hex := range f.FallHex { hex.Hex.State = StateNormal } f.FallHex = nil f.FallHex = make([]FallHex, 0) if hexMap2.CheckCollision() { hexShrink.InitAnimation() if f.postHook != nil { hexShrink.postHook = f.postHook } } else { if f.postHook != nil { f.postHook() } } } }
func main() { err := initGL() if err != nil { log.Printf("InitGL: %v", err) return } defer glfw.Terminate() mb := createBuffer() defer mb.Release() attr := mb.Colors() // Perform the rendering. for glfw.WindowParam(glfw.Opened) > 0 { gl.Clear(gl.COLOR_BUFFER_BIT) gl.LoadIdentity() // Center mesh on screen. wx, wy := glfw.WindowSize() px := (wx / 2) - ((Cols * CellWidth) / 2) py := (wy / 2) - ((Rows * CellHeight) / 2) gl.Translatef(float32(px), float32(py), 0) // Change the color of the quad under the mouse cursor. colorize(px, py, attr) // Render the mesh. mb.Render(gl.QUADS) glfw.SwapBuffers() } attr = nil }
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() }