Skip to content

Instantly share code, notes, and snippets.

@yves-chevallier
Created December 24, 2023 08:42
Show Gist options
  • Save yves-chevallier/b02426bb29947e22d6487127d64932a7 to your computer and use it in GitHub Desktop.
Save yves-chevallier/b02426bb29947e22d6487127d64932a7 to your computer and use it in GitHub Desktop.
Teapot test on WSL2
// g++ test.cpp -lGL -lGLEW -lglfw -lGLU -lsfml-window -lsfml-system -lglut
// Works under WSL2
#include <GL/glew.h>
#include <GL/glut.h>
#include <GLFW/glfw3.h>
#include <iostream>
#define WIDTH 800
#define HEIGHT 600
float angleX = 0.0f;
float angleY = 0.0f;
double lastMousePosX, lastMousePosY;
int main(int argc, char** argv) {
glutInit(&argc, argv);
if (!glfwInit()) throw std::runtime_error("Failed to initialize GLFW");
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Teapot", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
lastMousePosX = WIDTH / 2.0;
lastMousePosY = HEIGHT / 2.0;
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
throw std::runtime_error("Failed to initialize GLEW");
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)WIDTH / (double)HEIGHT, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glfwSetCursorPosCallback(
window, [](GLFWwindow* window, double xpos, double ypos) {
float sensibilite = 0.1f;
angleX += (float)(ypos - lastMousePosY) * sensibilite;
angleY += (float)(xpos - lastMousePosX) * sensibilite;
lastMousePosX = xpos;
lastMousePosY = ypos;
});
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(angleX, 1.0f, 0.0f, 0.0f);
glRotatef(angleY, 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glutWireTeapot(1.0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
}
@yves-chevallier
Copy link
Author

image

@yves-chevallier
Copy link
Author

For plain teapot:

// g++ test.cpp  -lGL -lGLEW -lglfw -lGLU -lsfml-window -lsfml-system -lglut
// Works under WSL2
#include <GL/glew.h>
#include <GL/glut.h>
#include <GLFW/glfw3.h>

#include <iostream>

#define WIDTH 800
#define HEIGHT 600

float angleX = 0.0f;
float angleY = 0.0f;
double lastMousePosX, lastMousePosY;

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    if (!glfwInit()) throw std::runtime_error("Failed to initialize GLFW");
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Teapot", NULL, NULL);
    if (!window) {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    lastMousePosX = WIDTH / 2.0;
    lastMousePosY = HEIGHT / 2.0;
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK)
        throw std::runtime_error("Failed to initialize GLEW");
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat lightPos[] = { 0.0f, 10.0f, 0.0f, 1.0f }; // Position de la lumière
    GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);

    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    gluPerspective(45.0, (double)WIDTH / (double)HEIGHT, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glfwSetCursorPosCallback(
        window, [](GLFWwindow* window, double xpos, double ypos) {
            float sensibilite = 0.5f;
            angleX += (float)(ypos - lastMousePosY) * sensibilite;
            angleY += (float)(xpos - lastMousePosX) * sensibilite;

            lastMousePosX = xpos;
            lastMousePosY = ypos;
        });
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        glTranslatef(0.0f, 0.0f, -4.0f);
        glRotatef(angleX, 1.0f, 0.0f, 0.0f);
        glRotatef(angleY, 0.0f, 1.0f, 0.0f);
        glColor3f(0.0f, 0.0f, 1.0f);
        glutSolidTeapot(1.0);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment