Пример #1
0
func (e *Engine) Start() {
	var err error

	e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl")
	if err != nil {
		panic(fmt.Sprintln("LoadProgram failed:", err))
	}

	e.shader.models, err = wavefront.Read("gopher.obj")
	check(err)

	e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "vertCoord")
	e.shader.projection = gl.GetUniformLocation(e.shader.program, "projection")
	e.shader.view = gl.GetUniformLocation(e.shader.program, "view")
	e.shader.modelx = gl.GetUniformLocation(e.shader.program, "modelx")
	e.shader.modely = gl.GetUniformLocation(e.shader.program, "modely")
	e.shader.color = gl.GetUniformLocation(e.shader.program, "color")

	for _, model := range e.shader.models {
		for _, group := range model.Groups {
			data := f32.Bytes(binary.LittleEndian, group.Vertexes...)
			color := group.Material.Ambient
			vertexCount := len(data)

			databuf := gl.CreateBuffer()
			e.shape.bufs = append(e.shape.bufs, Buf{vertexCount, databuf, color})

			gl.BindBuffer(gl.ARRAY_BUFFER, databuf)
			gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW)

		}

	}
}
Пример #2
0
func (e *Engine) Start() {
	var err error

	e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl")
	if err != nil {
		panic(fmt.Sprintln("LoadProgram failed:", err))
	}

	e.shader.models, err = wavefront.Read("spiritframe.obj")
	check(err)

	e.shader.projectionmatrix = gl.GetUniformLocation(e.shader.program, "u_projectionMatrix")
	e.shader.viewmatrix = gl.GetUniformLocation(e.shader.program, "u_viewMatrix")
	e.shader.modelmatrix = gl.GetUniformLocation(e.shader.program, "u_modelMatrix")
	e.shader.normalmatrix = gl.GetUniformLocation(e.shader.program, "u_normalMatrix")
	e.shader.lightdir = gl.GetUniformLocation(e.shader.program, "u_lightDirection")
	e.shader.lightmatrix = gl.GetUniformLocation(e.shader.program, "u_lightmatrix")

	e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "a_vertex")
	e.shader.normal = gl.GetAttribLocation(e.shader.program, "a_normal")
	e.shader.texcoord = gl.GetAttribLocation(e.shader.program, "a_texCoord")

	for _, model := range e.shader.models {
		for _, group := range model.Groups {
			//颜色
			color := group.Material.Ambient
			//顶点
			data := f32.Bytes(binary.LittleEndian, group.Vertexes...)
			vertexCount := len(group.Vertexes) / 3
			databuf := gl.CreateBuffer()
			gl.BindBuffer(gl.ARRAY_BUFFER, databuf)
			gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW)
			//UV坐标
			textcoords := f32.Bytes(binary.LittleEndian, group.Textcoords...)
			uvbuf := gl.CreateBuffer()
			gl.BindBuffer(gl.ARRAY_BUFFER, uvbuf)
			gl.BufferData(gl.ARRAY_BUFFER, textcoords, gl.STATIC_DRAW)
			//发现坐标
			normals := f32.Bytes(binary.LittleEndian, group.Normals...)
			normalbuf := gl.CreateBuffer()
			gl.BindBuffer(gl.ARRAY_BUFFER, normalbuf)
			gl.BufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW)

			tex, _ := LoadTexture(group.Material.Texturefile)
			e.shape.Objs = append(e.shape.Objs, Obj{vcount: vertexCount, coord: databuf, color: color, uvcoord: uvbuf, tex: tex, normal: normalbuf})

		}

	}
}
Пример #3
0
func (e *Engine) Start() {
	var err error

	e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl")
	if err != nil {
		panic(fmt.Sprintln("LoadProgram failed:", err))
	}

	e.shader.models, err = wavefront.Read("girl.obj")
	check(err)

	e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "vertCoord")
	e.shader.vertTexCoord = gl.GetAttribLocation(e.shader.program, "vertTexCoord")
	e.shader.projection = gl.GetUniformLocation(e.shader.program, "projection")
	e.shader.view = gl.GetUniformLocation(e.shader.program, "view")
	e.shader.modelx = gl.GetUniformLocation(e.shader.program, "modelx")
	e.shader.modely = gl.GetUniformLocation(e.shader.program, "modely")
	e.shader.color = gl.GetUniformLocation(e.shader.program, "color")
	e.shader.useuv = gl.GetUniformLocation(e.shader.program, "useuv")

	for _, model := range e.shader.models {
		for _, group := range model.Groups {
			//颜色
			color := group.Material.Ambient
			//顶点
			data := f32.Bytes(binary.LittleEndian, group.Vertexes...)
			vertexCount := len(group.Vertexes) / 3
			databuf := gl.CreateBuffer()
			gl.BindBuffer(gl.ARRAY_BUFFER, databuf)
			gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW)
			//UV坐标
			textcoords := f32.Bytes(binary.LittleEndian, group.Textcoords...)
			uvbuf := gl.CreateBuffer()
			gl.BindBuffer(gl.ARRAY_BUFFER, uvbuf)
			gl.BufferData(gl.ARRAY_BUFFER, textcoords, gl.STATIC_DRAW)
			//贴图文件
			var useuv bool
			tex, err := LoadTexture(group.Material.Texturefile)
			if err != nil {
				useuv = false
			} else {
				useuv = true
			}
			e.shape.Objs = append(e.shape.Objs, Obj{vcount: vertexCount, coord: databuf, color: color, useuv: useuv, uvcoord: uvbuf, tex: tex})

		}

	}
}