OpenGL Programming Guide - Red Book 예 프로그램 라이브러리 - 시리즈 - 2-Introduction to OpenGL-Part2

6529 단어 OpenGL
Example 1-3 : A Double-Buffered Program: double.c
원시 프로그램
#include 
#include 
#include 
#include "aux.h"

static GLfloat spin = 0.0;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(spin, 0.0, 0.0, 1.0);
    glRectf(-25.0, -25.0, 25.0, 25.0);
    glPopMatrix();

    glFlush();
    glXSwapBuffers(auxXDisplay(), auxXWindow());
}

void spinDisplay(void)
{
    spin = spin + 2.0;
    if (spin > 360.0)
        spin = spin - 360.0;
    display();
}

void startIdleFunc(AUX_EVENTREC *event)
{
    auxIdleFunc(spinDisplay);
}

void stopIdleFunc(AUX_EVENTREC *event)
{
    auxIdleFunc(0);
}

void myinit(void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glShadeModel(GL_FLAT);
}

void myReshape(GLsizei w, GLsizei h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h) 
        glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w, 
            50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
    else 
        glOrtho (-50.0*(GLfloat)w/(GLfloat)h, 
            50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity ();
}


int main(int argc, char** argv)
{
    auxInitDisplayMode(AUX_DOUBLE | AUX_RGBA);
    auxInitPosition(0, 0, 500, 500);
    auxInitWindow(argv[0]);
    myinit();
    auxReshapeFunc(myReshape);
    auxIdleFunc(spinDisplay);
    auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, startIdleFunc);
    auxMouseFunc(AUX_MIDDLEBUTTON, AUX_MOUSEDOWN, stopIdleFunc);
    auxMainLoop(display);
}

전례와 같이 필요한 헤더 파일과lib를 추가한 후
컴파일 오류stdcall
--------------------Configuration: Example_1_3 - Win32 Debug--------------------
Compiling...
Example_1_3.cpp
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(31) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(33) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(39) : error C2664: 'auxIdleFunc' : cannot convert parameter 1 from 'void (void)' to 'void (__stdcall *)(void)'
        None of the functions with this name in scope match the target type
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(76) : error C2664: 'auxReshapeFunc' : cannot convert parameter 1 from 'void (int,int)' to 'void (__stdcall *)(int,int)'
        None of the functions with this name in scope match the target type
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(77) : error C2664: 'auxIdleFunc' : cannot convert parameter 1 from 'void (void)' to 'void (__stdcall *)(void)'
        None of the functions with this name in scope match the target type
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(78) : error C2664: 'auxMouseFunc' : cannot convert parameter 3 from 'void (struct _AUX_EVENTREC *)' to 'void (__stdcall *)(struct _AUX_EVENTREC *)'
        None of the functions with this name in scope match the target type
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(79) : error C2664: 'auxMouseFunc' : cannot convert parameter 3 from 'void (struct _AUX_EVENTREC *)' to 'void (__stdcall *)(struct _AUX_EVENTREC *)'
        None of the functions with this name in scope match the target type
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(80) : error C2664: 'auxMainLoop' : cannot convert parameter 1 from 'void (void)' to 'void (__stdcall *)(void)'
        None of the functions with this name in scope match the target type
E:\OpenGLRB\Example_1_3\Example_1_3.cpp(81) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
Creating browse info file...

Example_1_3.exe - 6 error(s), 3 warning(s)

그중의
error C2664: 'auxMainLoop' : cannot convert parameter 1 from 'void (void)' to 'void (__stdcall *)(void)'
이 오류 처리 방안은 각 함수 앞에 을 추가합니다stdcall 기호
void __stdcall stopIdleFunc(AUX_EVENTREC *event)
{
    auxIdleFunc(0);
}

void __stdcall myinit(void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glShadeModel(GL_FLAT);
}
원시 코드는 X윈도 아래에서 작성된 것으로 윈도 VC,glx로 전환됩니다.h가 올바르지 않습니다.glXSwapBuffers(auxXDisplay (),auxXWindow ().함수 호출도 유효하지 않습니다. auxSwapBuffers()로 변경합니다.
조정 후 cpp 파일
// Example_1_3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include 
#include 

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glaux.lib")


static GLfloat spin = 0.0;

void __stdcall display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(spin, 0.0, 0.0, 1.0);
    glRectf(-25.0, -25.0, 25.0, 25.0);
    glPopMatrix();
	
    glFlush();
    //glXSwapBuffers(auxXDisplay(), auxXWindow());
    auxSwapBuffers();
}

void  __stdcall spinDisplay(void)
{
    spin = spin + 2.0;
    if (spin > 360.0)
        spin = spin - 360.0;
    display();
} 

void __stdcall startIdleFunc(AUX_EVENTREC *event)
{
    auxIdleFunc(spinDisplay);
}

void __stdcall stopIdleFunc(AUX_EVENTREC *event)
{
    auxIdleFunc(0);
}

void __stdcall myinit(void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glShadeModel(GL_FLAT);
}

void __stdcall myReshape(GLsizei w, GLsizei h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h) 
        glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w, 
		50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
    else 
        glOrtho (-50.0*(GLfloat)w/(GLfloat)h, 
		50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity ();
}


int main(int argc, char** argv)
{
    auxInitDisplayMode(AUX_DOUBLE | AUX_RGBA);
    auxInitPosition(0, 0, 500, 500);
    auxInitWindow(argv[0]);
    myinit();
    auxReshapeFunc(myReshape);
    auxIdleFunc(spinDisplay);
    auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, startIdleFunc);
    auxMouseFunc(AUX_MIDDLEBUTTON, AUX_MOUSEDOWN, stopIdleFunc);
    auxMainLoop(display);
	return 0;
}

후기: 이 프로그램은 Example1-1의 코드를 여러 함수에 분산하지만 절차 순서는 변하지 않습니다. 파라미터를 바꾸어 애니메이션 효과를 실현하려면 마우스 가운데 단추를 누르면 멈추고 왼쪽 단추를 누르면 시작합니다.
모든 aux의 시작 함수는 중요하지 않으며, 모든gl의 시작 함수는 반드시 이해하고 파악해야 한다.

좋은 웹페이지 즐겨찾기