Color Red not working

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Color Red not working

Pufferenco
i have been with this issue a couple of days now and i'm abselutly lost so basicly the color red or anything wich requires the R of rgb doesnt work but only with vertices textrenderer and images work fine here is my code


package com.pufferenco.OpenGL;

import com.jogamp.opengl.*;
import com.jogamp.opengl.util.texture.Texture;
import com.pufferenco.*;

import java.awt.*;
import java.io.IOException;
import java.util.Set;

import static com.pufferenco.Main.player;

public class GraphicsMain extends MGL implements GLEventListener {

    public int playerX = 0;
    public int playerY = 0;
    float x;
    float y;
    SubRender Render = new SubRender();

    static int ZOOM = 1;
    int ChunkSize = 2048 ;
    int TileSize  = 128;



    @Override
    public void init(GLAutoDrawable glAutoDrawable) {
        UpdateGL(glAutoDrawable);

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST);

        RPL Loader = new RPL(glAutoDrawable);



        //Initialises Camera
        gl.glViewport(0, 0, 1024, 1024);
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0.0f, 1024, 1024, 0.0f, 0f, 1f);
        gl.glMatrixMode(gl.GL_MODELVIEW);
        //

        //Enables JoglFunc
        gl.glEnable(gl.GL_BLEND);
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
        gl.glDisable(GL2.GL_LIGHTING);




        //Toggles VSync
        if(!((boolean) Settings.get("VSync"))) gl.setSwapInterval(0);
        else {
            if(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getRefreshRate() >= 60) gl.setSwapInterval(1);
            else gl.setSwapInterval(2);
        }
        //
    }



    @Override
    public void dispose(GLAutoDrawable glAutoDrawable) {
    }


    @Override
    public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height)
    {
        UpdateGL(glAutoDrawable);

        //Updates Camera
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0.0f, width, height, 0.0f, 10f, -10.0f);
        gl.glMatrixMode(gl.GL_MODELVIEW);
        //Updates Camera

    }

    @Override
    public void display(GLAutoDrawable glAutoDrawable)
    {
        G2DDrawer.mgl = this;
        playerX = player.X;
        playerY = (player.Y)  * -1;

        x = (frameWidth / 2 - 1024) - playerX%ChunkSize;
        y = (frameHeight / 2 - 1024) - playerY%ChunkSize;
        player.GChunkY = ((playerY) - (playerY)%ChunkSize )/ ChunkSize;
        player.GChunkX = ((playerX) - (playerX)%ChunkSize )/ ChunkSize;
        UpdateGL(glAutoDrawable);

        Clear();

        {
            try {
                Render.RenderChunks();
            } catch (IOException e) {e.printStackTrace();}//Renders Chunks

        }//Background

        {
            StaticDrawTexture("assets/player_placeholder.png", frameWidth / 2,  frameHeight / 2,0, 1);
        }//Entity

        {

        }//Objects

        {
            if(Main.Debug)
            {
                SetColor("TextColor");
                drawString(0,10,"FPS : " + (int)RPL.animator.getLastFPS() + "/" + RPL.animator.getFPS());
                drawString(0,20,"X/Y : " + playerX / TileSize + "/" + playerY / TileSize);
                drawString(0,30,"Chunk:X/Y : " + player.GChunkX + "/" + player.GChunkY);
                SetColor("DefaultColor");
            }
        }//UI

    }

    private class SubRender
    {

    public void RenderChunks() throws IOException {
        //G2DDrawer Drawer = new G2DDrawer(frameWidth,frameHeight);
        for (int Yc = -1; Yc <= 1; Yc++) {for (int Xc = -1; Xc <= 1; Xc++)
        {
                /*if (Main.CHUNKS.get(UTIL.getCoords(player.GChunkX + Xc, player.GChunkY + Yc)) != null) {
                    Chunk current = Main.CHUNKS.get(UTIL.getCoords(player.GChunkX + Xc, player.GChunkY + Yc));

                    if (current.loaded)
                    {
                        for (int Yb = 0; Yb <= 15; Yb++) {for (int Xb = 0; Xb <= 15; Xb++)
                            {
                                //SetColor("Yellow");
                                //StaticDrawTexture(Main.BIOMES.get(current.Tiles[Xb][Yb].Biome).Texture, (Xb * TileSize) + (Xc * ChunkSize) + x,(Yb * TileSize) + (Yc * ChunkSize) + y, 0f,1);

                            }}//renders Blocks
                *///not needed code
                        if(Main.Debug){

                            SetColor("Yellow");
                            for (int Yb = 0; Yb <= 15; Yb++) {for (int Xb = 0; Xb <= 15; Xb++) {
                                //DrawLine((Xc * ChunkSize) - x,(Yb * TileSize) + (Yc * ChunkSize) + y, (Xc * ChunkSize + ChunkSize)- x, (Yb * TileSize) + (Yc * ChunkSize) + y);
                                DrawSquare((Xc * ChunkSize) + (Xb * TileSize) + x,(Yc * ChunkSize) + (Yb * TileSize) + y, ChunkSize, ChunkSize);
                            }}
                            SetColor();


                            //SetColor(1,0,0);
                            //DrawSquare("Yellow",(Xc * ChunkSize) + x,(Yc * ChunkSize) + y, ChunkSize, ChunkSize);
                            //SetColor(1,1,1);
                        }

                    }
                }


            }
        }
    }








package com.pufferenco.OpenGL;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.util.awt.TextRenderer;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureCoords;

import java.util.HashMap;

public class MGL {

    interface Function {void call();}

    static final int QUADS = 7;
    static float frameHeight = 0;
    static float frameWidth = 0;
    static TextRenderer textRenderer = null;

    GL2 gl;
    static Texture currentTex = null;
    static boolean Debug = true;
    private static final Color DefaultColor = new Color(255,255,255,0);

    static HashMap<String, textureObject> Textures = new HashMap<>();
    private final HashMap<Integer,Boolean> GLSettings= new HashMap<>();

    void UpdateGL(GLAutoDrawable glAutoDrawable) {
        gl = glAutoDrawable.getGL().getGL2();
        //if(Debug)
        //glAutoDrawable.setGL(new DebugGL2(gl));
    }

    void Clear() {
        //gl.glClearColor(ClearColor[0], ClearColor[1], ClearColor[2], ClearColor[3]);
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
    }

    void SetClearColor(float Red, float Green, float Blue, float Alpha) {
        gl.glClearColor(Red,Green,Blue,Alpha);
    }

    void Rotate(float angle, float xAxis, float yAxis, float zAxis) {
        gl.glRotatef(angle, xAxis, yAxis, zAxis);
    }

    private void TextureUpdate(Texture texture)
    {
        if(texture == null) {new NullPointerException("texture is null").printStackTrace();}
        if(texture != currentTex) {
            if(currentTex != null) currentTex.disable(gl);
            currentTex = texture;
            texture.enable(gl);
            texture.bind(gl);
        }//Updates Texture
    }

     void DrawTexture(textureObject TextureObject, float x, float y , float z, float Amplifier) {
        //float temp = playerX;
        //playerX = playerY; //* (frameWidth / frameHeight);
        //playerY = (temp * -1); //* (frameHeight / frameWidth);

        Texture texture = TextureObject.texture;

        TextureUpdate(texture);

        float height = ((texture.getHeight()) * Amplifier);
        float width = ((texture.getWidth()) * Amplifier);

        RawDrawTexture(texture,x,y,z,height,width);

    }

    void RawDrawTexture(Texture texture, float x, float y, float z, float height, float width)
    {
        //gl.glColor3f(CurrentColor.getRed(), CurrentColor.getGreen(), CurrentColor.getBlue());

        class T { void P(int Corner) {
                TextureCoords texcoords = texture.getImageTexCoords();
                if(Corner == 0)gl.glTexCoord2f(texcoords.left(), texcoords.bottom());
                else if(Corner == 1)gl.glTexCoord2f(texcoords.left(), texcoords.top());
                else if(Corner == 2)gl.glTexCoord2f(texcoords.right(), texcoords.top());
                else if(Corner == 3)gl.glTexCoord2f(texcoords.right(), texcoords.bottom());
            }}
        T t = new T();

        gl.glBegin(gl.GL_QUADS);
        gl.glVertex3f(x, y , z);
        t.P(3);

        gl.glVertex3f(x, y + height , z);
        t.P(0);

        gl.glVertex3f(x + width, y + height, z);
        t.P(1);

        gl.glVertex3f(x + width, y, z);
        t.P(2);

        gl.glEnd();
    }



    void DrawTexture(String TextureName, int X, int Y , float Z, float Amplifier) {
        float x = ( (float)X) / frameWidth;
        float y = ((float)Y) / frameHeight;
        textureObject TextureObject = Textures.get(TextureName);
        DrawTexture(TextureObject, x, y, Z, Amplifier);
    }

    void DrawTexture(String TextureName, float X, float Y , float Z, float Amplifier) {
        //System.out.println(X + "/" + Y);
        textureObject TextureObject = Textures.get(TextureName);
        DrawTexture(TextureObject, X, Y, Z, Amplifier);
    }

    void StaticDrawTexture(String TextureName, float X, float Y , float Z, float Amplifier) {
        textureObject TextureObject = Textures.get(TextureName);
        //.out.println(X / GraphicsMain.frameWidth + "/" + Y / GraphicsMain.frameHeight + ":" + player.X + "/" + player.Y);
        DrawTexture(TextureObject, X, Y , Z, Amplifier + TextureObject.size);
    }

    void SetColor(String color)
    {
        Color CC = Color.get(color);

        textRenderer.setColor((float)CC.getRed(),(float)CC.getGreen(),(float)CC.getBlue(),(float)CC.getAlpha());
        gl.glColor4d(CC.getRed(),CC.getGreen(),CC.getBlue(),CC.getAlpha());
    }
    void SetColor()
    {
        Color CC = Color.get("DefaultColor");

        textRenderer.setColor((float)CC.getRed(),(float)CC.getGreen(),(float)CC.getBlue(),(float)CC.getAlpha());
        gl.glColor4d(CC.getRed(),CC.getGreen(),CC.getBlue(),CC.getAlpha());
    }

    void drawString(float x, float y, String text)
    {
        if(textRenderer != null)
        {
            textRenderer.beginRendering((int) frameWidth, (int) frameHeight);
            textRenderer.draw(text, (int) ((int) x), (int) (frameHeight - (int) (y)));
            textRenderer.endRendering();
        }
    }


    void DrawSquare(float x, float y, float height, float width)
    {
        gl.glColor3f(1,0,0);
        float xw = x + width;
        float yh = y + height;

        gl.glBegin(GL2.GL_LINES);
        gl.glVertex2d(x, yh);
        gl.glVertex2d(xw, yh);

        //Drawing bottom edge
        gl.glVertex2d(x,y);
        gl.glVertex2d(xw, y);

        //Drawing right edge
        gl.glVertex2d(x, yh);
        gl.glVertex2d(x, y);

        //Drawing left edge
        gl.glVertex2d(xw, yh);
        gl.glVertex2d(xw, y);

        gl.glEnd();
    }
}









package com.pufferenco.OpenGL;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.awt.TextRenderer;
import com.jogamp.opengl.util.texture.TextureIO;
import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
import com.pufferenco.Data;
import com.pufferenco.Main;
import com.pufferenco.Settings;


import static com.pufferenco.OpenGL.textureObject.TextureFunctions;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.IntBuffer;
import java.util.ArrayList;

public class RPL
{
    private static RPL rpl = null;
    private GL2 gl;
    static public FPSAnimator animator = null;
    static boolean trackTextureAmount;
    int TextureAmount = 1;

    public RPL(GLAutoDrawable glAutoDrawable)
    {
        rpl = this;
        gl = glAutoDrawable.getGL().getGL2();

        LoadTextures();

        int[] textureIds = new int[TextureAmount];
        gl.glGenTextures(TextureAmount, IntBuffer.wrap(textureIds));

        trackTextureAmount = false;


        LoadTextures();
    }

    static public void CreateGraphicRenderer(String name,int fps, int frameHeight,int frameWidth)
    {
        final GLProfile gp = GLProfile.get(GLProfile.GL2);
        GLCapabilities cap = new GLCapabilities(gp);
        final GLCanvas gc = new GLCanvas(cap);
        GraphicsMain bl = new GraphicsMain();
        gc.addGLEventListener(bl);
        gc.setSize(frameWidth,frameHeight);
        MGL.frameHeight = frameHeight;
        MGL.frameWidth = frameWidth;
        final JFrame frame = new JFrame (name);
        frame.setSize(frameWidth,frameHeight);
        frame.add(gc);
        frame.setVisible(true);
        gc.setFocusable(true);


        Color.GenColors();

        MGL.textRenderer = new TextRenderer(Settings.get("Font").equals("SystemDefault")? new JLabel().getFont() : Font.getFont((String) Settings.get("SystemDefault")), (Boolean) Settings.get("AntialiasingText"), false);


        {
            gc.addKeyListener(Main.Keylistener());//Key Listener

            {
                gc.addComponentListener(new java.awt.event.ComponentAdapter() {
                    public void componentResized(java.awt.event.ComponentEvent event) {
                        int width = event.getComponent().getWidth();
                        int height = event.getComponent().getHeight();
                        gc.setSize(width, height);
                        MGL.frameHeight = height;
                        MGL.frameWidth = width;
                    }
                });

                gc.addMouseWheelListener(e -> {
                    //if(e.getWheelRotation() <= -1){if(player.selectedFrame < 9)player.selectedFrame++;else player.selectedFrame = 1;}
                    //else if(e.getWheelRotation() >= 1)if(player.selectedFrame > 1)player.selectedFrame--;else player.selectedFrame = 9;
                    if(e.getWheelRotation() <= -1)GraphicsMain.ZOOM += 10;
                    else if(e.getWheelRotation() >= 1)GraphicsMain.ZOOM -= 10;
                });
            }//ResizeEvent
        }//Subscribes to input events

        {
            animator = new FPSAnimator(gc, fps, true);
            animator.setUpdateFPSFrames(60, null);
            animator.start();
        }//Create Animator

    }

    private textureObject NewTexture(String name, String Path, float size)
    {
        if(!trackTextureAmount)
        {
        if ((boolean) Settings.get("CompactImage"))
            return NewTexture(name,imageHandler.read(Path, 1),size * 1.5f);
        else
            return NewTexture(name,imageHandler.read(Path, (int) size),1);
        }
        else
            TextureAmount++;

        return new textureObject(null,0);
    }

    private textureObject NewTexture(String name, BufferedImage Image, float size)
    {
        textureObject e = new textureObject(AWTTextureIO.newTexture(gl.getGLProfile(),Flip(Image),true),size);
        //e.texture.setMustFlipVertically(true);
        MGL.Textures.put(name,e);
        return e;
    }

    private BufferedImage Flip(BufferedImage bufferedImage)
    {
        AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
        tx.translate(-bufferedImage.getWidth(null), 0);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        return op.filter(bufferedImage, null);
    }

    private static void UpdateTex(String name, BufferedImage Image, float size)
    {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(Image, "jpeg", os);
            InputStream is = new ByteArrayInputStream(os.toByteArray());
            MGL.Textures.get(name).texture.updateImage(rpl.gl, TextureIO.newTextureData(rpl.gl.getGLProfile(),is,false,
                    (boolean)Settings.get("CompressedUpdate")? "png" : "bmp"
            ));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void LoadTextures()
    {
        {
            NewTexture("assets/player_placeholder.png", "assets/player_placeholder.png",2);
            NewTexture("TEST","assets/test.png", 4);
        }//Misc
        {
            NewTexture("TREE",  "assets/TREE.png", 4);
            NewTexture("Tile",  "assets/Tile.png", 1);
            NewTexture("Ivy",  "assets/Ivy.png", 4);
        }//Objects
        {
            NewTexture("stick",  "assets/Textures/Objects/Stick.png", 2);
            //TextureLoader.Create("stick", 2, "assets/Textures/Objects/default.png");
        }//Items
        {
            NewTexture("grass",  "assets/Textures/TILES/grass.png", 2);
            NewTexture("sand", "assets/Textures/TILES/sand.png", 2);
            NewTexture("shallow_ocean",  "assets/Textures/TILES/shallow_ocean.png", 2);
            NewTexture("ocean",  "assets/Textures/TILES/ocean.png", 2);
            NewTexture("deep_ocean",  "assets/Textures/TILES/deep_ocean.png", 2);
        }//TILES
        {
            NewTexture("Upper_UI", "assets/textures/UI/UI.png", 4);
            NewTexture("icons","assets/Textures/UI/Icons.png", 4);
            NewTexture("Decoration","assets/Textures/UI/DisplayDecoration.png", 4);
            NewTexture("ItemSlot",  "assets/Textures/UI/ItemSlot_v2.png", 4);
            NewTexture("BackSlot",  "assets/Textures/UI/BackSlot.png", 1);
            NewTexture("ReversedBackSlot",  "assets/Textures/UI/ReversedBackSlot.png", 1);
            NewTexture("Belt",  "assets/Textures/UI/Belt.png", 1);
            NewTexture("SelectedItemSlot",  "assets/Textures/UI/ItemSlot_v2_selected.png", 4);
            NewTexture("Selection", "assets/Textures/UI/Selection.png", 4);
        }//UI
        {
            //NewTexture("BackGround", 4);
        }//Template
    }



}


the first is the main code the second are all the functions and the fourth is for starting
Reply | Threaded
Open this post in threaded view
|

Re: Color Red not working

gouessej
Administrator
Hello

Call glColor3f between glBegin and glEnd... or rather use VAO + VBO.
Julien Gouesse | Personal blog | Website