Jump to content
  • Advertisement
Sign in to follow this  
nickme

opengl: cannot render texture to another window.

This topic is 2517 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,
after successfully rendered a texture, i decided to do something different. i use the same texture, which was mapped to a 3D sin function graph and rotated on y-axis, and opened another sub window to render the same texture but this time it is flat on a rectangle on the z = 0 plane. the sin graph seen to works as what i wanted (spinning on the y-axis). but the other subwindow, which i render a flat rectangle texture did not appear as desired. it just cleared the screen with the color that i provided with the glClearColor(). the condensed program structure is provided as the following:

init_mainWindow();
init_subWindow1();
init_subWindow2();

and
render_mainWindow();
render_subWindow1();
render_subWindow2();

i tried various functions call but the render_subWindow2() seen did not do anything. i suspect that the coordinates are different from what i tried to map the texture coord to. i queried the system and can see that the viewport were the sizes of the window that i opened, but i do not know how to query the world coordinates of the window. that may be why the problem persisted.

another possibility, can i use the same texture that i opened in two different windows? tried adding some of the binding codes in render_subWindow1() to render_subWindow2(), but nothing worked. for example, the following codes are added to both func :

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture[0]);

note: my program was inspired by lighthouse 3d tutorials on the internet. the turorial is subwindow.

any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
hi,

i had included the source codes for the program here. i dont know the whether it is appropiate to include about 200 lines of code in a post, but i included it here. i am still debugging the program, it is only a draft. the size of file dog.tga is about 1028x372. maybe you can create your own tga file and run the program. i attack a out.png, it is a screen output of my running program. hope i included it correctly.

thanks

//
// Solid D3mapping with texture and subWindow.
//
#include <Windows.h>
#include <GLtools.h> // OpenGL toolkit
#include <stopwatch.h> // OpenGL toolkit
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>

int mainWindow, subWindow1, subWindow2;
const GLfloat PI=3.14159f;
const GLint winSize= 600, resolutionX=40, resolutionZ=40;
const GLint maxIter = 60, swin = winSize/4;
const GLfloat left=-2.0f*PI, right=PI*2.0f, bottom=-1.5f*PI, top=1.5f*PI;
const GLfloat deltax = (right - left)/GLfloat(resolutionX), deltaz = (top - bottom)/GLfloat(resolutionZ);
const GLfloat D3factor=0.5f, rad=1.f, TcoordX=1.0f/resolutionX, TcoordZ=1.0f/resolutionZ;
GLfloat y[resolutionZ+1][resolutionX+1]={0.0f}, rotate=0.0f;
GLuint texture[1];

GLfloat Sin(GLfloat cR, GLfloat cI) {
return sin(cR)*18.0f;
}

void D3draw() {
int i, j;
static CStopWatch rotTimer;
GLfloat z, x;

glClearColor(1.0, 1.0, 1.0, 1.0);
rotate = rotTimer.GetElapsedSeconds();
for (i=0, z=bottom; i < resolutionZ; z+=deltaz, i++) {
for (j=0, x=left; j < resolutionX; j++, x+=deltax) {
glBegin(GL_QUADS);
glTexCoord2f((float)j*TcoordX,(float)i*TcoordZ); glVertex3f(x, y[j], z);
glTexCoord2f((float)(j+1)*TcoordX,(float)i*TcoordZ); glVertex3f(x+deltax, y[j+1], z);
glTexCoord2f((float)(j+1)*TcoordX,(float)(i+1)*TcoordZ);glVertex3f(x+deltax, y[j+1][i+1],z+deltaz);
glTexCoord2f((float)j*TcoordX,(float)(i+1)*TcoordZ); glVertex3f(x, y[j][i+1], z+deltaz);
glEnd();
}
}
glutSwapBuffers();
// glutPostRedisplay();
}

void D3mapping() {
GLfloat x, z;
int i, j;

for (i = 0, z = bottom; i <= resolutionZ; i++, z+=deltaz)
for (j = 0, x = left; j <= resolutionX; j++, x+=deltax)
y[j] = Sin(x, z);
}

void render_MainWindow() {
glutSetWindow(mainWindow);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 0.0, 1.0);
glutSwapBuffers();
}

void render_SubWindow1(void) {
glutSetWindow(subWindow1);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glRotatef(rotate, 0.0, 1.0, 0.0);
D3draw();
}

void render_SubWindow2() {
glutSetWindow(subWindow2);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
glClearColor(1.0, 0.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
glEnd();
glutSwapBuffers();
}

void renderAll() {
render_MainWindow();
render_SubWindow1();
render_SubWindow2();
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
}

void shut_down() {
glDeleteTextures(1, texture);
glutDestroyWindow(subWindow1);
glutDestroyWindow(subWindow2);
glutDestroyWindow(mainWindow);
}

void keyboard(unsigned char key, int x, int y) {
if (key == 27) {
shut_down();
exit(0);
}
}

void init_mainWindow(){
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 0.0, 1.0);
}

void init_subWindow1(void) {
GLbyte *pBytes;
GLint imageWidth, imageHeight, iComponent;
GLenum format;

D3mapping();
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
pBytes = gltReadTGABits("dog.tga", &imageWidth, &imageHeight, &iComponent, &format);
if (pBytes == NULL){
printf(" **** Error in loading file in init() or incorrect file ****\n\n");
system("pause");
exit(0);
}
glFrontFace(GL_CW);
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, iComponent, imageWidth, imageHeight, 0, format, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(left-3.0, right+3.0, -maxIter*D3factor, maxIter*D3factor, 40.0, 90.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0.0, 50.0, 40.0, 0.0, 0.0, 0.0, 0.0, 3.867, -4.0);
}

void init_subWindow2(){
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(1.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, 4, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

int main(int argc, char** argv) {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(600, 50);
glutInitWindowSize(winSize, winSize);

mainWindow = glutCreateWindow(" Sin function with texture ");
glutDisplayFunc(render_MainWindow);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(renderAll);
init_mainWindow();

subWindow2 = glutCreateSubWindow(mainWindow, 0, winSize-swin+1, winSize, swin);
init_subWindow2();
glutDisplayFunc(render_SubWindow2);

subWindow1 = glutCreateSubWindow(mainWindow, 0, 0, winSize, winSize-swin);
init_subWindow1();
glutDisplayFunc(render_SubWindow1);

glutMainLoop();
shut_down();
return 0;
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!