[Solved] [Super Bible] Basic shader and point drawing isn't working?

I’m reading the ‘OpenGL Super Bible, 6th Edition’ and I tried to copy the exact code. But I can’t figure out why my point isn’t drawing.

package ecumene;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.PixelFormat;

public class Test1 {
	private static String fragmentSource[] = {
			"#version 430 core                     \n"+
			"                                      \n"+
			"out vec4 color;                       \n"+
			"                                      \n"+
			"void main(void)                       \n"+
			"{                                     \n"+
			"    color = vec4(0.0, 0.8, 1.0, 1.0); \n"+
			"}                                     \n"
    };
	
	private static String vertexSource[] ={
			"#version 430 core                           \n"+
			"                                            \n"+
			"void main(void)                             \n"+
			"{                                           \n"+
			"    gl_Position = vec4(0.0, 0.0, 0.5, 1.0); \n"+
			"}                                           \n"
	};
	
	private static int shaderProgram;
	private static int fragmentShader;
	private static int vertexShader;
	
	private static int vertexArray;
	
	public static void main(String[] args) throws Exception {
		{
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.setTitle("Screen");
			Display.setResizable(false);
			Display.create(new PixelFormat());
		}
		
		fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
		GL20.glShaderSource(fragmentShader, fragmentSource);
		GL20.glCompileShader(fragmentShader);
		
		vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
		GL20.glShaderSource(vertexShader, vertexSource);
		GL20.glCompileShader(vertexShader);
		
		shaderProgram = GL20.glCreateProgram();
		GL20.glAttachShader(shaderProgram, vertexShader);
		GL20.glAttachShader(shaderProgram, fragmentShader);
		GL20.glLinkProgram(shaderProgram);
		
		vertexArray = GL30.glGenVertexArrays();
		GL30.glBindVertexArray(vertexArray);
		
		GL11.glPointSize(40.0f);
		
		FloatBuffer backgroundColor = BufferUtils.createFloatBuffer(4);
		backgroundColor.put(new float[]{1.0f, 0.0f, 0.0f, 1.0f});
		backgroundColor.flip();
		
		while(!Display.isCloseRequested()){
			GL30.glClearBuffer(GL11.GL_COLOR, 0, backgroundColor);
			
			GL20.glUseProgram(shaderProgram);
			GL11.glDrawArrays(GL11.GL_POINT, 0, 1);
			
			Display.update();
		}
		
		GL20.glUseProgram(0);
		
		GL30.glDeleteVertexArrays(vertexArray);
		GL20.glDeleteShader(fragmentShader);
		GL20.glDeleteShader(vertexShader);
		GL20.glDeleteProgram(shaderProgram);
		Display.destroy();
	}
}

Here’s the result:

i think the point is not drawn cos’ there is no VBO bound to source the drawArrays call.

Yes, but this is what the book showed, but it was written in C++.

Try checking for gl errors. Also, try printing out shader errors.

I bet that you are not created your shader program properly, or something like that, so GL uses default shader.

EDIT–
GL11.glGetError()
GL20.glGetShaderLog() - or something along that

Running your code locally, checking for GL Errors, glDrawArrays throws an error: Invalid Enum. GL_POINT isn’t the right enum, it’s GL_POINTS :wink:

Here’s a good method to have around:


public static void checkGLError() {
    int error = glGetError();
    if(error != GL_NO_ERROR)
        throw new RuntimeException("GL Error: " + gluErrorString(error));
}

wooooooooooow.

Works now. Thank you so much!

As others mentioned before me use [icode]glGetError()[/icode] and [icode]glGetShaderInfoLog()[/icode]/[icode]glGetProgramInfoLog()[/icode].
Here’s how to use [icode]glGetError()[/icode]:


int error = GL11.glGetError();
if(error != GL11.GL_NO_ERROR){
	System.err.println(new OpenGLException(error).getMessage());
}

And the shader info logs:


String infoLog = GL20.glGetShaderInfoLog(id, GL20.glGetShaderi(id, GL20.GL_INFO_LOG_LENGTH));

I wrote this of the top of my head so I hope I didn’t make any mistakes.

By the way as someone who read this book I can tell you that these code snippets are meant to be really short demonstrations of the basic principles and you shouldn’t use these in real world situations. Normally you would detach and delete shaders after linking the shaderprogram, use VBOs filled with data inside VAOs with vertex attribute pointers, create and pass your own matrices to the shaders, etc.

Don’t worry because the book will teach you all of the above mentioned stuff, just keep it in mind that those examples in the beginning are all extremely simplified and exactly because of that they present some bad habits. :point:

Edit: Daymn, I was too slow. :stuck_out_tongue: I was thinking about POINTS vs POINT too, but I was quite unsure. It’s weird how sometimes OGL uses plural and sometimes singular namings, like GL_TRIANGLES and GL_TRIANGLE_STRIP, or the fact that you have to use GL_LINES for rendering but GL_LINE for [icode]glPolygonMode(…)[/icode]. :slight_smile:

Yeah, I checked for errors before. Didn’t print anything, ra4king was right, wrong enum. Thanks though :wink: