[solved]GLSL problem

Hello :wink:

I have some problem. I tried use GLSL into JOGL. I draw simple traingle which red green blue vertices. I obtain all blue triangle. What i do wrong?

OK I SOLVED IT:
gl.glShadeModel(GL.GL_SHADER_TYPE);
against:
gl.glShadeModel(GL.GL_FLAT);

Here is my code:

frag.txt ā€”


void main()
{
    gl_FragColor = gl_Color;
}

vert.txt ā€”


void main()
{
    gl_FrontColor = gl_Color;
    gl_Position = ftransform();
}


.java ---
package org.yourorghere;

import com.sun.opengl.util.Animator;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class SimpleShaderExample extends GLCanvas implements GLEventListener {

   private boolean updateUniformVars = true;
   private int vertexShaderProgram;
   private int fragmentShaderProgram;
   private int shaderprogram;
   private final Animator animator = new Animator(this);
   private float x = -2;
   private float y = -2;
   private float height = 4;
   private float width = 4;
   private int iterations = 255;

   public SimpleShaderExample() {
      addGLEventListener(this);
        animator.start();
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
     
        // generate a VBO pointer / handle
       
      
        // Enable VSync
        gl.setSwapInterval(1);
      gl.glShadeModel(GL.GL_FLAT);
      try {
         attachShaders(gl);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private String[] loadShaderSrc(String name){
      StringBuilder sb = new StringBuilder();
      try{
         InputStream is = getClass().getResourceAsStream(name);
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String line;
         while ((line = br.readLine())!=null){
            sb.append(line);
            sb.append('\n');
         }
         is.close();
      }
      catch (Exception e){
         e.printStackTrace();
      }
      System.out.println("Shader is "+sb.toString());
      return new String[]{sb.toString()};
   }

   private void attachShaders(GL gl) throws Exception {
      vertexShaderProgram = gl.glCreateShader(GL.GL_VERTEX_SHADER);
      fragmentShaderProgram = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);

      String[] vsrc = loadShaderSrc( "vert.txt" );
      gl.glShaderSource(vertexShaderProgram, 1, vsrc, null, 0);
      gl.glCompileShader(vertexShaderProgram);

      String[] fsrc = loadShaderSrc( "frag.txt");
      gl.glShaderSource(fragmentShaderProgram, 1, fsrc, null, 0);
      gl.glCompileShader(fragmentShaderProgram);

      shaderprogram = gl.glCreateProgram();
      gl.glAttachShader(shaderprogram, vertexShaderProgram);
      gl.glAttachShader(shaderprogram, fragmentShaderProgram);
      gl.glLinkProgram(shaderprogram);
      gl.glValidateProgram(shaderprogram);
      IntBuffer intBuffer = IntBuffer.allocate(1);
      gl.glGetProgramiv(shaderprogram, GL.GL_LINK_STATUS,intBuffer);
      if (intBuffer.get(0)!=1){
         gl.glGetProgramiv(shaderprogram, GL.GL_INFO_LOG_LENGTH,intBuffer);
         int size = intBuffer.get(0);
         System.err.println("Program link error: ");
         if (size>0){
            ByteBuffer byteBuffer = ByteBuffer.allocate(size);
            gl.glGetProgramInfoLog(shaderprogram, size, intBuffer, byteBuffer);
            for (byte b:byteBuffer.array()){
               System.err.print((char)b);
            }
         } else {
            System.out.println("Unknown");
         }
         System.exit(1);
      }
      gl.glLinkProgram(shaderprogram);
      gl.glUseProgram(shaderprogram);
   }

   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        if (height <= 0) { // avoid a divide by zero error!

            height = 1;
        }
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluOrtho2D(0, 1, 0, 1);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }



    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
       
      //if (updateUniformVars){
      //    updateUniformVars(gl);m
      //}

        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();

        
        // Draw A Quad
        //int temp = gl.glGetUniformLocation(shaderprogram, "t");
        //int min = gl.glGetUniformLocation(shaderprogram, "min");
        //int max = gl.glGetUniformLocation(shaderprogram, "max");
           //gl.glUniform1f(temp, 500.0f);
           //gl.glUniform1f(min, 400.0f);
           //gl.glUniform1f(max, 900.0f);
       gl.glBegin(GL.GL_TRIANGLES);
       
       
            gl.glColor3f(1.0f,0.0f,0.0f);
            gl.glVertex3f(0.5f, 1.0f, 0.0f);
            
            gl.glColor3f(0.0f,1.0f,0.0f);
            gl.glVertex3f(0.0f, -1.0f, 0.0f);
            
            gl.glColor3f(0.0f,0.0f,1.0f);
            gl.glVertex3f(1.0f, -1.0f, 0.0f);
  
      // Done Drawing The Quad
      gl.glEnd();

        // Flush all drawing operations to the graphics card
        //gl.glFlush();
    }


   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame jf = new JFrame();
            jf.setSize(800,600);
            jf.getContentPane().setLayout(new BorderLayout());
            jf.getContentPane().add(new SimpleShaderExample(), BorderLayout.CENTER);
            jf.setVisible(true);
            jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
         }
      });

   }
}

Iā€™m glad to see you fixed your problem :slight_smile: