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);
}
Related
I'm trying to write something in OpenGL, and I'm a beginner so sorry for any mistakes I make.
in general I just wanted to draw two triangles with different colours and I did using the following code:
float vertices[] = {
-0.5f, -0.6f, 0.0f,
0.5f, -0.6f, 0.0f,
0.4f, 0.5f, 0.0f,
0.5f, 0.6f, 0.0f,
-0.5f, 0.6f, 0.0f,
-0.4f, -0.5f, 0.0f
};
void display() {
std::cout << "frame";
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
// activate and specify pointer to vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
// draw a cube
glColor3f(1.0f, 0.0f, 0.0f); // Red
glDrawArrays(GL_TRIANGLES, 0, 3);
//glColor3f(0.0f, 1.0f, 0.0f); // Green
glDrawArrays(GL_TRIANGLES, 3, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush(); // Render now
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the infinitely event-processing loop
return 0;
}
now.. if I . want to draw both triangles in the same command I can do
glDrawArrays(GL_TRIANGLES, 0, 6);
but then it draws the two triangles in the same colour.
is there a way to draw each triangle in a different colour by still using only one glDrawArrays() command?
if not.. is there some other command I should go for ?
thank you
In the description of glDrawArrays it is written :
Instead of calling a GL procedure to pass each individual vertex attribute, you can use glVertexAttribPointer to prespecify separate arrays of vertices, normals, and colors and use them to construct a sequence of primitives with a single call to glDrawArrays.
Is that your solution ?
"if not.. is there some other command I should go for ?" Fixed function attributes and client-side capability is deprecated since decades.See Fixed Function Pipeline and Legacy OpenGL.
Read about Vertex Specification and Shader for a state of the art way of rendering.
Anyway you can define an array of color attributes by glColorPointer, so each vertex coordinate is associated to an individual color attribute:
float colors[] = {
1.0f, 0.0f, 0.0f, // red
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, // green
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colors);
glDrawArrays(GL_TRIANGLES, 0, 6);
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
.....
}
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
I have a GL.GL_QUAD_STRIP with a texture , since the GL.GL_QUAD_STRIP sizes are much larger then the texture image sizes , this image become stretched and blur , So I thought maybe there is a way to set this texture in "sliced" mode to make it more clearly and sharp .
Edit: "sliced" I mean in the location the images end I would be start to be drawn again except to be appear one whit hard stretch .
That's what I have so far -
double m_x , m_y , m_z ;
Texture m_cubeSides ;
public void display(GLAutoDrawable gLDrawable)
{
final GL gl = gLDrawable.getGL();
gl.glTexParameteri ( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T,GL.GL_LINEAR_MIPMAP_LINEAR);
gl.glTexParameteri( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S,GL.GL_LINEAR_MIPMAP_LINEAR);
m_cubeSides.bind();
gl.glBegin(GL.GL_QUAD_STRIP);
// Quad 1
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3d(m_x, m_y, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3d(m_x, 0, 0);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3d(0, m_y, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3d(0, 0, 0);
// Quad 2 - front
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3d(0, m_y, m_z);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3d(0, 0, m_z);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3d(m_x, m_y, m_z);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3d(m_x, 0, m_z);
// Quad 3 - east side
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3d(m_x, m_y, m_z);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3d(m_x, 0, m_z);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3d(m_x, m_y, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3d(m_x, 0, 0);
// Quad 4 - west side
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3d(0, m_y, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3d(0, 0, 0);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3d(0, m_y, m_z);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3d(0, 0, m_z);
gl.glEnd();
}
I'm not sure what you mean by sliced mode, however changing the texture coordinates would work. There are an awful lot of calls to glTexCoord2f so perhaps a variable to replace all the instances of 1.0f in the glTexCoord2f calls and then vary that till you get a nice result. You've already set the texture wrap mode to repeat which is required.
Something like:
float textureMax = 5.0f;
...
gl.glTexCoord2f(textureMax , 0.0f);
gl.glVertex3d(m_x, m_y, 0);
gl.glTexCoord2f(textureMax ,textureMax );
gl.glVertex3d(m_x, 0, 0);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3d(0, m_y, 0);
gl.glTexCoord2f(0.0f, textureMax );
gl.glVertex3d(0, 0, 0);
The downside of this though is that you need a texture you can tile, otherwise this may not look great.
Another option is a bigger texture, of course :)
I am working on a n-body code with "glut functions" display. I would like to display each body with a 2D texture from a bmp image. Currently, I can draw a single textured element with the following code :
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
glBindTexture(GL_TEXTURE_2D, texture[0]); // pick the texture.
glBegin(GL_QUADS); // begin drawing the textured quad.
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd(); // done drawing the textured quad.
In the first version of my code, I draw the positions of each body with the following display function :
void drawPoints()
{
GLuint vbo;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glEnableClientState(GL_VERTEX_ARRAY);
if (colorVBO) {
glBindBuffer(GL_ARRAY_BUFFER, id_colorVBO);
glColorPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glEnableClientState(GL_COLOR_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glColor3f(1, 1, 0);
glDrawArrays(GL_POINTS, 0, numBodies);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
where pos array contains the coordinates x, y, z of each body. I am using glDrawArrays function to draw all the points at the same time.
Could you tell me how to plot all the textured elements and not only one, i.e a way to use the coordinates pos array for indicate the positions of all the textured bodies and draw them.
Updated :
ok, I try to use glTexCoordPointer with the following display function :
void drawPoints()
{
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
glBindTexture(GL_TEXTURE_2D, texture[0]); // pick the texture.
glLoadIdentity(); // reset the view before we draw each star.
glTranslatef(0.0f, 0.0f, zoom); // zoom into the screen.
glEnableClientState(GL_VERTEX_ARRAY);
lEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
// Assign A Color Using Bytes
glColor4ub(30, 100, 120, 255);
glBegin(GL_QUADS); // Begin Drawing The Textured Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd(); // Done Drawing The Textured Quad
glDrawArrays(GL_QUADS, 0, numBodies);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
}
but only one textured element is displayed.
Anyone sees what's wrong ?
In addition to glVertexPointer, you should use glTexCoordPointer. Do not forget to enable the corresponding client state, GL_TEXTURE_COORD_ARRAY. There is also a complete example on the official wiki, with interleaved attributes.
Note that the new (3.0+) way is to use glVertexAttribPointer along with shaders. There's a very descriptive page on the wiki on that, as well.