func render(w *glfw.Window, r *gResources) { /* width, height := w.GetFramebufferSize() gl.Viewport(0, 0, int32(width), int32(height)) gl.Clear(gl.COLOR_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() */ //////////////// gl.UseProgram(r.program) gl.Uniform1f(r.uniforms.fadeFactor, r.fadeFactor) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.textures[0]) gl.Uniform1i(r.uniforms.textures[0], 0) gl.ActiveTexture(gl.TEXTURE1) gl.BindTexture(gl.TEXTURE_2D, r.textures[1]) gl.Uniform1i(r.uniforms.textures[1], 1) gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer) gl.VertexAttribPointer( uint32(r.attributes.position), /* attribute */ 2, /* size */ gl.FLOAT, /* type */ false, /* normalized? */ 8, /* stride */ gl.PtrOffset(0)) /* array buffer offset */ gl.EnableVertexAttribArray(uint32(r.attributes.position)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer) gl.DrawElements( gl.TRIANGLE_STRIP, /* mode */ 4, /* count */ gl.UNSIGNED_INT, /* type */ gl.PtrOffset(0)) /* element array buffer offset */ gl.DisableVertexAttribArray(uint32(r.attributes.position)) }
func (r *Renderer) Render() { // defer glfw.Terminate() shader := r.Shaders.textureFlat program := shader.program // gl.UseProgram(program) // gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00")) // // Configure global settings gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(1.0, 1.0, 1.0, 1.0) // // angle += elapsed // r.Mesh.modelView = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0}) // Render // gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &r.Mesh.modelView[0]) time := glfw.GetTime() _ = time - r.PreviousTime r.PreviousTime = time // fmt.Println(elapsed * 100) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UniformMatrix4fv(shader.uniforms["projection"], 1, false, &r.Projection[0]) gl.UniformMatrix4fv(shader.uniforms["camera"], 1, false, &r.Camera[0]) // TODO : batch triangles and use multiple textures for _, mesh := range r.Meshes { gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &mesh.modelView[0]) gl.Uniform1i(shader.uniforms["tex"], 0) gl.BindVertexArray(mesh.vao) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, mesh.textures[0]) gl.DrawArrays(gl.TRIANGLES, 0, int32(len(mesh.verticies)/5)) } // Maintenance r.Window.SwapBuffers() glfw.PollEvents() if r.Ready == false { r.Ready = true } }
func updateScreenTexture(texture uint32, pixels []maths.Rgba, width, height int) { gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.FLOAT, gl.Ptr(pixels)) }
func (loader *Loader) LoadTexture(file string) (uint32, error) { imgFile, err := os.Open(file) if err != nil { // Get the Folder of the current Executable dir, err := osext.ExecutableFolder() if err != nil { return 0, err } // Read the file and return content or error var secondErr error imgFile, secondErr = os.Open(fmt.Sprintf("%s/%s", dir, file)) if secondErr != nil { return 0, secondErr } } img, _, err := image.Decode(imgFile) if err != nil { return 0, err } rgba := image.NewRGBA(img.Bounds()) if rgba.Stride != rgba.Rect.Size().X*4 { return 0, fmt.Errorf("unsupported stride") } draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src) var texture uint32 gl.GenTextures(1, &texture) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix)) return texture, nil }
func createScreenTexture(width, height int) uint32 { var texture uint32 gl.GenTextures(1, &texture) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA32F, int32(width), int32(height), 0, gl.RGBA, gl.FLOAT, nil) return texture }
func createTexture(file string) (uint32, error) { imgFile, err := os.Open(file) if err != nil { return 0, err } img, _, err := image.Decode(imgFile) if err != nil { return 0, err } rgba := image.NewRGBA(img.Bounds()) if rgba.Stride != rgba.Rect.Size().X*4 { return 0, fmt.Errorf("unsupported stride") } draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src) var texture uint32 gl.GenTextures(1, &texture) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix)) return texture, nil }
func (objectLoader *WavefrontObject) DrawObject(shaderProgram uint32) { for _, object := range objectLoader.Objects { // Reads the uniform Locations modelUniform := gl.GetUniformLocation(shaderProgram, gl.Str("model\x00")) ambientUniform := gl.GetUniformLocation(shaderProgram, gl.Str("ambient\x00")) diffuseUniform := gl.GetUniformLocation(shaderProgram, gl.Str("diffuse\x00")) specularUniform := gl.GetUniformLocation(shaderProgram, gl.Str("specular\x00")) emissiveUniform := gl.GetUniformLocation(shaderProgram, gl.Str("emissive\x00")) // Send our uniforms variables to the currently bound shader if object.Material != nil { gl.Uniform4f(ambientUniform, object.Material.KaR, object.Material.KaG, object.Material.KaB, object.Material.Tr) // Ambient colour. gl.Uniform4f(diffuseUniform, object.Material.KdR, object.Material.KdG, object.Material.KdB, object.Material.Tr) // Diffuse colour. gl.Uniform4f(specularUniform, object.Material.KsR, object.Material.KsG, object.Material.KsB, object.Material.Tr) // Specular colour. gl.Uniform4f(emissiveUniform, object.Material.KeR, object.Material.KeG, object.Material.KeB, object.Material.Tr) // Emissive colour. if object.Material.Texture != 0 { textureUniform := gl.GetUniformLocation(shaderProgram, gl.Str("DiffuseTextureSampler\x00")) gl.Uniform1i(textureUniform, 0) normalTextureUniform := gl.GetUniformLocation(shaderProgram, gl.Str("NormalTextureSampler\x00")) gl.Uniform1i(normalTextureUniform, 1) specularTextureUniform := gl.GetUniformLocation(shaderProgram, gl.Str("SpecularTextureSampler\x00")) gl.Uniform1i(specularTextureUniform, 2) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, object.Material.Texture) gl.ActiveTexture(gl.TEXTURE1) gl.BindTexture(gl.TEXTURE_2D, object.Material.NormalMap) gl.ActiveTexture(gl.TEXTURE2) gl.BindTexture(gl.TEXTURE_2D, object.Material.SpecularMap) } if object.Material.Tr < 1.0 { // Enables Transparencies gl.Enable(gl.BLEND) // gl.BlendFunc(gl.SRC_COLOR, gl.ONE) // gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) } } // Geometry var size int32 // Used to get the byte size of the element (vertex index) array gl.UniformMatrix4fv(modelUniform, 1, false, &object.Model[0]) // Get the vertices uniform position verticesUniform := uint32(gl.GetAttribLocation(shaderProgram, gl.Str("position\x00"))) normalsUniform := uint32(gl.GetAttribLocation(shaderProgram, gl.Str("normal\x00"))) textureCoordinatesUniform := uint32(gl.GetAttribLocation(shaderProgram, gl.Str("texcoord\x00"))) // Describe our vertices array to OpenGL (it can't guess its format automatically) gl.BindBuffer(gl.ARRAY_BUFFER, object.VertexBufferObjectVertices) gl.VertexAttribPointer( verticesUniform, // attribute index 3, // number of elements per vertex, here (x,y,z) gl.FLOAT, // the type of each element false, // take our values as-is 0, // no extra data between each position nil, // offset of first element ) gl.EnableVertexAttribArray(normalsUniform) gl.BindBuffer(gl.ARRAY_BUFFER, object.VertexBufferObjectNormals) gl.VertexAttribPointer( normalsUniform, // attribute 3, // number of elements per vertex, here (x,y,z) gl.FLOAT, // the type of each element false, // take our values as-is 0, // no extra data between each position nil, // offset of first element ) gl.EnableVertexAttribArray(textureCoordinatesUniform) gl.BindBuffer(gl.ARRAY_BUFFER, object.VertexBufferObjectTextureCoords) gl.VertexAttribPointer( textureCoordinatesUniform, // attribute 2, // number of elements per vertex, here (u,v) gl.FLOAT, // the type of each element false, // take our values as-is 0, // no extra data between each position nil, // offset of first element ) size = int32(len(object.Vertex)) gl.PointSize(3.0) // Enable this line to show model in wireframe switch objectLoader.DrawMode { case 1: gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) default: gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) } gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, object.VertexBufferObjectFaces) gl.GetBufferParameteriv(gl.ELEMENT_ARRAY_BUFFER, gl.BUFFER_SIZE, &size) gl.DrawElements(gl.TRIANGLES, int32(len(object.Faces)), gl.UNSIGNED_SHORT, nil) // gl.DrawElements(gl.POINTS, int32(len(object.Faces)), gl.UNSIGNED_SHORT, nil) // Disables transparencies gl.Disable(gl.BLEND) } }