0

GLUT を使用する openGL プログラムがあり、ウィンドウのサイズを変更するときに左上隅から右下に移動することに固執しています。比率を下げようとしていますが、わかりません。

#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>

//global variables
GLsizei wh = 500, ww = 500;
double height;
 double width;
double top = 500;
double bottom = 450;
int right = 50;
int left = 0;
double ratiowidth = 500/500;
double ratioheight = 500/500;
void myinit(){
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void reshape(int w, int h){
if(h == 0){
    h = 1;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, (GLsizei) w, 0, (GLsizei)h);
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
int difference = h - wh;
int difference2 = w - ww;
left = left + difference2;
right = right + difference2;
wh = (GLsizei)h;
ww = (GLsizei)w;
height = h;
width = w;
}
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
    top = height;
    bottom = height - 50;
    right =  50;
    left = 0;
    ratiowidth = width/height;
    ratioheight = height/width;
}
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
    exit(0);
}
}
void display(){
glClear (GL_COLOR_BUFFER_BIT);
glColor3i (rand(), rand(), rand());
glBegin(GL_POLYGON);
  glVertex3f (left,top,0.0);
  glVertex3f (right, top, 0.0);
  glVertex3f (right, bottom, 0.0);
  glVertex3f (left, bottom, 0.0);
glEnd();
if(bottom == 0 || bottom < 0)
   bottom = 0;
else{
   left = left + ratiowidth;
   right = right + ratiowidth;
   top = top - ratioheight;
   bottom = bottom - ratioheight;
}
glutSwapBuffers();
usleep(10000);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(ww, wh);
glutInitWindowPosition(100,100);
glutCreateWindow("moving Square");
myinit();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
4

1 に答える 1

2
glBegin(GL_POLYGON);
  glVertex3f (left,top,0.0);
  glVertex3f (right, top, 0.0);
  glVertex3f (right, bottom, 0.0);
  glVertex3f (left, bottom, 0.0);
glEnd();

巻き順は慎重に。 glFrontFace()デフォルトはGL_CCWです。おそらくあなたはこれを意味しました:

glBegin(GL_POLYGON);
  glVertex3f (left, bottom, 0.0);
  glVertex3f (right, bottom, 0.0);
  glVertex3f (right, top, 0.0);
  glVertex3f (left,top,0.0);
glEnd();

編集:これはあなたがやろうとしていたことだと思います:

#include <GL/glut.h>

struct State
{
    double x;       // in units
    double y;    
    double xvel;    // in units per second
    double yvel;  
};
State curState = { 0 };

void myinit()
{
    curState.xvel = 100;
    curState.yvel = 100;
}

void Integrate( State* state, double dt )
{
    state->x += state->xvel * dt;
    state->y += state->yvel * dt;
}

double GetSeconds()
{
    return glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
}

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, (GLsizei)w, 0, (GLsizei)h);
}

void mouse(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {
        curState.x = 0;
        curState.y = 0;
    }
    if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
    {
        exit(0);
    }
}

void display()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glClear (GL_COLOR_BUFFER_BIT);

    float w = 20;
    float h = 20;
    float x = curState.x;
    float y = curState.y;

    glColor3ub(rand()%255, rand()%255, rand()%255);
    glBegin(GL_QUADS);
    glVertex2f( x - w/2, y - h/2 );
    glVertex2f( x + w/2, y - h/2 );
    glVertex2f( x + w/2, y + h/2 );
    glVertex2f( x - w/2, y + h/2 );
    glEnd();

    static double lastTime = GetSeconds();

    double curTime = GetSeconds();
    Integrate( &curState, curTime - lastTime );
    lastTime = curTime;

    glutSwapBuffers();
}

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("moving Square");
    myinit();
    glutReshapeFunc(reshape);
    glutTimerFunc(0, timer, 0);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
    return 0;
}

そこにはおそらくいくつかのC++イズムがあることに注意してください:)

于 2012-09-24T19:39:22.120 に答える