Viewport/image help needed

hello guys, i want my code to display the raw image “texture.raw”, and at the same time, display a flashing text exactly besides it in a seperate viewport or with some other way. Please help me with this, as this code doesnt do that, it just flashes the image and doesnt display the text. Please check the bold part in display method, i think thats where things are wrong…but i don;t know since i m a newbie.

#include "stdafx.h"
#include <stdlib.h>

#include "glut.h"
#include <windows.h>
#include <stdio.h>

//---
int frame=0;
int frame3=0;
int no_points=74;
float sx=1.0, sy=1.0, sx2=1.0,sy2=1.0, sx3=1.0,sy3=1.0, sx4=1.0,sy4=1.0, sx5=1.0,sy5=1.0;

const GLfloat colors[][3] = {
  { 255.0/255.0,   0.0/255.0,   0.0/255.0},
  { 128.0/255.0,   0.0/255.0,   0.0/255.0},
  { 205.0/255.0,   0.0/255.0,   0.0/255.0},
  {   0.0/255.0, 255.0/255.0,   0.0/255.0},
  {   0.0/255.0, 128.0/255.0,   0.0/255.0},
  {   0.0/255.0, 205.0/255.0,   0.0/255.0},
  {   0.0/255.0,   0.0/255.0, 255.0/255.0},
  {   0.0/255.0,   0.0/255.0, 128.0/255.0},
  {   0.0/255.0,   0.0/255.0, 205.0/255.0},
  { 255.0/255.0, 255.0/255.0,   0.0/255.0},
  { 128.0/255.0, 128.0/255.0,   0.0/255.0},
  { 205.0/255.0, 205.0/255.0,   0.0/255.0},
  {   0.0/255.0, 255.0/255.0, 255.0/255.0},
  {   0.0/255.0, 128.0/255.0, 128.0/255.0},
  {   0.0/255.0, 205.0/255.0, 205.0/255.0},
  { 255.0/255.0,   0.0/255.0, 255.0/255.0},
  { 128.0/255.0,   0.0/255.0, 128.0/255.0},
  { 205.0/255.0,   0.0/255.0, 205.0/255.0},
};





void renderSpacedBitmapString(float x, float y, int spacing, void *font, char *string) 
{
  char *c;
  int x1=x;
  for (c=string; *c != '\0'; c++) {
	glRasterPos2f(x1,y);
    	glutBitmapCharacter(font, *c);
	x1 = x1 + glutBitmapWidth(font,*c) + spacing;
  }
}

void text1()
{
	glColor3f(colors[frame3][0],colors[frame3][1],colors[frame3][2]);
	frame3+=1;
	renderSpacedBitmapString(130,200,1,GLUT_BITMAP_TIMES_ROMAN_24,"Food & Drink");
	if(frame3>18)	frame3=0;
}


//----

GLuint texture; //the array for our texture

GLfloat angle = 0.0;
int x;

//function to load the RAW file

GLuint LoadTexture( const char * filename, int width, int 
height )
{
    GLuint texture;
    unsigned char * data;
    FILE * file;

    //The following code will read in our RAW file
    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc( width * height * 3 );
    fread( data, width * height * 3, 1, file );
    fclose( file );

    glGenTextures( 1, &texture ); //generate the texture with 
    glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, 
GL_MODULATE ); //set texture environment parameters



    //The qualities are (in order from worst to best)
    //GL_NEAREST
    //GL_LINEAR
    //GL_LINEAR_MIPMAP_NEAREST
    //GL_LINEAR_MIPMAP_LINEAR

    //And if you go and use extensions, you can use Anisotropic 
    //even better quality, but this will do for now.
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
 GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
 GL_LINEAR );

    //Here we are setting the parameter to repeat the texture 
    //to the edge of our shape. 
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 
GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 
GL_REPEAT );

    //Generate the texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
 GL_RGB, GL_UNSIGNED_BYTE, data);
    free( data ); //free the texture
    return texture; //return whether it was successfull
}

void FreeTexture( GLuint texture )
{
  glDeleteTextures( 1, &texture );
}

void square (void) {
    glBindTexture( GL_TEXTURE_2D, texture ); //bind our texture
    glBegin (GL_QUADS);
    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0); //with 
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0); //so that
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();

//This is how texture coordinates are arranged
//
//  0,1   —–   1,1
//       |     |
//       |     |
//       |     |
//  0,0   —–   1,0

// With 0,0 being the bottom left and 1,1 being the top right.


// brick walls.
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
	glViewport (150, 30, (GLsizei)700, (GLsizei)500);
    glEnable( GL_TEXTURE_2D );
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    square();
    glutSwapBuffers();
    angle ++;
	//-------------------------
[b]//glMatrixMode (GL_PROJECTION);
	//glLoadIdentity();
	glViewport(0,0,(GLsizei)800,(GLsizei)600);
	text1();[/b]



}
void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (70, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE);
    glutInitWindowSize (800, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);

    //Load our texture
    texture = LoadTexture( "texture.raw", 256, 256 );

    glutMainLoop ();

    //Free our texture
    FreeTexture( texture );

    return 0;
}

Thats because you are swapping the buffers before you draw your text. And what is your projection matrix ? I think you should change your display function to this.


void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    glViewport (150, 30, (GLsizei)700, (GLsizei)500);
    glEnable( GL_TEXTURE_2D );
       square();
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,800,0,600);
    glViewport(0,0,(GLsizei)800,(GLsizei)600);
       text1();

    angle ++;
    glutSwapBuffers();
}

See if this helps,
Mobeen

Hey Mobeen, Thanks a lot for replying, i tried what you mentioned, and now it doesn’t display anything, just black screen. Any help??

I just saw you have not setup the depth buffer and the framebuffer format usually when we init we must specify this. Also u would need to turn the depth test off. I will post the correct code in a while.

Ok try this and tell me if it works.


#include <stdlib.h>

#include <gl/glut.h>
#include <windows.h>
#include <stdio.h>

//---
int frame=0;
int frame3=0;
int no_points=74;
float sx=1.0, sy=1.0, sx2=1.0,sy2=1.0, sx3=1.0,sy3=1.0, sx4=1.0,sy4=1.0, sx5=1.0,sy5=1.0;

const GLfloat colors[][3] = {
  { 255.0/255.0,   0.0/255.0,   0.0/255.0},
  { 128.0/255.0,   0.0/255.0,   0.0/255.0},
  { 205.0/255.0,   0.0/255.0,   0.0/255.0},
  {   0.0/255.0, 255.0/255.0,   0.0/255.0},
  {   0.0/255.0, 128.0/255.0,   0.0/255.0},
  {   0.0/255.0, 205.0/255.0,   0.0/255.0},
  {   0.0/255.0,   0.0/255.0, 255.0/255.0},
  {   0.0/255.0,   0.0/255.0, 128.0/255.0},
  {   0.0/255.0,   0.0/255.0, 205.0/255.0},
  { 255.0/255.0, 255.0/255.0,   0.0/255.0},
  { 128.0/255.0, 128.0/255.0,   0.0/255.0},
  { 205.0/255.0, 205.0/255.0,   0.0/255.0},
  {   0.0/255.0, 255.0/255.0, 255.0/255.0},
  {   0.0/255.0, 128.0/255.0, 128.0/255.0},
  {   0.0/255.0, 205.0/255.0, 205.0/255.0},
  { 255.0/255.0,   0.0/255.0, 255.0/255.0},
  { 128.0/255.0,   0.0/255.0, 128.0/255.0},
  { 205.0/255.0,   0.0/255.0, 205.0/255.0},
};





void renderSpacedBitmapString(float x, float y, int spacing, void *font, char *string) 
{
  char *c;
  int x1=x;
  for (c=string; *c != '\0'; c++) {
	glRasterPos2f(x1,y);
    	glutBitmapCharacter(font, *c);
	x1 = x1 + glutBitmapWidth(font,*c) + spacing;
  }
}

void text1()
{
    glColor3f(colors[frame3][0],colors[frame3][1],colors[frame3][2]);
	 
	frame3+=1;
	renderSpacedBitmapString(20,20,1,GLUT_BITMAP_TIMES_ROMAN_24,"Food & Drink");
	if(frame3>18)	frame3=0;
}


//----

GLuint texture; //the array for our texture

GLfloat angle = 0.0;
int x;

//function to load the RAW file

GLuint LoadTexture( const char * filename, int width, int 
height )
{
    GLuint texture;
    unsigned char * data;
    FILE * file;

    //The following code will read in our RAW file
    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc( width * height * 3 );
    fread( data, width * height * 3, 1, file );
    fclose( file );

    glGenTextures( 1, &texture ); //generate the texture with 
    glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); //set texture environment parameters



    //The qualities are (in order from worst to best)
    //GL_NEAREST
    //GL_LINEAR
    //GL_LINEAR_MIPMAP_NEAREST
    //GL_LINEAR_MIPMAP_LINEAR

    //And if you go and use extensions, you can use Anisotropic 
    //even better quality, but this will do for now.
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    //Here we are setting the parameter to repeat the texture 
    //to the edge of our shape. 
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

    //Generate the texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    free( data ); //free the texture
    return texture; //return whether it was successfull
}

void FreeTexture( GLuint texture )
{
  glDeleteTextures( 1, &texture );
}

void square (void) {
    glBindTexture( GL_TEXTURE_2D, texture ); //bind our texture
    glBegin (GL_QUADS);
    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0); //with 
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0); //so that
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();

//This is how texture coordinates are arranged
//
//  0,1   —–   1,1
//       |     |
//       |     |
//       |     |
//  0,0   —–   1,0

// With 0,0 being the bottom left and 1,1 being the top right.


// brick walls.
}
void SetOrthoForFont()
{	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0, 800, 0, 600);
	 
	glMatrixMode(GL_MODELVIEW);	
	glLoadIdentity();
}

void ResetPerspectiveProjection() 
{
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}
void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT );
 
	glViewport (150, 30, (GLsizei)700, (GLsizei)500);
     
	glEnable( GL_TEXTURE_2D );
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    square();
    glDisable(GL_TEXTURE_2D);
	 
    angle ++;
	//-------------------------
   
	glViewport(0,0,(GLsizei)800,(GLsizei)600);
	SetOrthoForFont();
	   text1();	 
	ResetPerspectiveProjection();
    glutSwapBuffers();

}
void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (70, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowSize (800, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);

    //Load our texture
    texture = LoadTexture( "texture.raw", 256, 256 );

    glutMainLoop ();

    //Free our texture
    FreeTexture( texture );

    return 0;
}

hey mobeen, Thank you soo much, your code does work.

but i changed the display method to the following and yes it works perfectly, but now what i basically want is, that the text to animate out on the screen (which should be easy) using scaling and translating with frames. Also, the image remains where it is, but the text just animates from small to big.

The problem with this is that its doing it tooo fast, the frames are going very fast per second. Please help me with this, i want it to go slow and the text to move slowly.

please check out the //new— to //—new parts in the following display method, thats where animation is happening and is controlled

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
	//glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
	gluLookAt (0.0, 0.0, 5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
	  //gluOrtho2D(0,800,0,600);
	glViewport (150, 80, (GLsizei)700, (GLsizei)500);
    glEnable( GL_TEXTURE_2D );
	//glTranslatef(1,0,0);
	square();

    
	//-------------------------
	//glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
	gluLookAt (0.0, 0.0,1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    gluOrtho2D(0,800,0,600);
   glViewport (100, 200, (GLsizei)700, (GLsizei)500);

    //--new  
 if((frame>=0) && (frame<50))
   {
	   glTranslatef(160 * (1- sx), 203 * (1 - sy), 0 );
		glScalef(sx, sy, 1.0);

			text1();
			sx=sx * 1.01;
			sy=sy * 1.01;		
   }//---new

//new---
	fprintf(stdout,"Frame number= %i
", frame);
	if(frame>50)	 sx=1.0,sy=1.0, sx2=1.0,sy2=1.0,  sx3=1.0,sy3=1.0, sx4=1.0,sy4=1.0, sx5=1.0,sy5=1.0;
	if(frame>50) frame=0;
//---new
	
	angle ++;
	frame++;//new

	glutSwapBuffers();


}

Hi, to do such an animation, u need to calculate the rotation amount based on the deltaT between two frames. To do that you can do something like this. First, initialize a start time variable somewhere at the init function like this.


float startTime = glutGet(GLUT_ELAPSED_TIME);

Then in the render function get the elapsed time again


void display() {
   float elapsedTime = glutGet(GLUT_ELAPSED_TIME);

   //see if 1 sec has elapsed
   if((elapsedTime - startTime)> 1000) {
     deltaT = (elapsedTime - starTime)/1000.0; //deltaT is in secs
     startTime = elapsedTime;
   }
...
//rest of the render function
if((frame>=0) && (frame<50))
   {
	   glTranslatef(160 * (1- sx), 203 * (1 - sy), 0 );
		glScalef(sx, sy, 1.0);

			text1();
			sx=sx * 1.01 * deltaT;
			sy=sy * 1.01 * deltaT;		
   }//---new


  
}

I have not run this code myself but this is how I would do it. This way the animation will play at a consistent rate irrespective of the machine’s processing speed. In your previous appraoch, the scaling would be faster on fast machine and vice versa.

See if this sorts out your problem.

Regards,
Mobeen

Hey Mobeen, thanks again, but this technique doesnt work, it’s still fast. There is no init function in my code hence i put the

float startTime = glutGet(GLUT_ELAPSED_TIME);

inside the reshape function, and tried putting in main and display functions too, but no luck.

OK I think u r not getting it. I will try to give u a detailed response soon.

Ok u can check this web page out they tell u what I could not explain well. Hope u can carryon on your own from here.

http://hdrlab.org.nz/articles/amiga-os-a…ion-using-glut/
http://www.sacredsoftware.net/tutorials/Animation/TimeBasedAnimation.xhtml

Hey mobeen, thanks for all your help, i finally figured it out how to control animation frame rate, but by creating one timer function and a visibility function in the code and then saying in the main; glutVisibilityFunc(visibility). And now my animation is almost done… :smiley: Thanks.