gpt4 book ai didi

c - 如何使用 OpenGL 绘制冰淇淋

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:12 24 4
gpt4 key购买 nike

如题,如何绘制3d冰淇淋?我画了一个圆锥体和一个球。但是球无法放入锥体中......我尝试了很多方法,但球要么在锥体后面,要么整个球都在锥体内......任何人都可以解决这个问题。我已按照讲师的说明进行操作,但仍然无法获得。

#include <Windows.h>
#include <gl/GL.h>
#include <math.h>
#include <time.h>
#include <gl/GLU.h>
#pragma comment (lib, "OpenGL32.lib")
#pragma comment (lib, "GLU32.lib")

#define WINDOW_TITLE "OpenGL Window"

LRESULT WINAPI WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
break;
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}
//--------------------------------------------------------------------

bool initPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));

pfd.cAlphaBits = 8;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 0;

pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;

pfd.iLayerType = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;

// choose pixel format returns the number most similar pixel format available
int n = ChoosePixelFormat(hdc, &pfd);

// set pixel format returns whether it sucessfully set the pixel format
if (SetPixelFormat(hdc, n, &pfd))
{
return true;
}
else
{
return false;
}
}
//--------------------------------------------------------------------

void display()
{
glPushMatrix();
glRotatef(120, 1.0, 0, 0);

GLUquadricObj * cylinder = NULL;
cylinder = gluNewQuadric();
glColor3f(1, 0, 0);
gluQuadricDrawStyle(cylinder, GLU_FILL);
gluCylinder(cylinder, 0.52, 0.0, 2.0, 30, 20);
gluDeleteQuadric(cylinder);

GLUquadricObj * sphere = NULL;
sphere = gluNewQuadric();
glColor3f(1, 1, 1);
gluQuadricDrawStyle(sphere, GLU_LINE);
gluSphere(sphere, 0.5, 20, 20);
gluDeleteQuadric(sphere);

glPopMatrix();
}
//--------------------------------------------------------------------

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = WindowProcedure;
wc.lpszClassName = WINDOW_TITLE;
wc.style = CS_HREDRAW | CS_VREDRAW;

if (!RegisterClassEx(&wc)) return false;

HWND hWnd = CreateWindow(WINDOW_TITLE, WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 640,
NULL, NULL, wc.hInstance, NULL);

//--------------------------------
// Initialize window for OpenGL
//--------------------------------

HDC hdc = GetDC(hWnd);

// initialize pixel format for the window
initPixelFormat(hdc);

// get an openGL context
HGLRC hglrc = wglCreateContext(hdc);

// make context current
if (!wglMakeCurrent(hdc, hglrc)) return false;

//--------------------------------
// End initialization
//--------------------------------

ShowWindow(hWnd, nCmdShow);

MSG msg;
ZeroMemory(&msg, sizeof(msg));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0f, +3.0f, -2.0f, +2.0f, -10.0f, +10.0f);

while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT) break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

display();

SwapBuffers(hdc);
}

UnregisterClass(WINDOW_TITLE, wc.hInstance);

return true;
}
//--------------------------------------------------------------------

最佳答案

请注意,通过 glBegin/glEnd 序列绘制、固定功能管线矩阵堆栈和每个顶点灯​​光模型的固定功能管线已被弃用数十年。了解 Fixed Function Pipeline并查看 Vertex SpecificationShader最先进的渲染方式。


无论如何,在PIXELFORMATDESCRIPTOR深度缓冲区已正确指定:

 pfd.cDepthBits = 24;

现在你必须使用深度缓冲。

旁注,颜色缓冲区位数应为 24 而不是 32,请参阅 cColorBits 的文档:

Specifies the number of color bitplanes in each color buffer. For RGBA pixel types, it is the size of the color buffer, excluding the alpha bitplanes. For color-index pixels, it is the size of the color-index buffer.


要使用深度缓冲 Depth Test必须由 glEnable 启用.
此外,必须在每帧开始时通过 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) 清除默认帧缓冲区的颜色缓冲区和深度缓冲区。

void display()
{
glEnable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glPushMatrix();
glRotatef(120, 1.0, 0, 0);

GLUquadricObj * cylinder = NULL;
cylinder = gluNewQuadric();
glColor3f(1, 0.5, 0);
gluQuadricDrawStyle(cylinder, GLU_FILL);
gluCylinder(cylinder, 0.52, 0.0, 2.0, 30, 20);
gluDeleteQuadric(cylinder);

GLUquadricObj * sphere = NULL;
sphere = gluNewQuadric();
glColor3f(1, 1, 0.5);
gluQuadricDrawStyle(sphere, GLU_FILL);
gluSphere(sphere, 0.5, 20, 20);
gluDeleteQuadric(sphere);

glPopMatrix();
}

查看预览,我将球体的 gluQuadricDrawStyleGLU_LINE 更改为 GL_FILL:

关于c - 如何使用 OpenGL 绘制冰淇淋,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53497613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com