我的OpenGL學習記錄1

來源:互聯網
上載者:User

剛剛開始自學OpenGL,記錄一點學習過程遇到的問題和收穫,協助自己也協助別人。

首先,這裡有一點OpenGL的安裝方法和入門的資料:

http://download.csdn.net/detail/family5love/4308943

照著上面的方法可以很容易地將OpenGL安裝到Visual Stdio 2005中,照著資料裡面的一點代碼運行一下,一個靜態矩形就繪製出來了。

學習教材為OpenGL編程基礎-第三版].Edward.Angel著:

http://download.csdn.net/detail/family5love/4327363

今天學習3.7節的滑鼠操作自己寫了個滑鼠繪製矩形的程式,程式中有個BUG,第一下點擊滑鼠就會繪製矩形,想了好久不知問題出在哪裡。程式原始碼如下如下:


//2012 05 24
//問題:為什麼第一下點擊滑鼠就會繪製矩形?

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

GLfloat x1,y1,x2,y2;
float ww,hh;

int num = 0;//記錄已點擊滑鼠左鍵次數
GLfloat loc[100][2] = {0};//座標記錄數組

//顯示回呼函數
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);

for(int i=0; i<=num; i+=2)
{
glColor3f(0.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(loc[i][0], loc[i][1]);
glVertex2f(loc[i][0], loc[i+1][1]);
glVertex2f(loc[i+1][0], loc[i+1][1]);
glVertex2f(loc[i+1][0], loc[i][1]);
glEnd();
}
//glutSwapBuffers();
glFlush();
}

//空閑回呼函數
void myIdle(void)
{
glutPostRedisplay();
}

//滑鼠響應回呼函數
void myMouse(int button, int state, int x, int y)
{

//記錄滑鼠左鍵當前座標
if(state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
{
loc[num][0] = x;
loc[num][1] = hh - y;
num++;
}

}

//初始化函數
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1,1,-1,1);
}

//視窗重構函數
void myReshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLfloat)w,0.0,(GLfloat)h);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w,h);

ww = w;
hh = h;
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(200,200);
glutInitWindowPosition(500,500);

glutCreateWindow("Mouse");
init();

glutMouseFunc(myMouse);
glutReshapeFunc(myReshape);
glutDisplayFunc(myDisplay);
glutIdleFunc(myIdle);

glutMainLoop();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.