Alpha blending textures and primitives - opengl

I have two textures and a triangle primitive(the colors for its vertices is defined by glColor4f).
Texture A looks like this:
Texture B looks like this:
Triangle Primitive P looks like this:
When I render texture A, and then texture B I get expected result i.e. The middle of the rectangle is transparent.
However if I render the primitive P followed by texture A, and then texture B, I get wrong colors. P is not pure red and the texture colors all turn black.
How do I fix this so that proper transparency and colors are maintained.
Texture Parameters:
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER));
GLfloat debugColor[] = {1.0f, 0.0f, 1.0f, 1.0f};
GL_CALL(glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, debugColor));
Blending function:
GL_CALL(glEnable(GL_BLEND));
GL_CALL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
Rendering of P, A and B:
if(true)
{
// Render Primitive P
GL_CALL(glBegin(GL_TRIANGLES));
GL_CALL(glColor4f(1.0f, 0.0f, 0.0f, 1.0f));
GL_CALL(glVertex2f(-0.5f, -0.5f));
GL_CALL(glColor4f(1.0f, 0.0f, 0.0f, 1.0f));
GL_CALL(glVertex2f(0.0f, 0.5f));
GL_CALL(glColor4f(1.0f, 0.0f, 0.0f, 1.0f));
GL_CALL(glVertex2f(0.5f, -0.5f));
glEnd();
}
if(true)
{
// Render Texture A
GL_CALL(glBindTexture(GL_TEXTURE_2D, textureId));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageWidth, imageHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image));
real32 screenPercentage = 0.25f;
GL_CALL(glBegin(GL_TRIANGLES));
// Lower triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 0.0f));
GL_CALL(glVertex2f(screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
// Upper triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
GL_CALL(glTexCoord2f(0.0f, 1.0f));
GL_CALL(glVertex2f(-screenPercentage, -screenPercentage));
glEnd();
}
if(true)
{
// Render Texture B
GL_CALL(glBindTexture(GL_TEXTURE_2D, textureId));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, window[0].offscreenBuffer.width, window[0].offscreenBuffer.height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, window[0].offscreenBuffer.data));
real32 screenPercentage = 1.0f;
GL_CALL(glBegin(GL_TRIANGLES));
// Lower triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 0.0f));
GL_CALL(glVertex2f(screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
// Upper triangle
GL_CALL(glTexCoord2f(0.0f, 0.0f));
GL_CALL(glVertex2f(-screenPercentage, screenPercentage));
GL_CALL(glTexCoord2f(1.0f, 1.0f));
GL_CALL(glVertex2f(screenPercentage, -screenPercentage));
GL_CALL(glTexCoord2f(0.0f, 1.0f));
GL_CALL(glVertex2f(-screenPercentage, -screenPercentage));
glEnd();
}
EDIT: After trying #Rabbid76 's solution:
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if(true)
{
// Render triangle with all vertices having color 1.0f, 0.0f, 0.0f, 1.0f
}
if(true)
{
// Render texture A with color set to 1.0f, 1.0f, 1.0f, 1.0f and the appropriate texture u,v coordinates
}
if(true)
{
// Render texture B with color set to 1.0f, 1.0f, 1.0f, 1.0f and the appropriate texture u,v coordinates
}
I get the following result:
The colors are still not right even tho the alpha of all the three entities is 1.0f and that of the hollow region is 0.0f

If texturing is enabled, then by default the color of the texel is multiplied by the current color, because by default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv.
This causes that the color of the texels of the texture is "mixed" by the last color which you have set by glColor4f.
When you render the texture, then the red color of the triangle is still set, this causes that the textures are tint red. The green and blue color channels get completely lost and since the textures have no red color channel, everything what is left is a black color.
To solve you issue, you have to set a white color, before you render the textures:
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
And disable GL_TEXTURE_2D before you render the triangle, but enable it before you render the textures.
Note there is always a texture bound, there is nothing like "no texture", the default texture object 0 is a valid object too.
This causes that the texture lookup gives a completely black color and this is mixed by the red color, which is set by glColor4f:
glDisable( GL_TEXTURE_2D );
if( true )
{
// Render Primitive P
.....
}
glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
glEnable( GL_TEXTURE_2D );
if ( true )
{
// Render Texture A
.....
}

Related

All texture colors affected by colored rectangle - OpenGL

I'm currently trying to color some rectangles to represent collision boxes for my game character. The issue is the color I'm trying to apply ONLY to the rectangles are being applied to my character and background textures as well. So if I draw a rect using the following code:
void DrawRect(v2f MinPoint, v2f MaxPoint)
{
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f); //Red color
glVertex2f(MaxPoint.x, MaxPoint.y);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(MinPoint.x, MaxPoint.y);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(MinPoint.x, MinPoint.y);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(MaxPoint.x, MinPoint.y);
glEnd();
glFlush();
}
Then right after draw my background texture:
void DrawBackground(ui32 TextureID, Drawable_Rect BackgroundImage, v2f MinUV, v2f MaxUV)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, TextureID);
glBegin(GL_QUADS);
glTexCoord2f(MinUV.x, MinUV.y);
glVertex2f(BackgroundImage.BottomLeft.x, BackgroundImage.BottomLeft.y);
glTexCoord2f(MaxUV.x, MinUV.y);
glVertex2f(BackgroundImage.BottomRight.x, BackgroundImage.BottomRight.y);
glTexCoord2f(MaxUV.x, MaxUV.y);
glVertex2f(BackgroundImage.TopRight.x, BackgroundImage.TopRight.y);
glTexCoord2f(MinUV.x, MaxUV.y);
glVertex2f(BackgroundImage.TopLeft.x, BackgroundImage.TopLeft.y);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}
Then both my rectangle AND my background are red instead of just my rectangle. Why is the color being applied to both my rectangle and my texture?
If texturing is enabled, then by default the color of the texel is multiplied by the current color, because by default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv.
This causes that the color of the texels of the texture is "mixed" by the last color which you have set by glColor3f:
Set a "white" color before you render the texture, to solve your issue:
glColor3f(1.0f, 1.0f, 1.0f);
Likewise you can change the environment mode to GL_REPLACE, instead:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

