鼠标点击生成一个对象 OpenGL GLUT

发布于 2025-01-13 11:00:58 字数 2484 浏览 3 评论 0原文

我创建了这段代码,其中平面跟随鼠标位置。

#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
float objX = 100;
float objY = 100;
float objSize = 50;

void motion(int x, int y) {
  objX = x;
  objY = y;
}

void drawRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}
void drawStaticRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}
int c = 0;
void display() {
  glClearColor(0, 0, 0, 1);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  const double w = glutGet(GLUT_WINDOW_WIDTH);
  const double h = glutGet(GLUT_WINDOW_HEIGHT);
  glOrtho(0, w, h, 0, -1, 1);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  drawRect(objX, objY, objSize);

  glutSwapBuffers();
}
void mouseClicks(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
      drawStaticRect(objX, objY, objSize);
    }
      glutPostRedisplay();
}
int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  glutInitWindowSize(600, 600);
  glutCreateWindow("GLUT");
  glutDisplayFunc(display);
  glutMouseFunc(mouseClicks);
  glutPassiveMotionFunc(motion);
  glutIdleFunc(display);
  glutMainLoop();
  return 0;
}

当按下鼠标左键时,我想在实际位置绘制对象,并重复该操作多次。


void drawStaticRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}

void mouseClicks(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
      drawStaticRect(objX, objY, objSize);
    }
      glutPostRedisplay();

我的想法是,当事件触发时,我只需调用一个在实际鼠标位置下生成四边形的函数,并调用 glutPostRedisplay() 来更新场景。

I have created this code where a plane follow the mouse position.

#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
float objX = 100;
float objY = 100;
float objSize = 50;

void motion(int x, int y) {
  objX = x;
  objY = y;
}

void drawRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}
void drawStaticRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}
int c = 0;
void display() {
  glClearColor(0, 0, 0, 1);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  const double w = glutGet(GLUT_WINDOW_WIDTH);
  const double h = glutGet(GLUT_WINDOW_HEIGHT);
  glOrtho(0, w, h, 0, -1, 1);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  drawRect(objX, objY, objSize);

  glutSwapBuffers();
}
void mouseClicks(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
      drawStaticRect(objX, objY, objSize);
    }
      glutPostRedisplay();
}
int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  glutInitWindowSize(600, 600);
  glutCreateWindow("GLUT");
  glutDisplayFunc(display);
  glutMouseFunc(mouseClicks);
  glutPassiveMotionFunc(motion);
  glutIdleFunc(display);
  glutMainLoop();
  return 0;
}

When the left mouse button is pressed i want to draw the object in the actual position, and repeat the operation as many times i want.


void drawStaticRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}

void mouseClicks(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
      drawStaticRect(objX, objY, objSize);
    }
      glutPostRedisplay();

My idea is when the event trigger i just call a function that generate a quads under the actual mouse position and call glutPostRedisplay() to update the scene.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文