Java Code Examples for com.jogamp.opengl.GL2GL3#glUseProgram()

The following examples show how to use com.jogamp.opengl.GL2GL3#glUseProgram() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Polygon.java    From jaamsim with Apache License 2.0 6 votes vote down vote up
public static void init(Renderer r, GL2GL3 gl) {
	int[] is = new int[1];
	gl.glGenBuffers(1, is, 0);
	_vertBuffer = is[0];

	Shader s = r.getShader(ShaderHandle.DEBUG);
	_progHandle = s.getProgramHandle();
	gl.glUseProgram(_progHandle);

	_modelViewMatVar = gl.glGetUniformLocation(_progHandle, "modelViewMat");
	_projMatVar = gl.glGetUniformLocation(_progHandle, "projMat");
	_colorVar = gl.glGetUniformLocation(_progHandle, "color");

	_cVar = gl.glGetUniformLocation(_progHandle, "C");
	_fcVar = gl.glGetUniformLocation(_progHandle, "FC");

	_posVar = gl.glGetAttribLocation(_progHandle, "position");

	_hasInitialized = true;
}
 
Example 2
Source File: MeshProto.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
private static void initBSInfo(Renderer r, GL2GL3 gl) {
	BatchShaderInfo si = bsInfo;

	si.progHandle = r.getShader(ShaderHandle.MESH_BATCH).getProgramHandle();
	int ph = bsInfo.progHandle;

	gl.glUseProgram(ph);

	si.instSpaceMatVar = gl.glGetAttribLocation(ph, "instSpaceMat");
	si.instSpaceNorMatVar = gl.glGetAttribLocation(ph, "instSpaceNorMat");

	// Bind the shader variables
	si.modelViewMatVar = gl.glGetUniformLocation(ph, "modelViewMat");
	si.projMatVar = gl.glGetUniformLocation(ph, "projMat");
	si.normalMatVar = gl.glGetUniformLocation(ph, "normalMat");

	si.posVar = gl.glGetAttribLocation(ph, "position");
	si.norVar = gl.glGetAttribLocation(ph, "normal");
	si.texCoordVar = gl.glGetAttribLocation(ph, "texCoord");

	si.diffTexVar = gl.glGetUniformLocation(ph, "diffTexs");

	si.diffuseColorVar = gl.glGetAttribLocation(ph, "diffuseColorV");
	si.diffTexIndexVar = gl.glGetAttribLocation(ph, "diffTexIndexV");
	si.ambientColorVar = gl.glGetAttribLocation(ph, "ambientColorV");
	si.specColorVar = gl.glGetAttribLocation(ph, "specColorV");
	si.shininessVar = gl.glGetAttribLocation(ph, "shininessV");

	si.lightDirVar = gl.glGetUniformLocation(ph, "lightDir");
	si.lightIntVar = gl.glGetUniformLocation(ph, "lightIntensity");
	si.numLightsVar = gl.glGetUniformLocation(ph, "numLights");

	si.cVar = gl.glGetUniformLocation(ph, "C");
	si.fcVar = gl.glGetUniformLocation(ph, "FC");

}
 
Example 3
Source File: MeshProto.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
private void initUniforms(Renderer renderer, Mat4d modelViewMat, Mat4d projMat, Mat4d viewMat, Mat4d normalMat) {
	GL2GL3 gl = renderer.getGL();

	lightsDirScratch[0].mult4(viewMat, lightsDir[0]);
	lightsDirScratch[1].mult4(viewMat, lightsDir[1]);

	lightsDirFloats[0] = (float)lightsDirScratch[0].x;
	lightsDirFloats[1] = (float)lightsDirScratch[0].y;
	lightsDirFloats[2] = (float)lightsDirScratch[0].z;

	lightsDirFloats[3] = (float)lightsDirScratch[1].x;
	lightsDirFloats[4] = (float)lightsDirScratch[1].y;
	lightsDirFloats[5] = (float)lightsDirScratch[1].z;

	for (int i = 0; i < usedShaders.length; ++i) {
		int shaderID = usedShaders[i];
		ShaderInfo si = sInfos[shaderID];

		gl.glUseProgram(si.meshProgHandle);

		gl.glUniformMatrix4fv(si.modelViewMatVar, 1, false, RenderUtils.MarshalMat4d(modelViewMat), 0);
		gl.glUniformMatrix4fv(si.projMatVar, 1, false, RenderUtils.MarshalMat4d(projMat), 0);
		gl.glUniformMatrix4fv(si.normalMatVar, 1, false, RenderUtils.MarshalMat4d(normalMat), 0);

		gl.glUniform3fv(si.lightDirVar, 2, lightsDirFloats, 0);
		gl.glUniform1fv(si.lightIntVar, 2, lightsInt, 0);
		gl.glUniform1i(si.numLightsVar, numLights);

		gl.glUniform1f(si.cVar, Camera.C);
		gl.glUniform1f(si.fcVar, Camera.FC);

	}
}
 
Example 4
Source File: Shader.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
public void useShader(GL2GL3 gl) {
	if (_status != ShaderStatus.GOOD) {
		assert(false);
	}

	gl.glUseProgram(_progHandle);
}
 
