Jump to content
  • Advertisement
Sign in to follow this  
CRACK123

OpenGL Rendering to a texture

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to do a render to a texture using glCopyTexSubImage but it seems to be rendering nothing except a while patch. This is the code I have for rendering to a texture. This part sets up the scene and renders to a texture as well as to a texture
#include <cstdio>
#include <cstring>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "projection.h"

GLuint texture[1];

void SetupTexture()
{	
		// render a triangle to a texture
		glPushMatrix();
		glBegin(GL_TRIANGLES);
			glColor3f(1, 0, 0);  glVertex3f(-1, -5, -5);
			glColor3f(0, 1, 0);  glVertex3f(1, -5, -5);
			glColor3f(0, 0, 1);  glVertex3f(0, -5, -10);
		glEnd();
		glPopMatrix();
	
		if(glIsTexture(texture[0]) == GL_FALSE)
		{
			glGenTextures(1, &texture[0]);
			glBindTexture(GL_TEXTURE_2D, texture[0]);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
		}
		else
		{
			glBindTexture(GL_TEXTURE_2D, texture[0]);
		}
		
		glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 64, 64);
		GLenum err = glGetError();
		
		if(err != GL_NO_ERROR)
		{
			fprintf(stderr, "Errors");
		}
		
}		



This is where I am calling the above function for now. Also I am mapping the texture and rendering to a quad.
		
void Render()
{
	SetupTexture();
	
	glEnable(GL_TEXTURE_2D);
	// render the render to texture
	glPushMatrix();
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex3f(0, 0, -1);
		glTexCoord2f(1, 0); glVertex3f(2, 0, -1);
		glTexCoord2f(1, 1); glVertex3f(2, 2, -1);
		glTexCoord2f(0, 1); glVertex3f(0, 2, -1);
	glEnd();
	glPopMatrix();
	
	glDisable(GL_TEXTURE_2D);
}



However with this code I get only a white quad for the textured quad. I do get the triangle on screen. Any idea what I am doing wrong ? Thanks

Share this post


Link to post
Share on other sites
Advertisement
whats your viewport set to? whats your projection set too as well?
you also never clear the depth or color buffer maybe your default clear color
is white and its just copying a region which has nothing rendered to it..

also, im not sure about this but you might want to glEnable(GL_TEXTURE_2D)
before creating the texture as well..

Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace
whats your viewport set to? whats your projection set too as well?
you also never clear the depth or color buffer maybe your default clear color
is white and its just copying a region which has nothing rendered to it..


Thats because I call it in a different function. glViewport is set 640x480. The projection is set at fov = 45, w/h = 640/480. near = 1.0f, far = 1000.0f;


void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef(0, 0, -15);

Render();

glPopMatrix();

SDL_GL_SwapBuffers();
}



Quote:

also, im not sure about this but you might want to glEnable(GL_TEXTURE_2D)
before creating the texture as well..


This didn't work.


Share this post


Link to post
Share on other sites
Make sure from glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 64, 64);
try this
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!