basic 3d scene yay

This commit is contained in:
Rusty Striker 2023-07-05 14:51:36 +03:00
parent a3f4639a90
commit 97a1427a02
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15

View file

@ -1,16 +1,21 @@
#include <GL/freeglut.h> #include <GL/freeglut.h>
#include <GL/freeglut_std.h> #include <GL/freeglut_std.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include "ui.h" #include "ui.h"
void testButtonClick();
// Ui stuff // Ui stuff
ui_button buttons[] = { ui_button buttons[] = {
{ {
/* pos */ { { 0.5, 0.5, 0.0 }, { -50.0, -25.0, 0.0 } }, /* pos */ { { 0.5, 0.5, 0.0 }, { -50.0, -25.0, 0.0 } },
/* size */ { 100.0, 50.0, 0.0 }, /* size */ { 100.0, 50.0, 0.0 },
/* onclick */ NULL, /* onclick */ testButtonClick,
/* text */ "Test :)" /* text */ "Test :)"
} }
}; };
@ -22,12 +27,24 @@ ui_slider sliders[] = {
} }
}; };
void testButtonClick() {
printf("Clicked!\n");
sliders[0].value = 0.5;
glutPostRedisplay();
}
void display(void) { void drawUi(void) {
printf("Drawing!\n"); glDisable(GL_LIGHTING);
// Clear screen
glClearColor(0.0, 0.0, 0.0, 1.0); // Dark theme :) glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity();
// set ui camera
int wWidth = glutGet(GLUT_WINDOW_WIDTH);
int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, wWidth, 0.0, wHeight);
// Draw buttons // Draw buttons
for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) { for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) {
ui_button_draw(b); ui_button_draw(b);
@ -40,12 +57,48 @@ void display(void) {
if(err != 0) { if(err != 0) {
printf("opengl error %d: %s\n", err, gluErrorString(err)); printf("opengl error %d: %s\n", err, gluErrorString(err));
} }
}
void drawWorld(void) {
// set perspective camera
GLdouble wWidth = (GLdouble) glutGet(GLUT_WINDOW_WIDTH);
GLdouble wHeight = (GLdouble) glutGet(GLUT_WINDOW_HEIGHT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 10.0, 20.0, (0.5 - sliders[0].value) * 10.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, wWidth / wHeight , 1.0, 100.0);
// Lights
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat light1[] = { 0.0, 1.0, 0.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light1);
// World
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 0.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-50.0, 0.0, -50.0);
glVertex3f(50.0, 0.0, -50.0);
glVertex3f(50.0, 0.0, 50.0);
glVertex3f(-50.0, 0.0, 50.0);
glEnd();
}
void display(void) {
// Clear screen
glClearColor(0.0, 0.0, 0.0, 1.0); // Dark theme :)
glClear(GL_COLOR_BUFFER_BIT);
drawWorld();
drawUi();
glutSwapBuffers(); glutSwapBuffers();
glFlush(); glFlush();
} }
void mouseEvent(int button, int state, int x, int y) { void mouseEvent(int button, int state, int x, int y) {
if(state == 1) { if(state == 0) {
for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) { for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) {
if(ui_button_mouse_over(b, x, y) && b->onClick != NULL) { if(ui_button_mouse_over(b, x, y) && b->onClick != NULL) {
b->onClick(); b->onClick();
@ -61,7 +114,6 @@ void mouseEvent(int button, int state, int x, int y) {
} }
void mouseWheelEvent(int wheel, int dir, int x, int y) { void mouseWheelEvent(int wheel, int dir, int x, int y) {
printf("wheel event\n");
for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) { for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) {
if(ui_slider_mouse_over(s, x, y)) { if(ui_slider_mouse_over(s, x, y)) {
s->value += dir * 0.05; s->value += dir * 0.05;