1
OpenGL과 함께 컬러 큐브가 표시된 프로그램이 있습니다. 나는 OpenGL 인스턴스를 표시하기 위해 자유 유리를 사용하고 있습니다. 이 프로그램은 마우스 왼쪽 버튼을 누른 채 사용자가 마우스로 큐브를 회전 할 수 있도록합니다. 각 마우스 움직임 후 커서는 윈도우의 중앙에 놓이게되고 glutPostRedisplay(); 단지 몇 번 호출되면 왼쪽 마우스 버튼을 놓을 때까지 호출되지 않습니다.glutPostRedisplay()가 항상 호출되는 것은 아닙니다.
my_cube.c
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <string.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/glew.h>
#include <GL/freeglut.h>
// ----------------------------------------------------------
// Function Prototypes
// ----------------------------------------------------------
void display();
void specialKeys();
// ----------------------------------------------------------
// Global Variables
// ----------------------------------------------------------
double rotate_y=0;
double rotate_x=0;
float deltaAngleX = 0.0f;
float deltaAngleY = 0.0f;
int xOrigin = -1;
int yOrigin = -1;
int MSAA;
int MouseState = 0;
void enableMultisample(int msaa){
if(msaa)
{
glEnable(GL_MULTISAMPLE);
glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
//detect current settings
GLint iMultiSample = 0;
GLint iNumSamples = 0;
glGetIntegerv(GL_SAMPLE_BUFFERS, &iMultiSample);
glGetIntegerv(GL_SAMPLES, &iNumSamples);
//printf("MSAA on, GL_SAMPLE_BUFFERS = %d, GL_SAMPLES = %d\n", iMultiSample, iNumSamples);
}
else
{
glDisable(GL_MULTISAMPLE);
//printf("MSAA off\n");
}
}
void mouseButton(int button, int state, int x, int y) {
// only start motion if the left button is pressed
if (button == GLUT_LEFT_BUTTON) {
// when the button is released
if (state == GLUT_UP) {
//rotate_x += deltaAngleX;
//rotate_y += deltaAngleY;
xOrigin = -1;
yOrigin = -1;
MouseState = 0;
}
else {// state = GLUT_DOWN
xOrigin = x;
yOrigin = y;
MouseState = 1;
}
}
}
void mouseMove(int x, int y) {
// this will only be true when the left button is down
if (xOrigin >= 0) {
int midWindowX = 1280/2;
int midWindowY = 720/2;
rotate_y -= (x - midWindowX)/10.0f;
rotate_x += (y - midWindowY)/10.0f;
glutWarpPointer(midWindowX, midWindowY);
//glutPostRedisplay();
printf("Rotate_X = %lf : Rotate_Y = %lf : Mouse_X = %d : Mouse_Y = %d\n", rotate_x, rotate_y, x, y);
glutPostRedisplay();
}
}
// ----------------------------------------------------------
// display() Callback function
// ----------------------------------------------------------
void display(){
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Reset transformations
//
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (1280.0f/720.0f), 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
enableMultisample(MSAA);
glTranslatef(0, 0, -4);
glRotatef(180, 1.0, 0.0, 0.0);
// Rotate when user changes rotate_x and rotate_y
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
// Reset rotations
if(rotate_x == 360.0f || rotate_x > 360.0f || rotate_x == -360 || rotate_x < -360) rotate_x = 0.0f;
if(rotate_y == 360.0f || rotate_y > 360.0f || rotate_y == -360 || rotate_y < -360) rotate_y = 0.0f;
//Multi-colored side - FRONT
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0); glVertex3f(-0.5, -0.5, -0.5); // P4 is green
glColor3f(0.0, 0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5); // P3 is blue
glColor3f(1.0, 0.0, 1.0); glVertex3f( 0.5, 0.5, -0.5); // P2 is purple
glColor3f(1.0, 0.0, 0.0); glVertex3f( 0.5, -0.5, -0.5); // P1 is red
glEnd();
// White side - BACK
glBegin(GL_POLYGON);
glColor3f( 1.0, 1.0, 1.0);
glVertex3f( 0.5, -0.5, 0.5);
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0);
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f( 0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0);
glVertex3f( 0.5, -0.5, -0.5);
glVertex3f( 0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glFlush();
glutSwapBuffers();
}
// ----------------------------------------------------------
// specialKeys() Callback Function
// ----------------------------------------------------------
void specialKeys(int key, int x, int y) {
if(!MouseState){
// Right arrow - increase rotation by 5 degree
if (key == GLUT_KEY_RIGHT)
rotate_y -= 5;
// Left arrow - decrease rotation by 5 degree
else if (key == GLUT_KEY_LEFT)
rotate_y += 5;
else if (key == GLUT_KEY_UP)
rotate_x -= 5;
else if (key == GLUT_KEY_DOWN)
rotate_x += 5;
// Request display update
glutPostRedisplay();
}
}
// ----------------------------------------------------------
// main() function
// ----------------------------------------------------------
int main(int argc, char* argv[]){
for(int i; i<argc; i++){
char *arg;
arg = argv[i];
const char msaaArg[4] = "MSAA";
char *ret;
ret = strstr(arg, msaaArg);
if(ret != NULL){
MSAA = (intptr_t)argv[i+1];
}
}
// Initialize GLUT and process user parameters
glutInit(&argc,argv);
// Set Initial Size of window
glutInitWindowSize(1280, 720);
// Enable Multisampling
glutSetOption(GLUT_MULTISAMPLE, 8);
// Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
// Create window
glewInit();
glutCreateWindow("");
// Get OpenGL Version
printf("Using OpenGL Version: %s\n", glGetString(GL_VERSION));
char title[150];
const char* temp;
strcpy(title, "Awesome Cube [OpenGL Version ");
strcat(title, glGetString(GL_VERSION));
temp = glGetString(GL_VERSION);
strcat(title, "]");
glutSetWindowTitle(title);
// Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);
// Enable Backface Culling
glEnable(GL_CULL_FACE);
// Move mouse to the middle of the screen
glutWarpPointer(1280/2, 720/2);
// Callback functions
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove);
// Pass control to GLUT for events
glutMainLoop();
return 0;
}
왜'glutPostRedisplay을 넣지 마십시오()''표시()'의 끝에? – Rabbid76
나는 항상 컴퓨터 리소스를 사용하는 것이 필요하다고 생각하지 않았습니다. 이미지가 필요할 때만 렌더링하려고했습니다. –