Mapping a 2D image to a 3D texture

Question:
I am trying to map a single 2D image to a 3D texture. When I visualize the output, OpenGL renders a white texture floating in black background. Why is this happening? Is it possible to map a 2D image to a 3D texture?
My goal is to place multiple 2D images along the z-dimension to resemble a volume. An example of such an approach is in this video: http://cvlab.epfl.ch/research/medical/em/synapses
Approaches attempted:
I took 2D images and mapped them to 2D textures. I have also used 2D texture arrays. Both of these approaches provide a good result. However, when I tried to paint the surface of a 3D texture with the same image, I get a white texture floating around in a black space.
Code
I have followed the texture mapping tutorial that is available on NeHe's website.
http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/
Except changes to these three functions, everything else in the program (present in the solution file in the website) is the same.
... // header file declarations
#include <glext.h>
PFNGLTEXIMAGE3DPROC glTexImage3D;
GLuint texture;
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
/* load an image file directly as a new OpenGL texture */
texture = SOIL_load_OGL_texture("Data/NeHe.bmp",SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,S OIL_FLAG_INVERT_Y);
// "Data/NeHe.bmp"
if(texture == 0) {return false;}
glTexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
if (glTexImage3D == NULL)
{
printf("Error in line %d: Couldn't load glTexImage3D function. Aborting.\n", __LINE__);
return -1;
}
glBindTexture(GL_TEXTURE_3D, texture);
glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 4096, 4096, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
return true;
}
int InitGL(GLvoid)
{
if (!LoadGLTextures())
{ return FALSE; }
glEnable(GL_TEXTURE_3D);
// glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); //
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective calculations
return TRUE; // Initialization Went OK
}
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
// glBindTexture(GL_TEXTURE_2D, texture[0]);
glBindTexture(GL_TEXTURE_3D, texture);
glBegin(GL_QUADS);
glTexCoord3f(0.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord3f(1.0f, 0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord3f(1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
xrot+=0.3f;
yrot+=0.2f;
zrot+=0.4f;
return TRUE;
}
...
It is better to use 3D textures instead of 2D ones as you will be able to apply rotations correctly using glRotate(). You will also need to do blending and/or alpha-testing to see different layers of your 2D image.
To create a 3D texture, you can create an array of dimension [width x height x number_of_slices] and then store the raw data slice by slice
Look at this tutorial: http://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering?msg=4729498#xx4729498xx
It's very good and it worked for me to do the same thing

OpenGL quadrilateral not being drawn

I am attempting to draw a quadrilateral (square) on the screen. I inserted this into a pre-existing program that drew a cylinder. This is in an orthographic modelview matrix, and I am almost positive the clipping volume is correct (It's 200 in any direction from the origin). In the display function I'm using, I pushed a matrix, translated forward (0.0,0.0,-20.0), called quadriliteral, and then popped the matrix. Are the any common openGL settings people use that I'm unaware of that may make this not visible? Is there a way to get the current clipping volume and print it out in the console?
void quadrilateral()
{
glLoadIdentity();
glColor3f(0.5f,0.0f,0.8f);
glBegin(GL_QUADS);
glVertex3f(-10.0f,-10.0f,0.0f);
glVertex3f(-10.0f,10.0f,0.0f);
glVertex3f(10.0f,10.0f,0.0f);
glVertex3f(10.0f,-10.0f,0.0f);
glEnd();
}
//This is called from a main function elsewhere
void draw(void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Cylinder");
glutReshapeFunc(CChangeSize);
glutSpecialFunc(CSpecialKeys);
glutKeyboardFunc(Ckeyboard);
glutDisplayFunc(CRenderScene);
CSetupRC();
glPopMatrix();
}
void CSetupRC()
{
// Light values and coordinates
GLfloat ambientLight[] = {0.4f, 0.4f, 0.4f, 1.0f };
GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specular[] = { 0.9f, 0.9f, 0.9f, 1.0f};
GLfloat lightPos[] = { -50.0f, 200.0f, 200.0f, 1.0f };
GLfloat specref[] = { 0.6f, 0.6f, 0.6f, 1.0f };
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glEnable(GL_CULL_FACE); // Do not calculate inside of solid object
glFrontFace(GL_CCW);
// Enable lighting
glEnable(GL_LIGHTING);
// Setup light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
// Position and turn on the light
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a moderate shine
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT,GL_SHININESS,64);
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);
}
// Called to draw scene
void CRenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Rotate about x and y axes
glRotatef(CxRot, 1.0f, 0.0f, 0.0f);
glRotatef(CyRot, 0.0f, 1.0f, 0.0f);
// Draw the cylinder
cylinder();
glPopMatrix();
// CylinderTwo
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Rotate about x and y axes
glRotatef(CxRot, 1.0f, 0.0f, 0.0f);
glRotatef(CyRot, 0.0f, 1.0f, 0.0f);
// Draw the cylinder
glRotatef(120.0f, 1.0f, 1.0f, 1.0f);
cylinderTwo();
glPopMatrix();
// Random square
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-30.0f,20.0f,10.0f);
quadrilateral();
glPopMatrix();
// Swap buffers
glutSwapBuffers();
}
It looks like your winding order is incorrect:
glEnable(GL_CULL_FACE);
this line means it will cull back faces.
glFrontFace(GL_CCW);
and this sets the front face to counter clockwise.
glVertex3f(-10.0f,-10.0f,0.0f);
glVertex3f(-10.0f,10.0f,0.0f);
glVertex3f(10.0f,10.0f,0.0f);
glVertex3f(10.0f,-10.0f,0.0f);
Here you have clockwise ordering of your vertices meaning you are drawing with the back to camera. either disable culling:
glDisable(GL_CULL_FACE);
make the front face clockwise:
glFrontFace(GL_CW);
or change the order of your vertices to a counter clockwise order.
Looks to me that your quad is wound backwards. You've enabled GL_CULL_FACE, which throws out any clockwise wound polygons, and your quad has a clockwise winding.
Either disable backface culling, or switch the order of your vertices to counter-clockwise.

OpenGL - Rendering into a Texture

I want to be able to render something into a texture, on OpenGL, so I can further use it whenever I want, without rendering everything over again. This website here gave me the guidelines to do it, without using the FrameBuffer. I don't want to do it with the FrameBuffer Object due to compatibility issues, since this old machine is not supporting it. I have done some code, which creates my texture, renders my scene, and I then create a Quad to render the texture on it. The only problem is that the texture is being rendered like an "Alpha Mask", it means, looks like it's only taking into account the Alpha Value, maintaining my rectangle always with the same color, but just changing the transparency on pixels. Here is some code I've done so far:
void CreateTexture ()
{
xSize = 512;
ySize = 512; //size of texture
//new array
char* colorBits = new char[ xSize * ySize * 3 ];
//texture creation..
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexImage2D(GL_TEXTURE_2D,0 ,3 , xSize,
ySize, 0 , GL_RGB,
GL_UNSIGNED_BYTE, colorBits);
//you can set other texture parameters if you want
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//clean up
delete[] colorBits;
}
Then:
int viewport[4];
glGetIntegerv(GL_VIEWPORT,(int*)viewport);
glViewport(0,0,xSize,ySize);
DrawScene(hDC);
//save data to texture using glCopyTexImage2D
glBindTexture(GL_TEXTURE_2D,texture);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
0,0, xSize, ySize, 0);
glClearColor(.0f, 0.5f, 0.5f, 1.0f); // Set The Clear Color To Medium Blue
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);
// glBindTexture(GL_TEXTURE_2D,texture);
And Finally:
glEnable(GL_TEXTURE_2D); // Enable 2D Texture Mapping
glBlendFunc(GL_DST_COLOR,GL_ONE); // Set Blending Mode
glEnable(GL_BLEND);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D,texture);
glRotatef(theta, 0.0f, 0.0f, 0.01f);
glBegin(GL_QUADS);
//Front Face
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-0.5, -0.5f, 0.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glEnd();
SwapBuffers(hDC);
The DrawScene() function simply renders a rectangle with a triangle on top of it, with each vertice having different colors.. nothing special.
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT );//| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
// glRotatef(theta, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.87f, -0.5f);
glEnd();
glPopMatrix();
I've found something on nVidia website which looks useful, for someone who cannot also do offscreen rendering with FBO:
http://developer.download.nvidia.com/SDK/9.5/Samples/samples.html
This website contains one project called "Simple P-Buffer", which basically contains an implementation of a P-buffer. The idea of the sample is that you make context switching to the pBuffer, while you want to draw pixels on offscreen mode, let's say. After drawing your scene with the normal rendering functions, we use glReadPixels to read the data from the pBuffer into an array of unsigned bytes (GLubyte). After that, we do context-switching once again, setting it back to the screen context, so that you can use glReadPixels to read the content from our array.
The method before FBOs were available was to use an alternate render buffer (see glDrawBuffer(GL_AUX0), then copy pixels from that buffer (see glReadBuffer) to the texture (see glCopyTexImage2D. Rendering directly into a texture requires FBOs.

Render to texture, the texture is not shown completely

Basically when I am rendering to texture, it looks like some part of the texture got lost.
package org.yourorghere;
import com.jogamp.opengl.util.GLBuffers;
import java.awt.Component;
import java.nio.ByteBuffer;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
public class GLRenderer implements GLEventListener {
int[] textureID = new int[1];
private int floorWidth=48, floorHeight=48;
int[] frameBufferID = new int[1];
int[] depthRenderBufferID = new int[1];
ByteBuffer pixels;
GLU glu;
public void init(GLAutoDrawable drawable) {
glu = new GLU();
System.out.println("init");
GL2 gl = drawable.getGL().getGL2();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Setup the drawing area and shading mode
gl.glShadeModel(GL2.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
renderShadowsToTexture(gl);
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
System.out.println("display");
float a = 1.0f;
gl.glMatrixMode(GL2.GL_PROJECTION);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();
glu.gluPerspective(60.0f, (((Component)drawable).getWidth()/
((Component)drawable).getHeight()), 1.0f, 50.0f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f);
// Clear the drawing area
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glTranslatef(-2.5f, 0.0f, 0.0f);
gl.glEnable(GL2.GL_TEXTURE_2D);
gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glBegin(GL2.GL_QUADS);
gl.glTexCoord2f(0, 0);
gl.glVertex3f(-1.0f,-1.0f, 0.0f);
gl.glTexCoord2f(0, a);
gl.glVertex3f(-1.0f, 1.0f, 0.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f( 1.0f, 1.0f, 0.0f);
gl.glTexCoord2f(a, 0);
gl.glVertex3f( 1.0f,-1.0f, 0.0f);
gl.glEnd();
gl.glDisable(GL2.GL_TEXTURE_2D);
gl.glRasterPos2d(3, -2);
gl.glDrawPixels(floorWidth, floorHeight, GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, pixels);
}
private void renderShadowsToTexture(GL2 gl) {
gl.glGenTextures(1, textureID, 0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
// null means reserve texture memory, but texels are undefined
gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, floorWidth, floorHeight,
0, GL2.GL_RGB, GL2.GL_FLOAT, null);
gl.glGenFramebuffers(1, frameBufferID, 0);
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferID[0]);
//Attach 2D texture to this FBO
gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0,
GL2.GL_TEXTURE_2D, textureID[0], 0);
// depth buffer
gl.glGenRenderbuffers(1, depthRenderBufferID, 0);
gl.glBindRenderbuffer(GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_DEPTH_COMPONENT,
floorWidth, floorHeight);
gl.glFramebufferRenderbuffer(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT,
GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
if(gl.glCheckFramebufferStatus(GL2.GL_FRAMEBUFFER) == GL2.GL_FRAMEBUFFER_COMPLETE)
System.out.println("[Viewer] GL_FRAMEBUFFER_COMPLETE!!");
else
System.out.println("..cazzo ^^");
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glPointSize(10.0f);
gl.glBegin(GL2.GL_POINTS);
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glVertex2d(1.0f, 1.0f); // THIS IS NOT SHOWN
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glVertex2d(-1.0f, -1.0f);
gl.glVertex2d(-0.9f, -0.9f);
gl.glEnd();
gl.glPopMatrix();
pixels = GLBuffers.newDirectByteBuffer(floorWidth*floorHeight*4);
gl.glReadPixels(0, 0, floorWidth, floorHeight, GL2.GL_RGBA,
GL2.GL_UNSIGNED_BYTE, pixels);
System.out.println("glIsTexture: "+gl.glIsTexture(textureID[0]));
// bind the back buffer for rendering
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);
}
public void dispose(GLAutoDrawable glad) {
// throw new UnsupportedOperationException("Not supported yet.");
System.out.println("dispose");
}
}
Starting from the left, the triangle and the first quad are rendered normally using the display() while the last quad on the right and the one below are respectively the quad rendered with the texture mapped on it and the quad showing what is inside the texture itself.
Basically I do not see the red point, only the blue ones. Why?
I'm not familiar with this particular OpenGL wrapper, but what I notice about your code is that in renderShadowsToTexture you do not configure two things: the viewport and the projection matrix. Both of these will affect the scaling of the resulting image.
The projection matrix will probably be the identity matrix (since you haven't run gluPerspective at all yet), which is reasonable for the coordinates you're using. But it is still good practice to set it explicitly to what you want for the sake of clarity and robustness (possibly with a pushMatrix/popMatrix around).
But I don't see where your code configures the viewport at all? Perhaps JOGL does that for you? If so, it will be the size of your window, not the size of the texture. This too-large viewport will cause portions of your scene to be cut off at the high-coordinate end, which is consistent with the texture you are seeing (note that the second blue point should be very close to the first one but shows up far away). So, you need to add to renderShadowsToTexture:
glViewport(0, 0, floorWidth, floorHeight)
and probably restore it afterward (or use glPushAttrib/glPopAttrib of GL_VIEWPORT_BIT).
Also, color components are red-green-blue, so your missing point will be green, not red.
According to Kevin Reid's answer (thanks to him) I revised renderShadowsToTexture(GL2 gl) and it worked great for me. I just wanted to share it below for newcomers.
private void renderShadowsToTexture(GL2 gl) {
gl.glGenTextures(1, textureID, 0);
gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
GL2.GL_NEAREST);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
GL2.GL_NEAREST);
// null means reserve texture memory, but texels are undefined
gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, floorWidth,
floorHeight, 0, GL2.GL_RGB, GL2.GL_FLOAT, null);
gl.glGenFramebuffers(1, frameBufferID, 0);
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferID[0]);
// Attach 2D texture to this FBO
gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0,
GL2.GL_TEXTURE_2D, textureID[0], 0);
// depth buffer
gl.glGenRenderbuffers(1, depthRenderBufferID, 0);
gl.glBindRenderbuffer(GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_DEPTH_COMPONENT,
floorWidth, floorHeight);
gl.glFramebufferRenderbuffer(GL2.GL_FRAMEBUFFER,
GL2.GL_DEPTH_ATTACHMENT, GL2.GL_RENDERBUFFER,
depthRenderBufferID[0]);
if (gl.glCheckFramebufferStatus(GL2.GL_FRAMEBUFFER) == GL2.GL_FRAMEBUFFER_COMPLETE)
System.out.println("[Viewer] GL_FRAMEBUFFER_COMPLETE!!");
else
System.out.println("..cazzo ^^");
gl.glViewport(0, 0, floorWidth, floorHeight);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, floorWidth, 0, floorHeight, -10, 10);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glPointSize(10.0f);
gl.glBegin(GL2.GL_POINTS);
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glVertex2d(20.0f, 32.0f); // THIS IS NOT SHOWN
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glVertex2d(20.0f, 10.0f);
gl.glVertex2d(0.9f, 0.9f);
gl.glEnd();
gl.glPopMatrix();
pixels = GLBuffers.newDirectByteBuffer(floorWidth * floorHeight * 4);
gl.glReadPixels(0, 0, floorWidth, floorHeight, GL2.GL_RGBA,
GL2.GL_UNSIGNED_BYTE, pixels);
System.out.println("glIsTexture: " + gl.glIsTexture(textureID[0]));
// bind the back buffer for rendering
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);
}