← Back to list

Computer Graphics (計算機圖學)(OpenGL)(新手小白的磨練)(for rookie)(review & qa)(I)

1.when to use void init()?

Alex .K · 2025-03-19 14:06 · 0 claps · 2.1 min read
#opengl #cpp #computer-graphics #csie #notebook
Open on Medium ↗

Computer Graphics (計算機圖學)(OpenGL)(新手小白的磨練)(for rookie)(review & qa)(I)

1.when to use void init()?

(if the program wasn’t complicated then you can set in main() ordisplay() will be more easily)

ans1–1:set up open_gl statistic (eg.背景顏色,深度測試,混和模式)

void init() {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  // 設定背景顏色(黑色)
    glEnable(GL_DEPTH_TEST);              // 啟用深度測試
}

ans1–2 :loaded shader(著色器) & texture(紋理)

void init() {
    GLuint shaderProgram = LoadShaders("vertex_shader.glsl", "fragment_shader.glsl");
    glUseProgram(shaderProgram);
}

ans1–3 :initialization VAO / VBO(頂點緩衝物件)

void init() {
    GLuint VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    // 傳送頂點資料
}

ans1–4 :initialization matrix or perspective(矩陣或視角設定)(如果你使用 GLM 或 OpenGL 內建函式)

void init() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, 1.0, 0.1, 100.0); // 設定投影矩陣
    glMatrixMode(GL_MODELVIEW);
}

2.let the window can zoom in and out with myReshape()

//global variance
GLsizei ww = 500, wh = 500; // 視窗寬高 (window height)
GLfloat x = ww / 2, y = wh / 2; // 正方形初始位置 

void myReshape(GLsizei w, GLsizei h) {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
    ww = w;
    wh = h;
    x = w / 2; // renew the middle position(更新中心位置)
    y = h / 2;
}

3.if use matrix translate in yout program then you will need to put on glPushMatrix(); and glPopMatrix();

use these 2 fun. everytime you want to move object form one place to another place ,will all from same start.(可以由同一個起點開始移動)

e.g. start = A

then i want to translate A to B ,if you want to then translate A to C ,if you don’t use push and pop then it will be A translate to B and B translate to C.(this wasn’t the initially you want right!)

   glPushMatrix();    //這份檔案的重點在講glPushMatrix 跟 glPopMatrix
        glColor3f(1.0, 0.0, 0.0);  // 紅色茶壺
        glTranslatef(-10, 10, 0);   // 平移到左上角
        glutSolidTeapot(3.0);
    glPopMatrix();

    glPushMatrix();
        glColor3f(0.0, 1.0, 0.0);  // 綠色茶壺
        glTranslatef(10, -10, 0);   // 平移到右下角
        glutSolidTeapot(3.0);
    glPopMatrix();

4.every input device trigger fun. will need glutPostRedisplay(); , like mouse click and keyboard input, to let the screen redraw

void mymouse(int button ,int state,int x, int y) 
{
    if (button ==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
    // 將滑鼠的座標從視窗座標轉換為 OpenGL 座標 (change mouse coordinate into opengl coordinate)
        teapotX = (x - 250) / 25;  // 250 是視窗的中心點,25 是縮放比例(250 is the middle of the window)
        teapotY = (250 - y) / 25;  // 250 是視窗的中心點,25 是縮放比例(25 is zoom ratio)

    glutPostRedisplay();  // 重新繪製畫面 (redraw screen)
}
//鍵盤函數(會記錄滑鼠的位置)當按下指定按鍵
void keyboard(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 'Q')
        teapotX = (x - 250) / 25;  // 250 是視窗的中心點,25 是縮放比例
        teapotY = (250 - y) / 25;  // 250 是視窗的中心點,25 是縮放比例

    glutPostRedisplay();  // 重新繪製畫面
}

5.template program(模板程式方便上課使用)

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

// 視窗大小
int ww = 500, wh = 500;

void init() {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    // 畫一條綠色線段
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_LINES);
    glVertex3d(0.0, 0.0, 0.0);
    glVertex3d(10.0, 10.0, 0.0);
    glEnd();

    glutSwapBuffers();
}

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

void mouse(int button, int state, int x, int y) {
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        printf("Left mouse button clicked at (%d, %d)\n", x, y);
    }
}

int main(int argc, char** argv) {
    srand((unsigned int)time(NULL)); // 初始化隨機數種子
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // 雙緩衝模式
    glutInitWindowSize(ww, wh);
    glutCreateWindow("OpenGL Template");

    init();

    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

메타데이터
post_id
b3626d5b6df0
slug
computer-graphics-計算機圖學-opengl-新手小白的磨練-for-rookie-with-gpt-review-qa-b3626d5b6df0
url
https://medium.com/@guopeijia123456789/computer-graphics-%E8%A8%88%E7%AE%97%E6%A9%9F%E5%9C%96%E5%AD%B8-opengl-%E6%96%B0%E6%89%8B%E5%B0%8F%E7%99%BD%E7%9A%84%E7%A3%A8%E7%B7%B4-for-rookie-with-gpt-review-qa-b3626d5b6df0
canonical_url
https://medium.com/@guopeijia123456789/computer-graphics-%E8%A8%88%E7%AE%97%E6%A9%9F%E5%9C%96%E5%AD%B8-opengl-%E6%96%B0%E6%89%8B%E5%B0%8F%E7%99%BD%E7%9A%84%E7%A3%A8%E7%B7%B4-for-rookie-with-gpt-review-qa-b3626d5b6df0
author_url
https://medium.com/@guopeijia123456789
status
ok
fetched_at
2026-06-15 20:49:13