Blended textures show up fine, but ordinary coloured shapes do not

╄→гoц情女王★ 提交于 2019-12-12 19:24:51

问题


For some reason enabling alpha blending results in me not being able to draw run-of-the-mill coloured shapes. The order in which everything is drawn makes no difference. Even if the only thing being drawn is the coloured shape, it still won't show.

Disabling alpha blending fixes this, but disables alpha blending (obviously). This leads me to believe the problem is in how I'm initializing openGL.

The textured objects are contained in the world, which is commented out. Commenting "world.run();" out makes no difference, only disabling alpha blending does.

public class Core {

int width=800, height=600;

//World world;

public void Start(){

    try {
        Display.setDisplayMode(new DisplayMode(width,height));
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(0);
    }

    initGL();
    System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));

    boolean Close = Display.isCloseRequested();

    //world = new World(width, height);

    while(!Close){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) || Display.isCloseRequested())
            Close = true;


        //world.run();
        GL11.glColor4d(1, 0, 0, 1);
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2d(0, 0);
        GL11.glVertex2d(0, 50);
        GL11.glVertex2d(50, 50);
        GL11.glVertex2d(50, 0);
        GL11.glEnd();

        Display.update();
        //Display.sync(60);
    }

}

public void initGL(){
    GL11.glEnable(GL11.GL_TEXTURE_2D);               

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);          

        // enable alpha blending
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glViewport(0,0,width,height);

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, width, height, 0, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
}

public static void main(String args[]){
    Core m = new Core();
    m.Start();
}

}

This is for a 2D app where I'm trying to draw metaballs behind the texture of a black-and-white world map.

Run-of-the-mill coloured shapes refers to the following,

GL11.glColor4d(1, 0, 0, 1);
GL11.glBegin(GL11.GL_QUADS);
    GL11.glVertex2d(0, 0);
    GL11.glVertex2d(0, 50);
    GL11.glVertex2d(50, 50);
    GL11.glVertex2d(50, 0);
GL11.glEnd();

Even if drawn on its own, as long as alpha blending is enabled, it won't show up.

UPDATE: The constructor for world was loading (but not drawing) a texture. Removing that part of the code lets the coloured square show up. I have deduced that the problem will occur as long as a texture is loaded, regardless of whether it is displayed or not.


回答1:


You've got glEnable(GL_TEXTURE_2D) in initGL, but I don't see it disabled anywhere.

You know you have to disable texturing if you want to draw an untextured object, right?



来源:https://stackoverflow.com/questions/13545852/blended-textures-show-up-fine-but-ordinary-coloured-shapes-do-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!