snowkit / snow

A low level cross platform framework for Haxe. Mac, Windows, Linux, Android, iOS, WebGL.
http://snowkit.org/snow
MIT License
133 stars 35 forks source link

Help: Can't render texture in OpenGL #96

Closed darmie closed 7 years ago

darmie commented 7 years ago

I am trying to render a 2d texture using Snow OpenGL API, but I can't seem to get it to work. All I get is a blank screen.

This is my Main.hx

import snow.api.Debug.*;
import snow.types.Types;
import snow.modules.opengl.GL;

typedef UserConfig = {}

@:log_as('app')
class Main extends snow.App 
{
    public var texture(default, null): GLTexture;

    private var image:AssetImage = null;

    function new() {}

    override function config( config:AppConfig ) : AppConfig 
    {

        config.window.title = 'Enzo Game Engine';

        return config;

    }

    override function ready() 
    {

        log('ready');

        //app.window.onrender = render;
        var imgProm = app.assets.image("assets/test.png");
        imgProm.then(function(d){

            image = d;

            log(d.image.pixels);

        }, function(e){
            throw e;
        });

    }

    override function onkeyup( keycode:Int, _,_, mod:ModState, _,_ ) 
    {

        if( keycode == Key.escape ) 
        {
            app.shutdown();
        }

    }

    override function update( delta:Float ) 
    {

    }

    override function tick( delta:Float ) 
    {

        GL.clearColor(1.0, 1.0, 1.0, 1.0);
        GL.clear(GL.COLOR_BUFFER_BIT);

        GL.enable(GL.TEXTURE_2D);

        //sprite.draw();

        texture = GL.createTexture();

        GL.bindTexture(GL.TEXTURE_2D, texture);
        GL.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, this.image.image.width, this.image.image.height, 0, GL.RGBA, GL.UNSIGNED_BYTE, this.image.image.pixels);
        GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);
        GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR);
        GL.generateMipmap(GL.TEXTURE_2D);

        GL.bindTexture(GL.TEXTURE_2D, null);

        GL.drawElements(GL.TRIANGLES, 1, GL.UNSIGNED_SHORT, 0);

    }

}