dsxfa26482
2015-01-13 00:22
浏览 113
已采纳

Go Lang OpenGL简单形状-黑屏

I need some assistance into why this piece of code produces a blank green window. I made this by combining examples from https://github.com/Jragonmiris/mathgl/blob/master/examples/opengl-tutorial/tutorial02/main.go and https://github.com/veandco/go-sdl2/blob/master/examples/opengl3.go. I guess i'm not sure if this is a bug with the GoLang sdl/gl framework or an issue with my OpenGL understanding. All this should draw is a cube.

My code is:

package main

import (
    "fmt"
    // gl "github.com/chsc/gogl/gl33"
    "github.com/veandco/go-sdl2/sdl"
    // "math"
    "github.com/Jragonmiris/mathgl"
    "github.com/go-gl/gl"
    "runtime"
    "time"
)

// var program gl.Program = 0
// var buffer gl.Buffer = 0

func MakeProgram(vert, frag string) gl.Program {

    vertShader, fragShader := gl.CreateShader(gl.VERTEX_SHADER), gl.CreateShader(gl.FRAGMENT_SHADER)
    vertShader.Source(vert)
    fragShader.Source(frag)

    vertShader.Compile()
    fragShader.Compile()

    prog := gl.CreateProgram()

    prog.AttachShader(vertShader)
    prog.AttachShader(fragShader)
    prog.Link()
    prog.Validate()
    fmt.Println(prog.GetInfoLog())

    return prog
}

func main() {
    var window *sdl.Window
    var context sdl.GLContext
    var event sdl.Event
    var running bool
    var err error

    runtime.LockOSThread()

    if 0 != sdl.Init(sdl.INIT_EVERYTHING) {
        panic(sdl.GetError())
    }
    window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED,
        sdl.WINDOWPOS_UNDEFINED,
        winWidth, winHeight, sdl.WINDOW_OPENGL)
    if err != nil {
        panic(err)
    }
    if window == nil {
        panic(sdl.GetError())
    }
    context = sdl.GL_CreateContext(window)
    if context == nil {
        panic(sdl.GetError())
    }

    if gl.Init() != 0 {
        panic("gl error")
    }

    gl.ClearColor(1.0, 1.0, 1.0, .5)
    gl.Viewport(0, 0, winWidth, winHeight)

    program := MakeProgram(vertexShaderSource, fragmentShaderSource)
    defer program.Delete()

    matrixID := program.GetUniformLocation("MVP")
    Projection := mathgl.Perspective(45.0, 4.0/3.0, 0.1, 100.0)
    View := mathgl.LookAt(4.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    Model := mathgl.Ident4f()
    MVP := Projection.Mul4(View).Mul4(Model) 

    gl.Enable(gl.DEPTH_TEST)
    gl.DepthFunc(gl.LESS)
    gl.Enable(gl.BLEND)
    gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

    vertexArray := gl.GenVertexArray()
    defer vertexArray.Delete()
    vertexArray.Bind()

    buffer := gl.GenBuffer()
    defer buffer.Delete()
    buffer.Bind(gl.ARRAY_BUFFER)
    gl.BufferData(gl.ARRAY_BUFFER, len(triangle_vertices)*4, &triangle_vertices, gl.STATIC_DRAW)

    running = true
    for running {
        for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            switch t := event.(type) {
            case *sdl.QuitEvent:
                running = false
            case *sdl.MouseMotionEvent:

                fmt.Printf(string(t.Timestamp))
            }
        }

        gl.Clear(gl.COLOR_BUFFER_BIT) // | gl.DEPTH_BUFFER_BIT)
        program.Use()
        matrixID.UniformMatrix4fv(false, MVP)
        attribLoc := gl.AttribLocation(0)
        attribLoc.EnableArray()
        buffer.Bind(gl.ARRAY_BUFFER)
        attribLoc.AttribPointer(3, gl.FLOAT, false, 0, nil)

        gl.DrawArrays(gl.TRIANGLES, 0, 3)

        attribLoc.DisableArray()

        time.Sleep(50 * time.Millisecond)

        sdl.GL_SwapWindow(window)
    }

    sdl.GL_DeleteContext(context)
    window.Destroy()
    sdl.Quit()
}



const (
    winTitle           = "OpenGL Shader"
    winWidth           = 640
    winHeight          = 480
    vertexShaderSource = `
#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;
void main(){

    gl_Position = MVP * vec4 (vertexPosition_modelspace,1.0);

}
`
    fragmentShaderSource = `
#version 330 core

// Ouput data
out vec3 color;

void main()
{

    // Output color = red 
    color = vec3(1,0,0);

}
`
)

var triangle_vertices = []float32{
    -.5, -.5, -.5,
    .5, -.5, -.5,
    0.0, 0.5, -.5,
}

So I'm still having trouble drawing a simple shape on the screen. I made a few changes such as simplifying my shape (a triangle). I created coordinates so they would be more towards the -z axis so I would be able to see them but that has not worked. I then set the MVP matrix (moving the camera back some) just to make sure. My shaders are simple as I am only passing in a vec3 vertex position and mat4 MVP matrix so believe shaders are working correctly? Sorry for all the confusion, i think i maybe missing something here.

Update: I also ran the version commands for opengl:

fmt.Println(gl.GetString(gl.VERSION))
fmt.Println(gl.GetString(gl.VENDOR))
fmt.Println(gl.GetString(gl.RENDERER))

for which the output was:

4.5.0 NVIDIA 347.09 NVIDIA Corporation GeForce GTX 650 Ti/PCIe/SSE2

Not sure if this has any impact?

Update: I have looked at some more examples and decided to try and add some sdl attributes but still no luck:

sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
sdl.GL_SetAttribute(sdl.GL_RED_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_GREEN_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_BLUE_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_ALPHA_SIZE, 8)

Update:

I modified this post to just include more recent code to not scare people away from TLDR.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dragon4808 2015-01-26 04:51
    已采纳

    I finally figured out what my problem was in this code.

    The first thing I had to do was

    positionAttrib := program.GetAttribLocation("vertexPosition_modelspace")
    

    for all the input variables going into the vertex shader. This was done after binding the VBO for each array.

    Next,

    If you notice my code above:

    gl.BufferData(gl.ARRAY_BUFFER, len(triangle_vertices)*4, &triangle_vertices, gl.STATIC_DRAW)
    

    I simply replaced it with triangle_vertices array, and not the address:

    gl.BufferData(gl.ARRAY_BUFFER, len(triangle_vertices)*4, triangle_vertices, gl.STATIC_DRAW)
    

    Doing this seemed to fix it.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dousa2794 2015-10-29 07:44

    I would post this as a comment, but I do not yet have enough reputation.

    The solution already provided nearly solved my similar issue, however not quite.

    Where the provided solution was

    gl.BufferData(gl.ARRAY_BUFFER, len(triangle_vertices)*4, triangle_vertices, gl.STATIC_DRAW)
    

    The actual code which solved my issue is

    gl.BufferData(gl.ARRAY_BUFFER, len(triangle_vertices)*4, gl.Ptr(triangle_vertices), gl.STATIC_DRAW)
    

    I have also answered a similar question with this, but in more detail, which can be found here: OpenGL Vertex Buffer doesn't draw anything in golang

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题