Shader compiles but vertices not drawn to window

Started by smithy545, July 08, 2018, 20:04:26

Previous topic - Next topic

smithy545

I'm new to OpenGL programming so right now I'm just trying to get a simple shader to display a 2-dimensional red triangle. Using various tutorials I've pieced together a program that I think should work but there seems to be a problem with the shader. When I comment out the glUseProgram line I see a white triangle where it's supposed to be according to the obj file vertices. However, when I put the glUseProgram line back in the code compiles successfully but all I get is a black screen. I've attached the shader files. Below is the render method and methods I use to separately load the shaders and compile them into a program.

private int loadShader(String shaderFile, int type) {
	// read the shader file
	ByteBuffer source;
	try {
		FileInputStream fis = new FileInputStream(shaderFile);
		FileChannel fc = fis.getChannel();
		source = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
		fc.close();
		fis.close();
	} catch(FileNotFoundException ex) {
		throw new AssertionError("Could not find file \"" + shaderFile + "\"");
	} catch(IOException ex) {
		throw new AssertionError("Error reading file \"" + shaderFile + "\"");
	}

	// compile the shader
	int shader = glCreateShader(type);
	PointerBuffer strings = BufferUtils.createPointerBuffer(1);
	IntBuffer lengths = BufferUtils.createIntBuffer(1);
	strings.put(0, source);
	lengths.put(0, source.remaining());

	// check the shader
	glShaderSource(shader, strings, lengths);
	glCompileShader(shader);
	int compiled = glGetShaderi(shader, GL_COMPILE_STATUS);
	String shaderLog = glGetShaderInfoLog(shader);
	if (shaderLog != null && shaderLog.trim().length() > 0) {
		System.err.println(shaderLog);
	}
	if (compiled == 0) {
		throw new AssertionError("Could not compile shader");
	}
	return shader;
}

public void init() {
	// normal glfw init and vertices loaded here but that's cut out because I know that works

	// creating the program
	String vertexShaderFile = "res/spaceship.vs";
	int vertexShader = loadShader(vertexShaderFile, GL_VERTEX_SHADER);
	String fragmentShaderFile = "res/spaceship.fs";
	int fragmentShader = loadShader(fragmentShaderFile, GL_FRAGMENT_SHADER);

	// compile the program
	int program = glCreateProgram();
	glAttachShader(program, vertexShader);
	glAttachShader(program, fragmentShader);
	glLinkProgram(program);

	// check the program
	int linked = glGetProgrami(program, GL_LINK_STATUS);
	String programLog = glGetProgramInfoLog(program);
	if(programLog != null && programLog.trim().length() > 0) {
		System.err.println(programLog);
	}
	if(linked == 0) {
		throw new AssertionError("Could not link program");
	}
	
	// this is a member variable
	programId = program;

        // Set the clear color
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

}
	
public void render() {
	glfwPollEvents();

	glViewport(0, 0, fbWidth, fbHeight);

	glClear(GL_COLOR_BUFFER_BIT);

	// trying to display in 2D
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, width, height, 0, -1, 1);

	// "programId" member variable
	int program = programId;

	// use shader
	glUseProgram(program);

	// vertex pos attribute buffer
	glEnableVertexAttribArray(0);
	// "vertices" is a member variable that I know works when glUseProgram(program) is commented out
	glBindBuffer(GL_ARRAY_BUFFER, vertices);
	glVertexAttribPointer(
			0,          // index
			2,           // size
			GL_FLOAT,         // type
			false,  // normalized?
			0,          // stride
			0          // offset
	);

	// This DOESN'T work and displays nothing on the screen unless the program is cleared
	glDrawArrays(GL_TRIANGLES, 0, numVertices);

	// free attrib arrays and clear program
	glDisableVertexAttribArray(0);
	glUseProgram(0);

	// Since the program is cleared this DOES WORK and displays a white triangle at the specified coordinates
	glBegin(GL_TRIANGLES);
	glVertex2f(10, 10);
	glVertex2f(10, 100);
	glVertex2f(100, 10);
	glEnd();

	// swap screen buffers
	glfwSwapBuffers(window);
}


And here are the simple shader files.

vertex shader:
spaceship.vs
#version 330

layout(location = 0) in vec2 vertexPosition_modelspace;

void main() {
  gl_Position = vec4(vertexPosition_modelspace, 0.0, 1.0);
}


fragment shader:
spaceship.fs
#version 330

out vec3 color;

void main() {
  color = vec3(1,0,0);
}