GL_POINTS画一个圆

时间:2018-08-22 18:59:12

标签: java opengl

每当我绘制GL_POINT时,我都会遇到一个问题,它会绘制一个圆圈,而我正在使用的所有示例都显示一个正方形,我已经做过一些研究,而且看起来可能是抗锯齿,但是我没有尝试修复它。

package main.ui;

import static com.jogamp.opengl.GL4.*;

import java.nio.FloatBuffer;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GL4;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLContext;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLCanvas;

public class CanvasHandler extends GLCanvas implements GLEventListener {

private static final long serialVersionUID = 1L;

private int rendering_program;
private int[] vao = new int[1];

public CanvasHandler(int x, int y, int width, int height) { 

    setBounds(x, y, width, height);
    setVisible(true);
    addGLEventListener(this);


}

@Override
public void display(GLAutoDrawable arg0) {

    GL4 gl = (GL4) GLContext.getCurrentGL();

    float colors[] = {1.0f, 0.0f, 0.0f, 1.0f}; 
    FloatBuffer color = Buffers.newDirectFloatBuffer(colors);
    gl.glClearBufferfv(GL_COLOR, 0 , color);

    //Drawing handler
    gl.glUseProgram(rendering_program);
    gl.glPointSize(400f);
    gl.glDrawArrays(GL_POINTS, 0, 1);

    //System.out.println(GL_POINT);

}

@Override
public void init(GLAutoDrawable arg0) {

    GL4 gl = (GL4) GLContext.getCurrentGL();
    rendering_program = createShaderProgram();

    gl.glGenVertexArrays(vao.length, vao, 0);
    gl.glBindVertexArray(vao[0]);

}

private int createShaderProgram() { 

    GL4 gl = (GL4) GLContext.getCurrentGL();

    String[] rawVertexShader = {

            "#version 430 \n",
            "void main(void) \n",
            "{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); } \n"

    };
    String[] rawFragmentShader = {

            "#version 430    \n",
            "out vec4 color; \n",
            "void main(void) \n",
            "{ \n",
            //"if(gl_FragCoord.x < 200) color = vec4(0.0, 1.0f, 0.0, 1.0f); else color = vec4(0.0f,0.0f,1.0f,1.0f);",
            "color = vec4(0.0f, 0.0f, 1.0f, 1.0f);",
            "} \n"

    };

    int vertexShader = gl.glCreateShader(GL_VERTEX_SHADER);
    int fragmentShader = gl.glCreateShader(GL_FRAGMENT_SHADER);

    gl.glShaderSource(vertexShader, rawVertexShader.length, rawVertexShader, null, 0);
    gl.glShaderSource(fragmentShader, rawFragmentShader.length, rawFragmentShader, null, 0);

    gl.glCompileShader(vertexShader);
    gl.glCompileShader(fragmentShader);

    int programid = gl.glCreateProgram();
    gl.glAttachShader(programid, vertexShader);
    gl.glAttachShader(programid, fragmentShader);
    gl.glLinkProgram(programid);

    gl.glDeleteShader(vertexShader);
    gl.glDeleteShader(fragmentShader);

    return programid;

}



@Override
public void dispose(GLAutoDrawable arg0) {



}



@Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {



}

}

开始的方式是在另一个类中。

1 个答案:

答案 0 :(得分:0)

尝试在渲染点之前调用以下命令来禁用点平滑:

glDisable(GL_POINT_SMOOTH);

话虽这么说,请注意,点绘制在很大程度上取决于驱动程序,特别是对于大尺寸的 (我看到驱动程序甚至不会超过16ish的点大小)。通常,GL_POINTS仅应用作快速调试工具,并且通常用于更小的尺寸。当您需要矩形时,请使用GL_QUADS(有四个顶点,每个顶点一个)。