Example 5
Source File: Skybox.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
public void render(int contextID, Renderer renderer, Camera cam) {

		if (textureURI == null) {
			return;
		}

		if (!isLoaded) {
			loadGPUAssets(renderer);
		}
		if (!VAOMap.containsKey(contextID)) {
			setupVAO(contextID, renderer);
		}

		GL2GL3 gl = renderer.getGL();

		int textureID = renderer.getTexCache().getTexID(gl, textureURI, false, false, false);
		if (textureID == TexCache.LOADING_TEX_ID) {
			// Sky box is not ready yet, get it next time
			return;
		}

		int vao = VAOMap.get(contextID);
		gl.glBindVertexArray(vao);

		gl.glUseProgram(progHandle);

		gl.glUniformMatrix4fv(projMatVar, 1, false, RenderUtils.MarshalMat4d(cam.getProjMat4d()), 0);

		Mat4d invViewMat = new Mat4d();
		invViewMat.setRot4(cam.getTransformRef().getRotRef());
		gl.glUniformMatrix4fv(invViewMatVar, 1, false, RenderUtils.MarshalMat4d(invViewMat), 0);

		gl.glActiveTexture(GL2GL3.GL_TEXTURE0);
		gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, textureID);
		// Disable mipmaps for skyboxes as the discontinuity screws them up
		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MIN_FILTER, GL2GL3.GL_LINEAR );

		gl.glUniform1i(texVar, 0);

		gl.glDrawArrays(GL2GL3.GL_TRIANGLES, 0, 6);

		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MIN_FILTER, GL2GL3.GL_LINEAR_MIPMAP_LINEAR );

		gl.glBindVertexArray(0);

	}
 
Example 6
Source File: OverlayTexture.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
@Override
public void render(int contextID, Renderer renderer,
		double windowWidth, double windowHeight, Camera cam, Ray pickRay) {

	if (!staticInit) {
		initStaticBuffers(renderer);
	}

	double x = _x;
	double y = _y;
	if (_alignRight) {
		x = windowWidth - _x - _width;
	}
	if (!_alignBottom) {
		y = windowHeight - _y - _height;
	}

	GL2GL3 gl = renderer.getGL();

	int textureID = renderer.getTexCache().getTexID(gl, _imageURI, _isTransparent, _isCompressed, false);

	if (textureID == TexCache.LOADING_TEX_ID) {
		return; // This texture is not ready yet
	}

	if (!VAOMap.containsKey(contextID)) {
		setupVAO(contextID, renderer);
	}

	int vao = VAOMap.get(contextID);
	gl.glBindVertexArray(vao);

	gl.glUseProgram(progHandle);

	gl.glUniform1i(hasTexVar, 1);

	gl.glActiveTexture(GL2GL3.GL_TEXTURE0);
	gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, textureID);
	gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_S, GL2GL3.GL_CLAMP_TO_EDGE);
	gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_T, GL2GL3.GL_CLAMP_TO_EDGE);
	gl.glUniform1i(texVar, 0);

	if (_isTransparent) {
		gl.glEnable(GL2GL3.GL_BLEND);
		gl.glBlendEquationSeparate(GL2GL3.GL_FUNC_ADD, GL2GL3.GL_MAX);
		gl.glBlendFuncSeparate(GL2GL3.GL_SRC_ALPHA, GL2GL3.GL_ONE_MINUS_SRC_ALPHA, GL2GL3.GL_ONE, GL2GL3.GL_ONE);
	}

	// Set the size and scale in normalized (-1, 1) coordinates
	double normX = x / (0.5 *  windowWidth) - 1;
	double normY = y / (0.5 * windowHeight) - 1;

	double normWidth  =  _width / (0.5 *  windowWidth);
	double normHeight = _height / (0.5 * windowHeight);

	gl.glUniform2f(offsetVar, (float)normX, (float)normY);
	gl.glUniform2f(sizeVar, (float)normWidth, (float)normHeight);

	// Draw
	gl.glDisable(GL2GL3.GL_CULL_FACE);
	gl.glDrawArrays(GL2GL3.GL_TRIANGLES, 0, 6);
	gl.glEnable(GL2GL3.GL_CULL_FACE);

	if (_isTransparent) {
		gl.glDisable(GL2GL3.GL_BLEND);
	}

}
 
Example 7
Source File: DebugUtils.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the GL assets needed, this should be called with the shared GL context
 * @param gl
 */
public static void init(Renderer r, GL2GL3 gl) {
	int[] is = new int[3];
	gl.glGenBuffers(3, is, 0);
	_aabbVertBuffer = is[0];
	_boxVertBuffer = is[1];
	_lineVertBuffer = is[2];

	Shader s = r.getShader(ShaderHandle.DEBUG);
	_debugProgHandle = s.getProgramHandle();
	gl.glUseProgram(_debugProgHandle);

	_modelViewMatVar = gl.glGetUniformLocation(_debugProgHandle, "modelViewMat");
	_projMatVar = gl.glGetUniformLocation(_debugProgHandle, "projMat");
	_colorVar = gl.glGetUniformLocation(_debugProgHandle, "color");

	_posVar = gl.glGetAttribLocation(_debugProgHandle, "position");

	_cVar = gl.glGetAttribLocation(_debugProgHandle, "C");
	_fcVar = gl.glGetAttribLocation(_debugProgHandle, "FC");

	// Build up a buffer of vertices for lines in a box
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, _aabbVertBuffer);

	FloatBuffer fb = FloatBuffer.allocate(3 * 2 * 12); // 12 line segments
	// Top lines
	app( 1,  1,  1, fb);
	app( 1, -1,  1, fb);

	app( 1, -1,  1, fb);
	app(-1, -1,  1, fb);

	app(-1, -1,  1, fb);
	app(-1,  1,  1, fb);

	app(-1,  1,  1, fb);
	app( 1,  1,  1, fb);

	// Bottom lines
	app( 1,  1, -1, fb);
	app( 1, -1, -1, fb);

	app( 1, -1, -1, fb);
	app(-1, -1, -1, fb);

	app(-1, -1, -1, fb);
	app(-1,  1, -1, fb);

	app(-1,  1, -1, fb);
	app( 1,  1, -1, fb);

	// Side lines
	app( 1,  1,  1, fb);
	app( 1,  1, -1, fb);

	app(-1,  1,  1, fb);
	app(-1,  1, -1, fb);

	app( 1, -1,  1, fb);
	app( 1, -1, -1, fb);

	app(-1, -1,  1, fb);
	app(-1, -1, -1, fb);

	fb.flip();

	gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, 3 * 2 * 12 * 4, fb, GL2GL3.GL_STATIC_DRAW);
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, 0);

	// Create a buffer for drawing rectangles
	// Build up a buffer of vertices for lines in a box
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, _boxVertBuffer);

	fb = FloatBuffer.allocate(3 * 2 * 4); // 4 line segments

	// lines
	app( 0.5f,  0.5f,  0, fb);
	app( 0.5f, -0.5f,  0, fb);

	app( 0.5f, -0.5f,  0, fb);
	app(-0.5f, -0.5f,  0, fb);

	app(-0.5f, -0.5f,  0, fb);
	app(-0.5f,  0.5f,  0, fb);

	app(-0.5f,  0.5f,  0, fb);
	app( 0.5f,  0.5f,  0, fb);

	fb.flip();

	gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, 3 * 2 * 4 * 4, fb, GL2GL3.GL_STATIC_DRAW);
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, 0);

}
 
Example 8
Source File: MeshProto.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private void renderBatch(int contextID, Renderer renderer,
        MeshBatch batch) {

	// TODO bounds
	// TODO size culling


	Material mat = _materials.get(batch.matIndex);
	SubMesh subMesh = _subMeshes.get(batch.meshIndex);

	int shaderID = mat.shaderID | Renderer.STATIC_BATCH_FLAG;

	GL2GL3 gl = renderer.getGL();
	GL4 gl4 = renderer.getGL4();

	if (!batch.vaoMaps[shaderID].containsKey(contextID)) {
		setupVAOForBatch(contextID, batch, renderer);
	}

	int vao = batch.vaoMaps[shaderID].get(contextID);
	gl.glBindVertexArray(vao);

	ShaderInfo si = sInfos[shaderID];

	gl.glUseProgram(si.meshProgHandle);

	// Setup uniforms for this object\
	if (mat._textureIndex != -1) {
		int texHandle = _textureHandles.get(mat._textureIndex);
		gl.glActiveTexture(GL2GL3.GL_TEXTURE0);
		gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, texHandle);
		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_S, GL2GL3.GL_REPEAT);
		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_T, GL2GL3.GL_REPEAT);
		gl.glUniform1i(si.texVar, 0);
	} else {
		gl.glUniform4fv(si.diffuseColorVar, 1, mat._diffuseColor.toFloats(), 0);
	}

	gl.glUniform3fv(si.ambientColorVar, 1, mat._ambientColor.toFloats(), 0);
	gl.glUniform3fv(si.specColorVar, 1, mat._specColor.toFloats(), 0);
	gl.glUniform1f(si.shininessVar, (float)mat._shininess);

	// Actually draw it
	gl.glDisable(GL2GL3.GL_CULL_FACE);

	//gl.glDrawElements(GL2GL3.GL_TRIANGLES, subMesh._numVerts, GL2GL3.GL_UNSIGNED_INT, 0);
	gl4.glDrawElementsInstanced(GL2GL3.GL_TRIANGLES, subMesh._numVerts, GL2GL3.GL_UNSIGNED_INT, 0, batch.numInstances);

	gl.glEnable(GL2GL3.GL_CULL_FACE);

	gl.glBindVertexArray(0);

}
 
Example 9
Source File: MeshProto.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
private void setupVAOForSubMeshImp(int contextID, int shaderID, SubMesh sub, Renderer renderer) {
	GL2GL3 gl = renderer.getGL();

	int vao = renderer.generateVAO(contextID, gl);
	sub.vaoMaps[shaderID].put(contextID, vao);
	gl.glBindVertexArray(vao);

	int progHandle = sInfos[shaderID].meshProgHandle;
	gl.glUseProgram(progHandle);

	int texCoordVar = gl.glGetAttribLocation(progHandle, "texCoord");

	// For some shaders the texCoordVar may be optimized away
	if (texCoordVar != -1) {
		if (sub._texCoordBuffer != 0) {
			// Texture coordinates
			gl.glEnableVertexAttribArray(texCoordVar);

			gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, sub._texCoordBuffer);
			gl.glVertexAttribPointer(texCoordVar, 2, GL2GL3.GL_FLOAT, false, 0, 0);
		} else {
			gl.glVertexAttrib2f(texCoordVar, 0, 0);
		}
	}

	int posVar = gl.glGetAttribLocation(progHandle, "position");
	gl.glEnableVertexAttribArray(posVar);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, sub._vertexBuffer);
	gl.glVertexAttribPointer(posVar, 3, GL2GL3.GL_FLOAT, false, 0, 0);

	// Normals
	int normalVar = gl.glGetAttribLocation(progHandle, "normal");
	gl.glEnableVertexAttribArray(normalVar);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, sub._normalBuffer);
	gl.glVertexAttribPointer(normalVar, 3, GL2GL3.GL_FLOAT, false, 0, 0);

	if (!flattenBuffers) {
		gl.glBindBuffer(GL2GL3.GL_ELEMENT_ARRAY_BUFFER, sub._indexBuffer);
	}

	gl.glBindVertexArray(0);

}
 
Example 10
Source File: MeshProto.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
private void renderSubMesh(SubMesh subMesh, int materialIndex,
                           Mat4d subInstTrans,
                           Mat4d subInstInvTrans,
                           int contextID,
                           Renderer renderer) {

	Material mat = _materials.get(materialIndex);
	int shaderID = mat.shaderID;

	GL2GL3 gl = renderer.getGL();

	if (!subMesh.vaoMaps[shaderID].containsKey(contextID)) {
		setupVAOForSubMesh(contextID, subMesh, renderer);
	}

	int vao = subMesh.vaoMaps[shaderID].get(contextID);
	gl.glBindVertexArray(vao);

	ShaderInfo si = sInfos[shaderID];

	gl.glUseProgram(si.meshProgHandle);

	// Setup uniforms for this object
	Mat4d subInstNorm = new Mat4d(subInstInvTrans);
	subInstNorm.transpose4();

	gl.glUniformMatrix4fv(si.bindSpaceMatVar, 1, false, RenderUtils.MarshalMat4d(subInstTrans), 0);
	gl.glUniformMatrix4fv(si.bindSpaceNorMatVar, 1, false, RenderUtils.MarshalMat4d(subInstNorm), 0);

	if (mat._textureIndex != -1) {
		int texHandle = _textureHandles.get(mat._textureIndex);
		gl.glActiveTexture(GL2GL3.GL_TEXTURE0);
		gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, texHandle);
		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_S, GL2GL3.GL_REPEAT);
		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_T, GL2GL3.GL_REPEAT);
		gl.glUniform1i(si.texVar, 0);
	} else {
		gl.glUniform4fv(si.diffuseColorVar, 1, mat._diffuseColor.toFloats(), 0);
	}

	gl.glUniform3fv(si.ambientColorVar, 1, mat._ambientColor.toFloats(), 0);
	gl.glUniform3fv(si.specColorVar, 1, mat._specColor.toFloats(), 0);
	gl.glUniform1f(si.shininessVar, (float)mat._shininess);

	if (mat._transType != MeshData.NO_TRANS) {
		gl.glBlendEquationSeparate(GL2GL3.GL_FUNC_ADD, GL2GL3.GL_MAX);

		if (mat._transType != MeshData.DIFF_ALPHA_TRANS) {
			gl.glBlendColor((float)mat._transColour.r,
			                (float)mat._transColour.g,
			                (float)mat._transColour.b,
			                (float)mat._transColour.a);
		}

		if (mat._transType == MeshData.A_ONE_TRANS) {
			gl.glBlendFuncSeparate(GL2GL3.GL_CONSTANT_ALPHA, GL2GL3.GL_ONE_MINUS_CONSTANT_ALPHA, GL2GL3.GL_ONE, GL2GL3.GL_ZERO);
		} else if (mat._transType == MeshData.RGB_ZERO_TRANS) {
			gl.glBlendFuncSeparate(GL2GL3.GL_ONE_MINUS_CONSTANT_COLOR, GL2GL3.GL_CONSTANT_COLOR, GL2GL3.GL_ONE, GL2GL3.GL_ZERO);
		} else if (mat._transType == MeshData.DIFF_ALPHA_TRANS) {
			gl.glBlendFuncSeparate(GL2GL3.GL_SRC_ALPHA, GL2GL3.GL_ONE_MINUS_SRC_ALPHA, GL2GL3.GL_ONE, GL2GL3.GL_ZERO);
		} else {
			assert(false); // Unknown transparency type
		}
	}

	// Actually draw it
	//gl.glPolygonMode(GL2GL3.GL_FRONT_AND_BACK, GL2GL3.GL_LINE);
	gl.glDisable(GL2GL3.GL_CULL_FACE);

	if (flattenBuffers) {
		gl.glDrawArrays(GL2GL3.GL_TRIANGLES, 0, subMesh._numVerts);
	} else {
		gl.glDrawElements(GL2GL3.GL_TRIANGLES, subMesh._numVerts, GL2GL3.GL_UNSIGNED_INT, 0);
	}
	gl.glEnable(GL2GL3.GL_CULL_FACE);

	// Reset the blend state
	if (mat._transType != MeshData.NO_TRANS) {
		gl.glBlendEquationSeparate(GL2GL3.GL_FUNC_ADD, GL2GL3.GL_MAX);
		gl.glBlendFuncSeparate(GL2GL3.GL_SRC_ALPHA, GL2GL3.GL_ONE_MINUS_SRC_ALPHA, GL2GL3.GL_ONE, GL2GL3.GL_ONE);
	}

	gl.glBindVertexArray(0);

}
 
Example 11
Source File: MeshProto.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
private void setupVAOForSubLine(int contextID, SubLine sub, Renderer renderer) {
	GL2GL3 gl = renderer.getGL();

	int vao = renderer.generateVAO(contextID, gl);
	sub.vaoMap.put(contextID, vao);
	gl.glBindVertexArray(vao);

	int prog = sub._progHandle;
	gl.glUseProgram(prog);

	int posVar = gl.glGetAttribLocation(prog, "position");
	gl.glEnableVertexAttribArray(posVar);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, sub._vertexBuffer);
	gl.glVertexAttribPointer(posVar, 3, GL2GL3.GL_FLOAT, false, 0, 0);

	gl.glBindVertexArray(0);

}
 
Example 12
Source File: MeshProto.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
private void initBatchUniforms(Renderer renderer, Mat4d modelViewMat, Mat4d projMat, Mat4d viewMat, Mat4d normalMat) {
	GL2GL3 gl = renderer.getGL();

	lightsDirScratch[0].mult4(viewMat, lightsDir[0]);
	lightsDirScratch[1].mult4(viewMat, lightsDir[1]);

	lightsDirFloats[0] = (float)lightsDirScratch[0].x;
	lightsDirFloats[1] = (float)lightsDirScratch[0].y;
	lightsDirFloats[2] = (float)lightsDirScratch[0].z;

	lightsDirFloats[3] = (float)lightsDirScratch[1].x;
	lightsDirFloats[4] = (float)lightsDirScratch[1].y;
	lightsDirFloats[5] = (float)lightsDirScratch[1].z;

	BatchShaderInfo si = bsInfo;

	gl.glUseProgram(si.progHandle);

	gl.glUniformMatrix4fv(si.modelViewMatVar, 1, false, RenderUtils.MarshalMat4d(modelViewMat), 0);
	gl.glUniformMatrix4fv(si.projMatVar, 1, false, RenderUtils.MarshalMat4d(projMat), 0);
	gl.glUniformMatrix4fv(si.normalMatVar, 1, false, RenderUtils.MarshalMat4d(normalMat), 0);

	gl.glUniform3fv(si.lightDirVar, 2, lightsDirFloats, 0);
	gl.glUniform1fv(si.lightIntVar, 2, lightsInt, 0);
	gl.glUniform1i(si.numLightsVar, numLights);

	gl.glUniform1f(si.cVar, Camera.C);
	gl.glUniform1f(si.fcVar, Camera.FC);

	int[] sampIDs = new int[_textureHandles.size()];
	for (int i = 0; i < sampIDs.length; ++i) {
		sampIDs[i] = i;
	}
	if (sampIDs.length > 0) {
		gl.glUniform1iv(si.diffTexVar, sampIDs.length, sampIDs, 0);
	}

	for (int i = 0; i < _textureHandles.size(); ++i) {
		gl.glActiveTexture(GL2GL3.GL_TEXTURE0+i);
		gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, _textureHandles.get(i));
		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_S, GL2GL3.GL_REPEAT);
		gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_T, GL2GL3.GL_REPEAT);
	}
}
 
Example 13
Source File: MeshProto.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
private void setupVAOForStaticLines(int contextID, Renderer renderer) {
	GL2GL3 gl = renderer.getGL();

	int vao = renderer.generateVAO(contextID, gl);
	_lineVAOs.put(contextID, vao);
	gl.glBindVertexArray(vao);

	Shader s= renderer.getShader(ShaderHandle.DEBUG_BATCH);
	assert(s.isGood());
	int prog = s.getProgramHandle();
	gl.glUseProgram(prog);

	int posVar = gl.glGetAttribLocation(prog, "position");
	gl.glEnableVertexAttribArray(posVar);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, linePosBuffer);
	gl.glVertexAttribPointer(posVar, 3, GL2GL3.GL_FLOAT, false, 0, 0);

	int colVar = gl.glGetAttribLocation(prog, "vertColor");
	gl.glEnableVertexAttribArray(colVar);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, lineInstColorBuffer);
	gl.glVertexAttribPointer(colVar, 4, GL2GL3. GL_UNSIGNED_BYTE, true, 0, 0);
	gl.glVertexAttribDivisor(colVar, 1);

	int instMatVar = gl.glGetAttribLocation(prog, "instMat");
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, lineTransBuffer);

	for (int i = 0; i < 4; ++i) {
		// Enable 4 variables because this is a matrix
		int varInd = instMatVar + i;

		gl.glEnableVertexAttribArray(varInd);

		gl.glVertexAttribPointer(varInd, 4, GL2GL3.GL_FLOAT, false, 16*4, i*4*4);
		gl.glVertexAttribDivisor(varInd, 1); // Per instance
	}

	gl.glBindVertexArray(0);

}
 
Example 14
Source File: DebugUtils.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
public static void renderAABB(int contextID, Renderer renderer,
                              AABB aabb, Color4d color, Camera cam) {

	if (aabb.isEmpty()) {
		return;
	}

	GL2GL3 gl = renderer.getGL();

	if (!_debugVAOMap.containsKey(contextID)) {
		setupDebugVAO(contextID, renderer);
	}

	int vao = _debugVAOMap.get(contextID);
	gl.glBindVertexArray(vao);

	gl.glUseProgram(_debugProgHandle);

	// Setup uniforms for this object
	Mat4d projMat = cam.getProjMat4d();
	Mat4d modelViewMat = new Mat4d();
	cam.getViewMat4d(modelViewMat);

	Mat4d aabbCenterMat = new Mat4d();
	aabbCenterMat.setTranslate3(aabb.center);
	modelViewMat.mult4(aabbCenterMat);
	modelViewMat.scaleCols3(aabb.radius);

	gl.glUniformMatrix4fv(_modelViewMatVar, 1, false, RenderUtils.MarshalMat4d(modelViewMat), 0);
	gl.glUniformMatrix4fv(_projMatVar, 1, false, RenderUtils.MarshalMat4d(projMat), 0);

	gl.glUniform4fv(_colorVar, 1, color.toFloats(), 0);

	gl.glUniform1f(_cVar, Camera.C);
	gl.glUniform1f(_fcVar, Camera.FC);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, _aabbVertBuffer);
	gl.glVertexAttribPointer(_posVar, 3, GL2GL3.GL_FLOAT, false, 0, 0);

	gl.glDrawArrays(GL2GL3.GL_LINES, 0, 12 * 2);

	gl.glBindVertexArray(0);

}
 
Example 15
Source File: MeshProto.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
public static void init(Renderer r, GL2GL3 gl) {
	for (int i = 0; i < Renderer.NUM_MESH_SHADERS; ++i) {
		ShaderInfo si = new ShaderInfo();
		sInfos[i] = si;

		si.meshProgHandle = r.getMeshShader(i).getProgramHandle();

		gl.glUseProgram(si.meshProgHandle);

		// Bind the shader variables
		si.modelViewMatVar = gl.glGetUniformLocation(si.meshProgHandle, "modelViewMat");
		si.projMatVar = gl.glGetUniformLocation(si.meshProgHandle, "projMat");
		si.normalMatVar = gl.glGetUniformLocation(si.meshProgHandle, "normalMat");
		si.diffuseColorVar = gl.glGetUniformLocation(si.meshProgHandle, "diffuseColor");
		si.ambientColorVar = gl.glGetUniformLocation(si.meshProgHandle, "ambientColor");
		si.specColorVar = gl.glGetUniformLocation(si.meshProgHandle, "specColor");
		si.shininessVar = gl.glGetUniformLocation(si.meshProgHandle, "shininess");
		si.texVar = gl.glGetUniformLocation(si.meshProgHandle, "diffuseTex");

		si.lightDirVar = gl.glGetUniformLocation(si.meshProgHandle, "lightDir");
		si.lightIntVar = gl.glGetUniformLocation(si.meshProgHandle, "lightIntensity");
		si.numLightsVar = gl.glGetUniformLocation(si.meshProgHandle, "numLights");

		si.cVar = gl.glGetUniformLocation(si.meshProgHandle, "C");
		si.fcVar = gl.glGetUniformLocation(si.meshProgHandle, "FC");

		if ((i & Renderer.STATIC_BATCH_FLAG) == 0) {
			// We do not use the uniform bind space variables when batch rendering
			si.bindSpaceMatVar = gl.glGetUniformLocation(si.meshProgHandle, "bindSpaceMat");
			si.bindSpaceNorMatVar = gl.glGetUniformLocation(si.meshProgHandle, "bindSpaceNorMat");
		}

	}

	numLights = 2;


	lightsDir[0] = new Vec4d(-0.3, -0.2, -0.5, 0.0);
	lightsDir[1] = new Vec4d( 0.5, 1.0, -0.1, 0.0);

	lightsDir[0].normalize3();
	lightsDir[1].normalize3();

	lightsInt[0] = 1f;
	lightsInt[1] = 0.5f;

	lightsDirScratch[0] = new Vec4d();
	lightsDirScratch[1] = new Vec4d();

	if (r.isIndirectSupported()) {
		initBSInfo(r, gl);
	}
}
 
Example 16
Source File: DebugUtils.java    From jaamsim with Apache License 2.0 3 votes vote down vote up
public static void renderBox(int contextID, Renderer renderer,
           Transform modelTrans, Vec4d scale, Color4d color, Camera cam) {

	GL2GL3 gl = renderer.getGL();

	if (!_debugVAOMap.containsKey(contextID)) {
		setupDebugVAO(contextID, renderer);
	}

	int vao = _debugVAOMap.get(contextID);
	gl.glBindVertexArray(vao);

	gl.glUseProgram(_debugProgHandle);

	gl.glUniform1f(_cVar, Camera.C);
	gl.glUniform1f(_fcVar, Camera.FC);

	// Setup uniforms for this object
	Mat4d projMat = cam.getProjMat4d();
	Mat4d modelViewMat = new Mat4d();
	cam.getViewMat4d(modelViewMat);

	modelViewMat.mult4(modelTrans.getMat4dRef());
	modelViewMat.scaleCols3(scale);

	gl.glUniformMatrix4fv(_modelViewMatVar, 1, false, RenderUtils.MarshalMat4d(modelViewMat), 0);
	gl.glUniformMatrix4fv(_projMatVar, 1, false, RenderUtils.MarshalMat4d(projMat), 0);

	gl.glUniform4fv(_colorVar, 1, color.toFloats(), 0);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, _boxVertBuffer);
	gl.glVertexAttribPointer(_posVar, 3, GL2GL3.GL_FLOAT, false, 0, 0);

	gl.glDrawArrays(GL2GL3.GL_LINES, 0, 4 * 2);

	gl.glBindVertexArray(0);
}
 
Example 17
Source File: Polygon.java    From jaamsim with Apache License 2.0 3 votes vote down vote up
private static void setupVAO(int contextID, Renderer renderer) {
	GL2GL3 gl = renderer.getGL();

	int vao = renderer.generateVAO(contextID, gl);

	_VAOMap.put(contextID, vao);
	gl.glBindVertexArray(vao);

	gl.glUseProgram(_progHandle);

	gl.glEnableVertexAttribArray(_posVar);

	gl.glBindVertexArray(0);

}
 
Example 18
Source File: OverlayPolygon.java    From jaamsim with Apache License 2.0 2 votes vote down vote up
@Override
public void render(int contextID, Renderer renderer,
	double windowWidth, double windowHeight, Camera cam, Ray pickRay) {

	if (!staticInit) {
		initStaticData(renderer);
	}

	GL2GL3 gl = renderer.getGL();

	if (!VAOMap.containsKey(contextID)) {
		setupVAO(contextID, renderer);
	}

	int vao = VAOMap.get(contextID);
	gl.glBindVertexArray(vao);

	gl.glUseProgram(progHandle);

	gl.glUniform4fv(colorVar, 1, color.toFloats(), 0);

	gl.glUniform1i(hasTexVar, 0);


	// Set the size and offset in normalized (-1, 1) coordinates
	double scaleX = 2.0 / windowWidth;
	double scaleY = 2.0 / windowHeight;

	double pixelWidth = 2.0/windowWidth;
	double pixelHeight = 2.0/windowHeight;

	double xOffset = -1.0 + 0.5*pixelWidth;
	double yOffset = -1.0 + 0.5*pixelHeight;

	if (originTop) {
		scaleY *= -1.0;
		yOffset = 1.0 - 0.5*pixelHeight;
	}

	if (originRight) {
		scaleX *= -1.0;
		xOffset = 1.0 - 0.5*pixelWidth;
	}

	gl.glUniform2f(offsetVar, (float)xOffset, (float)yOffset);
	gl.glUniform2f(sizeVar, (float)scaleX, (float)scaleY);

	gl.glEnableVertexAttribArray(posVar);

	gl.glDisable(GL2GL3.GL_CULL_FACE);

	// Build up a float buffer to pass to GL

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, glBuff);
	gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, pointsBuffer.limit() * 4, pointsBuffer, GL2GL3.GL_STATIC_DRAW);

	gl.glVertexAttribPointer(posVar, 2, GL2GL3.GL_FLOAT, false, 0, 0);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, 0);

	gl.glDrawArrays(GL2GL3.GL_TRIANGLE_FAN, 0, pointsBuffer.limit() / 2);

	gl.glEnable(GL2GL3.GL_CULL_FACE);

	gl.glBindVertexArray(0);
}
 
Example 19
Source File: HullProto.java    From jaamsim with Apache License 2.0 2 votes vote down vote up
public void render(int contextID, Renderer renderer,
           Mat4d modelViewMat,
           Camera cam) {

	GL2GL3 gl = renderer.getGL();

	Shader s = renderer.getShader(Renderer.ShaderHandle.HULL);

	int progHandle = s.getProgramHandle();
	gl.glUseProgram(progHandle);

	int modelViewMatVar = gl.glGetUniformLocation(progHandle, "modelViewMat");
	int projMatVar = gl.glGetUniformLocation(progHandle, "projMat");

	int cVar = gl.glGetUniformLocation(progHandle, "C");
	int fcVar = gl.glGetUniformLocation(progHandle, "FC");

	int[] is = new int[2];
	gl.glGenBuffers(2, is, 0);
	int vertexBuffer = is[0];
	int indexBuffer = is[1];

	List<Vec3d> verts = _hull.getVertices();
	// Generate the vertex buffer
	FloatBuffer fb = FloatBuffer.allocate(verts.size() * 3); //
	for (Vec3d v : verts) {
		RenderUtils.putPointXYZ(fb, v);
	}
	fb.flip();

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, vertexBuffer);
	gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, verts.size() * 3 * 4, fb, GL2GL3.GL_STATIC_DRAW);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, 0);

	// Generate the index buffer
	List<ConvexHull.HullFace> faces = _hull.getFaces();

	int numIndices = faces.size() * 3;

	IntBuffer ib = IntBuffer.allocate(faces.size() * 3); //
	for (ConvexHull.HullFace f : faces) {
		ib.put(f.indices, 0 ,3);
	}

	ib.flip();

	gl.glBindBuffer(GL2GL3.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
	gl.glBufferData(GL2GL3.GL_ELEMENT_ARRAY_BUFFER, faces.size() * 3 * 4, ib, GL2GL3.GL_STATIC_DRAW);

	gl.glBindBuffer(GL2GL3.GL_ELEMENT_ARRAY_BUFFER, 0);

	if (!_vaoMap.containsKey(contextID)) {
		setupVAO(contextID, renderer, progHandle, vertexBuffer, indexBuffer);
	}

	int vao = _vaoMap.get(contextID);
	gl.glBindVertexArray(vao);

	gl.glUseProgram(progHandle);

	// Setup uniforms for this object
	Mat4d projMat = cam.getProjMat4d();

	gl.glUniformMatrix4fv(modelViewMatVar, 1, false, RenderUtils.MarshalMat4d(modelViewMat), 0);
	gl.glUniformMatrix4fv(projMatVar, 1, false, RenderUtils.MarshalMat4d(projMat), 0);

	gl.glUniform1f(cVar, Camera.C);
	gl.glUniform1f(fcVar, Camera.FC);

	// Actually draw it

	gl.glEnable(GL2GL3.GL_BLEND);
	gl.glEnable(GL2GL3.GL_CULL_FACE);
	gl.glCullFace(GL2GL3.GL_BACK);

	gl.glBlendFunc(GL2GL3.GL_ONE, GL2GL3.GL_ONE_MINUS_SRC_ALPHA);
	gl.glBlendEquation(GL2GL3.GL_FUNC_ADD);

	gl.glDisable(GL2GL3.GL_DEPTH_TEST);

	//gl.glPolygonMode(GL2GL3.GL_FRONT_AND_BACK, GL2GL3.GL_LINE);

	gl.glDrawElements(GL2GL3.GL_TRIANGLES, numIndices, GL2GL3.GL_UNSIGNED_INT, 0);

	//gl.glPolygonMode(GL2GL3.GL_FRONT_AND_BACK, GL2GL3.GL_FILL);

	gl.glEnable(GL2GL3.GL_DEPTH_TEST);

	gl.glDisable(GL2GL3.GL_CULL_FACE);
	gl.glCullFace(GL2GL3.GL_BACK);

	gl.glDisable(GL2GL3.GL_BLEND);

	gl.glBindVertexArray(0);

	gl.glDeleteBuffers(2, is, 0);

}
 
Example 20
Source File: OverlayLine.java    From jaamsim with Apache License 2.0 2 votes vote down vote up
@Override
public void render(int contextID, Renderer renderer,
	double windowWidth, double windowHeight, Camera cam, Ray pickRay) {

	if (!staticInit) {
		initStaticData(renderer);
	}

	GL2GL3 gl = renderer.getGL();

	if (!VAOMap.containsKey(contextID)) {
		setupVAO(contextID, renderer);
	}

	int vao = VAOMap.get(contextID);
	gl.glBindVertexArray(vao);

	gl.glUseProgram(progHandle);

	gl.glUniform4fv(colorVar, 1, color.toFloats(), 0);

	gl.glUniform1i(hasTexVar, 0);


	// Set the size and offset in normalized (-1, 1) coordinates
	double scaleX = 2.0 / windowWidth;
	double scaleY = 2.0 / windowHeight;

	double pixelWidth = 2.0/windowWidth;
	double pixelHeight = 2.0/windowHeight;

	double xOffset = -1.0 + 0.5*pixelWidth;
	double yOffset = -1.0 + 0.5*pixelHeight;

	if (originTop) {
		scaleY *= -1.0;
		yOffset = 1.0 - 0.5*pixelHeight;
	}

	if (originRight) {
		scaleX *= -1.0;
		xOffset = 1.0 - 0.5*pixelWidth;
	}

	gl.glUniform2f(offsetVar, (float)xOffset, (float)yOffset);
	gl.glUniform2f(sizeVar, (float)scaleX, (float)scaleY);

	gl.glEnableVertexAttribArray(posVar);

	gl.glDisable(GL2GL3.GL_CULL_FACE);

	if (!gl.isGLcore())
		gl.glLineWidth((float)lineWidth);
	else
		gl.glLineWidth(1.0f);

	// Build up a float buffer to pass to GL

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, lineGLBuff);
	gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, lineBuffer.limit() * 4, lineBuffer, GL2GL3.GL_STATIC_DRAW);

	gl.glVertexAttribPointer(posVar, 2, GL2GL3.GL_FLOAT, false, 0, 0);

	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, 0);

	gl.glDrawArrays(GL2GL3.GL_LINES, 0, lineBuffer.limit() / 2);

	gl.glLineWidth(1.0f);

	gl.glEnable(GL2GL3.GL_CULL_FACE);

	gl.glBindVertexArray(0);
}