基本dev c++的openGL配置我简述一下
(1)Dev C++里面OpenGl是已经装好了的,我们要做的就是下载安装GLUT。
(2)解压,将 glut32.dll 和 glut.dll 拷贝到 c:\windows\system32下面
将 glut32.lib 和 glut.lib 拷贝到Dev C++安装目录下的 lib 目录下,
将 glut.h 拷贝到Dev C++安装目录下的 \include\gl\ 目录下(
(3) 方式一:文件——新建——项目——basic——console application——c项目——确定
(还有方式二,下面会说)
(4)项目-〉项目属性-〉参数,把下面的这段复制到连接器中-〉确定
-lglut32
-lglu32
-lopengl32
-lglaux
-lgdi32
-lwinmm
-glut.h
(5)工具——编译选项,连接器命令添加-static-libgcc -lopengl32 -lglu32 -lgdi32 -lwinmm
(5)编译运行
(6)设置如图
(7)如果遇到的问题:运行报错缺少glut32.dll文件
我已经将glut32.dll放入System32文件夹了,但是解决不了这个问题。。。由于一些原因需要将该文件放到SysWow64文件夹里才行。
(8)到这里简单的图形是可以生成的,比如矩形,圆什么的
但是写巴恩斯利蕨Barnsley Fern机会报错如下
报错:main.o main.c:(.text+0xe): undefined reference to _imp__glClear' main.o main.c:(.text+0x1fc): undefined reference to
glutInit’
collect2.exe [Error] ld returned 1 exit status
\Makefile.win recipe for target ‘test1.exe’ failed
然后Makefile.win自动到第25行,显示有错。
尝试了各种方法,我这个人比较倔强,我弄了几乎一个通宵,看各种有类似问题的blog,但就是解决不了。最终每一个能解决我的问题
后来我尝试用换一种方式,解决方案如下:
把创建项目的方式改一下,换成dev c++自带的openGL.
项目创建方式二:文件——>新建——项目——multMedia——OpenGL,取名,确定。这时它会自动生成有关openGL的一些代码。这些代码不动,把自己的代码和函数写进去。最终结果
最终代码:
#include <windows.h>
#include <gl/gl.h>
#include <GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<time.h>
void fractalFern(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.4f, 0.8f, 0.4f);
glBegin(GL_POINTS);
GLfloat x=0,y=0;
srand((unsigned)time(0));
int i;
for (i = 0; i < 50000;i++){
int n = (int) 100*rand()/(RAND_MAX + 1);
//printf("%d ", n);
GLfloat _x, _y;
if (n < 1){
_x = 0; _y = 0.16*y;
}
else if(n < 8){
_x = 0.2*x - 0.26*y;
_y = 0.23*x + 0.22*y + 1.6;
}
else if (n < 15){
_x = -0.15*x + 0.28*y;
_y = 0.26*x + 0.24*y + 0.44;
}
else{
_x = 0.85*x + 0.04*y;
_y = -0.04*x + 0.85*y + 1.6;
}
x = _x;
y = _y;
glVertex2f(_x/10, _y/10-0.3);
glFlush();
}
glEnd();
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(450, 450);
glutCreateWindow("巴恩斯利蕨");
glutDisplayFunc(&fractalFern);
glutMainLoop();
return 0;
}
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 256, 256,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
/* OpenGL animation code goes here */
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glRotatef (theta, 0.0f, 0.0f, 1.0f);
glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (0.87f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f); glVertex2f (-0.87f, -0.5f);
glEnd ();
glPopMatrix ();
SwapBuffers (hDC);
theta += 1.0f;
Sleep (1);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
(3)后来写实验二 ,字体变形和画曲线,我遇到了同样的问题,但是我已经按照上面的做了。经过我不懈努力,用方式一:文件——新建——项目——basic——console application——c项目——确定 来也是可以的,方式二也可以。
配置参数一样,但是一定记得加上以下头文件
#define GLUT_DISABLE_ATEXIT_HACK
#include<time.h>
#include<windef.h>
#include<wingdi.h>
#include<windows.h>
#include <GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
//更确切说用如下
#include<time.h>
#include<stdlib.h>
#include<windef.h>
#include<wingdi.h>
#include<windows.h>
然后以下分别是两位优秀的同学写的代码,姐妹黄,以及大兄弟余。
姐妹黄
//#include "pch.h"
//#include <iostream>
#define GLUT_DISABLE_ATEXIT_HACK
#include<time.h>
#include<windef.h>
#include<wingdi.h>
#include<windows.h>
#include <GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define bNum 21 //bezier曲线的数量
#define point fPoint //使用哪个点阵
#define tana -0.64 //倾斜的度数
GLfloat p1[] = {
0.237f, 0.620f };//初始化点
GLfloat p2[] = {
0.237f, 0.620f };
GLfloat p3[] = {
0.237f, 0.120f };
GLfloat p4[] = {
0.237f, 0.120f };
GLfloat tPoint[16][8] = {
//T点阵
{
0.237f,0.620f,0.237f,0.620f,0.237f,0.120f,0.237f,0.120f},
{
0.237f,0.120f,0.237f,0.035f,0.226f,0.024f,0.143f,0.019f},
{
0.143f,0.019f,0.143f,0.019f,0.143f,0.000f,0.143f,0.000f},
{
0.143f,0.000f,0.143f,0.000f,0.435f,0.000f,0.435f,0.000f},
{
0.435f,0.000f,0.435f,0.000f,0.435f,0.019f,0.435f,0.019f},
{
0.435f,0.019f,0.353f,0.023f,0.339f,0.036f,0.339f,0.109f},
{
0.339f,0.109f,0.339f,0.108f,0.339f,0.620f,0.339f,0.620f},</