Floating point textures

I am trying to create a floating point texture and display it full screen but it doesn’t want to work, it shows the quad but the quad is completely white.

So I tried uploading unsigned bytes but that also doesn’t want to work. I have created many OpenGL programs before but for some weird reason this code doesn’t want to work.

Thanks.

Code (Using SDL) : main.cpp


#include <iostream>
#include <cmath>
#include <SDL/SDL.h>
#include <GL/gl.h>

void init();
void cycle();

bool quit = false;

int main(int argc, char** args)
{
	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

	SDL_Surface* buffer = SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL);
	
	init();
	
	while (true)
	{
		SDL_Event event;
		while (SDL_PollEvent(&event))
			if (event.type == SDL_QUIT)
				quit = true;
				
		if (quit) break;
		
		cycle();
		SDL_GL_SwapBuffers();	
	}
	
	SDL_Quit();
}

void init()
{

}

void cycle()
{
	GLuint texture;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	
	float *buffer = new float[2 * 2 * 3];
	
	buffer[ 0] = 0.0f; buffer[ 1] = 0.0f, buffer[ 2] = 0.0f;
	buffer[ 3] = 1.0f; buffer[ 4] = 0.0f, buffer[ 5] = 0.0f;
	buffer[ 6] = 0.0f; buffer[ 7] = 1.0f, buffer[ 8] = 0.0f;
	buffer[ 9] = 0.0f; buffer[10] = 0.0f, buffer[11] = 1.0f;
	
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, 2, 2, 0, GL_RGB, GL_FLOAT, buffer);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glBindTexture(GL_TEXTURE_2D, texture);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f,0.0f); glVertex2f(-1.0f,-1.0f);
		glTexCoord2f(1.0f,0.0f); glVertex2f( 1.0f,-1.0f);
		glTexCoord2f(1.0f,1.0f); glVertex2f( 1.0f, 1.0f);
		glTexCoord2f(0.0f,1.0f); glVertex2f(-1.0f, 1.0f);
	glEnd();

	glDeleteTextures(1, &texture);
	
	delete buffer;
}

something is wrong with your code. buffer contains 12 values where your texture is 2x2 = 4 values. only 4 of them are taken on glTexImage2D. also you create and delete texture every time on cycle(). what is the point of that?

Aren’t there three values per pixel, for each channel (RGB)?

This is only a test. Is it OK to call glTexImage2D() every frame destroying the previous data or is it just overwritten?

Later on the values in the buffer will change each frame, so assume they changing each frame :slight_smile:

actually your right sorry. what im thinking early in the morning? what hardware your on? test if the floating point format is supported by your hardware. it totally ok to do so, will give you the worst performance though. better create once and update texture with glTexSubImage2D.

Graphics Card: NVIDIA GeForce 7300GT 256MB

Will change the texture creation.

EDIT: Changed


GLuint texture;

void init()
{
    glGenTextures(1, &texture);
}

void end()
{
    glDeleteTextures(1, &texture);
}

void cycle()
{
    glBindTexture(GL_TEXTURE_2D, texture);
	
    float *buffer = new float[2 * 2 * 3];
	
    buffer[ 0] = 0.0f; buffer[ 1] = 0.0f, buffer[ 2] = 0.0f;
    buffer[ 3] = 1.0f; buffer[ 4] = 0.0f, buffer[ 5] = 0.0f;
    buffer[ 6] = 0.0f; buffer[ 7] = 1.0f, buffer[ 8] = 0.0f;
    buffer[ 9] = 0.0f; buffer[10] = 0.0f, buffer[11] = 1.0f;
	
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, 2, 2, 0, GL_RGB, GL_FLOAT, buffer);
	
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glBegin(GL_QUADS);
        glTexCoord2f(0.0f,0.0f); glVertex2f(-1.0f,-1.0f);
        glTexCoord2f(1.0f,0.0f); glVertex2f( 1.0f,-1.0f);
        glTexCoord2f(1.0f,1.0f); glVertex2f( 1.0f, 1.0f);
        glTexCoord2f(0.0f,1.0f); glVertex2f(-1.0f, 1.0f);
    glEnd();
	
    delete buffer;
}

Found the problem. I forgot:
glEnable(GL_TEXTURE_2D);

Thanks anyway :slight_smile: