求一个C语言Opengl代码

作者&投稿:勾莎 (若有异议请与网页底部的电邮联系)
C语言如何用OpenGL~

  OpenGL就是基于C语言的,只需要下载OpenGL的SDK库安装即可,在编写源码时:
  1、添加头文件glut.h。
  注意glut.h文件中已经包含gl.h,glu.h在实际编译中可以只加入头文件glut.h,很多相关的例子都是这样的,但是在mingwstudio上编译发现,在glut.h前还是需要加入glu.h, gl.h.如:
  #include
  #include
  #include
  2、在工程中添加OpenGL的库,有关命令行加入,glu32 opengl32 glut32库就可以编译了。
  

如果只用C里的函数是不行的 必须通过其它的图形程序接口(例如OpenGL,DirectX等)可以办到

// Bounce.c
// Demonstrates a simple animated rectangle program with GLUT
// OpenGL SuperBible, 2nd Edition
// Richard S. Wright Jr.
#include"stdafx.h"
#include <windows.h>
#include <gl/glut.h>

// Initial square position and size
GLfloat x1 = 100.0f;
GLfloat y1 = 150.0f;
GLsizei rsize = 50;

// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;

// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;

// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);

// Set current drawing color to red
// R G B
glColor3f(1.0f, 0.0f, 0.0f);

// Draw a filled rectangle with current color
glRectf(x1, y1, x1+rsize, y1+rsize);

// Flush drawing commands
glutSwapBuffers();
}

// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
{

// Reverse direction when you reach left or right edge
if(x1 > windowWidth-rsize || x1 < 0)
xstep = -xstep;

// Reverse direction when you reach top or bottom edge
if(y1 > windowHeight-rsize || y1 < 0)
ystep = -ystep;

// Check bounds. This is incase the window is made
// smaller and the rectangle is outside the new
// clipping volume
if(x1 > windowWidth-rsize)
x1 = windowWidth-rsize-1;

if(y1 > windowHeight-rsize)
y1 = windowHeight-rsize-1;
// Actually move the square
x1 += xstep;
y1 += ystep;

// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}

// Setup the rendering state
void SetupRC(void)
{
// Set clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}

// Called by GLUT library when the window has chanaged size
void ChangeSize(GLsizei w, GLsizei h)
{
// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Keep the square square, this time, save calculated
// width and height for later use
if (w <= h)
{
windowHeight = 250.0f*h/w;
windowWidth = 250.0f;
}
else
{
windowWidth = 250.0f*w/h;
windowHeight = 250.0f;
}

// Set the clipping volume
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

// Main program entry point
void main(void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);

SetupRC();

glutMainLoop();
}

//是正方形的,你改一下吧~
程序还是自己试着编,这样才会进步,加油哈!

百度两下


编写好一个C语言程序,如何把数据一直保存下来
用数组保存。定义一个数组。如:int a[6]={1,2,3,4,5,6};然后输出 for(i=0;i<6;i++){ printf("%d",a[i]);} 代码如下:include<stdio.h> int main(){ int a[6]={1,2,3,4,5,6};int i;for(i=0;i<=5;i++)printf("%d\\n",a[i]);} ...

C语言函数编写:文件复制
C语言实现一个简单的文件复制功能,Linux环境下。思路步骤:(下代码最重要的逻辑步骤清晰)第一步:打开源文件(要复制的文件),打开文件的方式以读的方式就可以了。Linux C打开文件的库函数有:int open(const char *pathname,int flags),int open(const char *pathname,mode_t mode),以及 FILE...

用C语言:编写一个计算机的串行端口程序
2H:文件名结束 3H:文件结束 这样,每当发送机发送一个字节以后,就等待接受机发回送信号,这回 送信号就是发送机发送过来的那个字节。发送机接收到回送信号后,把 它与刚发送的字节相比较,如果相同,就发送新的半个字节,否则就重 新发送。新数据与旧数据通过信息位来区分。下面就是我用C语言编写...

C语言如何打开一个课执行程序,然后向这个被打开的程序输入一个按键...
for(i=0;tempc[i];i++,j++)acharp[j]=tempc[i];acharp[j]=0;cout << acharp <<endl;\/\/打开qq程序 \/\/ return 0;ShellExecute(NULL,TEXT("open"),TEXT(acharp\/*"C:\\\\Program Files\\\\Tencent\\\\QQ\\\\Bin\\\\QQ.exe"*\/),NULL,NULL,SW_SHOW);Sleep(3000);\/\/等待qq打开必须确保没...

c语言同一个文件可不可以打开两次,读两次?
更多关于c语言的知识 > 正在求助 换一换 回答问题,赢新手礼包 苦等1分钟: 现在对陌生人怎样称呼好 回答 苦等9分钟: 有一个看球又懂球的女朋友,是一种怎样的体 回答 苦等18分钟: iOS11.3正式版有哪些改变 回答 苦等20分钟: NBA单赛季出手最多的是谁 回答 苦等32分钟: 不会发脾气不敢发脾气...

c语言怎么打开文件内容
1、首先需要下载专门的c语言编程软件,一般从百度下载就可以。2、打开下载好的c语言编程软件,界面如下图所示。3、在标题下方点击打开项目,操作如下图所示。4、软件会自动帮你找到自己做的文件,点击需要打开的文件。5、可以看到文件打开,自己可以进行查看,或者修改,添加程序。

C语言编译后FEOPEN函数
这里会依次打开命令行输入的每个文件 即a b c 对于每个文件,执行fc,也就是 从文件中依次读出字符,直到#为止。每个字符,输出putchar(c-32);文件中都是小写字符,c-32即转为大写,因为大小写的ascii码差值为32 于是,输出每个文件中,#前,小写转为大写的结果。即 AAAABBBBCCCC ...

要交作业了,用c语言编写一个工资管理系统
要交作业了,用c语言编写一个工资管理系统 没太多要求,能实现插入,删除,查找功能就可以,内容包括编号,姓名,年龄,性别,工资,做了老长时间了,总是有问题,那位大虾帮帮忙吧!poacher12345@163.com哥们不要太复杂的指导... 没太多要求,能实现插入,删除,查找功能就可以,内容包括编号,姓名,年龄,性别,工资,做了老长...

怎么用C语言打开一个MP3文件吖?
nRetCode = 1;} else { \/\/"Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";CFileDialog OpenFileDlg(TRUE ,NULL ,NULL ,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT ,_T("MP3 文件(*.mp3)|*.mp3||"...

C语言,Win32程序,GetOpenFileName与fopen
其实我觉得不一定是TCHAR的问题,因为你的工程本身就没有设置成UNICODE编码格式,所以其实TCHAR其实就是char,你自己看TCHAR的声明就知道了,然后你用MessageBox显示出来看是 "d:\\1.txt",可是你要知道,在windows中要表示一个斜杠,需要两个斜杠,所以你需要的是"d:\\\\1.txt"这样的字符串。所以你才会...

恩施市18426136879: 求一个C语言Opengl代码 -
友孟特子: // Bounce.c// Demonstrates a simple animated rectangle program with GLUT// OpenGL SuperBible, 2nd Edition// Richard S. Wright Jr.#include"stdafx.h"#include <windows.h>#include <gl/glut.h>// Initial square position and size GLfloat x1 = 100.0...

恩施市18426136879: 急!求一个OpenGL编写的源代码,简单的,最好是原创小作品.希望好心人帮帮忙~
友孟特子: //Win32 Console Application #include &lt;gl/glut.h&gt; #include &lt;math.h&gt; const int n = 60; const GLfloat R = 0.2f; const GLfloat Pi = 3.1415926536f; void myDisplay(void) { int i; glClear(GL_COLOR_BUFFER_BIT); //点 glPointSize(2.0); ...

恩施市18426136879: 求一个用C语言绘制的图形代码 -
友孟特子: #include int main() { int i=3; int j; int k; for(j=0;j<4;j++,i--) { for(k=i;k>0;k--) { printf(" "); } for(k=0;k<2*j+1;k++) { printf("*"); } printf("\n"); } for(i=1,j=3;j>0;j--,i++) { for(k=i;k>0;k--) { printf(" "); } for(k=0;k<2*j-1;k++) { printf("*"); } printf("\n"); } }

恩施市18426136879: 求c语言改错!用opengl绘制一个网格但是有错.
友孟特子: 两次画的是一样 for(int x=0; x&lt;10; x++) { glBegin(GL_LINE_LOOP); glVertex3f(x,0,0); glVertex3f(x,10,0); glVertex3f(x+1,10,0); glVertex3f(x+1,0,0); glEnd(); }; 试试

恩施市18426136879: 求一个带注释的opengl代码,简单点的就好,交作业用 -
友孟特子: #include <stdlib.h>#include <stdio.h>#include <memory.h>#include <windows.h> #include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>//#include "bmp.h"#include "GL/glext.h"#pragma comment( linker, "/subsystem:\"windows\" /entry...

恩施市18426136879: 求C++ DirectX或OpenGL计算机图形学 完整例子 -
友孟特子: #include <windows.h>#include <gl/glut.h>#include<gl/gl.h>#include<gl/glu.h>// 函数RenderScene用于在窗口中绘制需要的图形void RenderScene(void){//用当前清除色清除颜色缓冲区,即设定窗口的背景色glClear(GL_COLOR_BUFFER_...

恩施市18426136879: 求一个c语言完整程序 -
友孟特子: #include <stdio.h> main() { int n,k=0,i,m,t=1; printf("请输入一个正整数(不超过5位):"); scanf("%d",&n); m=n; // 保存原始数据 if (n>=0 && n<100000) { // 确定是几位数 while (n) { k++; n/=10; } printf("%d是个%d位数\n",m,k); // 为...

恩施市18426136879: openGL代码题,求大神,在线等!! -
友孟特子: //伪代码,不能运行,懂的自然懂#include main() { Initialize(); glColor3f(0,0,0,0); glOrtho(0,1,0,1,-1,1); glBegin(GL_POLYGON); glVertex3f(0,0,0); glVertex3f(1,0,0); glVertex3f(0,1,0); glEnd(); glFlush(); }

恩施市18426136879: 求一个VC++6.0环境下的OPENGL三维视角转换程序源码 -
友孟特子: 要做好后直接发过来的,做的好的话(3)continue表示不再继续往下执行~此时k==4.然后判断c

恩施市18426136879: 在vc++6.0怎么建立一个关于OpenGL的工程 -
友孟特子: 跟写普通的C程序一样,创建C控制台和C++ source file,只是要把opengl的函数库include

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网