PDA

View Full Version : why my code can not render a stabiliza display?



Suvcon
01-17-2001, 07:04 AM
Well,it flash and dont display smooth.why?

(the 1.bmp is a 32*32 bmp file.)
========================================

#include <gl/glut.h>
#include <gl/glaux.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

#define NUM 50
int style(0);
float R(36.0f/NUM),D(0.002f);
AUX_RGBImageRec* image;
GLuint texture[4];
int rage(1);


struct {
float ro;
float di;
GLubyte R,G,B;
int k;
} star[NUM];

void beforedawn()
{
srand( (unsigned)time( NULL ) );

for(int i(0);i<NUM;i++)
{
star[i].ro=0.0f;
star[i].di=0.0f;

{ star[i].R=( rand() % 256);
star[i].G=( rand() % 256);
star[i].B=( rand() % 256);
}
if(style<0) star[i].k=( rand() % 4 ); else star[i].k=style;
}
}

void GetImage()
{
image=auxDIBImageLoad("1.bmp");
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,3,image->sizeX,image->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,image->data);

image=auxDIBImageLoad("2.bmp");
glBindTexture(GL_TEXTURE_2D,texture[1]);
glTexImage2D(GL_TEXTURE_2D,0,3,image->sizeX,image->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,image->data);

image=auxDIBImageLoad("3.bmp");
glBindTexture(GL_TEXTURE_2D,texture[2]);
glTexImage2D(GL_TEXTURE_2D,0,3,image->sizeX,image->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,image->data);

image=auxDIBImageLoad("4.bmp");
glBindTexture(GL_TEXTURE_2D,texture[3]);
glTexImage2D(GL_TEXTURE_2D,0,3,image->sizeX,image->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,image->data);

}


void renderScene(void)
{

glClear(GL_COLOR_BUFFER_BIT);

glPolygonMode(GL_FRONT,GL_FILL);

int temp=(rage%NUM);
for(int i(0);i<temp;i++)
{
glBindTexture(GL_TEXTURE_2D,texture[star[i].k]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR); // Linear Filtering

glPushMatrix();
glRotatef(star[i].ro,0.0f,0.0f,1.0f);
glTranslatef(star[i].di,0.0f,0.0f);
glColor4ub(star[i].R,star[i].G,star[i].B,255);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);glVertex3f(-0.1f,-0.1f,0.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(0.1f,-0.1f,0.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(0.1f,0.1f,0.0f) ;
glTexCoord2f(0.0f,1.0f);glVertex3f(-0.1f,0.1f,0.0f);
glEnd();
glPopMatrix();
star[i].ro+=R;star[i].di+=D;
}
rage++;

//SwapBuffers(wglGetCurrentDC());glFinish();

glutSwapBuffers();

}


void keyfunc(unsigned char key,int x,int y)
{
}


void changeSize(int w, int h)
{
if(h == 0) h = 1; float ratio = 1.0* w / h;
glMatrixMode(GL_PROJECTION);glLoadIdentity();
glViewport(0, 0, w, h);

gluPerspective(45,ratio,1,10);

glMatrixMode(GL_MODELVIEW);glLoadIdentity();
gluLookAt(0.0,0.0,5.0, 0.0,0.0,-1.0, 0.0f,1.0f,0.0f);
}

void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Use the Texture!!");
glClearColor(0.0f,0.0f,0.0f,1.0f);

glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE);
beforedawn();

glEnable(GL_TEXTURE_2D);
glGenTextures(4,texture);
GetImage();

glutKeyboardFunc(keyfunc);
glutReshapeFunc(changeSize);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();

}

Suvcon
01-18-2001, 07:56 AM
No one wanna reply? 8(

NOAH IV
01-18-2001, 08:45 AM
Try separate your "stars" in four groups with the same textures...

Then you just call glBindTexture four times, instead of call it in the loop for each star.
I think it will help a little.